207 lines
5.3 KiB
C++
207 lines
5.3 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 update_plane_item();
|
|
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); }
|
|
};
|
|
|
|
class World {
|
|
public:
|
|
// Type of model
|
|
util::WorldType world_type_;
|
|
|
|
// 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_;
|
|
|
|
// Redirects.
|
|
//
|
|
using Redirects = std::map<int64_t, int64_t>;
|
|
Redirects redirects_;
|
|
|
|
void run_scheduled_threads(int64_t clk);
|
|
static void store_global_pointer(lua_State *L, World *w);
|
|
|
|
// Check if main thread has nothing on the stack
|
|
bool stack_is_clear() const { return lua_gettop(state()) == 0; }
|
|
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(util::WorldType wt);
|
|
|
|
// Destructor.
|
|
//
|
|
// Not currently functional.
|
|
//
|
|
~World();
|
|
|
|
// get_lua_state
|
|
//
|
|
// Get the lua interpreter associated with this world model.
|
|
//
|
|
lua_State *state() const { 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);
|
|
|
|
// 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();
|
|
|
|
// 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.
|
|
//
|
|
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.
|
|
//
|
|
void snapshot();
|
|
void rollback();
|
|
};
|
|
|
|
#endif // WORLD_HPP
|