]> git.localhorst.tv Git - blank.git/blobdiff - src/world/World.hpp
impersonate command
[blank.git] / src / world / World.hpp
index 9ec4cb2faf7fae204c53672e0b1f6e800a09e9dc..771bc0c8c4fd072d8dd59c82c642828c66a30441 100644 (file)
@@ -1,29 +1,33 @@
 #ifndef BLANK_WORLD_WORLD_HPP_
 #define BLANK_WORLD_WORLD_HPP_
 
-#include "BlockTypeRegistry.hpp"
-#include "ChunkLoader.hpp"
+#include "ChunkStore.hpp"
 #include "Entity.hpp"
 #include "Generator.hpp"
-#include "../model/shapes.hpp"
+#include "Player.hpp"
+#include "../graphics/glm.hpp"
+#include "../rand/GaloisLFSR.hpp"
 
+#include <cstdint>
 #include <list>
+#include <string>
 #include <vector>
-#include <glm/glm.hpp>
 
 
 namespace blank {
 
-class BlockLighting;
-class DirectionalLighting;
+class BlockTypeRegistry;
+class EntityCollision;
+class Viewport;
 class WorldCollision;
 
 class World {
 
 public:
        struct Config {
-               // initial player position
-               glm::vec3 spawn = { 0.0f, 0.0f, 0.0f };
+               std::string name = "default";
+               // chunk base where new players are spawned
+               glm::ivec3 spawn = { 0, 0, 0 };
                // direction facing towards(!) the light
                glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
                // fade out reaches 1/e (0.3679) at 1/fog_density,
@@ -31,48 +35,115 @@ public:
                // I chose 0.011 because it yields 91 and 124 for those, so
                // slightly less than 6 and 8 chunks
                float fog_density = 0.011f;
-
-               Generator::Config gen = Generator::Config();
-               ChunkLoader::Config load = ChunkLoader::Config();
        };
 
-       explicit World(const Config &);
+       World(const BlockTypeRegistry &, const Config &);
+       ~World();
+
+       const std::string &Name() const noexcept { return config.name; }
 
+       /// get the shared random source for this world
+       GaloisLFSR &Random() noexcept { return rng; }
+
+       /// check if this ray hits a block
+       /// depth in the collision is the distance between the ray's
+       /// origin and the intersection point
+       /// reference is the chunk offset of the ray in world space
        bool Intersection(
                const Ray &,
-               const glm::mat4 &M,
-               Chunk *&chunk,
-               int &blkid,
-               float &dist,
-               glm::vec3 &normal);
+               const ExactLocation::Coarse &reference,
+               WorldCollision &);
 
-       bool Intersection(const Entity &e, std::vector<WorldCollision> &);
-       void Resolve(Entity &e, std::vector<WorldCollision> &);
-
-       BlockTypeRegistry &BlockTypes() { return blockType; }
+       /// check if this ray hits an entity
+       /// intersections with the reference are not tested
+       /// the ray is assumed to be in world space offset by entity's chunk coords
+       bool Intersection(
+               const Ray &,
+               const Entity &reference,
+               EntityCollision &);
+
+       /// check if given entity intersects with the world
+       bool Intersection(const Entity &e, const EntityState &, std::vector<WorldCollision> &);
+       /// combine contacts into a single penetration vector
+       /// depth is given to point towards position of given state
+       static glm::vec3 CombinedInterpenetration(
+               const EntityState &,
+               const std::vector<WorldCollision> &) noexcept;
+
+       /// check if given box (M * AABB) intersects with the world
+       /// M is assumed to be calculated in reference to given chunk coords
+       bool Intersection(
+               const AABB &box,
+               const glm::mat4 &M,
+               const glm::ivec3 &reference,
+               std::vector<WorldCollision> &);
+
+       const BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
+       ChunkStore &Chunks() noexcept { return chunks; }
+
+       /// add player with given name
+       /// returns nullptr if the name is already taken
+       Player *AddPlayer(const std::string &name);
+       /// add player with given name and ID
+       /// returns nullptr if the name or ID is already taken
+       Player *AddPlayer(const std::string &name, std::uint32_t id);
+       /// add an entity with an autogenerated ID
+       Entity &AddEntity();
+       /// add entity with given ID
+       /// returns nullptr if the ID is already taken
+       Entity *AddEntity(std::uint32_t id);
+       /// add entity with given ID
+       /// returs an existing entity if ID is already taken
+       Entity &ForceAddEntity(std::uint32_t id);
+
+       /// get the player with given name
+       /// returns nullptr if no player bears this name
+       Player *FindPlayer(const std::string &name);
+       /// get an entity with given name
+       /// returns nullptr if name doesn't refer to any entity
+       /// note that unlike players, entity names are not unique
+       Entity *FindEntity(const std::string &name);
+
+       std::list<Player> &Players() noexcept { return players; }
+       const std::list<Player> &Players() const noexcept { return players; }
+       std::list<Entity> &Entities() noexcept { return entities; }
+       const std::list<Entity> &Entities() const noexcept { return entities; }
+
+       // dt in ms
+       void Update(int dt);
 
-       Entity &Player() { return *player; }
-       Entity &AddEntity() { entities.emplace_back(); return entities.back(); }
+       /// fix state so entity doesn't intersect with the solid world
+       void ResolveWorldCollision(const Entity &, EntityState &);
+       /// get force due to gravity at given location
+       glm::vec3 GravityAt(const ExactLocation &) const noexcept;
 
-       Chunk &PlayerChunk();
-       Chunk &Next(const Chunk &to, const glm::tvec3<int> &dir);
+       void Render(Viewport &);
+       void RenderDebug(Viewport &);
 
-       void Update(int dt);
+private:
+       using EntityHandle = std::list<Entity>::iterator;
+       EntityHandle RemoveEntity(EntityHandle &);
 
-       void Render(BlockLighting &, DirectionalLighting &);
+       /// calculate light direction and intensity at entity's location
+       void GetLight(
+               const Entity &entity,
+               glm::vec3 &direction,
+               glm::vec3 &color,
+               glm::vec3 &ambient
+       );
 
 private:
-       BlockTypeRegistry blockType;
-       CuboidShape blockShape;
-       StairShape stairShape;
-       CuboidShape slabShape;
+       Config config;
+
+       const BlockTypeRegistry &block_type;
 
-       Generator generate;
-       ChunkLoader chunks;
+       ChunkStore chunks;
 
-       Entity *player;
+       std::list<Player> players;
        std::list<Entity> entities;
 
+       GaloisLFSR rng;
+
        glm::vec3 light_direction;
        float fog_density;