Game.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #include <Text.h>
  3. #include <Punkt.h>
  4. #include <Thread.h>
  5. #include <Network.h>
  6. #include <Critical.h>
  7. #include "Dimension.h"
  8. #include "Player.h"
  9. #include "WorldGenerator.h"
  10. #include "Constants.h"
  11. #include "WorldUpdate.h"
  12. #include "WorldLoader.h"
  13. #include "TickOrganizer.h"
  14. #include "Server.h"
  15. #include "InMemoryBuffer.h"
  16. #include "RecipieLoader.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 const static unsigned char TERMINATE = 1;
  59. inline const static unsigned char WORLD_UPDATE = 2;
  60. inline const static unsigned char API_MESSAGE = 3;
  61. inline const static 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. bool isChunkLoaded(int x, int y, int dimension) const;
  101. Framework::Either<Block*, int> zBlockAt(Framework::Vec3<int> location, int dimension) const;
  102. Block* zRealBlockInstance(Framework::Vec3<int> location, int dimension);
  103. Dimension* zDimension(int id) const;
  104. static Framework::Punkt getChunkCenter(int x, int y);
  105. Area getChunckArea(Punkt center) const;
  106. Framework::Text getWorldDirectory() const;
  107. void requestArea(Area area);
  108. void save() const;
  109. void requestStop();
  110. void addDimension(Dimension* d);
  111. int getNextEntityId();
  112. WorldGenerator* zGenerator() const;
  113. Entity* zEntity(int id, int dimensionId) const;
  114. Entity* zEntity(int id) const;
  115. Entity* zNearestEntity(int dimensionId, Framework::Vec3<float> pos, std::function<bool(Entity*)> filter);
  116. const RecipieLoader& getRecipies() const;
  117. void doLater(std::function<void()> action);
  118. static Game* INSTANCE;
  119. static void initialize(Framework::Text name, Framework::Text worldsDir);
  120. };