Game.h 6.2 KB

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