Game.h 6.1 KB

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