Game.h 4.2 KB

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