Game.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. TickOrganizer* ticker;
  72. Framework::Text path;
  73. bool stop;
  74. __int64 tickId;
  75. Critical cs;
  76. int nextEntityId;
  77. WorldGenerator* generator;
  78. WorldLoader* loader;
  79. RecipieLoader recipies;
  80. double totalTickTime;
  81. int tickCounter;
  82. void thread() override;
  83. Game(Framework::Text name, Framework::Text worldsDir);
  84. public:
  85. ~Game();
  86. void initialize();
  87. void api(Framework::InMemoryBuffer* zRequest, GameClient* zOrigin);
  88. void updateLightning(int dimensionId, Vec3<int> location);
  89. void updateLightningWithoutWait(int dimensionId, Vec3<int> location);
  90. void broadcastMessage(NetworkMessage* response);
  91. void sendMessage(NetworkMessage* response, Entity* zTargetPlayer);
  92. bool requestWorldUpdate(WorldUpdate* update);
  93. bool checkPlayer(Framework::Text name, Framework::Text secret);
  94. bool existsPlayer(Framework::Text name);
  95. Framework::Text createPlayer(Framework::Text name);
  96. GameClient* addPlayer(FCKlient* client, Framework::Text name);
  97. bool doesChunkExist(int x, int y, int dimension);
  98. bool isChunkLoaded(int x, int y, int dimension) const;
  99. Framework::Either<Block*, int> zBlockAt(Framework::Vec3<int> location, int dimension) const;
  100. Block* zRealBlockInstance(Framework::Vec3<int> location, int dimension);
  101. Dimension* zDimension(int id) const;
  102. static Framework::Punkt getChunkCenter(int x, int y);
  103. Area getChunckArea(Punkt center) const;
  104. Framework::Text getWorldDirectory() const;
  105. void requestArea(Area area);
  106. void save() const;
  107. void requestStop();
  108. void addDimension(Dimension* d);
  109. int getNextEntityId();
  110. WorldGenerator* zGenerator() const;
  111. Entity* zEntity(int id, int dimensionId) const;
  112. Entity* zEntity(int id) const;
  113. Entity* zNearestEntity(int dimensionId, Framework::Vec3<float> pos, std::function<bool(Entity*)> filter);
  114. const RecipieLoader& getRecipies() const;
  115. static Game* INSTANCE;
  116. static void initialize(Framework::Text name, Framework::Text worldsDir);
  117. };