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

240 lines
7.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"
#include <memory>
#include <algorithm>
class TextGame : public DrivenEngine {
private:
using StringVec = LuaConsole::StringVec;
UniqueWorld world_;
LuaConsole console_;
Gui gui_;
int64_t gui_place_;
int64_t actor_id_;
int64_t printbuffer_line_;
void do_lua_command(const StringVec &cmd);
void do_syntax_command(const StringVec &cmd);
void do_view_command(const StringVec &cmd);
void do_menu_command(const StringVec &cmd);
void do_choose_command(const StringVec &cmd);
void do_quit_command(const StringVec &cmd);
void do_snapshot_command(const StringVec &cmd);
void do_rollback_command(const StringVec &cmd);
void do_tick_command(const StringVec &cmd);
void do_command(const StringVec &exp);
void check_redirects();
void channel_printbuffer();
public:
virtual void event_init(int argc, char *argv[]);
virtual void event_update();
};
void TextGame::do_lua_command(const StringVec &words) {
assert(world_->stack_is_clear());
if (words.size() != 2) {
stdostream() << "lua command (lua) takes a single string" << std::endl;
return;
}
const std::string &exp = words[1];
InvocationData dummyresult;
Invocation inv(Invocation::KIND_LUA, actor_id_, actor_id_, exp, dummyresult);
world_->invoke(inv);
}
2021-01-23 16:10:29 -05:00
void TextGame::do_syntax_command(const StringVec &words) {
stdostream() << "Syntax Error: ";
for (int i = 1; i < int(words.size()); i++) {
stdostream() << words[i] << " ";
}
stdostream() << std::endl;
}
2021-01-23 16:10:29 -05:00
2021-02-02 16:29:07 -05:00
void TextGame::do_view_command(const StringVec &cmd) {
if (cmd.size() != 1) {
stdostream() << "v command (view) takes no arguments" << std::endl;
2021-02-02 16:29:07 -05:00
return;
}
2021-03-30 18:35:08 -04:00
for (int64_t id : world_->get_near(actor_id_, 100, true)) {
2021-02-16 13:31:34 -05:00
const Tangible *tan = world_->tangible_get(id);
2021-03-28 13:27:28 -04:00
const AnimStep &aqback = tan->anim_queue_.back();
stdostream() << id << ": " << aqback.graphic() << " " << aqback.plane() << " " << aqback.xyz().debug_string() << std::endl;
2021-02-02 16:29:07 -05:00
}
}
void TextGame::do_menu_command(const StringVec &cmd) {
int64_t id;
if (cmd.size() == 1) {
2021-03-30 18:35:08 -04:00
id = actor_id_;
2021-02-02 16:29:07 -05:00
} else if (cmd.size() == 2) {
id = util::strtoint(cmd[1], -1);
} else {
stdostream() << "m command (menu) expects a tangible ID or defaults to actor_id" << std::endl;
2021-02-02 16:29:07 -05:00
return;
}
2021-02-16 13:31:34 -05:00
gui_place_ = id;
2021-03-30 18:35:08 -04:00
world_->update_gui(actor_id_, id, &gui_);
2021-02-07 13:38:29 -05:00
int index = 0;
for (const GuiElt &elt : gui_.elts()) {
stdostream() << index << " " << elt.label() << std::endl;
2021-02-07 13:38:29 -05:00
index += 1;
}
2021-02-02 16:29:07 -05:00
}
void TextGame::do_choose_command(const StringVec &cmd) {
int64_t index;
2021-02-10 16:22:24 -05:00
if (cmd.size() == 1) {
index = util::strtoint(cmd[0], -1);
2021-02-02 16:29:07 -05:00
} else {
stdostream() << "c command (choose) expects a menu line number" << std::endl;
2021-02-02 16:29:07 -05:00
return;
}
2021-02-10 16:22:24 -05:00
const Gui::EltVec &elts = gui_.elts();
if ((index < 0) || (index >= int(elts.size()))) {
stdostream() << "No menu item #" << index << std::endl;
2021-02-10 16:22:24 -05:00
return;
}
2021-02-16 13:31:34 -05:00
std::string action = elts[index].action();
stdostream() << "Invoking plan: " << action << std::endl;
InvocationData dummyresult;
2021-02-25 14:09:16 -05:00
dummyresult["flavor"] = "chocolate";
dummyresult["color"] = "blue";
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_place_, action, dummyresult);
world_->invoke(inv);
2021-02-02 16:29:07 -05:00
}
2021-10-15 14:47:12 -04:00
2021-02-09 17:15:54 -05:00
void TextGame::do_snapshot_command(const StringVec &cmd) {
if (cmd.size() != 1) {
stdostream() << "s command (snapshot) takes no arguments" << std::endl;
2021-02-09 17:15:54 -05:00
return;
}
2021-02-16 13:31:34 -05:00
world_->snapshot();
2021-02-09 17:15:54 -05:00
}
void TextGame::do_rollback_command(const StringVec &cmd) {
if (cmd.size() != 1) {
stdostream() << "r command (rollback) takes no arguments" << std::endl;
2021-02-09 17:15:54 -05:00
return;
}
2021-02-16 13:31:34 -05:00
world_->rollback();
2021-02-09 17:15:54 -05:00
}
2021-10-13 19:41:59 -04:00
void TextGame::do_tick_command(const StringVec &cmd) {
int64_t clock;
if (cmd.size() == 2) {
clock = util::strtoint(cmd[1], -1);
} else {
stdostream() << "t command (tick) expects a time value" << std::endl;
2021-10-13 19:41:59 -04:00
return;
}
world_->run_scheduled_threads(clock);
}
2021-02-07 13:38:29 -05:00
void TextGame::do_quit_command(const StringVec &cmd) {
if (cmd.size() != 1) {
stdostream() << "q command (quit) takes no arguments" << std::endl;
2021-02-07 13:38:29 -05:00
return;
}
actor_id_ = 0;
2021-02-07 13:38:29 -05:00
}
2021-02-02 16:29:07 -05:00
void TextGame::do_command(const StringVec &words) {
if (words[0] == "lua") do_lua_command(words);
else if (words[0] == "syntax") do_syntax_command(words);
else if (words[0] == "v") do_view_command(words);
2021-02-10 16:22:24 -05:00
else if (words[0] == "m") do_menu_command(words);
else if (words[0] == "q") do_quit_command(words);
else if (words[0] == "s") do_snapshot_command(words);
else if (words[0] == "r") do_rollback_command(words);
2021-10-13 19:41:59 -04:00
else if (words[0] == "t") do_tick_command(words);
2021-02-10 16:22:24 -05:00
else if (util::validinteger(words[0])) do_choose_command(words);
else {
stdostream() << "Unknown command: " << words[0] << std::endl;
2021-02-02 16:29:07 -05:00
}
}
2021-03-30 18:35:08 -04:00
void TextGame::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-03-30 18:35:08 -04:00
gui_.clear();
}
}
}
void TextGame::channel_printbuffer() {
const PrintBuffer *printbuffer = world_->get_printbuffer(actor_id_);
if (printbuffer == nullptr) return;
if (printbuffer->first_line() > printbuffer_line_) {
printbuffer_line_ = printbuffer->first_line();
}
while (printbuffer_line_ < printbuffer->first_unchecked()) {
stdostream() << printbuffer->nth(printbuffer_line_) << std::endl;
printbuffer_line_ += 1;
}
if (printbuffer_line_ > printbuffer->first_line()) {
InvocationData data;
Invocation inv(Invocation::KIND_FLUSH_PRINTS, actor_id_, actor_id_, std::to_string(printbuffer_line_), data);
world_->invoke(inv);
}
}
void TextGame::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();
printbuffer_line_ = 0;
stdostream() << "Login actor ID: " << actor_id_ << std::endl;
get_stdio_channel()->set_prompt(console_.get_prompt());
}
2021-10-05 12:54:37 -04:00
void TextGame::event_update()
2021-01-23 16:10:29 -05:00
{
2021-10-05 12:54:37 -04:00
world_->update_source(get_lua_source());
2021-01-23 16:10:29 -05:00
while (true) {
std::string line = get_stdio_channel()->in()->readline();
if (line == "") break;
console_.add(line);
const StringVec &words = console_.words();
if (!words.empty()) {
do_command(words);
2021-10-07 14:58:20 -04:00
console_.clear();
channel_printbuffer();
2021-01-23 16:10:29 -05:00
}
2021-10-05 12:54:37 -04:00
check_redirects();
if (actor_id_ == 0) {
stop_driver();
return;
}
get_stdio_channel()->set_prompt(console_.get_prompt());
2021-01-23 16:10:29 -05:00
}
}
UniqueDrivenEngine make_TextGame() {
return UniqueDrivenEngine(new TextGame);
}