Game.h 3.0 KB

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