commit 07383b8f
Network changes. Not tested yet
git-svn-id: https://blockattack.svn.sourceforge.net/svnroot/blockattack/trunk@150 9d7177f8-192b-0410-8f35-a16a89829b06
Changed files
| M | source/code/BlockGame.hpp before |
| M | source/code/NetworkThing.hpp before |
diff --git a/source/code/BlockGame.hpp b/source/code/BlockGame.hpp
index 8137ac7..50153f1 100644
--- a/source/code/BlockGame.hpp
+++ b/source/code/BlockGame.hpp
@@ -207,6 +207,14 @@ public:
void PushLine();
void Update(int newtick);
void PerformAction(int tick, int action, string param);
+ /**
+ *
+ * @param tick Tick of the action
+ * @param action The action
+ * @param param Params.
+ * @return 1 if an action was selected
+ */
+ int GotAction(int &tick,int &action,string ¶m);
#if NETWORK
//network play
void playNetwork(int tx, int ty,unsigned int ticks);
@@ -302,14 +310,6 @@ private:
///////////////////////////// AI ends here! //////////////////////////////
//////////////////////////////////////////////////////////////////////////
- /**
- *
- * @param tick Tick of the action
- * @param action The action
- * @param param Params.
- * @return 1 if an action was selected
- */
- int GotAction(int &tick,int &action,string ¶m);
void ActionPerformed(int action, string param);
void PushLineInternal();
//Updates evrything, if not called nothing happends
diff --git a/source/code/NetworkThing.hpp b/source/code/NetworkThing.hpp
index 8fb6dfc..9308462 100644
--- a/source/code/NetworkThing.hpp
+++ b/source/code/NetworkThing.hpp
@@ -21,6 +21,9 @@ http://blockattack.sf.net
===========================================================================
*/
+#include "BlockGame.hpp"
+
+
#if NETWORK
//The network interface
@@ -212,16 +215,33 @@ public:
if (weAreConnected)
{
//cout << "Creating package" << endl;
- boardPackage boardpack = bgHome->getPackage();
- ENetPacket * packet = enet_packet_create (&boardpack,
- sizeof(boardPackage),
- 0);
- //Now lets send the package
- if (weAreAServer)
- enet_host_broadcast (server, 0, packet);
- else if (weAreAClient)
- enet_peer_send (peer, 0, packet);
- //cout << "Package sent" << endl;
+ Sint32 tick;
+ Sint32 action;
+ Sint32 paramsize;
+ string param;
+ while(bgHome->GotAction(tick,action,param))
+ {
+ paramsize = param.length();
+ char *tmpPacket = (char*)malloc(sizeof(Sint32)*3+param.length()+1);
+ if(!tmpPacket) {
+ cerr << "Failed to alloc memoroy" << endl;
+ exit(-3);
+ }
+ memcpy(tmpPacket,&tick,sizeof(Sint32));
+ memcpy(tmpPacket+sizeof(Sint32),&action,sizeof(Sint32));
+ memcpy(tmpPacket+sizeof(Sint32)*2,¶msize,sizeof(Sint32));
+ memcpy(tmpPacket+sizeof(Sint32)*3,param.c_str(),paramsize+1);
+ ENetPacket * packet = enet_packet_create (tmpPacket,
+ sizeof(Sint32)*3+param.length()+1,
+ ENET_PACKET_FLAG_RELIABLE);
+ //Now lets send the package
+ if (weAreAServer)
+ enet_host_broadcast (server, 0, packet);
+ else if (weAreAClient)
+ enet_peer_send (peer, 0, packet);
+ //cout << "Package sent" << endl;
+ free(tmpPacket);
+ }
//See if we are game over and in that case notify the other player
if ((gameHasStarted)&&(bgHome->isGameOver()))
@@ -293,10 +313,17 @@ public:
//cout << "Package recieved" << endl;
if (event.channelID==0) //Unreliable (only boardPacks)
{
- boardPackage bpack;
+ Sint32 tick;
+ Sint32 action;
+ string param;
+ memcpy(&tick,event.packet->data,sizeof(Sint32));
+ memcpy(&action,event.packet->data+sizeof(Sint32),sizeof(Sint32));
+ param = (const char*) (event.packet->data+sizeof(Sint32)*3);
+ //boardPackage bpack;
//cout << "Package size: "<< event.packet->dataLength << " should be: " << sizeof(boardPackage) << endl;
- memcpy(&bpack,(const char*)event.packet->data,sizeof(boardPackage));
- bgAway->setBoard(bpack);
+ //memcpy(&bpack,(const char*)event.packet->data,sizeof(boardPackage));
+ //bgAway->setBoard(bpack);
+ bgAway->PerformAction(tick,action,param);
}
if (event.channelID==1) //reliable (used for GameOver notifications only!)
{