Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "Game.h"
  2. #include "Zeit.h"
  3. #include "Player.h"
  4. #include "OverworldDimension.h"
  5. #include "AddChunkUpdate.h"
  6. using namespace Framework;
  7. GameClient::GameClient( Player *zPlayer, FCKlient *client )
  8. : ReferenceCounter(),
  9. zPlayer( zPlayer ),
  10. client( client ),
  11. writer( client->zClient() ),
  12. viewDistance( 5 ),
  13. first( 1 ),
  14. online( 1 )
  15. {}
  16. GameClient::~GameClient()
  17. {
  18. client->release();
  19. }
  20. void GameClient::sendWorldUpdate( WorldUpdate *zUpdate )
  21. {
  22. if( zPlayer->getCurrentDimensionId() == zUpdate->getAffectedDimension() )
  23. {
  24. auto pos = ( Vec3<int> )zPlayer->getPosition();
  25. if( abs( pos.x - zUpdate->getMinAffectedPoint().x ) < viewDistance * CHUNK_SIZE || ( abs( pos.x - zUpdate->getMaxAffectedPoint().x ) < viewDistance * CHUNK_SIZE ) || abs( pos.y - zUpdate->getMinAffectedPoint().y ) < viewDistance * CHUNK_SIZE || ( abs( pos.y - zUpdate->getMaxAffectedPoint().y ) < viewDistance * CHUNK_SIZE ) )
  26. {
  27. cs.Enter();
  28. writer.schreibe( (char *)&Message::INGAME_MESSAGE, 1 );
  29. writer.schreibe( (char *)&Message::WORLD_UPDATE, 1 );
  30. zUpdate->write( &writer );
  31. cs.Leave();
  32. }
  33. }
  34. }
  35. void GameClient::reply( Game *zGame )
  36. {
  37. cs.Enter();
  38. for( auto req = requests.getIterator(); req; req++ )
  39. zGame->api( req, this );
  40. requests.leeren();
  41. cs.Leave();
  42. int x = (int)zPlayer->getPosition().x;
  43. int y = (int)zPlayer->getPosition().y;
  44. int d = zPlayer->getCurrentDimensionId();
  45. // send world to client
  46. if( first )
  47. {
  48. first = 0;
  49. for( int xP = x - CHUNK_SIZE * viewDistance; x <= x + CHUNK_SIZE * viewDistance; x += CHUNK_SIZE )
  50. {
  51. for( int yP = y - CHUNK_SIZE * viewDistance; y <= y + CHUNK_SIZE * viewDistance; y += CHUNK_SIZE )
  52. {
  53. Chunk *chunk = zGame->zDimension( d )->zChunk( zGame->getChunkCenter( xP, yP ) );
  54. if( chunk )
  55. {
  56. AddChunkUpdate update( dynamic_cast<Chunk *>( chunk->getThis() ) );
  57. sendWorldUpdate( &update );
  58. }
  59. }
  60. }
  61. zGame->requestArea( { x - CHUNK_SIZE * viewDistance, y - CHUNK_SIZE * viewDistance, x + CHUNK_SIZE * viewDistance, y + CHUNK_SIZE * viewDistance, d } );
  62. }
  63. else
  64. {
  65. Punkt lastMin = zGame->getChunkCenter( (int)lastPos.x - CHUNK_SIZE * viewDistance, (int)lastPos.y - CHUNK_SIZE * viewDistance );
  66. Punkt curMin = zGame->getChunkCenter( x - CHUNK_SIZE * viewDistance, y - CHUNK_SIZE * viewDistance );
  67. Punkt lastMax = zGame->getChunkCenter( (int)lastPos.x + CHUNK_SIZE * viewDistance, (int)lastPos.y + CHUNK_SIZE * viewDistance );
  68. Punkt curMax = zGame->getChunkCenter( x + CHUNK_SIZE * viewDistance, y + CHUNK_SIZE * viewDistance );
  69. for( int xP = curMin.x; x <= curMax.x; x += CHUNK_SIZE )
  70. {
  71. for( int yP = curMin.y; y <= curMax.y; y += CHUNK_SIZE )
  72. {
  73. if( xP < lastMin.x || xP > lastMax.x || yP < lastMin.y || yP > lastMax.y )
  74. {
  75. Chunk *chunk = zGame->zDimension( d )->zChunk( zGame->getChunkCenter( x, y ) );
  76. if( chunk )
  77. {
  78. AddChunkUpdate update( dynamic_cast<Chunk *>( chunk->getThis() ) );
  79. sendWorldUpdate( &update );
  80. }
  81. else
  82. {
  83. zGame->requestArea( zGame->getChunckArea( Punkt( xP, yP ) ) );
  84. }
  85. }
  86. }
  87. }
  88. }
  89. lastPos = zPlayer->getPosition();
  90. }
  91. void GameClient::logout()
  92. {
  93. online = 0;
  94. }
  95. void GameClient::addMessage( StreamReader *reader )
  96. {
  97. short len = 0;
  98. reader->lese( (char *)&len, 2 );
  99. InMemoryBuffer *buffer = new InMemoryBuffer();
  100. char *tmp = new char[ len ];
  101. reader->lese( tmp, len );
  102. buffer->schreibe( tmp, len );
  103. delete[]tmp;
  104. cs.Enter();
  105. requests.add( buffer );
  106. cs.Leave();
  107. }
  108. bool GameClient::isOnline() const
  109. {
  110. return online;
  111. }
  112. void GameClient::sendResponse( NetworkResponse *zResponse )
  113. {
  114. if( zResponse->isAreaAffected( { lastPos.x - (float)CHUNK_SIZE * (float)viewDistance, lastPos.y - (float)CHUNK_SIZE * (float)viewDistance, 0.f }, { lastPos.x + (float)CHUNK_SIZE * (float)viewDistance, lastPos.y + (float)CHUNK_SIZE * (float)viewDistance, (float)WORLD_HEIGHT } ) )
  115. {
  116. cs.Leave();
  117. writer.schreibe( (char *)&Message::INGAME_MESSAGE, 1 );
  118. writer.schreibe( (char *)&Message::API_MESSAGE, 1 );
  119. zResponse->writeTo( client->zWriter() );
  120. cs.Leave();
  121. }
  122. }
  123. Player *GameClient::zEntity() const
  124. {
  125. return zPlayer;
  126. }
  127. Game::Game( Framework::Text name, Framework::Text worldsDir )
  128. : Thread(),
  129. name( name ),
  130. dimensions( new RCArray<Dimension>() ),
  131. updates( new RCArray<WorldUpdate>() ),
  132. clients( new RCArray<GameClient>() ),
  133. generator( 0 ),
  134. loader( new WorldLoader( this ) ),
  135. ticker( new TickOrganizer() ),
  136. path( (const char *)( worldsDir + "/" + name ) ),
  137. stop( 0 ),
  138. tickId( 0 ),
  139. nextEntityId( 0 )
  140. {
  141. if( !DateiExistiert( worldsDir + "/" + name ) )
  142. DateiPfadErstellen( worldsDir + "/" + name + "/" );
  143. Datei d;
  144. d.setDatei( path + "/eid" );
  145. if( d.existiert() )
  146. {
  147. d.open( Datei::Style::lesen );
  148. d.lese( (char *)&nextEntityId, 4 );
  149. d.close();
  150. }
  151. int seed = 0;
  152. int index = 0;
  153. for( char *n = name; n; n++ )
  154. seed += (int)pow( (float)*n * 31, ( float )++index );
  155. generator = new WorldGenerator( seed, this );
  156. start();
  157. }
  158. Game::~Game()
  159. {
  160. dimensions->release();
  161. updates->release();
  162. clients->release();
  163. generator->release();
  164. loader->release();
  165. }
  166. void Game::thread()
  167. {
  168. ZeitMesser m;
  169. while( !stop )
  170. {
  171. m.messungStart();
  172. ticker->nextTick();
  173. int index = 0;
  174. Array<int> removed;
  175. cs.Enter();
  176. for( auto player = clients->getIterator(); player; player++, index++ )
  177. {
  178. if( !player->isOnline() )
  179. {
  180. Datei pFile;
  181. pFile.setDatei( path + "/player/" + player->zEntity()->getName() );
  182. if( pFile.open( Datei::Style::schreiben ) )
  183. PlayerEntityType::INSTANCE->saveEntity( player->zEntity(), &pFile );
  184. removed.add( player, 0 );
  185. }
  186. }
  187. for( auto i = removed.getIterator(); i; i++ )
  188. clients->remove( i );
  189. cs.Leave();
  190. for( auto dim = dimensions->getIterator(); dim; dim++ )
  191. dim->tickEntities( this );
  192. while( updates->z( 0 ) )
  193. {
  194. WorldUpdate *update = updates->z( 0 );
  195. for( auto client = clients->getIterator(); client; client++ )
  196. client->sendWorldUpdate( update );
  197. if( !zDimension( update->getAffectedDimension() ) )
  198. addDimension( new Dimension( update->getAffectedDimension() ) );
  199. update->onUpdate( zDimension( update->getAffectedDimension() ) );
  200. updates->remove( 0 );
  201. }
  202. for( auto client = clients->getIterator(); client; client++ )
  203. client->reply( this );
  204. m.messungEnde();
  205. double sec = m.getSekunden();
  206. if( sec < 0.05 )
  207. Sleep( (int)( ( 0.05 - sec ) * 1000 ) );
  208. }
  209. save();
  210. }
  211. void Game::api( Framework::StreamReader *zRequest, GameClient *zOrigin )
  212. {
  213. char type;
  214. zRequest->lese( &type, 1 );
  215. NetworkResponse response;
  216. switch( type )
  217. {
  218. case 1: // world
  219. {
  220. int dimensionId;
  221. zRequest->lese( (char *)&dimensionId, 4 );
  222. Dimension *dim = zDimension( dimensionId );
  223. if( !dim )
  224. {
  225. dim = new Dimension( dimensionId );
  226. addDimension( dim );
  227. }
  228. dim->api( zRequest, &response );
  229. break;
  230. }
  231. default:
  232. std::cout << "received unknown api request in game with type " << (int)type << "\n";
  233. }
  234. if( response.isBroadcast() )
  235. distributeResponse( &response );
  236. else
  237. zOrigin->sendResponse( &response );
  238. }
  239. void Game::distributeResponse( NetworkResponse *zResponse )
  240. {
  241. for( auto client = clients->getIterator(); client; client++ )
  242. client->sendResponse( zResponse );
  243. }
  244. void Game::requestWorldUpdate( WorldUpdate *update )
  245. {
  246. cs.Enter();
  247. updates->add( update );
  248. cs.Leave();
  249. }
  250. GameClient *Game::addPlayer( FCKlient *client, Framework::Text name )
  251. {
  252. cs.Enter();
  253. Datei pFile;
  254. pFile.setDatei( path + "/player/" + name );
  255. Player *player;
  256. if( !pFile.existiert() || !pFile.open( Datei::Style::lesen ) )
  257. {
  258. player = (Player *)PlayerEntityType::INSTANCE->createEntityAt( Vec3<float>( 0, 0, 0 ), OverworldDimension::ID, this );
  259. }
  260. else
  261. {
  262. player = (Player *)PlayerEntityType::INSTANCE->loadEntity( this, &pFile );
  263. pFile.close();
  264. }
  265. requestArea( { (int)player->getPosition().x - 100, (int)player->getPosition().y - 100, (int)player->getPosition().x + 100, (int)player->getPosition().y + 100, player->getCurrentDimensionId() } );
  266. while( !zDimension( OverworldDimension::ID ) )
  267. {
  268. cs.Leave();
  269. Sleep( 1000 );
  270. cs.Enter();
  271. }
  272. zDimension( OverworldDimension::ID )->addEntity( player );
  273. GameClient *gameClient = new GameClient( player, client );
  274. clients->add( gameClient );
  275. cs.Leave();
  276. return dynamic_cast<GameClient *>( gameClient->getThis() );
  277. }
  278. bool Game::doesChunkExist( int x, int y, int dimension ) const
  279. {
  280. Dimension *dim = zDimension( dimension );
  281. return ( dim && dim->hasChunck( x, y ) ) || loader->existsChunk( x, y, dimension );
  282. }
  283. Block *Game::zBlockAt( Framework::Vec3<int> location, int dimension ) const
  284. {
  285. Dimension *dim = zDimension( dimension );
  286. if( dim )
  287. return dim->zBlock( location );
  288. return 0;
  289. }
  290. Dimension *Game::zDimension( int id ) const
  291. {
  292. for( auto dim = dimensions->getIterator(); dim; dim++ )
  293. {
  294. if( dim->getDimensionId() == id )
  295. return dim;
  296. }
  297. return 0;
  298. }
  299. Framework::Punkt Game::getChunkCenter( int x, int y ) const
  300. {
  301. return Punkt( (int)floor( ( (float)x + CHUNK_SIZE / 2 ) / CHUNK_SIZE ), (int)floor( ( (float)y + CHUNK_SIZE / 2 ) / CHUNK_SIZE ) );
  302. }
  303. Area Game::getChunckArea( Punkt center ) const
  304. {
  305. return { center.x - CHUNK_SIZE / 2, center.y - CHUNK_SIZE / 2, center.x + CHUNK_SIZE / 2, center.y + CHUNK_SIZE / 2, 0 };
  306. }
  307. Framework::Text Game::getWorldDirectory() const
  308. {
  309. return path;
  310. }
  311. void Game::requestArea( Area area )
  312. {
  313. generator->requestGeneration( area );
  314. loader->requestLoading( area );
  315. }
  316. void Game::save() const
  317. {
  318. Datei d;
  319. d.setDatei( path + "/eid" );
  320. d.open( Datei::Style::schreiben );
  321. d.schreibe( (char *)&nextEntityId, 4 );
  322. d.close();
  323. for( auto dim = dimensions->getIterator(); dim; dim++ )
  324. dim->save( path );
  325. }
  326. void Game::requestStop()
  327. {
  328. stop = 1;
  329. warteAufThread( 1000000 );
  330. }
  331. void Game::addDimension( Dimension *d )
  332. {
  333. dimensions->add( d );
  334. }
  335. int Game::getNextEntityId()
  336. {
  337. cs.Enter();
  338. int result = nextEntityId++;
  339. cs.Leave();
  340. return result;
  341. }