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

215 lines
5.6 KiB
C++
Raw Normal View History

#ifndef WORLD_HPP
#define WORLD_HPP
#include "luastack.hpp"
2021-01-16 01:24:33 -05:00
#include "planemap.hpp"
#include "idalloc.hpp"
#include "animqueue.hpp"
#include "invocation.hpp"
2021-02-18 17:21:25 -05:00
#include "sched.hpp"
2021-01-16 01:24:33 -05:00
#include "source.hpp"
2021-02-07 17:26:48 -05:00
#include "gui.hpp"
2021-02-09 17:15:54 -05:00
#include "luasnap.hpp"
2021-02-18 14:56:12 -05:00
#include <set>
#include <utility>
#include <memory>
2021-01-16 01:24:33 -05:00
#include <unordered_map>
2021-01-16 01:24:33 -05:00
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);
2021-01-16 01:24:33 -05:00
public:
// Always points back to the world model.
World *world_;
2021-01-22 17:35:23 -05:00
// Animation queue.
2021-01-16 01:24:33 -05:00
//
AnimQueue anim_queue_;
2021-01-22 17:35:23 -05:00
// 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.
//
2021-01-22 17:35:23 -05:00
PlaneItem plane_item_;
2021-01-16 01:24:33 -05:00
// 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);
2021-01-22 17:35:23 -05:00
// Get the ID
//
int64_t id() { return plane_item_.id(); }
2021-02-25 15:43:05 -05:00
void update_plane_item();
2021-07-18 17:48:39 -04:00
bool is_an_actor() { return (id_player_pool_.get_fifo_capacity() > 0); }
void configure_id_pool_for_actor() { id_player_pool_.set_fifo_capacity(20); }
2021-01-16 01:24:33 -05:00
};
class World {
public:
2021-07-18 17:48:39 -04:00
// Type of model
util::WorldType world_type_;
2021-02-09 17:15:54 -05:00
// A lua intepreter with snapshot function.
2021-01-16 01:24:33 -05:00
//
2021-02-09 17:15:54 -05:00
LuaSnap lua_snap_;
2021-01-16 01:24:33 -05:00
// 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_;
2021-01-16 01:24:33 -05:00
// Thread schedule: must include every thread, except
2021-02-18 17:21:25 -05:00
// for the one currently-executing thread.
2021-02-18 14:56:12 -05:00
//
2021-02-18 17:21:25 -05:00
Schedule thread_sched_;
2021-02-18 14:56:12 -05:00
// Serialized snapshot of world model.
StreamBuffer snapshot_;
2021-03-30 18:35:08 -04:00
// Redirects.
//
using Redirects = std::map<int64_t, int64_t>;
Redirects redirects_;
2021-02-18 17:21:25 -05:00
void run_scheduled_threads(int64_t clk);
static void store_global_pointer(lua_State *L, World *w);
2021-03-30 18:35:08 -04:00
// Check if main thread has nothing on the stack
bool stack_is_clear() const { return lua_gettop(state()) == 0; }
// Invoke an action plan.
//
void invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const InvocationData &idata);
2021-01-16 01:24:33 -05:00
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.
//
2021-07-18 17:48:39 -04:00
World(util::WorldType wt);
2021-01-16 01:24:33 -05:00
// Destructor.
//
// Not currently functional.
//
~World();
2021-01-16 01:24:33 -05:00
// get_lua_state
//
// Get the lua interpreter associated with this world model.
//
2021-03-30 18:35:08 -04:00
lua_State *state() const { return lua_snap_.state(); }
2021-01-16 01:24:33 -05:00
2021-02-07 13:38:29 -05:00
// get_near
2021-01-22 17:35:23 -05:00
//
// Get a list of the tangibles that are near the player. If 'exclude_nowhere' is
// true, exclude any tangibles on the nowhere plane.
2021-01-17 16:23:10 -05:00
//
std::vector<int64_t> get_near(int64_t player_id, float radius, bool exclude_nowhere);
2021-01-22 17:35:23 -05:00
// Make a tangible.
2021-01-17 16:23:10 -05:00
//
2021-01-22 17:35:23 -05:00
// 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);
2021-01-17 16:23:10 -05:00
2021-01-22 17:35:23 -05:00
// Get a pointer to the specified tangible.
//
Tangible *tangible_get(int64_t id);
2021-02-25 15:43:05 -05:00
// 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);
2021-01-17 16:23:10 -05:00
// Delete the specified tangible.
//
void tangible_delete(lua_State *L, int64_t id);
2021-03-30 18:35:08 -04:00
// Create a login actor.
//
// Creates a tangible of class 'login' and returns its ID.
// This is used to create a temporary actor which is used during
// the login process.
//
int64_t create_login_actor();
// Fetch all redirects and clear the redirects table.
//
Redirects fetch_redirects();
2021-02-07 17:26:48 -05:00
// Probe the 'interface' function of the specified sprite.
//
void update_gui(int64_t actor_id, int64_t place_id, Gui *gui);
// Invoke an Invocation object.
2021-02-16 13:31:34 -05:00
//
// This is the primary dispatcher for all operations that mutate a world model.
// To mutate a world model, create an invocation, then invoke it.
//
void invoke(const Invocation &inv);
// fetch_global_pointer
2021-01-16 01:24:33 -05:00
//
// Given a lua state, fetch the world model associated with
// that lua state.
//
2021-02-25 14:09:16 -05:00
static World *fetch_global_pointer(lua_State *L);
2021-02-16 13:31:34 -05:00
// Serialize and deserialize.
//
void serialize(StreamBuffer *sb);
void deserialize(StreamBuffer *sb);
// Snapshot and rollback.
2021-02-16 13:31:34 -05:00
//
void snapshot();
void rollback();
};
#endif // WORLD_HPP