Game.h 3.4 KB

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