More refactoring and cleanup

This commit is contained in:
2021-11-16 13:14:59 -05:00
parent 077b2fc23b
commit 51a95cc964
8 changed files with 49 additions and 36 deletions

View File

@@ -347,6 +347,7 @@ public:
handle_new_outgoing_sockets();
handle_console_output();
handle_console_input();
handle_console_output();
int mstimeout = short_sleep_ ? 0 : 100;
handle_socket_input_output(mstimeout);
handle_clock();

View File

@@ -36,12 +36,22 @@ bool Gui::has_action(const std::string &action) const {
}
std::string Gui::get_action(int index) {
if ((index < 0) || (index >= elts_.size())) {
if ((index < 0) || (index >= int(elts_.size()))) {
return "";
}
return elts_[index].action();
}
std::string Gui::menu_debug_string() const {
std::ostringstream oss;
int index = 0;
for (const GuiElt &elt : elts()) {
oss << index << " " << elt.label() << std::endl;
index += 1;
}
return oss.str();
}
LuaDefine(gui_menu_item, "c") {
Gui *gui = Gui::fetch_global_pointer(L);
LuaArg laction, llabel;

View File

@@ -30,13 +30,17 @@ class Gui {
public:
using EltVec = std::vector<GuiElt>;
private:
int64_t place_;
EltVec elts_;
public:
Gui() { place_ = 0; }
int64_t place() { return place_; }
const EltVec &elts() const { return elts_; }
void clear() { elts_.clear(); }
void clear(int64_t p) { place_ = p; elts_.clear(); }
bool has_action(const std::string &action) const;
void menu_item(const std::string &action, const std::string &label);
std::string get_action(int index);
std::string menu_debug_string() const;
// Put a pointer to a gui into the lua registry.
//

View File

@@ -18,7 +18,6 @@ public:
LuaConsole console_;
PrintChanneler print_channeler_;
Gui gui_;
int64_t gui_place_;
public:
void set_initial_state() {
@@ -104,22 +103,14 @@ public:
}
void do_view_command(const StringVec &cmd) {
for (int64_t id : world_->get_near(actor_id_, 100, true)) {
const Tangible *tan = world_->tangible_get(id);
const AnimStep &aqback = tan->anim_queue_.back();
stdostream() << id << ": " << aqback.graphic() << " " << aqback.plane() << " " << aqback.xyz().debug_string() << std::endl;
}
stdostream() << world_->tangibles_near_debug_string(actor_id_, 100);
}
void do_menu_command(const StringVec &cmd) {
world_to_asynchronous();
gui_place_ = util::strtoint(cmd[1], actor_id_);
world_->update_gui(actor_id_, gui_place_, &gui_);
int index = 0;
for (const GuiElt &elt : gui_.elts()) {
stdostream() << index << " " << elt.label() << std::endl;
index += 1;
}
int64_t place = util::strtoint(cmd[1], actor_id_);
world_->update_gui(actor_id_, place, &gui_);
stdostream() << gui_.menu_debug_string();
}
void do_choose_command(const StringVec &cmd) {
@@ -129,7 +120,7 @@ public:
return;
}
stdostream() << "Invoking plan: " << action << std::endl;
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_place_, action);
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_.place(), action);
send_invocation(inv);
}

View File

@@ -26,7 +26,6 @@ private:
LuaConsole console_;
PrintChanneler print_channeler_;
Gui gui_;
int64_t gui_place_;
int64_t actor_id_;
void do_lua_command(const StringVec &words) {
@@ -38,21 +37,13 @@ private:
}
void do_view_command(const StringVec &cmd) {
for (int64_t id : world_->get_near(actor_id_, 100, true)) {
const Tangible *tan = world_->tangible_get(id);
const AnimStep &aqback = tan->anim_queue_.back();
stdostream() << id << ": " << aqback.graphic() << " " << aqback.plane() << " " << aqback.xyz().debug_string() << std::endl;
}
stdostream() << world_->tangibles_near_debug_string(actor_id_, 100);
}
void do_menu_command(const StringVec &cmd) {
gui_place_ = util::strtoint(cmd[1], actor_id_);
world_->update_gui(actor_id_, gui_place_, &gui_);
int index = 0;
for (const GuiElt &elt : gui_.elts()) {
stdostream() << index << " " << elt.label() << std::endl;
index += 1;
}
int64_t place = util::strtoint(cmd[1], actor_id_);
world_->update_gui(actor_id_, place, &gui_);
stdostream() << gui_.menu_debug_string();
}
void do_choose_command(const StringVec &cmd) {
@@ -61,7 +52,7 @@ private:
stdostream() << "Invalid menu item #" << std::endl;
return;
}
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_place_, action);
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_.place(), action);
stdostream() << "Invoking: " << inv.debug_string() << std::endl;
world_->invoke(inv);
}
@@ -104,7 +95,7 @@ private:
if (p.first == actor_id_) {
actor_id_ = p.second;
stdostream() << "Login actor ID: " << actor_id_ << std::endl;
gui_.clear();
gui_.clear(0);
}
}
}

View File

@@ -237,7 +237,9 @@ util::IdVector World::get_near_unsorted(int64_t player_id, float radius, bool ex
// Find out where's the center of the world.
const AnimStep &aqback = player->anim_queue_.back();
if (exclude_nowhere && (aqback.plane() == "nowhere")) {
return IdVector();
IdVector idv;
idv.push_back(player_id);
return idv;
}
return plane_map_.scan_radius(aqback.plane(), aqback.xyz().x, aqback.xyz().y, radius, player_id);
@@ -274,7 +276,7 @@ int64_t World::create_login_actor() {
void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
assert(stack_is_clear());
gui->clear();
gui->clear(place_id);
lua_State *L = state();
LuaVar actor, place, ugui, func, tangibles, mt, index;
@@ -316,7 +318,7 @@ void World::update_gui(int64_t actor_id, int64_t place_id, Gui *gui) {
close_lthread_state();
Gui::store_global_pointer(L, nullptr);
if (status != 0) {
gui->clear();
gui->clear(0);
std::cerr << lua_tostring(L, -1);
LS.result();
return;

View File

@@ -30,6 +30,16 @@ std::string World::tangible_ids_debug_string() const {
return util::id_vector_debug_string(idv);
}
std::string World::tangibles_near_debug_string(int64_t actor, int64_t distance) {
std::ostringstream result;
for (int64_t id : get_near(actor, distance, true)) {
const Tangible *tan = tangible_get(id);
const AnimStep &aqback = tan->anim_queue_.back();
result << id << ": " << aqback.graphic() << " " << aqback.plane() << " " << aqback.xyz().debug_string() << std::endl;
}
return result.str();
}
std::string World::tangible_pprint(int64_t id) const {
lua_State *L = state();
LuaVar tangibles, tan, meta;

View File

@@ -115,8 +115,8 @@ public:
// 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. The unsorted version returns
// the tangibles in an unpredictable order.
// true, exclude any tangibles on the nowhere plane (but still include the player himself).
// The unsorted version returns the tangibles in an unpredictable order.
//
IdVector get_near(int64_t player_id, float radius, bool exclude_nowhere) const;
IdVector get_near_unsorted(int64_t player_id, float radius, bool exclude_nowhere) const;
@@ -290,6 +290,10 @@ public:
//
std::string tangible_ids_debug_string() const;
// Get a list of all tangibles near the target as a string.
//
std::string tangibles_near_debug_string(int64_t actor, int64_t distance);
// Shows the TID (table ID) of the tables that were numbered.
// TIDs are in alphabetical order. Any table without a TID
// shows up as "unknown"