diff --git a/source/code/BlockGame.cpp b/source/code/BlockGame.cpp index 1b049e4..84c5efc 100644 --- a/source/code/BlockGame.cpp +++ b/source/code/BlockGame.cpp @@ -23,6 +23,7 @@ http://blockattack.sf.net #include "BlockGame.hpp" +#include //////////////////////////////////////////////////////////////////////////////// @@ -113,9 +114,9 @@ BlockGame::~BlockGame() void BlockGame::setGameSpeed(Uint8 globalSpeedLevel) { - format f("GAMESPEED %1%"); + format f("%1%"); f % globalSpeedLevel; - ActionPerformed(f.str()); + ActionPerformed(ACTION_GAMESPEED,f.str()); switch (globalSpeedLevel) { case 0: @@ -141,9 +142,9 @@ void BlockGame::setGameSpeed(Uint8 globalSpeedLevel) void BlockGame::setHandicap(Uint8 globalHandicap) { - format f("HANDICAP %1%"); + format f("%1%"); f % globalHandicap; - ActionPerformed(f.str()); + ActionPerformed(ACTION_HANDICAP, f.str()); handicap=1000*((Uint32)globalHandicap); } @@ -257,9 +258,9 @@ int BlockGame::GetCursorY() void BlockGame::MoveCursorTo(int x, int y) { - format f("MC2 %1% %2%"); + format f("%1% %2%"); f % x % y; - ActionPerformed(f.str()); + ActionPerformed(ACTION_MOVECURSORTO,f.str()); cursorx = x; cursory = y; } @@ -357,13 +358,13 @@ void BlockGame::NewGame(int tx, int ty, unsigned int ticks) lastAImove = ticks+3000; showGame = true; theReplay = Replay(); - ActionPerformed("NEW"); + ActionPerformed(ACTION_NEW,""); } //NewGame void BlockGame::NewTimeTrialGame(int x,int y, unsigned int ticks) { NewGame(x,y,ticks); - ActionPerformed("NEWTT"); + ActionPerformed(ACTION_NEWTT,""); timetrial = true; putStartBlocks(); } @@ -450,7 +451,7 @@ void BlockGame::nextLevel(unsigned int ticks) void BlockGame::NewVsGame(int tx, int ty, BlockGame *target,unsigned int ticks) { NewGame(tx, ty,ticks); - ActionPerformed("NEWVS"); + ActionPerformed(ACTION_NEWVS,""); vsMode = true; putStartBlocks(); garbageTarget = target; @@ -504,7 +505,7 @@ void BlockGame::playNetwork(int tx, int ty,unsigned int ticks) //Prints "winner" and ends game void BlockGame::setPlayerWon() { - ActionPerformed("WON"); + ActionPerformed(ACTION_WIN,""); if (!bGameOver) { gameEndedAfter = ticks-gameStartedAt; //We game ends now! @@ -548,7 +549,7 @@ void BlockGame::setDisconnect() //Prints "draw" and ends the game void BlockGame::setDraw() { - ActionPerformed("DRAW"); + ActionPerformed(ACTION_DRAW, ""); bGameOver = true; if(!AI_Enabled && !bReplaying) { @@ -742,9 +743,9 @@ void BlockGame::putStartBlocks() void BlockGame::putStartBlocks(Uint32 n) { - format f("STARTBLOCKS %1%"); + format f("%1%"); f % n; - ActionPerformed(f.str()); + ActionPerformed(ACTION_STARTBLOCKS ,f.str()); for (int i=0; i<7; i++) for (int j=0; j<30; j++) { @@ -907,9 +908,9 @@ void BlockGame::ReduceStuff() //Creates garbage using a given wide and height bool BlockGame::CreateGarbage(int wide, int height) { - format f("CG %1% %2%"); + format f("%1% %2%"); f % wide % height; - ActionPerformed(f.str()); + ActionPerformed(ACTION_CREATEGARBAGE,f.str()); #if NETWORK if (bNetworkPlayer) { @@ -953,7 +954,7 @@ bool BlockGame::CreateGarbage(int wide, int height) //Creates garbage using a given wide and height bool BlockGame::CreateGreyGarbage() { - ActionPerformed("GG"); + ActionPerformed(ACTION_CREATEGRAYGARBAGE,""); #if NETWORK if (bNetworkPlayer) { @@ -1373,7 +1374,7 @@ void BlockGame::ClearBlocks() //prints "Game Over" and ends game void BlockGame::SetGameOver() { - ActionPerformed("GAMEOVER"); + ActionPerformed(ACTION_GAMEOVER ,""); if (!bGameOver) { gameEndedAfter = ticks-gameStartedAt; //We game ends now! @@ -1388,7 +1389,7 @@ void BlockGame::SetGameOver() } else { - strncpy(theReplay.name,name,30); + theReplay.setName(name); } theReplay.setFinalFrame(getPackage(), 0); bGameOver = true; @@ -1474,9 +1475,9 @@ void BlockGame::FallDown() //Moves the cursor, receaves N,S,E or W as a char an moves as desired void BlockGame::MoveCursor(char way) { - format f("MC %1%"); + format f("%1%"); f % way; - ActionPerformed(f.str()); + ActionPerformed(ACTION_MOVECURSOR,f.str()); if (!bGameOver) //If game over nothing happends { if ((way == 'N') && ((cursory<10)||(TowerHeight>12) ||(((pixels==bsize)||(pixels==0)) && (cursory<11)))) @@ -1493,6 +1494,7 @@ void BlockGame::MoveCursor(char way) //switches the two blocks at the cursor position, unless game over void BlockGame::SwitchAtCursor() { + ActionPerformed(ACTION_SWITCH,""); if ((board[cursorx][cursory+1]<7) && (board[cursorx+1][cursory+1]<7) && (!bGameOver) && ((!puzzleMode)||(MovesLeft>0)) && (gameStartedAt ticks+50) + while(newtick > ticks+50) { - ticks = newtick; + ticks+=50; Update(); } } -void BlockGame::ActionPerformed(string action) { +void BlockGame::Update(int newtick) +{ + + UpdateInternal(newtick); +} + +void BlockGame::ActionPerformed(int action, string param) +{ if(bGameOver) return; - theReplay.addAction(ticks,action); + theReplay.addAction(ticks-gameStartedAt,action,param); +} + +void BlockGame::PerformAction(int tick, int action, string param) +{ + UpdateInternal(tick); + switch(action) + { + case ACTION_UPDATE: + break; + case ACTION_MOVECURSOR: + MoveCursor(param.at(0)); + break; + case ACTION_MOVECURSORTO: + { + stringstream ss(param); + int p1,p2; + ss >> p1 >> p2; + MoveCursorTo(p1,p2); + } + break; + case ACTION_SWITCH: + SwitchAtCursor(); + break; + case ACTION_PUSH: + PushLine(); + break; + case ACTION_CREATEGARBAGE: + { + stringstream ss(param); + int p1,p2; + ss >> p1 >> p2; + CreateGarbage(p1,p2); + } + break; + case ACTION_CREATEGRAYGARBAGE: + CreateGreyGarbage(); + break; + case ACTION_GAMEOVER: + SetGameOver(); + break; + case ACTION_WIN: + setPlayerWon(); + break; + case ACTION_DRAW: + setDraw(); + break; + case ACTION_GAMESPEED: + setGameSpeed(boost::lexical_cast(param)); + break; + case ACTION_HANDICAP: + setHandicap(boost::lexical_cast(param)); + break; + case ACTION_NEW: + case ACTION_NEWTT: + case ACTION_NEWVS: + break; + case ACTION_STARTBLOCKS: + putStartBlocks(boost::lexical_cast(param)); + break; + case ACTION_NOP: + break; + default: + cerr << "Unknown action: " << action << " " << param << endl; + break; + }; } //////////////////////////////////////////////////////////////////////////////// ///////////////////////// BlockAttack class end //////////////////////////////// diff --git a/source/code/BlockGame.hpp b/source/code/BlockGame.hpp index 6a365d8..e1d5ce1 100644 --- a/source/code/BlockGame.hpp +++ b/source/code/BlockGame.hpp @@ -43,6 +43,24 @@ http://blockattack.sf.net #define CHAINPLACE 10000000 #define NUMBEROFCHAINS 100 +#define ACTION_UPDATE 0 +#define ACTION_MOVECURSOR 1 +#define ACTION_MOVECURSORTO 2 +#define ACTION_SWITCH 3 +#define ACTION_PUSH 4 +#define ACTION_CREATEGARBAGE 5 +#define ACTION_CREATEGRAYGARBAGE 6 +#define ACTION_GAMEOVER 7 +#define ACTION_WIN 8 +#define ACTION_DRAW 9 +#define ACTION_GAMESPEED 10 +#define ACTION_HANDICAP 11 +#define ACTION_NEW 12 +#define ACTION_NEWTT 13 +#define ACTION_NEWVS 14 +#define ACTION_STARTBLOCKS 15 +#define ACTION_NOP 16 + //////////////////////////////////////////////////////////////////////////////// //The BloackGame class represents a board, score, time etc. for a single player/ //////////////////////////////////////////////////////////////////////////////// @@ -295,12 +313,14 @@ private: private: - void ActionPerformed(string action); + void ActionPerformed(int action, string param); //Updates evrything, if not called nothing happends void Update(); + void UpdateInternal(int newtick); public: void Update(int newtick); + void PerformAction(int tick, int action, string param); }; //////////////////////////////////////////////////////////////////////////////// ///////////////////////// BlockAttack class end //////////////////////////////// diff --git a/source/code/replay.cpp b/source/code/replay.cpp index 49481a3..cc74e33 100644 --- a/source/code/replay.cpp +++ b/source/code/replay.cpp @@ -42,7 +42,7 @@ Replay::Replay(const Replay& r) nrOfFrames = r.nrOfFrames; isLoaded = r.isLoaded; theResult = r.theResult; - strncpy(name,r.name,sizeof(name)); + name = r.name; actions = r.actions; } @@ -122,10 +122,10 @@ bool Replay::saveReplay(string filename) saveFile.open(filename.c_str(),ios::binary|ios::trunc); if (saveFile) { - saveFile << "0 PLAYER 1\n"; - saveFile << "0 NAME " << name << "\n"; + saveFile << "0 16 PLAYER 1\n"; + saveFile << "0 16 NAME " << name << "\n"; for(int i = 0;i < actions.size(); ++i) { - saveFile << actions.at(i).time << " " << actions.at(i).action << "\n"; + saveFile << actions.at(i).time << " " << actions.at(i).action << " " << actions.at(i).param << "\n"; } saveFile.close(); return true; @@ -182,34 +182,38 @@ bool Replay::saveReplay(string filename,Replay p2) bool Replay::loadReplay(string filename) { isLoaded = true; + actions.clear(); ifstream loadFile; loadFile.open(filename.c_str(),ios::binary); if (loadFile) { - Uint32 version; - loadFile.read(reinterpret_cast(&version),sizeof(Uint32)); - switch (version) - { - case 3: - cout << "Loading a version 3 save game" << endl; - Uint8 nrOfPlayers; - boardPackage bp; - loadFile.read(reinterpret_cast(&nrOfPlayers),sizeof(Uint8)); - loadFile.read(reinterpret_cast(&nrOfFrames),sizeof(Uint32)); - bps.clear(); - for (int i=0; (i> time >> action; + if(action == 16) { - loadFile.read(reinterpret_cast(&bp),sizeof(bp)); - bps.push_back(bp); + string command; + loadFile >> command; + if(command == "NAME") + getline(loadFile,name,'\n'); + if(command == "PLAYER") { + getline(loadFile,restOfLine,'\n'); + if(restOfLine != "1") + break; + } + } else + { + getline(loadFile,restOfLine,'\n'); + Action a; + a.time = time; + a.action = action; + a.param = restOfLine; + actions.push_back(a); } - loadFile.read(reinterpret_cast(&finalPack),sizeof(finalPack)); - loadFile.read(reinterpret_cast(&theResult),sizeof(theResult)); - loadFile.read(reinterpret_cast(&name),sizeof(name)); - break; - default: - cout << "Unknown version" << endl; - return false; - }; + + } loadFile.close(); cout << "Loaded 1 player" << endl; } @@ -277,10 +281,21 @@ bool Replay::loadReplay2(string filename) } -void Replay::addAction(int tick, string action) { - cout << tick << " " << action << endl; +void Replay::setName(string name) +{ + this->name = name; +} + +string Replay::getName() const +{ + return name; +} + +void Replay::addAction(int tick, int action, string param) { + cout << tick << " " << action << " " << param << endl; Action a; a.time = tick; a.action = action; + a.param = param; actions.push_back(a); } diff --git a/source/code/replay.h b/source/code/replay.h index d3520ff..aa9d85b 100644 --- a/source/code/replay.h +++ b/source/code/replay.h @@ -58,7 +58,8 @@ struct boardPackage //92 bytes struct Action { Uint32 time; - string action; + int action; + string param; }; class Replay @@ -76,12 +77,12 @@ private: enum { gameOver=0, winner, looser, draw } theResult; //If we are loaded from a file, then we are read only bool isLoaded; - +//Store player name + string name; public: - //Store player name - char name[30]; + Replay(); //Constructor Replay(const Replay& r); //Copy constructor @@ -93,14 +94,16 @@ public: int getFinalStatus(); //Return the result: winner, looser, draw or gameOver bool isFinnished(Uint32); //Returns true if we are done - //New replay type 1.5.0+: - void addAction(int tick, string action); + //New replay type 2.0.0+: + void addAction(int tick, int action, string param); //Ok, I'll ignore this for some time, and just save to file, if however, we ever use a dynamic saving structure, we are fucked bool saveReplay(string); //Saves a replay bool saveReplay(string,Replay p2); //saves a replay, plus another replay given as a parameter bool loadReplay(string); //laods a replay - bool loadReplay2(string); //loads the second part of the replay file, if it exists, returns false otherwise + bool loadReplay2(string); + void setName(string name); + string getName() const; //loads the second part of the replay file, if it exists, returns false otherwise };