Files
integration/luprex/core/cpp/textgame.cpp

137 lines
4.2 KiB
C++
Raw Normal View History

2021-01-23 16:10:29 -05:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <vector>
#include <string>
#include "luastack.hpp"
#include "util.hpp"
2021-02-07 13:38:29 -05:00
#include "gui.hpp"
#include "invocation.hpp"
2021-02-16 13:31:34 -05:00
#include "world.hpp"
2021-01-23 16:10:29 -05:00
#include "traceback.hpp"
#include "textgame.hpp"
#include "luaconsole.hpp"
#include "pprint.hpp"
2021-11-11 13:56:49 -05:00
#include "printbuffer.hpp"
#include <memory>
#include <algorithm>
class TextGame : public DrivenEngine {
private:
using StringVec = LuaConsole::StringVec;
UniqueWorld world_;
LuaConsole console_;
2021-11-11 13:56:49 -05:00
PrintChanneler print_channeler_;
Gui gui_;
int64_t actor_id_;
2021-11-11 13:56:49 -05:00
void do_lua_command(const StringVec &words) {
2021-11-16 12:20:11 -05:00
world_->invoke(Invocation(Invocation::KIND_LUA, actor_id_, actor_id_, words[1]));
}
2021-01-23 16:10:29 -05:00
2021-11-11 13:56:49 -05:00
void do_syntax_command(const StringVec &words) {
2021-11-16 12:20:11 -05:00
stdostream() << "Syntax Error: " << words[1] << std::endl;
}
2021-01-23 16:10:29 -05:00
2021-11-11 13:56:49 -05:00
void do_view_command(const StringVec &cmd) {
2021-11-16 13:14:59 -05:00
stdostream() << world_->tangibles_near_debug_string(actor_id_, 100);
2021-02-02 16:29:07 -05:00
}
2021-11-11 13:56:49 -05:00
void do_menu_command(const StringVec &cmd) {
2021-11-16 13:14:59 -05:00
int64_t place = util::strtoint(cmd[1], actor_id_);
world_->update_gui(actor_id_, place, &gui_);
stdostream() << gui_.menu_debug_string();
2021-02-07 13:38:29 -05:00
}
2021-02-02 16:29:07 -05:00
2021-11-11 13:56:49 -05:00
void do_choose_command(const StringVec &cmd) {
2021-11-16 12:20:11 -05:00
std::string action = gui_.get_action(util::strtoint(cmd[1], -1));
if (action == "") {
stdostream() << "Invalid menu item #" << std::endl;
2021-11-11 13:56:49 -05:00
return;
}
2021-11-16 13:14:59 -05:00
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_.place(), action);
2021-11-16 12:20:11 -05:00
stdostream() << "Invoking: " << inv.debug_string() << std::endl;
2021-11-11 13:56:49 -05:00
world_->invoke(inv);
2021-02-10 16:22:24 -05:00
}
2021-02-02 16:29:07 -05:00
2021-11-11 13:56:49 -05:00
void do_snapshot_command(const StringVec &cmd) {
world_->snapshot();
2021-02-09 17:15:54 -05:00
}
2021-11-11 13:56:49 -05:00
void do_rollback_command(const StringVec &cmd) {
world_->rollback();
2021-10-13 19:41:59 -04:00
}
2021-11-11 13:56:49 -05:00
void do_tick_command(const StringVec &cmd) {
2021-11-16 12:20:11 -05:00
world_->run_scheduled_threads(util::strtoint(cmd[1], -1));
2021-02-07 13:38:29 -05:00
}
2021-11-11 13:56:49 -05:00
void do_quit_command(const StringVec &cmd) {
actor_id_ = 0;
}
void do_command(const StringVec &words) {
if (words.empty()) return;
else if (words[0] == "lua") do_lua_command(words);
else if (words[0] == "syntax") do_syntax_command(words);
else if (words[0] == "view") do_view_command(words);
else if (words[0] == "menu") do_menu_command(words);
else if (words[0] == "quit") do_quit_command(words);
else if (words[0] == "snap") do_snapshot_command(words);
else if (words[0] == "roll") do_rollback_command(words);
else if (words[0] == "tick") do_tick_command(words);
2021-11-16 12:20:11 -05:00
else if (words[0] == "choose") do_choose_command(words);
2021-11-11 13:56:49 -05:00
else {
2021-11-16 12:20:11 -05:00
stdostream() << "Unsupported command: " << words[0] << std::endl;
2021-11-11 13:56:49 -05:00
}
2021-02-02 16:29:07 -05:00
}
2021-11-11 13:56:49 -05:00
void check_redirects() {
World::Redirects redir = world_->fetch_redirects();
for (const auto &p : redir) {
if (p.first == actor_id_) {
actor_id_ = p.second;
stdostream() << "Login actor ID: " << actor_id_ << std::endl;
2021-11-16 13:14:59 -05:00
gui_.clear(0);
2021-11-11 13:56:49 -05:00
}
2021-03-30 18:35:08 -04:00
}
}
2021-11-11 13:56:49 -05:00
void event_init(int argc, char *argv[])
{
world_.reset(new World(util::WORLD_TYPE_STANDALONE));
world_->update_source(get_lua_source());
world_->run_unittests();
actor_id_ = world_->create_login_actor();
stdostream() << "Login actor ID: " << actor_id_ << std::endl;
get_stdio_channel()->set_prompt(console_.get_prompt());
}
2021-11-11 13:56:49 -05:00
void event_update()
{
world_->update_source(get_lua_source());
while (true) {
std::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
get_stdio_channel()->set_prompt(console_.get_prompt());
do_command(console_.get_command());
if (print_channeler_.channel(world_->get_printbuffer(actor_id_), stdostream())) {
world_->invoke(print_channeler_.invocation(actor_id_));
}
check_redirects();
if (actor_id_ == 0) {
stop_driver();
break;
}
2021-10-05 12:54:37 -04:00
}
2021-01-23 16:10:29 -05:00
}
2021-11-11 13:56:49 -05:00
};
2021-01-23 16:10:29 -05:00
UniqueDrivenEngine make_TextGame() {
return UniqueDrivenEngine(new TextGame);
}