Probel with RPG battle

Started by
4 comments, last by MTT 20 years, 8 months ago
Ok, this isnt the most beautifull code but i am having a problem with it. It still has alot of work to do on it but i dont want to code anymore untill i get this sorted out. I am pretty sure that the problem is in the battle function, but it might be in the drawscreen funtion because i call that from the battle function. Now at the botoom of the screen i get 0 errors, 0 warnings. Run the program, it sais dungeon battle has performed an illegal opetration. I am hopeing you can tell me what that illegal operation is. Enuf talking, here is the code.

#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;

#define GRID_WIDTH1 77
#define GRID_HIGHT1 23

void LoadMap(CHAR_INFO ScreenBuffer[], COORD BuffSize, int PicNum);
void DrawScreen(CHAR_INFO ScreenBuffer[], int PlayerX, int PlayerY);
void Battle(CHAR_INFO ScreenBuffer[]);

int PVit = 5;
int PStr = 5;
int PLif = PVit * 5;
int PDmg = PStr * 2;

int MVit = 5;
int MStr = 2;
int MLif = MVit * 5;
int MDmg = MStr * 2;


//the main function

void main()
{
	int PicNum = 2;
	
	CHAR_INFO ScreenBuffer[GRID_WIDTH1 * GRID_HIGHT1];
	COORD BuffSize = {GRID_WIDTH1, GRID_HIGHT1};

	LoadMap(ScreenBuffer, BuffSize, PicNum);
	
	Battle(ScreenBuffer);

	return;
}

void LoadMap(CHAR_INFO ScreenBuffer[], COORD BuffSize, int PicNum)
{	
	ifstream fin;			//creates a file input thing

	
	//checks which map to load, error checks, then loads the map

	switch(PicNum)
	{
		case 1:
		fin.open("demon.txt");
		if (fin.fail())
		{	return;	}
		break;

		case 2:
		fin.open("skeleton.txt");
		if (fin.fail())
		{	return;	}
		break;
	}
	
	//Reads in file into buffer charecter by charecter, it first goes

	//along the first horizontal row, once it finishes it increments the

	//verticle row by 1 and reads it horizontaly again until the map is

	//completely read into the buffer

	char temp;
	for (int y = 0; y < BuffSize.Y; y++)
	{	
		for (int x = 0; x < BuffSize.X; x++)
		{
			fin >> temp;
			
			switch(temp)
			{
				case ''0'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = '' '';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''1'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''X'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
				break;

				case ''2'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''|'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''3'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''/'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''4'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''_'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''5'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = 92;
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''6'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''<'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''7'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''>'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''A'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''A'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''C'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''C'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''E'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''E'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;
				
				case ''G'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''G'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''I'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''I'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''K'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''K'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''M'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''M'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''N'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''N'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''R'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''R'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''T'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''T'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				case ''U'':
				ScreenBuffer[x + y * GRID_WIDTH1].Char.AsciiChar = ''U'';
				ScreenBuffer[x + y * GRID_WIDTH1].Attributes = FOREGROUND_RED;
				break;

				//this is incase none of those charecters are found

				//it creates an error and automaticly exits

				default:
				fin.close();
				cout << "error2"; return;
				break;
			
			}//end switch

		}//end x for loop

	}//end y for loop


	fin.close();			//close the file because we are done with it

	return;					//exits this function


}//end LoadMap



void DrawScreen(CHAR_INFO ScreenBuffer[], int PlayerX, int PlayerY)
{
	
	//tells the computer the 4 corners of the area we a drawing to,

	//a coord storing the size of the buffer, the corner we are drawing

	//from (top left, and a handle for acess to the screen

	SMALL_RECT DrawRect = {1, 1, 1 + (GRID_WIDTH1 - 1), 1 + (GRID_HIGHT1 - 1)};
	COORD BuffSize = {GRID_WIDTH1, GRID_HIGHT1};
	COORD ZeroZero = {0, 0};
	HANDLE hOutput;			

	//This function gets a standard hanlde for hOutput and allows

	//acess to our screen buffer

	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

	ScreenBuffer[PlayerX + PlayerY * GRID_WIDTH1].Char.AsciiChar = ''>'';
	ScreenBuffer[PlayerX + PlayerY * GRID_WIDTH1].Attributes = FOREGROUND_BLUE;

	//draws out the contents of the screen

	WriteConsoleOutput(hOutput, ScreenBuffer, BuffSize, ZeroZero, &DrawRect);

	return;					//exits function


}//End drawscreen




void Battle(CHAR_INFO ScreenBuffer[])
{
	//initializes a structure that stores what has been input, the position

	//of  our charecter (this is used later in colision detection),

	//somthing that thells the computer the number of events that were

	//input, and a handle for acess to the input

	COORD CursorPos = {19,55};
	INPUT_RECORD InputRecord;
	DWORD Events = 0;
	HANDLE hInput;
	
	while(PLif > 0 && MLif > 0)
	{
		DrawScreen(ScreenBuffer, CursorPos.X, CursorPos.Y);
		
		bool bPlayerChose = false;
		
		//lets the computer know if the player was moved and if a key

		//was pressed

		bool bPlayerMoved = false;
		int bKeyDown = 0;

		//gets a standard input handle then processes any commands

		hInput = GetStdHandle(STD_INPUT_HANDLE);
		ReadConsoleInput(hInput, &InputRecord, 1, &Events);
	
		//This is for NT based users, on windows 95, 98, ect. it only reads

		//when the key is pressed it only reads it when the button goes from

		//off to on but in NT it reads it when it goes off to on also so this

		//is so they too only move 1 space per press

		bKeyDown = InputRecord.Event.KeyEvent.bKeyDown;	

	
		//this entire thing processes what key is pressed, makes sure the

		//player hasnt gone over the edge of the grid, moves the player,

		//then lets the computer know the player has moved.

		if(InputRecord.EventType == KEY_EVENT && bKeyDown)
		{
		
			if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
			{
				if(CursorPos.X != 66)
				{
					CursorPos.X = 66;
					bPlayerMoved = true;
				}
			}

			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
			{
				if(CursorPos.X != 55)
				{
					CursorPos.X = 55;
					bPlayerMoved = true;
				}
			}
	
			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
			{
				if(CursorPos.Y != 21)
				{
					CursorPos.Y = 21;
					bPlayerMoved = true;
				}
			}

			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
			{
				if(CursorPos.Y != 19)
				{
					CursorPos.Y = 19;
					bPlayerMoved = true;
				}
			}

			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RETURN)
			{
				bPlayerChose = true;
			}
			
			else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
			{
				exit(0);
			}
		
		}	
		
		if(bPlayerMoved = true)
		{
			DrawScreen(ScreenBuffer, CursorPos.X, CursorPos.Y);
		}

		if(bPlayerChose = true)
		{
			MLif -= PDmg;
			PLif -= MDmg;
		}
	}
	
	return;
}
and here is a map file just incase you are wondering

11111111111111111111111111111111111111111111111111111111111111111111111111111
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
10000000000000000000000000000000000000000000000000000000000000000000000000001
11111111111111111111111111111111111111111111111111111111111111111111111111111
10000000000000000000000000000000000000000000000000001000000000000000000000001
1000000000000000000000000000000000000000000000000000100ATTACK00000MAGIC000001
10000000000000000000000000000000000000000000000000001000000000000000000000001
1000000000000000000000000000000000000000000000000000100ITEM0000000RUN00000001
10000000000000000000000000000000000000000000000000001000000000000000000000001
11111111111111111111111111111111111111111111111111111111111111111111111111111
__________________________________________________________________________________________ "yuo have it all wrong. admiralbiunary is me not the other way reorubnd poppet. just becuase hea was here first doesnt meean hes any mopre valid than me yuo rassit pigf." YodaTheCoda
--------------------------http://www.gamedev.net/community/forums/icons/icon51.gif ... Hammer time
Advertisement
Please put a link to the source and file on a webpage instead. It can''t really be copy pasted from this forum very good.

----------------------------------------------
Petter Nordlander

"There are only 10 kinds of people in the world. The who understand binary and those who don''t"
----------------------------------------------Petter Nordlander"There are only 10 kinds of people in the world. They who understand binary and those who do not"
<< Run the program, it said dungeon battle has performed an illegal opetration. >>

You need to narrow down the problem, figure out exactly where its getting the illegal operation. You can put in stops (printf or cout at points). Could be your array ScreenBuffer goes out of range.

You should comment what all the variables mean, and comment almost every line, rather than groups of lines.

Character and vertical are spelled like this.

Phil P
Did you Debug? You might be able to find what is causing the problem that way...
The monkeys are listening...
Yeah, i sorta half aseed my way through and decided i would comment and organize when i get it a litlle further. il see if i can narrow it down at all though.

__________________________________________________________________________________________
"yuo have it all wrong. admiralbiunary is me not the other way reorubnd poppet. just becuase hea was here first doesnt meean hes any mopre valid than me yuo rassit pigf." YodaTheCoda
--------------------------http://www.gamedev.net/community/forums/icons/icon51.gif ... Hammer time
Ok the problem has appered to be on this line of code

COORD CursorPos = {19, 55};

__________________________________________________________________________________________
"yuo have it all wrong. admiralbiunary is me not the other way reorubnd poppet. just becuase hea was here first doesnt meean hes any mopre valid than me yuo rassit pigf." YodaTheCoda
--------------------------http://www.gamedev.net/community/forums/icons/icon51.gif ... Hammer time

This topic is closed to new replies.

Advertisement