Game.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #include <Text.h>
  3. #include <Punkt.h>
  4. #include <Thread.h>
  5. #include <Network.h>
  6. #include <Critical.h>
  7. #include "Dimension.h"
  8. #include "Player.h"
  9. #include "WorldGenerator.h"
  10. #include "Constants.h"
  11. #include "WorldUpdate.h"
  12. #include "WorldLoader.h"
  13. #include "TickOrganizer.h"
  14. #include "Server.h"
  15. #include "InMemoryBuffer.h"
  16. class FCKlient;
  17. class GameClient : public virtual Framework::ReferenceCounter
  18. {
  19. private:
  20. Player* zPlayer;
  21. FCKlient* client;
  22. Critical background;
  23. Critical foreground;
  24. Critical other;
  25. Framework::Synchronizer updateSync;
  26. RCArray<InMemoryBuffer> requests;
  27. Vec3<float> lastPos;
  28. RCArray<WorldUpdate> updateQueue;
  29. int viewDistance;
  30. bool first;
  31. bool online;
  32. bool finished;
  33. public:
  34. GameClient( Player* zPlayer, FCKlient* client );
  35. ~GameClient();
  36. void sendWorldUpdate( WorldUpdate* update );
  37. void reply();
  38. void logout();
  39. void addMessage( StreamReader* reader );
  40. bool isOnline() const;
  41. void sendResponse( NetworkResponse* zResponse );
  42. Player* zEntity() const;
  43. private:
  44. class Message
  45. {
  46. public:
  47. inline const static unsigned char TERMINATE = 1;
  48. inline const static unsigned char WORLD_UPDATE = 2;
  49. inline const static unsigned char API_MESSAGE = 3;
  50. inline const static unsigned char POSITION_UPDATE = 4;
  51. };
  52. };
  53. class Game : public virtual Framework::Thread
  54. {
  55. private:
  56. Framework::Text name;
  57. Framework::RCArray<Dimension>* dimensions;
  58. Framework::RCArray<WorldUpdate>* updates;
  59. Framework::RCArray<GameClient>* clients;
  60. TickOrganizer* ticker;
  61. Framework::Text path;
  62. bool stop;
  63. __int64 tickId;
  64. Critical cs;
  65. int nextEntityId;
  66. WorldGenerator* generator;
  67. WorldLoader* loader;
  68. void thread() override;
  69. Game( Framework::Text name, Framework::Text worldsDir );
  70. public:
  71. ~Game();
  72. void initialize();
  73. void api( Framework::StreamReader* zRequest, GameClient* zOrigin );
  74. void distributeResponse( NetworkResponse* zResponse );
  75. bool requestWorldUpdate( WorldUpdate* update );
  76. GameClient* addPlayer( FCKlient* client, Framework::Text name );
  77. bool doesChunkExist( int x, int y, int dimension );
  78. bool isChunkLoaded( int x, int y, int dimension ) const;
  79. Framework::Either<Block*, int> zBlockAt( Framework::Vec3<int> location, int dimension ) const;
  80. Block* zRealBlockInstance( Framework::Vec3<int> location, int dimension );
  81. Dimension* zDimension( int id ) const;
  82. static Framework::Punkt getChunkCenter( int x, int y );
  83. Area getChunckArea( Punkt center ) const;
  84. Framework::Text getWorldDirectory() const;
  85. void requestArea( Area area );
  86. void save() const;
  87. void requestStop();
  88. void addDimension( Dimension* d );
  89. int getNextEntityId();
  90. WorldGenerator* zGenerator() const;
  91. Entity* zEntity( int id, int dimensionId ) const;
  92. Entity* zNearestEntity( int dimensionId, Framework::Vec3<float> pos, std::function<bool( Entity* )> filter );
  93. static Game* INSTANCE;
  94. static void initialize( Framework::Text name, Framework::Text worldsDir );
  95. };