Game.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include "RecipieLoader.h"
  17. class FCKlient;
  18. class GameClient : public Framework::Thread
  19. {
  20. private:
  21. Player* zPlayer;
  22. FCKlient* client;
  23. Critical background;
  24. Critical foreground;
  25. Critical other;
  26. Framework::Synchronizer updateSync;
  27. RCArray<InMemoryBuffer> requests;
  28. RCArray<WorldUpdate> updateQueue;
  29. RCArray<NetworkMessage> backgroundQueue;
  30. RCArray<NetworkMessage> foregroundQueue;
  31. Framework::Synchronizer foregroundQueueSync;
  32. Framework::Synchronizer backgroundQueueSync;
  33. Critical queueCs;
  34. Framework::Synchronizer emptyForegroundQueueSync;
  35. Framework::Synchronizer emptyBackgroundQueueSync;
  36. int viewDistance;
  37. bool first;
  38. bool online;
  39. bool finished;
  40. bool backgroundFinished;
  41. bool foregroundFinished;
  42. public:
  43. GameClient(Player* zPlayer, FCKlient* client);
  44. ~GameClient();
  45. void thread() override;
  46. void sendWorldUpdate(WorldUpdate* update);
  47. void reply();
  48. void logout();
  49. void addMessage(StreamReader* reader);
  50. bool isOnline() const;
  51. void sendResponse(NetworkMessage* response);
  52. Player* zEntity() const;
  53. void sendTypes();
  54. class Message
  55. {
  56. public:
  57. inline const static unsigned char TERMINATE = 1;
  58. inline const static unsigned char WORLD_UPDATE = 2;
  59. inline const static unsigned char API_MESSAGE = 3;
  60. inline const static unsigned char POSITION_UPDATE = 4;
  61. };
  62. };
  63. class Game : public virtual Framework::Thread
  64. {
  65. private:
  66. Framework::Text name;
  67. Framework::RCArray<Dimension>* dimensions;
  68. Framework::RCArray<WorldUpdate>* updates;
  69. Framework::RCArray<GameClient>* clients;
  70. TickOrganizer* ticker;
  71. Framework::Text path;
  72. bool stop;
  73. __int64 tickId;
  74. Critical cs;
  75. int nextEntityId;
  76. WorldGenerator* generator;
  77. WorldLoader* loader;
  78. RecipieLoader recipies;
  79. double totalTickTime;
  80. int tickCounter;
  81. void thread() override;
  82. Game(Framework::Text name, Framework::Text worldsDir);
  83. public:
  84. ~Game();
  85. void initialize();
  86. void api(Framework::InMemoryBuffer* zRequest, GameClient* zOrigin);
  87. void updateLightning(int dimensionId, Vec3<int> location);
  88. void updateLightningWithoutWait(int dimensionId, Vec3<int> location);
  89. void broadcastMessage(NetworkMessage* response);
  90. void sendMessage(NetworkMessage* response, Entity* zTargetPlayer);
  91. bool requestWorldUpdate(WorldUpdate* update);
  92. GameClient* addPlayer(FCKlient* client, Framework::Text name);
  93. bool doesChunkExist(int x, int y, int dimension);
  94. bool isChunkLoaded(int x, int y, int dimension) const;
  95. Framework::Either<Block*, int> zBlockAt(Framework::Vec3<int> location, int dimension) const;
  96. Block* zRealBlockInstance(Framework::Vec3<int> location, int dimension);
  97. Dimension* zDimension(int id) const;
  98. static Framework::Punkt getChunkCenter(int x, int y);
  99. Area getChunckArea(Punkt center) const;
  100. Framework::Text getWorldDirectory() const;
  101. void requestArea(Area area);
  102. void save() const;
  103. void requestStop();
  104. void addDimension(Dimension* d);
  105. int getNextEntityId();
  106. WorldGenerator* zGenerator() const;
  107. Entity* zEntity(int id, int dimensionId) const;
  108. Entity* zEntity(int id) const;
  109. Entity* zNearestEntity(int dimensionId, Framework::Vec3<float> pos, std::function<bool(Entity*)> filter);
  110. const RecipieLoader& getRecipies() const;
  111. static Game* INSTANCE;
  112. static void initialize(Framework::Text name, Framework::Text worldsDir);
  113. };