Game.h 5.2 KB

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