Implement the core of the login system

This commit is contained in:
2021-03-30 18:35:08 -04:00
parent 795a45c950
commit aa7de03c57
4 changed files with 113 additions and 32 deletions

View File

@@ -72,29 +72,9 @@ void Tangible::deserialize(StreamBuffer *sb) {
update_plane_item();
}
void Tangible::be_a_player() {
if (!id_player_pool_.fifo_enabled()) {
id_player_pool_.enable_fifo();
AnimStep asinit;
asinit.set_graphic("player");
anim_queue_.add(world_->id_global_pool_.get_one(), asinit);
anim_queue_.keep_only(1);
update_plane_item();
LuaVar classtab, mt, place, tangibles;
LuaStack LS(world_->state(), classtab, mt, place, tangibles);
LS.makeclass(classtab, "player");
LS.rawget(tangibles, LuaRegistry, "tangibles");
LS.rawget(place, tangibles, id());
LS.getmetatable(mt, place);
LS.rawset(mt, "__index", classtab);
LS.result();
}
}
void World::init_standalone() {
assert(stack_is_clear());
// Load the lua source from disk then rebuild the environment.
source_db_.update();
source_db_.rebuild();
@@ -102,9 +82,7 @@ void World::init_standalone() {
// Run unit tests.
source_db_.run_unittests();
// Create the player tangible.
Tangible *player = tangible_make(state(), 1, false);
player->be_a_player();
assert(stack_is_clear());
}
Tangible *World::tangible_get(int64_t id) {
@@ -207,7 +185,50 @@ Tangible *World::tangible_make(lua_State *L, int64_t id, bool pushdb) {
return t.get();
}
World::Redirects World::fetch_redirects() {
World::Redirects result = std::move(redirects_);
redirects_.clear();
return std::move(result);
}
// void Tangible::be_a_player() {
// if (!id_player_pool_.fifo_enabled()) {
// id_player_pool_.enable_fifo();
// AnimStep asinit;
// asinit.set_graphic("player");
// anim_queue_.add(world_->id_global_pool_.get_one(), asinit);
// anim_queue_.keep_only(1);
// update_plane_item();
// LuaVar classtab, mt, place, tangibles;
// LuaStack LS(world_->state(), classtab, mt, place, tangibles);
// LS.makeclass(classtab, "player");
// LS.rawget(tangibles, LuaRegistry, "tangibles");
// LS.rawget(place, tangibles, id());
// LS.getmetatable(mt, place);
// LS.rawset(mt, "__index", classtab);
// LS.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();
tan->id_player_pool_.enable_fifo();
assert(stack_is_clear());
return tan->id();
}
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
assert(stack_is_clear());
gui->clear();
lua_State *L = state();
@@ -256,9 +277,12 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
// And we're done.
LS.result();
assert(stack_is_clear());
}
void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &action, const GuiResult &gres) {
assert(stack_is_clear());
// Validate that the action is legal.
Gui validation_gui;
update_gui(actor_id, place_id, &validation_gui);
@@ -334,9 +358,11 @@ void World::invoke_plan(int64_t actor_id, int64_t place_id, const std::string &a
// then run the thread queue.
thread_sched_.add(0, tid, place_id);
run_scheduled_threads(0);
assert(stack_is_clear());
}
void World::run_scheduled_threads(int64_t clk) {
assert(stack_is_clear());
lua_State *L = state();
LuaVar tangibles, place, mt, threads, thread;
LuaStack LS(L, tangibles, place, mt, threads, thread);
@@ -391,9 +417,12 @@ void World::run_scheduled_threads(int64_t clk) {
}
}
LS.result();
assert(stack_is_clear());
}
void World::serialize(StreamBuffer *sb) {
assert(stack_is_clear());
assert(redirects_.empty());
int64_t wc0 = sb->write_count();
lua_snap_.serialize(sb);
id_global_pool_.serialize(sb);
@@ -405,9 +434,12 @@ void World::serialize(StreamBuffer *sb) {
}
int64_t wc1 = sb->write_count();
std::cerr << "World serialized to " << wc1-wc0 << " bytes." << std::endl;
assert(stack_is_clear());
}
void World::deserialize(StreamBuffer *sb) {
assert(stack_is_clear());
redirects_.clear();
lua_snap_.deserialize(sb);
id_global_pool_.deserialize(sb);
thread_sched_.deserialize(sb);
@@ -435,6 +467,7 @@ void World::deserialize(StreamBuffer *sb) {
++iter;
}
}
assert(stack_is_clear());
}
void World::snapshot() {
@@ -561,6 +594,16 @@ LuaDefine(tangible_get, "c") {
return LS.result();
}
LuaDefine(tangible_redirect, "c") {
LuaArg actor1, actor2;
LuaStack LS(L, actor1, actor2);
World *w = World::fetch_global_pointer(L);
Tangible *tan1 = w->tangible_get(L, actor1.index());
Tangible *tan2 = w->tangible_get(L, actor2.index());
w->redirects_[tan1->id()] = tan2->id();
return LS.result();
}
LuaDefine(world_wait, "f") {
if ((lua_gettop(L) != 1) || (lua_type(L, -1) != LUA_TNUMBER)) {
luaL_error(L, "Argument to wait must be a number.");