Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. ticker( new TickOrganizer() ),
  134. path( (const char *)( worldsDir + "/" + name ) ),
  135. stop( 0 ),
  136. tickId( 0 ),
  137. nextEntityId( 0 ),
  138. generator( 0 ),
  139. loader( 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. loader = new WorldLoader( this );
  157. start();
  158. }
  159. Game::~Game()
  160. {
  161. dimensions->release();
  162. updates->release();
  163. clients->release();
  164. generator->release();
  165. loader->release();
  166. }
  167. void Game::thread()
  168. {
  169. ZeitMesser m;
  170. while( !stop )
  171. {
  172. m.messungStart();
  173. ticker->nextTick();
  174. int index = 0;
  175. Array<int> removed;
  176. cs.Enter();
  177. for( auto player = clients->getIterator(); player; player++, index++ )
  178. {
  179. if( !player->isOnline() )
  180. {
  181. Datei pFile;
  182. pFile.setDatei( path + "/player/" + player->zEntity()->getName() );
  183. if( pFile.open( Datei::Style::schreiben ) )
  184. PlayerEntityType::INSTANCE->saveEntity( player->zEntity(), &pFile );
  185. removed.add( player, 0 );
  186. }
  187. }
  188. for( auto i = removed.getIterator(); i; i++ )
  189. clients->remove( i );
  190. cs.Leave();
  191. for( auto dim = dimensions->getIterator(); dim; dim++ )
  192. dim->tickEntities( this );
  193. while( updates->z( 0 ) )
  194. {
  195. WorldUpdate *update = updates->z( 0 );
  196. for( auto client = clients->getIterator(); client; client++ )
  197. client->sendWorldUpdate( update );
  198. if( !zDimension( update->getAffectedDimension() ) )
  199. addDimension( new Dimension( update->getAffectedDimension() ) );
  200. update->onUpdate( zDimension( update->getAffectedDimension() ) );
  201. updates->remove( 0 );
  202. }
  203. for( auto client = clients->getIterator(); client; client++ )
  204. client->reply( this );
  205. m.messungEnde();
  206. double sec = m.getSekunden();
  207. if( sec < 0.05 )
  208. Sleep( (int)( ( 0.05 - sec ) * 1000 ) );
  209. }
  210. save();
  211. }
  212. void Game::api( Framework::StreamReader *zRequest, GameClient *zOrigin )
  213. {
  214. char type;
  215. zRequest->lese( &type, 1 );
  216. NetworkResponse response;
  217. switch( type )
  218. {
  219. case 1: // world
  220. {
  221. int dimensionId;
  222. zRequest->lese( (char *)&dimensionId, 4 );
  223. Dimension *dim = zDimension( dimensionId );
  224. if( !dim )
  225. {
  226. dim = new Dimension( dimensionId );
  227. addDimension( dim );
  228. }
  229. dim->api( zRequest, &response );
  230. break;
  231. }
  232. default:
  233. std::cout << "received unknown api request in game with type " << (int)type << "\n";
  234. }
  235. if( response.isBroadcast() )
  236. distributeResponse( &response );
  237. else
  238. zOrigin->sendResponse( &response );
  239. }
  240. void Game::distributeResponse( NetworkResponse *zResponse )
  241. {
  242. for( auto client = clients->getIterator(); client; client++ )
  243. client->sendResponse( zResponse );
  244. }
  245. void Game::requestWorldUpdate( WorldUpdate *update )
  246. {
  247. cs.Enter();
  248. updates->add( update );
  249. cs.Leave();
  250. }
  251. GameClient *Game::addPlayer( FCKlient *client, Framework::Text name )
  252. {
  253. cs.Enter();
  254. Datei pFile;
  255. pFile.setDatei( path + "/player/" + name );
  256. Player *player;
  257. if( !pFile.existiert() || !pFile.open( Datei::Style::lesen ) )
  258. {
  259. player = (Player *)PlayerEntityType::INSTANCE->createEntityAt( Vec3<float>( 0, 0, 0 ), OverworldDimension::ID, this );
  260. }
  261. else
  262. {
  263. player = (Player *)PlayerEntityType::INSTANCE->loadEntity( this, &pFile );
  264. pFile.close();
  265. }
  266. requestArea( { (int)player->getPosition().x - 100, (int)player->getPosition().y - 100, (int)player->getPosition().x + 100, (int)player->getPosition().y + 100, player->getCurrentDimensionId() } );
  267. while( !zDimension( OverworldDimension::ID ) )
  268. {
  269. cs.Leave();
  270. Sleep( 1000 );
  271. cs.Enter();
  272. }
  273. zDimension( OverworldDimension::ID )->addEntity( player );
  274. GameClient *gameClient = new GameClient( player, client );
  275. clients->add( gameClient );
  276. cs.Leave();
  277. return dynamic_cast<GameClient *>( gameClient->getThis() );
  278. }
  279. bool Game::doesChunkExist( int x, int y, int dimension ) const
  280. {
  281. Dimension *dim = zDimension( dimension );
  282. return ( dim && dim->hasChunck( x, y ) ) || loader->existsChunk( x, y, dimension );
  283. }
  284. Block *Game::zBlockAt( Framework::Vec3<int> location, int dimension ) const
  285. {
  286. Dimension *dim = zDimension( dimension );
  287. if( dim )
  288. return dim->zBlock( location );
  289. return 0;
  290. }
  291. Dimension *Game::zDimension( int id ) const
  292. {
  293. for( auto dim = dimensions->getIterator(); dim; dim++ )
  294. {
  295. if( dim->getDimensionId() == id )
  296. return dim;
  297. }
  298. return 0;
  299. }
  300. Framework::Punkt Game::getChunkCenter( int x, int y ) const
  301. {
  302. return Punkt( (int)floor( ( (float)x + CHUNK_SIZE / 2 ) / CHUNK_SIZE ), (int)floor( ( (float)y + CHUNK_SIZE / 2 ) / CHUNK_SIZE ) );
  303. }
  304. Area Game::getChunckArea( Punkt center ) const
  305. {
  306. return { center.x - CHUNK_SIZE / 2, center.y - CHUNK_SIZE / 2, center.x + CHUNK_SIZE / 2, center.y + CHUNK_SIZE / 2, 0 };
  307. }
  308. Framework::Text Game::getWorldDirectory() const
  309. {
  310. return path;
  311. }
  312. void Game::requestArea( Area area )
  313. {
  314. generator->requestGeneration( area );
  315. loader->requestLoading( area );
  316. }
  317. void Game::save() const
  318. {
  319. Datei d;
  320. d.setDatei( path + "/eid" );
  321. d.open( Datei::Style::schreiben );
  322. d.schreibe( (char *)&nextEntityId, 4 );
  323. d.close();
  324. for( auto dim = dimensions->getIterator(); dim; dim++ )
  325. dim->save( path );
  326. }
  327. void Game::requestStop()
  328. {
  329. stop = 1;
  330. warteAufThread( 1000000 );
  331. }
  332. void Game::addDimension( Dimension *d )
  333. {
  334. dimensions->add( d );
  335. }
  336. int Game::getNextEntityId()
  337. {
  338. cs.Enter();
  339. int result = nextEntityId++;
  340. cs.Leave();
  341. return result;
  342. }