2021-01-06 15:10:21 -05:00
|
|
|
|
|
|
|
|
#include "world.hpp"
|
|
|
|
|
#include "idalloc.hpp"
|
2021-01-22 19:10:47 -05:00
|
|
|
#include "animqueue.hpp"
|
2021-02-07 17:26:48 -05:00
|
|
|
#include "gui.hpp"
|
|
|
|
|
#include "traceback.hpp"
|
2021-08-23 23:34:30 -04:00
|
|
|
#include "tablecmp.hpp"
|
2021-01-16 01:24:33 -05:00
|
|
|
#include <iostream>
|
2021-01-06 15:10:21 -05:00
|
|
|
|
2021-03-19 13:41:41 -04:00
|
|
|
void World::store_global_pointer(lua_State *L, World *v) {
|
|
|
|
|
lua_pushstring(L, "world");
|
|
|
|
|
lua_pushlightuserdata(L, v);
|
|
|
|
|
lua_rawset(L, LUA_REGISTRYINDEX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World *World::fetch_global_pointer(lua_State *L) {
|
|
|
|
|
lua_pushstring(L, "world");
|
|
|
|
|
lua_rawget(L, LUA_REGISTRYINDEX);
|
|
|
|
|
World *result = (World *)lua_touserdata(L, -1);
|
|
|
|
|
if (result == nullptr) {
|
|
|
|
|
luaL_error(L, "No world pointer stored.");
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return result;
|
2021-01-17 16:23:10 -05:00
|
|
|
}
|
|
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
World::~World() {
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 17:48:39 -04:00
|
|
|
World::World(util::WorldType wt) {
|
|
|
|
|
// Master world model by default.
|
|
|
|
|
world_type_ = wt;
|
|
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
// Initialize the ID allocator in master mode.
|
2021-08-03 11:25:12 -04:00
|
|
|
if (wt == util::WORLD_TYPE_MASTER || wt == util::WORLD_TYPE_STANDALONE) {
|
2021-07-18 17:48:39 -04:00
|
|
|
id_global_pool_.init_master();
|
|
|
|
|
} else {
|
|
|
|
|
id_global_pool_.init_synch();
|
|
|
|
|
}
|
2021-01-17 16:23:10 -05:00
|
|
|
|
2021-01-16 01:24:33 -05:00
|
|
|
// Prepare to manipulate the lua state.
|
2021-08-10 10:41:06 -04:00
|
|
|
LuaVar world, globtab;
|
|
|
|
|
LuaStack LS(state(), world, globtab);
|
2021-01-16 01:24:33 -05:00
|
|
|
|
|
|
|
|
// Put the world pointer into the lua registry.
|
2021-02-25 14:09:16 -05:00
|
|
|
World::store_global_pointer(state(), this);
|
|
|
|
|
|
|
|
|
|
// Clear the global GUI pointer.
|
|
|
|
|
Gui::store_global_pointer(state(), nullptr);
|
2021-01-16 01:24:33 -05:00
|
|
|
|
2021-08-10 10:41:06 -04:00
|
|
|
// Set the tabletype of the registry.
|
2021-08-23 23:34:30 -04:00
|
|
|
LS.settabletype(LuaRegistry, LUA_TT_REGISTRY);
|
2021-08-10 10:41:06 -04:00
|
|
|
|
|
|
|
|
// Set the tabletype of the global environment.
|
|
|
|
|
LS.getglobaltable(globtab);
|
2021-08-23 23:34:30 -04:00
|
|
|
LS.settabletype(globtab, LUA_TT_GLOBALENV);
|
2021-08-10 10:41:06 -04:00
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
// Create the tangibles table in the registry.
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawset(LuaRegistry, "tangibles", LuaNewTable);
|
2021-01-17 16:23:10 -05:00
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
// Initialize the SourceDB. At this stage, the sourcedb is
|
|
|
|
|
// empty, so it's just populating the lua builtins.
|
2021-02-28 16:32:42 -05:00
|
|
|
source_db_.init(state());
|
2021-01-22 17:35:23 -05:00
|
|
|
source_db_.rebuild();
|
2021-01-23 16:31:16 -05:00
|
|
|
|
|
|
|
|
LS.result();
|
2021-07-18 17:48:39 -04:00
|
|
|
assert (stack_is_clear());
|
2021-01-22 17:35:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-18 17:48:39 -04:00
|
|
|
Tangible::Tangible(World *w, int64_t id) : world_(w), anim_queue_(w->world_type_), id_player_pool_(&w->id_global_pool_) {
|
2021-03-19 13:41:41 -04:00
|
|
|
plane_item_.set_id(id);
|
2021-03-25 15:34:43 -04:00
|
|
|
plane_item_.track(&w->plane_map_);
|
2021-03-19 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
2021-02-25 15:43:05 -05:00
|
|
|
void Tangible::update_plane_item() {
|
2021-03-28 13:27:28 -04:00
|
|
|
const AnimStep &aqback = anim_queue_.back();
|
|
|
|
|
plane_item_.set_pos(aqback.plane(), aqback.xyz().x, aqback.xyz().y, aqback.xyz().z);
|
2021-02-25 15:43:05 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-14 18:17:34 -04:00
|
|
|
void Tangible::serialize(StreamBuffer *sb) {
|
|
|
|
|
anim_queue_.serialize(sb);
|
|
|
|
|
id_player_pool_.serialize(sb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Tangible::deserialize(StreamBuffer *sb) {
|
|
|
|
|
anim_queue_.deserialize(sb);
|
|
|
|
|
id_player_pool_.deserialize(sb);
|
|
|
|
|
update_plane_item();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 17:35:23 -05:00
|
|
|
|
|
|
|
|
Tangible *World::tangible_get(int64_t id) {
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
2021-03-14 18:17:34 -04:00
|
|
|
return iter->second.get();
|
2021-01-22 17:35:23 -05:00
|
|
|
}
|
2021-01-06 15:10:21 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-26 13:12:44 -04:00
|
|
|
const Tangible *World::tangible_get(int64_t id) const {
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
} else {
|
|
|
|
|
return iter->second.get();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
World::TanVector World::tangible_get_all(const IdVector &ids) const {
|
|
|
|
|
TanVector result(ids.size());
|
2021-07-25 19:22:39 -04:00
|
|
|
for (int i = 0; i < int(ids.size()); i++) {
|
|
|
|
|
result[i] = tangible_get(ids[i]);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *World::tangible_get(const LuaStack &LS, LuaSlot tab) {
|
|
|
|
|
int64_t id = tangible_id(LS, tab);
|
|
|
|
|
if (id == 0) {
|
|
|
|
|
luaL_error(LS.state(), "parameter is not a tangible");
|
2021-02-25 15:43:05 -05:00
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *result = tangible_get(id);
|
2021-02-25 15:43:05 -05:00
|
|
|
if (result == nullptr) {
|
2021-08-23 23:34:30 -04:00
|
|
|
luaL_error(LS.state(), "parameter is not a tangible");
|
2021-02-25 15:43:05 -05:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
int64_t World::tangible_id(const LuaStack &LS, LuaSlot tab) {
|
|
|
|
|
int64_t id = 0;
|
|
|
|
|
if (LS.istable(tab) && LS.gettabletype(tab) == LUA_TT_TANGIBLE) {
|
|
|
|
|
lua_State *L = LS.state();
|
|
|
|
|
if (lua_getmetatable(L, tab.index())) {
|
|
|
|
|
lua_pushstring(L, "id");
|
|
|
|
|
lua_rawget(L, -2);
|
|
|
|
|
if (lua_type(L, -1) == LUA_TNUMBER) {
|
|
|
|
|
id = lua_tointeger(L, -1);
|
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 12:40:12 -04:00
|
|
|
void World::tangible_delete(int64_t id) {
|
|
|
|
|
lua_State *L = state();
|
2021-03-19 13:41:41 -04:00
|
|
|
LuaVar tangibles, database;
|
|
|
|
|
LuaStack LS(L, tangibles, database);
|
|
|
|
|
|
|
|
|
|
// Fetch the C++ side of the tangible.
|
|
|
|
|
auto iter = tangibles_.find(id);
|
|
|
|
|
if (iter == tangibles_.end()) {
|
2021-08-03 12:40:12 -04:00
|
|
|
LS.result();
|
|
|
|
|
return; // Nothing to delete.
|
2021-03-19 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch the lua side of the tangible.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(database, tangibles, id);
|
|
|
|
|
assert(LS.istable(database));
|
|
|
|
|
|
|
|
|
|
// Clear out the database.
|
|
|
|
|
LS.clearmetatable(database);
|
|
|
|
|
LS.cleartable(database);
|
2021-08-23 23:34:30 -04:00
|
|
|
LS.settabletype(database, LUA_TT_DEADTANGIBLE);
|
2021-03-19 13:41:41 -04:00
|
|
|
|
2021-08-03 12:40:12 -04:00
|
|
|
// Remove the lua portion from the tangibles table.
|
2021-03-19 13:41:41 -04:00
|
|
|
LS.rawset(tangibles, id, LuaNil);
|
|
|
|
|
|
2021-08-03 12:40:12 -04:00
|
|
|
// Remove the C++ portion from the tangibles table.
|
2021-03-19 13:41:41 -04:00
|
|
|
tangibles_.erase(iter);
|
|
|
|
|
LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
|
|
|
|
|
void World::tangible_walkto(int64_t id, int64_t animid, float x, float y) {
|
|
|
|
|
Tangible *t = tangible_get(id);
|
|
|
|
|
assert(animid != 0);
|
|
|
|
|
assert(t != nullptr);
|
|
|
|
|
AnimStep step;
|
|
|
|
|
step.set_action("walkto");
|
|
|
|
|
step.set_x(x);
|
|
|
|
|
step.set_y(y);
|
|
|
|
|
t->anim_queue_.add(animid, step);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-03 12:40:12 -04:00
|
|
|
std::string World::tangible_anim_debug_string(int64_t id) const {
|
|
|
|
|
const Tangible *t = tangible_get(id);
|
|
|
|
|
if (t == 0) return "no such tangible";
|
|
|
|
|
return t->anim_queue_.steps_debug_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
std::string World::tangible_ids_debug_string() const {
|
|
|
|
|
util::IdVector idv;
|
|
|
|
|
for (const auto &pair : tangibles_) {
|
|
|
|
|
idv.push_back(pair.first);
|
|
|
|
|
}
|
|
|
|
|
std::sort(idv.begin(), idv.end());
|
|
|
|
|
return util::id_vector_debug_string(idv);
|
|
|
|
|
}
|
2021-08-13 17:02:35 -04:00
|
|
|
|
|
|
|
|
std::string World::numbered_tables_debug_string() const {
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar ntmap, tab, tid;
|
|
|
|
|
LuaStack LS(L, ntmap, tab, tid);
|
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
|
|
|
|
|
// Fetch the numbered tables map.
|
|
|
|
|
LS.rawget(ntmap, LuaRegistry, "ntmap");
|
|
|
|
|
|
|
|
|
|
// Iterate over the map. For each table, if it has
|
|
|
|
|
// a TID, output that. Otherwise, output "unknown".
|
|
|
|
|
for (int i = 1; i < 10000; i++) {
|
|
|
|
|
LS.rawgeti(tab, ntmap, i);
|
|
|
|
|
if (!LS.istable(tab)) break;
|
|
|
|
|
LS.rawget(tid, tab, "TID");
|
|
|
|
|
if (LS.isstring(tid)) {
|
|
|
|
|
result.push_back(LS.ckstring(tid));
|
|
|
|
|
} else {
|
|
|
|
|
result.push_back("unknown");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
|
for (const std::string &s : result) {
|
|
|
|
|
oss << s << ";";
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
std::string World::paired_tables_debug_string(lua_State *master) const {
|
2021-08-13 17:02:35 -04:00
|
|
|
lua_State *synch = state();
|
|
|
|
|
LuaVar mntmap, sntmap, mtab, stab, mtid, stid;
|
|
|
|
|
LuaStack MLS(master, mntmap, mtab, mtid);
|
|
|
|
|
LuaStack SLS(synch, sntmap, stab, stid);
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> result;
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
|
|
|
|
|
// Fetch the numbered tables map.
|
|
|
|
|
MLS.rawget(mntmap, LuaRegistry, "ntmap");
|
|
|
|
|
SLS.rawget(sntmap, LuaRegistry, "ntmap");
|
2021-08-23 23:34:30 -04:00
|
|
|
int m_ntables = MLS.rawlen(mntmap);
|
|
|
|
|
int s_ntables = MLS.rawlen(sntmap);
|
|
|
|
|
assert(m_ntables == s_ntables);
|
2021-08-13 17:02:35 -04:00
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
for (int i = 1; i <= m_ntables; i++) {
|
2021-08-13 17:02:35 -04:00
|
|
|
MLS.rawget(mtab, mntmap, i);
|
2021-08-23 23:34:30 -04:00
|
|
|
SLS.rawget(stab, sntmap, i);
|
|
|
|
|
if (MLS.istable(mtab) && SLS.istable(stab)) {
|
|
|
|
|
std::string mname = "unknown";
|
|
|
|
|
std::string sname = "unknown";
|
2021-08-13 17:02:35 -04:00
|
|
|
MLS.rawget(mtid, mtab, "TID");
|
|
|
|
|
if (MLS.isstring(mtid)) {
|
|
|
|
|
mname = MLS.ckstring(mtid);
|
|
|
|
|
}
|
|
|
|
|
SLS.rawget(stid, stab, "TID");
|
|
|
|
|
if (SLS.isstring(stid)) {
|
|
|
|
|
sname = SLS.ckstring(stid);
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
result.push_back(std::make_pair(mname, sname));
|
2021-08-13 17:02:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MLS.result();
|
|
|
|
|
SLS.result();
|
|
|
|
|
std::sort(result.begin(), result.end());
|
|
|
|
|
for (const auto &pair : result) {
|
|
|
|
|
oss << pair.first << "=" << pair.second << ";";
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::tangible_set_string(int64_t id, const std::string &path, const std::string &value) {
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar tangibles, tab, subtab;
|
|
|
|
|
LuaStack LS(L, tangibles, tab, subtab);
|
|
|
|
|
|
|
|
|
|
// Fetch the lua side of the tangible.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(tab, tangibles, id);
|
|
|
|
|
assert(LS.istable(tab));
|
2021-08-03 11:25:12 -04:00
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
// Split the path parts into the table names and final part.
|
|
|
|
|
util::StringVec pathparts = util::split(path, '.');
|
|
|
|
|
assert(pathparts.size() >= 1);
|
|
|
|
|
std::string finalpart = pathparts.back();
|
|
|
|
|
pathparts.pop_back();
|
|
|
|
|
|
|
|
|
|
// Create subtables as necessary.
|
|
|
|
|
for (const std::string &subname : pathparts) {
|
|
|
|
|
LS.rawget(subtab, tab, subname);
|
|
|
|
|
if (LS.isnil(subtab)) {
|
|
|
|
|
LS.set(subtab, LuaNewTable);
|
|
|
|
|
LS.rawset(tab, subname, subtab);
|
|
|
|
|
}
|
|
|
|
|
assert(LS.istable(subtab));
|
|
|
|
|
LS.set(tab, subtab);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the string value.
|
|
|
|
|
LS.rawset(tab, finalpart, value);
|
|
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::tangible_copy_global(int64_t id, const std::string &path, const std::string &global) {
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar tangibles, tab, subtab, globtab, value;
|
|
|
|
|
LuaStack LS(L, tangibles, tab, subtab, globtab, value);
|
|
|
|
|
|
|
|
|
|
// Fetch the lua side of the tangible.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(tab, tangibles, id);
|
|
|
|
|
assert(LS.istable(tab));
|
|
|
|
|
|
|
|
|
|
// Split the path parts into the table names and final part.
|
|
|
|
|
util::StringVec pathparts = util::split(path, '.');
|
|
|
|
|
assert(pathparts.size() >= 1);
|
|
|
|
|
std::string finalpart = pathparts.back();
|
|
|
|
|
pathparts.pop_back();
|
|
|
|
|
|
|
|
|
|
// Create subtables as necessary.
|
|
|
|
|
for (const std::string &subname : pathparts) {
|
|
|
|
|
LS.rawget(subtab, tab, subname);
|
|
|
|
|
if (LS.isnil(subtab)) {
|
|
|
|
|
LS.set(subtab, LuaNewTable);
|
|
|
|
|
LS.rawset(tab, subname, subtab);
|
|
|
|
|
}
|
|
|
|
|
assert(LS.istable(subtab));
|
|
|
|
|
LS.set(tab, subtab);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy the global value.
|
|
|
|
|
LS.getglobaltable(globtab);
|
|
|
|
|
LS.rawget(value, globtab, global);
|
|
|
|
|
LS.rawset(tab, finalpart, value);
|
|
|
|
|
LS.result();
|
|
|
|
|
//assert(stack_is_clear());
|
|
|
|
|
}
|
2021-08-03 11:25:12 -04:00
|
|
|
|
2021-08-09 12:54:32 -04:00
|
|
|
util::IdVector World::get_near_unsorted(int64_t player_id, float radius, bool exclude_nowhere) const {
|
2021-07-26 13:12:44 -04:00
|
|
|
const Tangible *player = tangible_get(player_id);
|
2021-03-30 19:30:58 -04:00
|
|
|
if (player == nullptr) {
|
2021-07-25 19:22:39 -04:00
|
|
|
return IdVector();
|
2021-03-30 19:30:58 -04:00
|
|
|
}
|
2021-01-22 19:10:47 -05:00
|
|
|
|
2021-02-07 13:38:29 -05:00
|
|
|
// Find out where's the center of the world.
|
2021-03-28 13:27:28 -04:00
|
|
|
const AnimStep &aqback = player->anim_queue_.back();
|
|
|
|
|
if (exclude_nowhere && (aqback.plane() == "nowhere")) {
|
2021-07-25 19:22:39 -04:00
|
|
|
return IdVector();
|
2021-03-25 15:34:43 -04:00
|
|
|
}
|
2021-02-07 13:38:29 -05:00
|
|
|
|
2021-03-28 13:27:28 -04:00
|
|
|
return plane_map_.scan_radius(aqback.plane(), aqback.xyz().x, aqback.xyz().y, radius, player_id);
|
2021-01-22 19:10:47 -05:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 12:54:32 -04:00
|
|
|
util::IdVector World::get_near(int64_t player_id, float radius, bool exclude_nowhere) const {
|
|
|
|
|
util::IdVector v = get_near_unsorted(player_id, radius, exclude_nowhere);
|
|
|
|
|
std::sort(v.begin(), v.end());
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 17:35:23 -05:00
|
|
|
Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
|
2021-08-03 11:25:12 -04:00
|
|
|
// Get a state if we don't already have one.
|
|
|
|
|
if (L == nullptr) {
|
|
|
|
|
L = state();
|
|
|
|
|
assert(!pushdb);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
LuaVar tangibles, metatab;
|
|
|
|
|
LuaRet database;
|
|
|
|
|
LuaStack LS(L, tangibles, database, metatab);
|
|
|
|
|
|
2021-01-22 17:35:23 -05:00
|
|
|
// Allocate an ID if we don't already have one.
|
|
|
|
|
if (id == 0) id = id_global_pool_.alloc_id_for_thread(L);
|
2021-01-17 16:23:10 -05:00
|
|
|
|
|
|
|
|
// Create the C++ part of the structure.
|
2021-03-14 18:17:34 -04:00
|
|
|
std::unique_ptr<Tangible> &t = tangibles_[id];
|
|
|
|
|
assert (t == nullptr);
|
|
|
|
|
t.reset(new Tangible(this, id));
|
2021-01-17 16:23:10 -05:00
|
|
|
|
|
|
|
|
// Create the tangible's database and metatable.
|
|
|
|
|
LS.set(database, LuaNewTable);
|
|
|
|
|
LS.set(metatab, LuaNewTable);
|
|
|
|
|
LS.setmetatable(database, metatab);
|
|
|
|
|
|
2021-08-10 10:41:06 -04:00
|
|
|
// Mark the tangible using the tabletype field.
|
2021-08-23 23:34:30 -04:00
|
|
|
LS.settabletype(database, LUA_TT_TANGIBLE);
|
|
|
|
|
LS.settabletype(metatab, LUA_TT_TANGIBLEMETA);
|
2021-08-10 10:41:06 -04:00
|
|
|
|
2021-01-17 16:23:10 -05:00
|
|
|
// Store the database into the tangibles table.
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
2021-01-17 16:23:10 -05:00
|
|
|
LS.rawset(tangibles, id, database);
|
|
|
|
|
|
|
|
|
|
// Populate the database and metatable with initial stuff.
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawset(database, "inventory", LuaNewTable);
|
|
|
|
|
LS.rawset(metatab, "id", id);
|
2021-02-17 13:38:22 -05:00
|
|
|
LS.rawset(metatab, "threads", LuaNewTable);
|
2021-02-10 16:47:45 -05:00
|
|
|
// LS.rawset(metatab, "__metatable", LuaNil);
|
2021-01-17 16:23:10 -05:00
|
|
|
|
|
|
|
|
LS.result();
|
2021-01-22 17:35:23 -05:00
|
|
|
if (!pushdb) lua_pop(L, 1);
|
2021-03-14 18:17:34 -04:00
|
|
|
return t.get();
|
2021-01-17 16:23:10 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-30 18:35:08 -04:00
|
|
|
World::Redirects World::fetch_redirects() {
|
|
|
|
|
World::Redirects result = std::move(redirects_);
|
|
|
|
|
redirects_.clear();
|
|
|
|
|
return std::move(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t World::create_login_actor() {
|
|
|
|
|
Tangible *tan = tangible_make(state(), 0, true);
|
|
|
|
|
LuaArg database;
|
|
|
|
|
LuaVar classtab, mt;
|
|
|
|
|
LuaStack LS(state(), database, classtab, mt);
|
|
|
|
|
LS.makeclass(classtab, "login");
|
|
|
|
|
LS.getmetatable(mt, database);
|
|
|
|
|
LS.rawset(mt, "__index", classtab);
|
|
|
|
|
LS.result();
|
2021-07-18 17:48:39 -04:00
|
|
|
tan->configure_id_pool_for_actor();
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
return tan->id();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 17:26:48 -05:00
|
|
|
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-02-07 17:26:48 -05:00
|
|
|
gui->clear();
|
2021-02-09 17:15:54 -05:00
|
|
|
lua_State *L = state();
|
2021-02-07 17:26:48 -05:00
|
|
|
|
2021-02-16 13:31:34 -05:00
|
|
|
LuaVar actor, place, ugui, func, tangibles, mt, index;
|
|
|
|
|
LuaStack LS(L, actor, place, ugui, func, tangibles, mt, index);
|
2021-02-07 17:26:48 -05:00
|
|
|
|
|
|
|
|
// Get the actor and place.
|
2021-02-10 16:47:45 -05:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
2021-02-07 17:26:48 -05:00
|
|
|
LS.rawget(actor, tangibles, actor_id);
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(actor) || !LS.istable(place)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the interface closure.
|
2021-02-16 13:31:34 -05:00
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(index, mt, "__index");
|
|
|
|
|
if (!LS.istable(index)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(func, index, "interface");
|
2021-02-07 17:26:48 -05:00
|
|
|
if (!LS.isfunction(func)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call the interface function.
|
|
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, actor.index());
|
|
|
|
|
lua_pushvalue(L, place.index());
|
2021-02-25 14:09:16 -05:00
|
|
|
Gui::store_global_pointer(L, gui);
|
|
|
|
|
int status = traceback_pcall(L, 2, 0);
|
|
|
|
|
Gui::store_global_pointer(L, nullptr);
|
2021-02-07 17:26:48 -05:00
|
|
|
if (status != 0) {
|
|
|
|
|
gui->clear();
|
|
|
|
|
std::cerr << lua_tostring(L, -1);
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// And we're done.
|
|
|
|
|
LS.result();
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-02-07 17:26:48 -05:00
|
|
|
}
|
2021-02-16 13:31:34 -05:00
|
|
|
|
2021-07-19 15:32:34 -04:00
|
|
|
void World::invoke(const Invocation &inv) {
|
|
|
|
|
switch (inv.kind()) {
|
|
|
|
|
case Invocation::KIND_PLAN:
|
|
|
|
|
invoke_plan(inv.actor(), inv.place(), inv.action(), inv.data());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// Do nothing. Standard behavior for any invalid command is to
|
|
|
|
|
// simply do nothing at all. Perhaps eventually we may add a flag
|
|
|
|
|
// to the world model to indicate that we've detected an invalid
|
|
|
|
|
// command, to allow us to close the connection to a client that
|
|
|
|
|
// is misbehaving.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const InvocationData &idata) {
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
|
2021-02-16 14:15:40 -05:00
|
|
|
// Validate that the action is legal.
|
|
|
|
|
Gui validation_gui;
|
|
|
|
|
update_gui(actor_id, place_id, &validation_gui);
|
|
|
|
|
if (!validation_gui.has_action(action)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 19:30:58 -04:00
|
|
|
// Get the actor and place. Make sure both exist.
|
2021-02-18 14:56:12 -05:00
|
|
|
Tangible *tactor = tangible_get(actor_id);
|
2021-03-30 19:30:58 -04:00
|
|
|
Tangible *tplace = tangible_get(place_id);
|
|
|
|
|
if ((tactor == nullptr) || (tplace == nullptr)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get an ID batch for the thread, and take one for the thread itself.
|
2021-03-14 18:17:34 -04:00
|
|
|
int64_t id_batch = tactor->id_player_pool_.get_batch();
|
2021-02-18 14:56:12 -05:00
|
|
|
int64_t tid = id_batch++;
|
2021-02-16 13:31:34 -05:00
|
|
|
|
2021-02-18 14:56:12 -05:00
|
|
|
// Set up for Lua manipulation.
|
|
|
|
|
lua_State *L = state();
|
2021-07-19 15:32:34 -04:00
|
|
|
LuaVar actor, place, func, tangibles, mt, index, actions, thread, threads, message, invdata;
|
|
|
|
|
LuaStack LS(L, actor, place, func, tangibles, mt, index, actions, thread, threads, message, invdata);
|
2021-02-16 13:31:34 -05:00
|
|
|
|
|
|
|
|
// Get the actor and place.
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(actor, tangibles, actor_id);
|
|
|
|
|
LS.rawget(place, tangibles, place_id);
|
|
|
|
|
if (!LS.istable(actor) || !LS.istable(place)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the action closure.
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(index, mt, "__index");
|
|
|
|
|
if (!LS.istable(index)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(actions, index, "action");
|
|
|
|
|
if (!LS.istable(actions)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(func, actions, action);
|
|
|
|
|
if (!LS.isfunction(func)) {
|
|
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-19 15:32:34 -04:00
|
|
|
// Convert the InvocationData into a lua table.
|
|
|
|
|
LS.newtable(invdata);
|
|
|
|
|
for (const auto &p : idata) {
|
|
|
|
|
LS.rawset(invdata, p.first, p.second);
|
2021-02-25 14:09:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 13:38:22 -05:00
|
|
|
// Create a new thread, set up function and parameters.
|
|
|
|
|
lua_State *CO = LS.newthread(thread);
|
2021-02-16 13:31:34 -05:00
|
|
|
lua_pushvalue(L, func.index());
|
|
|
|
|
lua_pushvalue(L, actor.index());
|
|
|
|
|
lua_pushvalue(L, place.index());
|
2021-07-19 15:32:34 -04:00
|
|
|
lua_pushvalue(L, invdata.index());
|
2021-02-17 13:38:22 -05:00
|
|
|
lua_xmove(L, CO, 4);
|
|
|
|
|
|
2021-02-18 14:56:12 -05:00
|
|
|
// Store the thread into place's thread table.
|
2021-02-18 16:19:43 -05:00
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
2021-02-18 14:56:12 -05:00
|
|
|
LS.result();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-02-18 16:19:43 -05:00
|
|
|
LS.rawset(threads, tid, thread);
|
2021-02-18 14:56:12 -05:00
|
|
|
LS.result();
|
2021-02-20 19:17:20 -05:00
|
|
|
|
2021-02-18 14:56:12 -05:00
|
|
|
// Push the thread's ID into the runnable thread queue,
|
|
|
|
|
// then run the thread queue.
|
2021-02-18 17:21:25 -05:00
|
|
|
thread_sched_.add(0, tid, place_id);
|
|
|
|
|
run_scheduled_threads(0);
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-02-18 14:56:12 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 17:21:25 -05:00
|
|
|
void World::run_scheduled_threads(int64_t clk) {
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-02-18 14:56:12 -05:00
|
|
|
lua_State *L = state();
|
2021-02-18 16:19:43 -05:00
|
|
|
LuaVar tangibles, place, mt, threads, thread;
|
|
|
|
|
LuaStack LS(L, tangibles, place, mt, threads, thread);
|
2021-02-18 14:56:12 -05:00
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
|
2021-02-18 17:21:25 -05:00
|
|
|
while (thread_sched_.ready(clk)) {
|
|
|
|
|
SchedEntry sched = thread_sched_.pop();
|
|
|
|
|
LS.rawget(place, tangibles, sched.place_id());
|
|
|
|
|
if (!LS.istable(place)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LS.getmetatable(mt, place);
|
|
|
|
|
if (!LS.istable(mt)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(threads, mt, "threads");
|
|
|
|
|
if (!LS.istable(threads)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LS.rawget(thread, threads, sched.thread_id());
|
|
|
|
|
if (!LS.isthread(thread)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resume the coroutine.
|
|
|
|
|
lua_State *CO = LS.ckthread(thread);
|
2021-02-20 19:17:20 -05:00
|
|
|
int top = lua_gettop(CO);
|
2021-02-28 15:05:45 -05:00
|
|
|
int status = lua_resume(CO, nullptr, (top > 0) ? (top - 1) : 0);
|
2021-02-18 17:21:25 -05:00
|
|
|
|
|
|
|
|
// Three possible outcomes: finished, yielded, or errored.
|
|
|
|
|
if (status == LUA_YIELD) {
|
|
|
|
|
// When the wait statement yields, it yields the desired timestamp.
|
2021-02-20 19:17:20 -05:00
|
|
|
if ((lua_gettop(CO) != 1) || (!lua_isnumber(CO, 1))) {
|
|
|
|
|
std::cerr << "Thread yielded incorrectly. Killing it." << std::endl;
|
|
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
} else {
|
|
|
|
|
lua_Number delay = lua_tonumber(CO, 1);
|
|
|
|
|
lua_settop(CO, 0);
|
|
|
|
|
std::cerr << "Thread wait = " << delay << std::endl;
|
|
|
|
|
thread_sched_.add(sched.clock() + int64_t(delay), sched.thread_id(), sched.place_id());
|
|
|
|
|
std::cerr << "Added to schedule." << std::endl;
|
|
|
|
|
}
|
2021-06-03 13:29:19 -04:00
|
|
|
} else if (status == LUA_OK) {
|
2021-02-18 17:21:25 -05:00
|
|
|
// Successfully ran to completion. Remove from thread table.
|
|
|
|
|
std::cerr << "Thread ran to completion." << std::endl;
|
|
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
} else {
|
2021-02-20 19:17:20 -05:00
|
|
|
// Generated an error. Add a traceback, print, and kill the coroutine.
|
|
|
|
|
traceback_coroutine(CO);
|
|
|
|
|
std::cerr << lua_tostring(CO, -1);
|
2021-02-18 17:21:25 -05:00
|
|
|
LS.rawset(threads, sched.thread_id(), LuaNil);
|
|
|
|
|
}
|
2021-02-16 13:31:34 -05:00
|
|
|
}
|
|
|
|
|
LS.result();
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-02-16 13:31:34 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-16 12:19:37 -04:00
|
|
|
void World::serialize(StreamBuffer *sb) {
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
assert(redirects_.empty());
|
2021-08-03 11:25:12 -04:00
|
|
|
// int64_t wc0 = sb->total_writes();
|
2021-03-16 12:19:37 -04:00
|
|
|
lua_snap_.serialize(sb);
|
|
|
|
|
id_global_pool_.serialize(sb);
|
|
|
|
|
thread_sched_.serialize(sb);
|
2021-07-19 17:32:24 -04:00
|
|
|
sb->write_uint32(tangibles_.size());
|
2021-03-16 12:19:37 -04:00
|
|
|
for (const auto &p : tangibles_) {
|
|
|
|
|
sb->write_int64(p.first);
|
|
|
|
|
p.second->serialize(sb);
|
|
|
|
|
}
|
2021-08-03 11:25:12 -04:00
|
|
|
// int64_t wc1 = sb->total_writes();
|
|
|
|
|
// std::cerr << "World serialized to " << wc1-wc0 << " bytes." << std::endl;
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-03-16 12:19:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::deserialize(StreamBuffer *sb) {
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
|
|
|
|
redirects_.clear();
|
2021-03-16 12:19:37 -04:00
|
|
|
lua_snap_.deserialize(sb);
|
|
|
|
|
id_global_pool_.deserialize(sb);
|
|
|
|
|
thread_sched_.deserialize(sb);
|
|
|
|
|
// Mark all tangibles for deletion by setting ID to zero.
|
|
|
|
|
for (const auto &p : tangibles_) {
|
|
|
|
|
p.second->plane_item_.set_id(0);
|
|
|
|
|
}
|
|
|
|
|
// Deserialize tangibles.
|
2021-07-19 17:32:24 -04:00
|
|
|
size_t ntan = sb->read_uint32();
|
2021-03-16 12:19:37 -04:00
|
|
|
for (size_t i = 0; i < ntan; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
|
|
|
|
std::unique_ptr<Tangible> &t = tangibles_[id];
|
|
|
|
|
if (t == nullptr) {
|
|
|
|
|
t.reset(new Tangible(this, id));
|
|
|
|
|
} else {
|
|
|
|
|
t->plane_item_.set_id(id);
|
|
|
|
|
}
|
|
|
|
|
t->deserialize(sb);
|
|
|
|
|
}
|
|
|
|
|
// Delete tangibles that didn't get deserialized.
|
|
|
|
|
for (auto iter = tangibles_.begin(); iter != tangibles_.end(); ) {
|
|
|
|
|
if (iter->second->plane_item_.id() == 0) {
|
|
|
|
|
tangibles_.erase(iter++);
|
|
|
|
|
} else {
|
|
|
|
|
++iter;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-30 18:35:08 -04:00
|
|
|
assert(stack_is_clear());
|
2021-03-16 12:19:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::snapshot() {
|
|
|
|
|
snapshot_.clear();
|
|
|
|
|
serialize(&snapshot_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::rollback() {
|
|
|
|
|
assert(!snapshot_.at_eof());
|
|
|
|
|
deserialize(&snapshot_);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
void World::difference_transmit(int64_t actor_id, World *master, StreamBuffer *sb) {
|
2021-07-25 19:22:39 -04:00
|
|
|
StreamBuffer tsb;
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
// Get the actor in both models. s_actor may be nil.
|
2021-07-26 13:12:44 -04:00
|
|
|
const Tangible *s_actor = tangible_get(actor_id);
|
|
|
|
|
const Tangible *m_actor = master->tangible_get(actor_id);
|
|
|
|
|
assert(m_actor != nullptr);
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
// Pass one: update the actor essentials. Create actor if doesn't exist.
|
2021-07-26 13:12:44 -04:00
|
|
|
assert(tsb.at_eof());
|
|
|
|
|
diff_actor_essentials(m_actor, s_actor, &tsb);
|
2021-07-25 19:22:39 -04:00
|
|
|
tsb.copy_into(sb);
|
|
|
|
|
patch_actor_essentials(&tsb);
|
|
|
|
|
|
2021-07-26 13:12:44 -04:00
|
|
|
// Get the list of tangibles visible in either model.
|
|
|
|
|
// Some tangibles may be missing in the master, some may be missing in the sync.
|
2021-07-30 13:22:23 -04:00
|
|
|
util::IdVector visible = util::sort_union_id_vectors(
|
2021-08-09 12:54:32 -04:00
|
|
|
master->get_near_unsorted(actor_id, RadiusVisibility, true),
|
|
|
|
|
get_near_unsorted(actor_id, RadiusVisibility, true));
|
2021-08-03 11:25:12 -04:00
|
|
|
TanVector m_visible = master->tangible_get_all(visible);
|
2021-07-26 13:12:44 -04:00
|
|
|
TanVector s_visible = tangible_get_all(visible);
|
|
|
|
|
assert(m_visible.size() == s_visible.size());
|
|
|
|
|
|
|
|
|
|
// Pass two: update visible animations.
|
|
|
|
|
assert(tsb.at_eof());
|
|
|
|
|
diff_visible_animations(m_visible, s_visible, &tsb);
|
2021-07-25 19:22:39 -04:00
|
|
|
tsb.copy_into(sb);
|
|
|
|
|
patch_visible_animations(&tsb);
|
2021-07-26 13:12:44 -04:00
|
|
|
|
|
|
|
|
// Copy the version number from master animqueue to synch animqueue.
|
|
|
|
|
for (int i = 0; i < int(m_visible.size()); i++) {
|
|
|
|
|
const Tangible *m_tan = m_visible[i];
|
|
|
|
|
if (m_tan != nullptr) {
|
|
|
|
|
Tangible *s_tan = const_cast<Tangible *>(s_visible[i]);
|
|
|
|
|
if (s_tan == nullptr) {
|
|
|
|
|
s_tan = tangible_get(m_tan->id());
|
|
|
|
|
assert(s_tan != nullptr);
|
|
|
|
|
}
|
|
|
|
|
s_tan->anim_queue_.update_version(m_tan->anim_queue_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 12:54:32 -04:00
|
|
|
// Obtain the list of tangibles whose tables we're going to transmit.
|
|
|
|
|
util::IdVector closetans = get_near(actor_id, RadiusClose, true);
|
|
|
|
|
assert(closetans == master->get_near(actor_id, RadiusClose, true));
|
|
|
|
|
util::HashValue closehash = util::hash_id_vector(closetans);
|
|
|
|
|
|
|
|
|
|
// Confirm the close tangibles with the client.
|
|
|
|
|
sb->write_hashvalue(closehash);
|
|
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
// Number tables in both the master and the synchronous model.
|
|
|
|
|
// Meanwhile, the client will number tables in the client-synchronous model.
|
|
|
|
|
master->number_lua_tables(closetans);
|
2021-08-23 23:34:30 -04:00
|
|
|
number_lua_tables(closetans);
|
2021-08-13 17:02:35 -04:00
|
|
|
|
|
|
|
|
// Pair tables from the synchronous and master models.
|
2021-08-23 23:34:30 -04:00
|
|
|
pair_lua_tables(closetans, master->state());
|
|
|
|
|
int ncreate = pair_new_tables(closetans, master->state());
|
2021-08-13 17:02:35 -04:00
|
|
|
sb->write_int32(ncreate);
|
|
|
|
|
create_new_tables(ncreate);
|
|
|
|
|
|
2021-07-26 13:12:44 -04:00
|
|
|
assert(tsb.at_eof());
|
2021-07-25 19:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
void World::apply_differences(StreamBuffer *sb) {
|
2021-08-09 12:54:32 -04:00
|
|
|
int64_t actor_id = patch_actor_essentials(sb);
|
2021-08-03 11:25:12 -04:00
|
|
|
patch_visible_animations(sb);
|
2021-08-09 12:54:32 -04:00
|
|
|
util::IdVector closetans = get_near(actor_id, RadiusClose, true);
|
|
|
|
|
util::HashValue closehash = util::hash_id_vector(closetans);
|
|
|
|
|
util::HashValue m_closehash = sb->read_hashvalue();
|
|
|
|
|
assert(closehash == m_closehash);
|
2021-08-23 23:34:30 -04:00
|
|
|
number_lua_tables(closetans);
|
2021-08-13 17:02:35 -04:00
|
|
|
int ncreate = sb->read_int32();
|
|
|
|
|
create_new_tables(ncreate);
|
2021-08-03 11:25:12 -04:00
|
|
|
}
|
|
|
|
|
|
2021-07-26 13:12:44 -04:00
|
|
|
void World::diff_actor_essentials(const Tangible *m_actor, const Tangible *s_actor, StreamBuffer *sb) {
|
|
|
|
|
sb->write_int64(m_actor->id());
|
2021-08-03 11:25:12 -04:00
|
|
|
if (s_actor == nullptr) {
|
|
|
|
|
m_actor->id_player_pool_.serialize(sb);
|
|
|
|
|
m_actor->anim_queue_.serialize(sb);
|
|
|
|
|
} else {
|
|
|
|
|
s_actor->id_player_pool_.diff(m_actor->id_player_pool_, sb);
|
|
|
|
|
s_actor->anim_queue_.diff(m_actor->anim_queue_, sb);
|
|
|
|
|
}
|
2021-07-25 19:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 12:54:32 -04:00
|
|
|
int64_t World::patch_actor_essentials(StreamBuffer *sb) {
|
2021-07-25 19:22:39 -04:00
|
|
|
int64_t actor_id = sb->read_int64();
|
|
|
|
|
Tangible *s_actor = tangible_get(actor_id);
|
2021-08-03 11:25:12 -04:00
|
|
|
if (s_actor == nullptr) {
|
|
|
|
|
s_actor = tangible_make(nullptr, actor_id, false);
|
|
|
|
|
s_actor->id_player_pool_.deserialize(sb);
|
|
|
|
|
s_actor->anim_queue_.deserialize(sb);
|
|
|
|
|
} else {
|
|
|
|
|
s_actor->id_player_pool_.patch(sb);
|
|
|
|
|
s_actor->anim_queue_.patch(sb);
|
|
|
|
|
}
|
2021-07-25 19:22:39 -04:00
|
|
|
s_actor->update_plane_item();
|
2021-08-09 12:54:32 -04:00
|
|
|
return actor_id;
|
2021-07-25 19:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
2021-07-26 13:12:44 -04:00
|
|
|
void World::diff_visible_animations(const TanVector &mvis, const TanVector &svis, StreamBuffer *sb) {
|
2021-07-25 19:22:39 -04:00
|
|
|
// For each tangible missing in the synchronous model, send the
|
|
|
|
|
// necessary information to create the tangible.
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
int count_pos = sb->total_writes();
|
|
|
|
|
int count = 0;
|
2021-07-26 13:12:44 -04:00
|
|
|
for (int i = 0; i < int(svis.size()); i++) {
|
|
|
|
|
const Tangible *mt = mvis[i];
|
|
|
|
|
const Tangible *st = svis[i];
|
2021-07-25 19:22:39 -04:00
|
|
|
if (st == nullptr) {
|
|
|
|
|
count += 1;
|
|
|
|
|
sb->write_int64(mt->id());
|
2021-08-03 11:25:12 -04:00
|
|
|
mt->anim_queue_.serialize(sb);
|
2021-07-25 19:22:39 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(count_pos, count);
|
|
|
|
|
|
|
|
|
|
// For each tangible present in the synchronous model that doesn't
|
|
|
|
|
// exist in the master model, send command to delete it.
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
count_pos = sb->total_writes();
|
|
|
|
|
count = 0;
|
2021-07-26 13:12:44 -04:00
|
|
|
for (int i = 0; i < int(svis.size()); i++) {
|
|
|
|
|
const Tangible *mt = mvis[i];
|
|
|
|
|
const Tangible *st = svis[i];
|
2021-07-25 19:22:39 -04:00
|
|
|
if (mt == nullptr) {
|
|
|
|
|
count += 1;
|
|
|
|
|
sb->write_int64(st->id());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(count_pos, count);
|
|
|
|
|
|
|
|
|
|
// For each tangible present in both models, compare
|
|
|
|
|
// the animation queues.
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
count_pos = sb->total_writes();
|
|
|
|
|
count = 0;
|
2021-07-26 13:12:44 -04:00
|
|
|
for (int i = 0; i < int(svis.size()); i++) {
|
|
|
|
|
const Tangible *mt = mvis[i];
|
|
|
|
|
const Tangible *st = svis[i];
|
2021-07-25 19:22:39 -04:00
|
|
|
if ((mt != nullptr) && (st != nullptr)) {
|
|
|
|
|
if (st->anim_queue_.need_patch(mt->anim_queue_)) {
|
|
|
|
|
count++;
|
|
|
|
|
sb->write_int64(st->id());
|
|
|
|
|
st->anim_queue_.diff(mt->anim_queue_, sb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(count_pos, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::patch_visible_animations(StreamBuffer *sb) {
|
|
|
|
|
// Receive create messages.
|
|
|
|
|
int count = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
|
|
|
|
Tangible *t = tangible_make(state(), id, false);
|
2021-08-03 11:25:12 -04:00
|
|
|
t->anim_queue_.deserialize(sb);
|
2021-07-25 19:22:39 -04:00
|
|
|
t->update_plane_item();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Receive delete messages
|
|
|
|
|
count = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
2021-08-03 12:40:12 -04:00
|
|
|
tangible_delete(id);
|
2021-07-25 19:22:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Receive update messages
|
|
|
|
|
count = sb->read_int32();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
int64_t id = sb->read_int64();
|
|
|
|
|
Tangible *t = tangible_get(id);
|
|
|
|
|
assert(t != nullptr);
|
|
|
|
|
t->anim_queue_.patch(sb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
int World::number_lua_tables(const IdVector &basis) {
|
|
|
|
|
// This is conceptually recursive, but we're going to use an
|
|
|
|
|
// explicit stack (the lua stack).
|
|
|
|
|
lua_State *L = state();
|
|
|
|
|
LuaVar tnmap, ntmap, tangibles, tab, key, val, xid;
|
|
|
|
|
LuaStack LS(L, tnmap, ntmap, tangibles, tab, key, val, xid);
|
|
|
|
|
LS.set(tnmap, LuaNewTable);
|
|
|
|
|
LS.set(ntmap, LuaNewTable);
|
|
|
|
|
LS.rawset(LuaRegistry, "tnmap", tnmap);
|
|
|
|
|
LS.rawset(LuaRegistry, "ntmap", ntmap);
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
int nextid = 1;
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
|
|
|
|
|
// Push all subtables onto the stack. Note that we may push
|
|
|
|
|
// the same table twice, that's OK.
|
|
|
|
|
for (int64_t id : basis) {
|
|
|
|
|
LS.rawget(tab, tangibles, id);
|
|
|
|
|
assert(LS.istable(tab));
|
|
|
|
|
// Traverse subtables.
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(tab, key, val)) {
|
2021-08-23 23:34:30 -04:00
|
|
|
if (LS.istable(val) && LS.gettabletype(val)==LUA_TT_GENERAL) {
|
2021-08-13 17:02:35 -04:00
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pop tables from the stack one by one. If the table is not
|
|
|
|
|
// already numbered, number it and push subtables onto the stack.
|
|
|
|
|
while (lua_gettop(L) > top) {
|
|
|
|
|
lua_replace(L, tab.index());
|
|
|
|
|
LS.rawget(xid, tnmap, tab);
|
|
|
|
|
if (LS.isnil(xid)) {
|
|
|
|
|
int id = nextid++;
|
|
|
|
|
LS.rawset(tnmap, tab, id);
|
|
|
|
|
LS.rawset(ntmap, id, tab);
|
|
|
|
|
// Traverse the metatable.
|
|
|
|
|
LS.getmetatable(val, tab);
|
2021-08-23 23:34:30 -04:00
|
|
|
if (LS.istable(val) && LS.gettabletype(val)==LUA_TT_GENERAL) {
|
2021-08-13 17:02:35 -04:00
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
// Traverse the subtables.
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(tab, key, val)) {
|
2021-08-23 23:34:30 -04:00
|
|
|
if (LS.istable(val) && LS.gettabletype(val)==LUA_TT_GENERAL) {
|
2021-08-13 17:02:35 -04:00
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
return nextid - 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::unnumber_lua_tables() {
|
|
|
|
|
// All we have to do is remove these tables from the registry.
|
|
|
|
|
LuaStack LS(state());
|
|
|
|
|
LS.rawset(LuaRegistry, "tnmap", LuaNil);
|
|
|
|
|
LS.rawset(LuaRegistry, "ntmap", LuaNil);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
void World::pair_lua_tables(const IdVector &basis, lua_State *master) {
|
2021-08-13 17:02:35 -04:00
|
|
|
lua_State *synch = state();
|
|
|
|
|
LuaVar stangibles, mtangibles, sntmap, mntmap, stnmap, mtnmap, stab, mtab, skey, mkey, sval, mval, sidx, midx;
|
|
|
|
|
LuaStack SLS(synch, stangibles, stab, skey, sval, sntmap, stnmap, sidx);
|
|
|
|
|
LuaStack MLS(master, mtangibles, mtab, mkey, mval, mntmap, mtnmap, midx);
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
// Fetch the tangible databases
|
2021-08-13 17:02:35 -04:00
|
|
|
SLS.rawget(stangibles, LuaRegistry, "tangibles");
|
|
|
|
|
MLS.rawget(mtangibles, LuaRegistry, "tangibles");
|
2021-08-23 23:34:30 -04:00
|
|
|
|
|
|
|
|
// Fetch the synchronous model tnmap and ntmap
|
2021-08-13 17:02:35 -04:00
|
|
|
SLS.rawget(stnmap, LuaRegistry, "tnmap");
|
|
|
|
|
SLS.rawget(sntmap, LuaRegistry, "ntmap");
|
|
|
|
|
assert(SLS.istable(stnmap));
|
|
|
|
|
assert(SLS.istable(sntmap));
|
2021-08-23 23:34:30 -04:00
|
|
|
|
|
|
|
|
// Initialize the master model tnmap and ntmap
|
|
|
|
|
MLS.set(mtnmap, LuaNewTable);
|
|
|
|
|
MLS.set(mntmap, LuaNewTable);
|
|
|
|
|
MLS.rawset(LuaRegistry, "tnmap", mtnmap);
|
|
|
|
|
MLS.rawset(LuaRegistry, "ntmap", mntmap);
|
|
|
|
|
int s_ntables = SLS.rawlen(sntmap);
|
|
|
|
|
for (int i = 1; i <= s_ntables; i++) {
|
|
|
|
|
MLS.rawset(mntmap, i, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Keep track of which tables are already paired
|
|
|
|
|
std::vector<bool> paired;
|
|
|
|
|
paired.assign(s_ntables + 1, false);
|
|
|
|
|
|
|
|
|
|
// This records the top of the stack.
|
2021-08-13 17:02:35 -04:00
|
|
|
int mtop = lua_gettop(master);
|
|
|
|
|
|
|
|
|
|
for (int64_t id : basis) {
|
|
|
|
|
MLS.rawget(mtab, mtangibles, id);
|
|
|
|
|
SLS.rawget(stab, stangibles, id);
|
|
|
|
|
assert(MLS.istable(mtab));
|
|
|
|
|
assert(SLS.istable(stab));
|
|
|
|
|
MLS.set(mkey, LuaNil);
|
|
|
|
|
while (MLS.next(mtab, mkey, mval)) {
|
|
|
|
|
if (!MLS.issortablekey(mkey)) continue;
|
|
|
|
|
if (!MLS.istable(mval)) continue;
|
2021-08-23 23:34:30 -04:00
|
|
|
MLS.movesortablekey(mkey, SLS, skey);
|
2021-08-13 17:02:35 -04:00
|
|
|
SLS.rawget(sval, stab, skey);
|
|
|
|
|
if (!SLS.istable(sval)) continue;
|
|
|
|
|
lua_checkstack(master, 20);
|
|
|
|
|
lua_checkstack(synch, 20);
|
|
|
|
|
lua_pushvalue(master, mval.index());
|
|
|
|
|
lua_pushvalue(synch, sval.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (lua_gettop(master) > mtop) {
|
|
|
|
|
lua_replace(master, mtab.index());
|
|
|
|
|
lua_replace(synch, stab.index());
|
2021-08-23 23:34:30 -04:00
|
|
|
// If the master table is not a general table, skip.
|
|
|
|
|
if (MLS.gettabletype(mtab) != LUA_TT_GENERAL) continue;
|
|
|
|
|
// If the master table is already paired, skip.
|
2021-08-13 17:02:35 -04:00
|
|
|
MLS.rawget(midx, mtnmap, mtab);
|
2021-08-23 23:34:30 -04:00
|
|
|
if (MLS.isnumber(midx)) continue;
|
|
|
|
|
// If the synch table is not a table, skip.
|
|
|
|
|
if (!SLS.istable(stab)) continue;
|
|
|
|
|
// If the synch table doesn't have a number, skip.
|
2021-08-13 17:02:35 -04:00
|
|
|
SLS.rawget(sidx, stnmap, stab);
|
|
|
|
|
if (!SLS.isnumber(sidx)) continue;
|
2021-08-23 23:34:30 -04:00
|
|
|
int idx = SLS.ckinteger(sidx);
|
|
|
|
|
assert((idx >= 1) && (idx <= s_ntables));
|
|
|
|
|
// Pair the tables.
|
|
|
|
|
MLS.rawset(mtnmap, mtab, idx);
|
|
|
|
|
MLS.rawset(mntmap, idx, mtab);
|
|
|
|
|
paired[idx] = true;
|
|
|
|
|
// Potentially pair the metatables.
|
2021-08-13 17:02:35 -04:00
|
|
|
MLS.getmetatable(mval, mtab);
|
|
|
|
|
if (MLS.istable(mval)) {
|
|
|
|
|
SLS.getmetatable(sval, stab);
|
|
|
|
|
if (SLS.istable(sval)) {
|
|
|
|
|
lua_pushvalue(master, mval.index());
|
|
|
|
|
lua_pushvalue(synch, sval.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Pair the subtables.
|
|
|
|
|
MLS.set(mkey, LuaNil);
|
|
|
|
|
while (MLS.next(mtab, mkey, mval)) {
|
|
|
|
|
if (!MLS.issortablekey(mkey)) continue;
|
|
|
|
|
if (!MLS.istable(mval)) continue;
|
2021-08-23 23:34:30 -04:00
|
|
|
MLS.movesortablekey(mkey, SLS, skey);
|
2021-08-13 17:02:35 -04:00
|
|
|
SLS.rawget(sval, stab, skey);
|
|
|
|
|
if (!SLS.istable(sval)) continue;
|
|
|
|
|
lua_checkstack(master, 20);
|
|
|
|
|
lua_checkstack(synch, 20);
|
|
|
|
|
lua_pushvalue(master, mval.index());
|
|
|
|
|
lua_pushvalue(synch, sval.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MLS.result();
|
|
|
|
|
SLS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
int World::pair_new_tables(const IdVector &basis, lua_State *master) {
|
|
|
|
|
// This is conceptually recursive, but we're going to use an
|
|
|
|
|
// explicit stack (the lua stack).
|
|
|
|
|
lua_State *L = master;
|
|
|
|
|
LuaVar tnmap, ntmap, tangibles, tab, key, val, xid;
|
|
|
|
|
LuaStack LS(L, tnmap, ntmap, tangibles, tab, key, val, xid);
|
|
|
|
|
LS.rawget(tnmap, LuaRegistry, "tnmap");
|
|
|
|
|
LS.rawget(ntmap, LuaRegistry, "ntmap");
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
int ntables = LS.rawlen(ntmap);
|
|
|
|
|
std::vector<bool> visited;
|
|
|
|
|
visited.assign(ntables + 1, false);
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
|
|
|
|
|
// Push all subtables onto the stack. Note that we may push
|
|
|
|
|
// the same table twice, that's OK.
|
|
|
|
|
for (int64_t id : basis) {
|
|
|
|
|
LS.rawget(tab, tangibles, id);
|
|
|
|
|
assert(LS.istable(tab));
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(tab, key, val)) {
|
|
|
|
|
if (LS.istable(val) && LS.gettabletype(val)==LUA_TT_GENERAL) {
|
|
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pop tables from the stack one by one. If the table is not
|
|
|
|
|
// numbered, number it. If it is not visited, visit it.
|
|
|
|
|
while (lua_gettop(L) > top) {
|
|
|
|
|
lua_replace(L, tab.index());
|
|
|
|
|
int id = 0;
|
|
|
|
|
LS.rawget(xid, tnmap, tab);
|
|
|
|
|
if (!LS.isnumber(xid)) {
|
|
|
|
|
id = visited.size();
|
|
|
|
|
LS.rawset(tnmap, tab, id);
|
|
|
|
|
LS.rawset(ntmap, id, tab);
|
|
|
|
|
visited.push_back(false);
|
|
|
|
|
} else {
|
|
|
|
|
id = LS.cknumber(xid);
|
|
|
|
|
assert((id >= 0) && (id < int(visited.size())));
|
|
|
|
|
}
|
|
|
|
|
if (!visited[id]) {
|
|
|
|
|
visited[id] = true;
|
|
|
|
|
// Traverse the metatable.
|
|
|
|
|
LS.getmetatable(val, tab);
|
|
|
|
|
if (LS.istable(val) && LS.gettabletype(val)==LUA_TT_GENERAL) {
|
|
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
// Traverse the subtables.
|
|
|
|
|
LS.set(key, LuaNil);
|
|
|
|
|
while (LS.next(tab, key, val)) {
|
|
|
|
|
if (LS.istable(val) && LS.gettabletype(val)==LUA_TT_GENERAL) {
|
|
|
|
|
lua_checkstack(L, 10);
|
|
|
|
|
lua_pushvalue(L, val.index());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-13 17:02:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
|
|
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
return visited.size() - 1 - ntables;
|
2021-08-13 17:02:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void World::create_new_tables(int n) {
|
|
|
|
|
LuaVar tnmap, ntmap, tab;
|
|
|
|
|
LuaStack LS(state(), tnmap, ntmap, tab);
|
|
|
|
|
LS.rawget(tnmap, LuaRegistry, "tnmap");
|
|
|
|
|
LS.rawget(ntmap, LuaRegistry, "ntmap");
|
|
|
|
|
assert(LS.istable(tnmap));
|
|
|
|
|
assert(LS.istable(ntmap));
|
|
|
|
|
int ntables = LS.rawlen(ntmap);
|
|
|
|
|
int nextid = ntables + 1;
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
|
int id = nextid++;
|
|
|
|
|
LS.newtable(tab);
|
|
|
|
|
LS.rawset(ntmap, id, tab);
|
|
|
|
|
LS.rawset(tnmap, tab, id);
|
|
|
|
|
}
|
|
|
|
|
LS.result();
|
|
|
|
|
assert(stack_is_clear());
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
void World::diff_lua_tables(lua_State *master, StreamBuffer *sb) {
|
|
|
|
|
lua_State *synch = state();
|
|
|
|
|
LuaVar sntmap, mntmap, stnmap, mtnmap;
|
|
|
|
|
LuaStack SLS(synch, sntmap, stnmap);
|
|
|
|
|
LuaStack MLS(master, mntmap, mtnmap);
|
|
|
|
|
SLS.rawget(sntmap, LuaRegistry, "ntmap");
|
|
|
|
|
MLS.rawget(mntmap, LuaRegistry, "ntmap");
|
|
|
|
|
SLS.rawget(stnmap, LuaRegistry, "tnmap");
|
|
|
|
|
MLS.rawget(mtnmap, LuaRegistry, "tnmap");
|
|
|
|
|
int m_ntables = MLS.rawlen(mntmap);
|
|
|
|
|
int s_ntables = SLS.rawlen(sntmap);
|
|
|
|
|
assert(m_ntables == s_ntables);
|
|
|
|
|
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
int write_count_after = sb->total_writes();
|
|
|
|
|
int nmodified = 0;
|
|
|
|
|
int s_top = lua_gettop(synch);
|
|
|
|
|
int m_top = lua_gettop(master);
|
|
|
|
|
for (int id = 1; id <= m_ntables; id++) {
|
|
|
|
|
lua_pushvalue(master, mtnmap.index());
|
|
|
|
|
lua_rawgeti(master, mtnmap.index(), id);
|
|
|
|
|
if (lua_type(master, -1) == LUA_TTABLE) {
|
|
|
|
|
lua_pushvalue(synch, stnmap.index());
|
|
|
|
|
lua_rawgeti(synch, sntmap.index(), id);
|
|
|
|
|
int tw = sb->total_writes();
|
|
|
|
|
sb->write_int32(id);
|
|
|
|
|
nmodified += 1;
|
|
|
|
|
if (!tablecmp_diff(synch, master, true, sb)) {
|
|
|
|
|
sb->unwrite_to(tw);
|
|
|
|
|
nmodified -= 1;
|
|
|
|
|
}
|
|
|
|
|
assert(lua_gettop(synch) == s_top);
|
|
|
|
|
assert(lua_gettop(master) == m_top);
|
|
|
|
|
} else {
|
|
|
|
|
lua_pop(master, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(write_count_after, nmodified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void World::diff_tangible_databases(const IdVector &basis, lua_State *master, StreamBuffer *sb) {
|
|
|
|
|
lua_State *synch = state();
|
|
|
|
|
LuaVar stnmap, mtnmap, stangibles, mtangibles;
|
|
|
|
|
LuaStack SLS(synch, stnmap, stangibles);
|
|
|
|
|
LuaStack MLS(master, mtnmap, mtangibles);
|
|
|
|
|
SLS.rawget(stnmap, LuaRegistry, "tnmap");
|
|
|
|
|
MLS.rawget(mtnmap, LuaRegistry, "tnmap");
|
|
|
|
|
SLS.rawget(stangibles, LuaRegistry, "tangibles");
|
|
|
|
|
MLS.rawget(mtangibles, LuaRegistry, "tangibles");
|
|
|
|
|
|
|
|
|
|
sb->write_int32(0);
|
|
|
|
|
int write_count_after = sb->total_writes();
|
|
|
|
|
int nmodified = 0;
|
|
|
|
|
int s_top = lua_gettop(synch);
|
|
|
|
|
int m_top = lua_gettop(master);
|
|
|
|
|
for (int64_t id : basis) {
|
|
|
|
|
lua_pushvalue(master, mtnmap.index());
|
|
|
|
|
lua_rawgeti(master, mtangibles.index(), id);
|
|
|
|
|
lua_pushvalue(synch, stnmap.index());
|
|
|
|
|
lua_rawgeti(synch, stangibles.index(), id);
|
|
|
|
|
int tw = sb->total_writes();
|
|
|
|
|
sb->write_int64(id);
|
|
|
|
|
nmodified += 1;
|
|
|
|
|
if (!tablecmp_diff(synch, master, false, sb)) {
|
|
|
|
|
sb->unwrite_to(tw);
|
|
|
|
|
nmodified -= 1;
|
|
|
|
|
}
|
|
|
|
|
assert(lua_gettop(synch) == s_top);
|
|
|
|
|
assert(lua_gettop(master) == m_top);
|
|
|
|
|
}
|
|
|
|
|
sb->overwrite_int32(write_count_after, nmodified);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-03-28 13:27:28 -04:00
|
|
|
LuaDefine(tangible_animstate, "c") {
|
2021-02-25 15:43:05 -05:00
|
|
|
LuaArg tanobj;
|
2021-03-28 13:27:28 -04:00
|
|
|
LuaRet graphic, plane, x, y, z, facing;
|
|
|
|
|
LuaStack LS(L, tanobj, graphic, plane, x, y, z, facing);
|
2021-02-25 15:43:05 -05:00
|
|
|
World *w = World::fetch_global_pointer(L);
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *tan = w->tangible_get(LS, tanobj);
|
2021-03-28 13:27:28 -04:00
|
|
|
const AnimStep &aqback = tan->anim_queue_.back();
|
|
|
|
|
LS.set(graphic, aqback.graphic());
|
|
|
|
|
LS.set(plane, aqback.plane());
|
|
|
|
|
LS.set(x, aqback.xyz().x);
|
|
|
|
|
LS.set(y, aqback.xyz().y);
|
|
|
|
|
LS.set(z, aqback.xyz().z);
|
|
|
|
|
LS.set(facing, aqback.facing());
|
2021-03-19 13:41:41 -04:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 16:32:48 -05:00
|
|
|
LuaDefine(tangible_animate, "c") {
|
|
|
|
|
LuaArg tanobj, config;
|
|
|
|
|
LuaStack LS(L, tanobj, config);
|
2021-02-25 15:43:05 -05:00
|
|
|
World *w = World::fetch_global_pointer(L);
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *tan = w->tangible_get(LS, tanobj);
|
2021-02-25 15:43:05 -05:00
|
|
|
int64_t id = w->id_global_pool_.alloc_id_for_thread(L);
|
2021-06-03 13:29:19 -04:00
|
|
|
const AnimStep &prev = tan->anim_queue_.back();
|
2021-03-28 15:12:09 -04:00
|
|
|
AnimStep step;
|
2021-06-03 13:29:19 -04:00
|
|
|
step.from_lua(L, config.index(), prev);
|
2021-03-28 15:22:45 -04:00
|
|
|
if (step.action() == "") {
|
|
|
|
|
luaL_error(L, "animation action must be specified");
|
|
|
|
|
}
|
2021-03-28 15:12:09 -04:00
|
|
|
tan->anim_queue_.add(id, step);
|
2021-02-25 15:43:05 -05:00
|
|
|
tan->update_plane_item();
|
2021-01-17 16:23:10 -05:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-19 13:41:41 -04:00
|
|
|
LuaDefine(tangible_setclass, "c") {
|
|
|
|
|
LuaArg tanobj, classname;
|
|
|
|
|
LuaVar classtab, mt;
|
|
|
|
|
LuaStack LS(L, tanobj, classname, classtab, mt);
|
|
|
|
|
World *w = World::fetch_global_pointer(L);
|
2021-08-23 23:34:30 -04:00
|
|
|
w->tangible_get(LS, tanobj);
|
2021-03-19 13:41:41 -04:00
|
|
|
LS.getclass(classtab, classname);
|
|
|
|
|
LS.getmetatable(mt, tanobj);
|
|
|
|
|
LS.rawset(mt, "__index", classtab);
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(tangible_delete, "c") {
|
|
|
|
|
LuaArg tanobj;
|
|
|
|
|
LuaStack LS(L, tanobj);
|
|
|
|
|
World *w = World::fetch_global_pointer(L);
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *tan = w->tangible_get(LS, tanobj);
|
2021-08-03 12:40:12 -04:00
|
|
|
assert(tan != nullptr); // this should be checked above.
|
2021-03-30 19:30:58 -04:00
|
|
|
if (tan->is_an_actor()) {
|
|
|
|
|
luaL_error(L, "Cannot delete a player using tangible.delete, use tangible.redirect instead.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-08-03 12:40:12 -04:00
|
|
|
w->tangible_delete(tan->id());
|
2021-03-19 13:41:41 -04:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 15:12:09 -04:00
|
|
|
LuaDefine(tangible_build, "c") {
|
|
|
|
|
LuaArg config;
|
|
|
|
|
LuaVar classname, classtab, mt;
|
|
|
|
|
LuaRet database;
|
|
|
|
|
LuaStack LS(L, config, classname, classtab, database, mt);
|
|
|
|
|
|
|
|
|
|
LS.checktable(config);
|
|
|
|
|
// Get the class of the new tangible.
|
|
|
|
|
LS.rawget(classname, config, "class");
|
|
|
|
|
if (LS.isnil(classname)) {
|
|
|
|
|
luaL_error(L, "must specify a class name");
|
|
|
|
|
}
|
|
|
|
|
LS.getclass(classtab, classname);
|
|
|
|
|
|
|
|
|
|
// Parse the initial animation step.
|
2021-06-03 13:29:19 -04:00
|
|
|
AnimStep initstep, blank;
|
|
|
|
|
initstep.from_lua(L, config.index(), blank);
|
2021-03-28 15:12:09 -04:00
|
|
|
if (!initstep.has_xyz()) {
|
|
|
|
|
luaL_error(L, "You must specify (X,Y,Z) for new tangible");
|
|
|
|
|
}
|
|
|
|
|
if (!initstep.has_plane()) {
|
|
|
|
|
luaL_error(L, "You must specify plane for new tangible");
|
|
|
|
|
}
|
|
|
|
|
if (!initstep.has_graphic()) {
|
|
|
|
|
luaL_error(L, "You must specify graphic for new tangible");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: generate error if there's extra crap in the config table.
|
|
|
|
|
|
|
|
|
|
World *w = World::fetch_global_pointer(L);
|
|
|
|
|
Tangible *tan = w->tangible_make(L, 0, true);
|
|
|
|
|
lua_replace(L, database.index());
|
|
|
|
|
|
|
|
|
|
// Update the class of the new tangible.
|
|
|
|
|
LS.getmetatable(mt, database);
|
|
|
|
|
LS.rawset(mt, "__index", classtab);
|
|
|
|
|
|
|
|
|
|
// Update the animation queue and planemap of the new tangible.
|
|
|
|
|
int64_t stepid = w->id_global_pool_.alloc_id_for_thread(L);
|
|
|
|
|
tan->anim_queue_.add(stepid, initstep);
|
|
|
|
|
tan->update_plane_item();
|
|
|
|
|
|
|
|
|
|
return LS.result();
|
2021-01-17 16:23:10 -05:00
|
|
|
}
|
2021-02-20 19:17:20 -05:00
|
|
|
|
2021-03-28 13:27:28 -04:00
|
|
|
LuaDefine(tangible_get, "c") {
|
|
|
|
|
LuaArg id;
|
|
|
|
|
LuaVar tangibles;
|
|
|
|
|
LuaRet database;
|
|
|
|
|
LuaStack LS(L, id, tangibles, database);
|
|
|
|
|
int64_t nid = LS.ckinteger(id);
|
|
|
|
|
LS.rawget(tangibles, LuaRegistry, "tangibles");
|
|
|
|
|
LS.rawget(database, tangibles, id);
|
|
|
|
|
if (!LS.istable(database)) {
|
|
|
|
|
luaL_error(L, "Not a tangible ID: %d", nid);
|
|
|
|
|
}
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 18:35:08 -04:00
|
|
|
LuaDefine(tangible_redirect, "c") {
|
2021-03-30 19:30:58 -04:00
|
|
|
LuaArg actor1, actor2, bldz;
|
|
|
|
|
LuaStack LS(L, actor1, actor2, bldz);
|
2021-03-30 18:35:08 -04:00
|
|
|
World *w = World::fetch_global_pointer(L);
|
2021-03-30 19:30:58 -04:00
|
|
|
bool bulldoze = LS.ckboolean(bldz);
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *tan1 = w->tangible_get(LS, actor1);
|
2021-03-30 19:30:58 -04:00
|
|
|
if (!tan1->is_an_actor()) {
|
|
|
|
|
luaL_error(L, "redirect source is not an actor");
|
|
|
|
|
}
|
|
|
|
|
if (LS.isnil(actor2)) {
|
|
|
|
|
w->redirects_[tan1->id()] = 0;
|
|
|
|
|
} else {
|
2021-08-23 23:34:30 -04:00
|
|
|
Tangible *tan2 = w->tangible_get(LS, actor2);
|
2021-07-18 17:48:39 -04:00
|
|
|
tan2->configure_id_pool_for_actor();
|
2021-03-30 19:30:58 -04:00
|
|
|
w->redirects_[tan1->id()] = tan2->id();
|
|
|
|
|
}
|
|
|
|
|
if (bulldoze) {
|
2021-08-03 12:40:12 -04:00
|
|
|
w->tangible_delete(tan1->id());
|
2021-03-30 19:30:58 -04:00
|
|
|
}
|
2021-03-30 18:35:08 -04:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
LuaDefine(tangible_id, "c") {
|
|
|
|
|
LuaArg tanobj;
|
|
|
|
|
LuaRet id;
|
|
|
|
|
LuaStack LS(L, tanobj, id);
|
|
|
|
|
LS.set(id, World::tangible_id(LS, tanobj));
|
|
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 19:17:20 -05:00
|
|
|
LuaDefine(world_wait, "f") {
|
2021-02-25 14:09:16 -05:00
|
|
|
if ((lua_gettop(L) != 1) || (lua_type(L, -1) != LUA_TNUMBER)) {
|
2021-02-20 19:17:20 -05:00
|
|
|
luaL_error(L, "Argument to wait must be a number.");
|
|
|
|
|
}
|
|
|
|
|
return lua_yield(L, 1);
|
|
|
|
|
}
|
2021-02-25 14:09:16 -05:00
|
|
|
|
|
|
|
|
LuaDefine(world_getregistry, "f") {
|
|
|
|
|
lua_pushvalue(L, LUA_REGISTRYINDEX);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2021-08-03 11:25:12 -04:00
|
|
|
|
2021-08-23 23:34:30 -04:00
|
|
|
LuaDefine(world_xtype, "f") {
|
2021-08-10 10:41:06 -04:00
|
|
|
LuaArg tab;
|
2021-08-23 23:34:30 -04:00
|
|
|
LuaRet rtype;
|
|
|
|
|
LuaStack LS(L, tab, rtype);
|
|
|
|
|
int xt = LS.xtype(tab);
|
|
|
|
|
LS.set(rtype, xt);
|
2021-08-10 10:41:06 -04:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(world_settabletype, "f") {
|
|
|
|
|
LuaArg tab, ttype;
|
|
|
|
|
LuaStack LS(L, tab, ttype);
|
|
|
|
|
if (!LS.istable(tab)) {
|
|
|
|
|
luaL_error(L, "Not a table");
|
|
|
|
|
}
|
|
|
|
|
int tt = LS.ckinteger(ttype);
|
2021-08-23 23:34:30 -04:00
|
|
|
if ((tt < LUA_TT_GENERAL) || (tt > LUA_TT_CLASS)) {
|
2021-08-10 10:41:06 -04:00
|
|
|
luaL_error(L, "table type out of range");
|
|
|
|
|
}
|
2021-08-23 23:34:30 -04:00
|
|
|
LS.settabletype(tab, tt);
|
2021-08-10 10:41:06 -04:00
|
|
|
return LS.result();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
static bool worlds_identical(const std::unique_ptr<World> &w1, const std::unique_ptr<World> &w2) {
|
|
|
|
|
StreamBuffer sbw1, sbw2;
|
|
|
|
|
w1->serialize(&sbw1);
|
|
|
|
|
w2->serialize(&sbw2);
|
|
|
|
|
return sbw1.contents_equal(&sbw2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LuaDefine(unittests_world, "c") {
|
|
|
|
|
std::unique_ptr<World> m, ss, cs;
|
|
|
|
|
StreamBuffer sb;
|
2021-08-23 23:34:30 -04:00
|
|
|
int ncreate;
|
2021-08-03 11:25:12 -04:00
|
|
|
|
2021-08-13 17:02:35 -04:00
|
|
|
// Test the numbering of lua tables. We create some general
|
|
|
|
|
// tables using tangible_set_string. Then we install some
|
|
|
|
|
// specialty tables (not numberable) using tangible_copy_global.
|
|
|
|
|
// Finally, we number and dump the list of numbered tables.
|
|
|
|
|
m.reset(new World(util::WORLD_TYPE_MASTER));
|
|
|
|
|
m->tangible_make(0, 123, false);
|
|
|
|
|
m->tangible_set_string(123, "inventory.TID", "inventory");
|
|
|
|
|
m->tangible_set_string(123, "transactions.TID", "transactions");
|
|
|
|
|
m->tangible_set_string(123, "skills.TID", "skills");
|
|
|
|
|
m->tangible_set_string(123, "skills.leet.TID", "skills.leet");
|
|
|
|
|
m->tangible_set_string(123, "inventory.cplx.TID", "inventory.cplx");
|
|
|
|
|
m->tangible_copy_global(123, "math", "math");
|
|
|
|
|
m->tangible_copy_global(123, "gltab", "_G");
|
2021-08-23 23:34:30 -04:00
|
|
|
m->number_lua_tables(util::id_vector_create(123));
|
2021-08-13 17:02:35 -04:00
|
|
|
LuaAssertStrEq(L, m->numbered_tables_debug_string(),
|
|
|
|
|
"inventory;inventory.cplx;skills;skills.leet;transactions;");
|
|
|
|
|
|
|
|
|
|
// Now we're going to create a synchronous model that's similar to, but not
|
|
|
|
|
// exactly the same as that master model, and we're going to pair the tables.
|
|
|
|
|
// Only some of these tables should pair: inventory, skills, and skills.leet
|
|
|
|
|
ss.reset(new World(util::WORLD_TYPE_S_SYNC));
|
|
|
|
|
ss->tangible_make(0, 123, false);
|
|
|
|
|
ss->tangible_set_string(123, "inventory.TID", "inventory");
|
|
|
|
|
ss->tangible_set_string(123, "skills.TID", "skills");
|
|
|
|
|
ss->tangible_set_string(123, "skills.crap.TID", "skills.crap");
|
|
|
|
|
ss->tangible_set_string(123, "skills.leet.TID", "skills.leet");
|
|
|
|
|
ss->tangible_set_string(123, "math.TID", "math");
|
|
|
|
|
ss->tangible_set_string(123, "gltab.TID", "gltab");
|
2021-08-23 23:34:30 -04:00
|
|
|
ss->number_lua_tables(util::id_vector_create(123));
|
2021-08-13 17:02:35 -04:00
|
|
|
LuaAssertStrEq(L, ss->numbered_tables_debug_string(),
|
|
|
|
|
"gltab;inventory;math;skills;skills.crap;skills.leet;");
|
2021-08-23 23:34:30 -04:00
|
|
|
ss->pair_lua_tables(util::id_vector_create(123), m->state());
|
|
|
|
|
LuaAssertStrEq(L, ss->paired_tables_debug_string(m->state()),
|
2021-08-13 17:02:35 -04:00
|
|
|
"inventory=inventory;skills=skills;skills.leet=skills.leet;");
|
|
|
|
|
|
|
|
|
|
// Test the creation of new tables during difference transmission.
|
|
|
|
|
// The master world model above has two tables that couldn't be paired
|
|
|
|
|
// to the client: inventory.cplx, and transactions. These two tables
|
|
|
|
|
// should be paired to new, created tables.
|
2021-08-23 23:34:30 -04:00
|
|
|
ncreate = m->pair_new_tables(util::id_vector_create(123), m->state());
|
2021-08-13 17:02:35 -04:00
|
|
|
LuaAssert(L, ncreate == 2);
|
|
|
|
|
ss->create_new_tables(ncreate);
|
2021-08-23 23:34:30 -04:00
|
|
|
LuaAssertStrEq(L, ss->paired_tables_debug_string(m->state()),
|
2021-08-13 17:02:35 -04:00
|
|
|
"inventory=inventory;inventory.cplx=unknown;skills=skills;skills.leet=skills.leet;transactions=unknown;");
|
|
|
|
|
|
|
|
|
|
// Create new clean world models.
|
2021-08-03 11:25:12 -04:00
|
|
|
m.reset(new World(util::WORLD_TYPE_MASTER));
|
|
|
|
|
ss.reset(new World(util::WORLD_TYPE_S_SYNC));
|
|
|
|
|
cs.reset(new World(util::WORLD_TYPE_C_SYNC));
|
|
|
|
|
|
2021-08-03 12:40:12 -04:00
|
|
|
// Create some tangibles, and add some animations.
|
2021-08-03 11:25:12 -04:00
|
|
|
m->tangible_make(0, 123, false);
|
|
|
|
|
m->tangible_make(0, 345, false);
|
2021-08-03 12:40:12 -04:00
|
|
|
m->tangible_walkto(123, 770, 3, 4);
|
|
|
|
|
m->tangible_walkto(345, 771, 6, 2);
|
2021-08-03 11:25:12 -04:00
|
|
|
LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123,345");
|
2021-08-03 12:40:12 -04:00
|
|
|
LuaAssertStrEq(L, m->tangible_anim_debug_string(123),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=770 action=walkto x=3 y=4; ");
|
|
|
|
|
LuaAssertStrEq(L, m->tangible_anim_debug_string(345),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=771 action=walkto x=6 y=2; ");
|
|
|
|
|
|
|
|
|
|
// Now difference transmit all that to the client.
|
2021-08-03 11:25:12 -04:00
|
|
|
ss->difference_transmit(123, m.get(), &sb);
|
|
|
|
|
cs->apply_differences(&sb);
|
|
|
|
|
LuaAssertStrEq(L, ss->tangible_ids_debug_string(), "123,345");
|
2021-08-03 12:40:12 -04:00
|
|
|
LuaAssertStrEq(L, ss->tangible_anim_debug_string(123),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=770 action=walkto x=3 y=4; ");
|
|
|
|
|
LuaAssertStrEq(L, ss->tangible_anim_debug_string(345),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=771 action=walkto x=6 y=2; ");
|
2021-08-03 11:25:12 -04:00
|
|
|
LuaAssert(L, worlds_identical(ss, cs));
|
|
|
|
|
|
2021-08-03 12:40:12 -04:00
|
|
|
// Now add some more animation records to the master.
|
|
|
|
|
m->tangible_walkto(123, 772, 7, 3);
|
|
|
|
|
m->tangible_walkto(345, 773, 2, 5);
|
|
|
|
|
LuaAssertStrEq(L, m->tangible_anim_debug_string(123),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=770 action=walkto x=3 y=4; "
|
|
|
|
|
"id=772 action=walkto x=7 y=3; ");
|
|
|
|
|
LuaAssertStrEq(L, m->tangible_anim_debug_string(345),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=771 action=walkto x=6 y=2; "
|
|
|
|
|
"id=773 action=walkto x=2 y=5; ");
|
|
|
|
|
|
|
|
|
|
// Now difference transmit all that to the client again.
|
|
|
|
|
ss->difference_transmit(123, m.get(), &sb);
|
|
|
|
|
cs->apply_differences(&sb);
|
|
|
|
|
LuaAssertStrEq(L, ss->tangible_anim_debug_string(123),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=770 action=walkto x=3 y=4; "
|
|
|
|
|
"id=772 action=walkto x=7 y=3; ");
|
|
|
|
|
LuaAssertStrEq(L, ss->tangible_anim_debug_string(345),
|
|
|
|
|
"id=0 action= plane= x=0 y=0 z=0 facing=0 graphic=; "
|
|
|
|
|
"id=771 action=walkto x=6 y=2; "
|
|
|
|
|
"id=773 action=walkto x=2 y=5; ");
|
|
|
|
|
LuaAssert(L, worlds_identical(ss, cs));
|
|
|
|
|
|
|
|
|
|
// Delete tangible 345.
|
|
|
|
|
m->tangible_delete(345);
|
|
|
|
|
LuaAssertStrEq(L, m->tangible_ids_debug_string(), "123");
|
|
|
|
|
|
|
|
|
|
// And difference transmit
|
|
|
|
|
ss->difference_transmit(123, m.get(), &sb);
|
|
|
|
|
cs->apply_differences(&sb);
|
|
|
|
|
LuaAssertStrEq(L, ss->tangible_ids_debug_string(), "123");
|
|
|
|
|
LuaAssert(L, worlds_identical(ss, cs));
|
|
|
|
|
|
2021-08-03 11:25:12 -04:00
|
|
|
return 0;
|
|
|
|
|
}
|