Game.h 4.4 KB

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