Files
integration/luprex/core/cpp/world.hpp

189 lines
4.7 KiB
C++

#ifndef WORLD_HPP
#define WORLD_HPP
#include "luastack.hpp"
#include "planemap.hpp"
#include "idalloc.hpp"
#include "animqueue.hpp"
#include "sched.hpp"
#include "source.hpp"
#include "gui.hpp"
#include "luasnap.hpp"
#include <set>
#include <utility>
#include <memory>
#include <unordered_map>
class World;
class Tangible {
private:
friend class World;
// Serialize and deserialize
//
// The tangible's ID is not serialized. When you serialize a tangible, you
// should probably serialize the ID separately.
//
// The Lua portion of the tangible is not serialized here. Instead, the lua
// portion is serialized when you serialize the lua state as a whole.
//
// PlaneItem is not serialized. The deserialize routine rebuilds the
// PlaneItem from the AnimQueue.
//
// World pointer is not serialized.
//
void serialize(StreamBuffer *sb);
void deserialize(StreamBuffer *sb);
public:
// Always points back to the world model.
World *world_;
// Animation queue.
//
AnimQueue anim_queue_;
// Plane Item.
//
// The PlaneItem also contains this tangible's ID.
// To move this PlaneItem, update the anim_queue first, then call
// update_plane_item, which copies the data from the anim_queue.
//
PlaneItem plane_item_;
// Player ID pool
//
// This is present in every tangible, whether a player or not.
// However, the fifo is only enabled in logged-in players.
//
IdPlayerPool id_player_pool_;
// constructor.
//
Tangible(World *w, int64_t id);
// Get the ID
//
int64_t id() { return plane_item_.id(); }
void be_a_player();
void update_plane_item();
};
class World {
public:
// A lua intepreter with snapshot function.
//
LuaSnap lua_snap_;
// The Global ID Pool.
//
IdGlobalPool id_global_pool_;
// Source Database.
//
SourceDB source_db_;
PlaneMap plane_map_;
// Tangibles table.
//
std::unordered_map<int64_t, std::unique_ptr<Tangible>> tangibles_;
// Thread schedule: must include every thread, except
// for the one currently-executing thread.
//
Schedule thread_sched_;
// Serialized snapshot of world model.
StreamBuffer snapshot_;
void run_scheduled_threads(int64_t clk);
static void store_global_pointer(lua_State *L, World *w);
public:
// Constructor.
//
// The constructor also calls 'lua_open' to create a new
// lua interpreter for this world model. The lua interpreter
// is stored in world::lua_state_. A significant amount of
// initial setup is done by this constructor.
//
World();
// Destructor.
//
// Not currently functional.
//
~World();
// Initialize for single-player mode.
//
void init_standalone();
// get_lua_state
//
// Get the lua interpreter associated with this world model.
//
lua_State *state() { return lua_snap_.state(); }
// get_near
//
// Get a list of the tangibles that are near the player. If 'exclude_nowhere' is
// true, exclude any tangibles on the nowhere plane.
//
std::vector<int64_t> get_near(int64_t player_id, float radius, bool exclude_nowhere);
// Make a tangible.
//
// If the ID is zero, allocates an ID using the thread's ID allocator. If
// pushdb is true, pushes the tangible's database onto the lua stack.
// Otherwise, leaves the lua stack untouched.
//
Tangible *tangible_make(lua_State *L, int64_t id, bool pushdb);
// Get a pointer to the specified tangible.
//
Tangible *tangible_get(int64_t id);
// Get a pointer to the specified tangible.
//
// The value on the lua stack should be a valid lua tangible. If not,
// a lua error is generated.
//
Tangible *tangible_get(lua_State *L, int idx);
// Delete the specified tangible.
//
void tangible_delete(lua_State *L, int64_t id);
// Probe the 'interface' function of the specified sprite.
//
void update_gui(int64_t actor_id, int64_t place_id, Gui *gui);
// Invoke an action plan.
//
// Eventually we'll add another parameter for gui-state.
//
void invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const GuiResult &gres);
// fetch_global_pointer
//
// Given a lua state, fetch the world model associated with
// that lua state.
//
static World *fetch_global_pointer(lua_State *L);
// Serialize and deserialize.
//
void serialize(StreamBuffer *sb);
void deserialize(StreamBuffer *sb);
// Snapshot and rollback - temporary.
//
void snapshot();
void rollback();
};
#endif // WORLD_HPP