Game.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(NetworkResponse* zResponse);
  43. Player* zEntity() const;
  44. void sendTypes();
  45. private:
  46. class Message
  47. {
  48. public:
  49. inline const static unsigned char TERMINATE = 1;
  50. inline const static unsigned char WORLD_UPDATE = 2;
  51. inline const static unsigned char API_MESSAGE = 3;
  52. inline const static unsigned char POSITION_UPDATE = 4;
  53. };
  54. };
  55. class Game : public virtual Framework::Thread
  56. {
  57. private:
  58. Framework::Text name;
  59. Framework::RCArray<Dimension>* dimensions;
  60. Framework::RCArray<WorldUpdate>* updates;
  61. Framework::RCArray<GameClient>* clients;
  62. TickOrganizer* ticker;
  63. Framework::Text path;
  64. bool stop;
  65. __int64 tickId;
  66. Critical cs;
  67. int nextEntityId;
  68. WorldGenerator* generator;
  69. WorldLoader* loader;
  70. RecipieLoader recipies;
  71. void thread() override;
  72. Game(Framework::Text name, Framework::Text worldsDir);
  73. public:
  74. ~Game();
  75. void initialize();
  76. void api(Framework::StreamReader* zRequest, GameClient* zOrigin);
  77. void distributeResponse(NetworkResponse* zResponse);
  78. bool requestWorldUpdate(WorldUpdate* update);
  79. GameClient* addPlayer(FCKlient* client, Framework::Text name);
  80. bool doesChunkExist(int x, int y, int dimension);
  81. bool isChunkLoaded(int x, int y, int dimension) const;
  82. Framework::Either<Block*, int> zBlockAt(Framework::Vec3<int> location, int dimension) const;
  83. Block* zRealBlockInstance(Framework::Vec3<int> location, int dimension);
  84. Dimension* zDimension(int id) const;
  85. static Framework::Punkt getChunkCenter(int x, int y);
  86. Area getChunckArea(Punkt center) const;
  87. Framework::Text getWorldDirectory() const;
  88. void requestArea(Area area);
  89. void save() const;
  90. void requestStop();
  91. void addDimension(Dimension* d);
  92. int getNextEntityId();
  93. WorldGenerator* zGenerator() const;
  94. Entity* zEntity(int id, int dimensionId) const;
  95. Entity* zNearestEntity(int dimensionId, Framework::Vec3<float> pos, std::function<bool(Entity*)> filter);
  96. static Game* INSTANCE;
  97. static void initialize(Framework::Text name, Framework::Text worldsDir);
  98. };