229 lines
6.7 KiB
C++
229 lines
6.7 KiB
C++
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <signal.h>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <iostream>
|
|
#include "luastack.hpp"
|
|
#include "util.hpp"
|
|
#include "gui.hpp"
|
|
#include "invocation.hpp"
|
|
#include "world.hpp"
|
|
#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_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_lua(const std::string &exp);
|
|
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_view_command(const StringVec &cmd) {
|
|
if (cmd.size() != 1) {
|
|
std::cerr << "v command (view) takes no arguments" << std::endl;
|
|
return;
|
|
}
|
|
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();
|
|
std::cerr << id << ": " << aqback.graphic() << " " << aqback.plane() << " " << aqback.xyz().debug_string() << std::endl;
|
|
}
|
|
}
|
|
|
|
void TextGame::do_menu_command(const StringVec &cmd) {
|
|
int64_t id;
|
|
if (cmd.size() == 1) {
|
|
id = actor_id_;
|
|
} else if (cmd.size() == 2) {
|
|
id = util::strtoint(cmd[1], -1);
|
|
} else {
|
|
std::cerr << "m command (menu) expects a tangible ID or defaults to actor_id" << std::endl;
|
|
return;
|
|
}
|
|
gui_place_ = id;
|
|
world_->update_gui(actor_id_, id, &gui_);
|
|
int index = 0;
|
|
for (const GuiElt &elt : gui_.elts()) {
|
|
std::cerr << index << " " << elt.label() << std::endl;
|
|
index += 1;
|
|
}
|
|
}
|
|
|
|
void TextGame::do_choose_command(const StringVec &cmd) {
|
|
int64_t index;
|
|
if (cmd.size() == 1) {
|
|
index = util::strtoint(cmd[0], -1);
|
|
} else {
|
|
std::cerr << "c command (choose) expects a menu line number" << std::endl;
|
|
return;
|
|
}
|
|
const Gui::EltVec &elts = gui_.elts();
|
|
if ((index < 0) || (index >= int(elts.size()))) {
|
|
std::cerr << "No menu item #" << index << std::endl;
|
|
return;
|
|
}
|
|
std::string action = elts[index].action();
|
|
std::cerr << "Invoking plan: " << action << std::endl;
|
|
InvocationData dummyresult;
|
|
dummyresult["flavor"] = "chocolate";
|
|
dummyresult["color"] = "blue";
|
|
Invocation inv(Invocation::KIND_PLAN, actor_id_, gui_place_, action, dummyresult);
|
|
world_->invoke(inv);
|
|
}
|
|
|
|
void TextGame::do_lua(const std::string &exp) {
|
|
assert(world_->stack_is_clear());
|
|
InvocationData dummyresult;
|
|
Invocation inv(Invocation::KIND_LUA, actor_id_, actor_id_, exp, dummyresult);
|
|
world_->invoke(inv);
|
|
}
|
|
|
|
void TextGame::do_snapshot_command(const StringVec &cmd) {
|
|
if (cmd.size() != 1) {
|
|
std::cerr << "s command (snapshot) takes no arguments" << std::endl;
|
|
return;
|
|
}
|
|
world_->snapshot();
|
|
}
|
|
|
|
void TextGame::do_rollback_command(const StringVec &cmd) {
|
|
if (cmd.size() != 1) {
|
|
std::cerr << "r command (rollback) takes no arguments" << std::endl;
|
|
return;
|
|
}
|
|
world_->rollback();
|
|
}
|
|
|
|
void TextGame::do_tick_command(const StringVec &cmd) {
|
|
int64_t clock;
|
|
if (cmd.size() == 2) {
|
|
clock = util::strtoint(cmd[1], -1);
|
|
} else {
|
|
std::cerr << "t command (tick) expects a time value" << std::endl;
|
|
return;
|
|
}
|
|
world_->run_scheduled_threads(clock);
|
|
}
|
|
|
|
void TextGame::do_quit_command(const StringVec &cmd) {
|
|
if (cmd.size() != 1) {
|
|
std::cerr << "q command (quit) takes no arguments" << std::endl;
|
|
return;
|
|
}
|
|
actor_id_ = 0;
|
|
stop_driver();
|
|
}
|
|
|
|
void TextGame::do_command(const StringVec &words) {
|
|
if (words[0] == "v") do_view_command(words);
|
|
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);
|
|
else if (words[0] == "t") do_tick_command(words);
|
|
else if (util::validinteger(words[0])) do_choose_command(words);
|
|
else {
|
|
std::cerr << "Unknown command: " << words[0] << std::endl;
|
|
}
|
|
}
|
|
|
|
void TextGame::check_redirects() {
|
|
World::Redirects redir = world_->fetch_redirects();
|
|
for (const auto &p : redir) {
|
|
if (p.first == actor_id_) {
|
|
actor_id_ = p.second;
|
|
std::cerr << "Login actor ID: " << actor_id_ << std::endl;
|
|
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()) {
|
|
std::cerr << "* " << printbuffer->nth(printbuffer_line_) << std::endl;
|
|
printbuffer_line_ += 1;
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
std::cerr << "Login actor ID: " << actor_id_ << std::endl;
|
|
get_stdio_channel()->out()->write_bytes(console_.get_prompt());
|
|
}
|
|
|
|
void TextGame::event_update()
|
|
{
|
|
world_->update_source(get_lua_source());
|
|
while (true) {
|
|
std::string line = get_stdio_channel()->in()->readline();
|
|
if (line == "") break;
|
|
console_.add(line);
|
|
int action = console_.action();
|
|
if (action == LuaConsole::DO_LUA) {
|
|
do_lua(console_.lua_expression());
|
|
console_.clear();
|
|
channel_printbuffer();
|
|
} else if (action == LuaConsole::DO_COMMAND) {
|
|
do_command(console_.words());
|
|
console_.clear();
|
|
channel_printbuffer();
|
|
} else if (action == LuaConsole::DO_SYNTAX) {
|
|
std::cerr << console_.syntax() << std::endl;
|
|
console_.clear();
|
|
}
|
|
check_redirects();
|
|
if (actor_id_ == 0) {
|
|
stop_driver();
|
|
return;
|
|
}
|
|
get_stdio_channel()->out()->write_bytes(console_.get_prompt());
|
|
}
|
|
}
|
|
|
|
UniqueDrivenEngine make_TextGame() {
|
|
return UniqueDrivenEngine(new TextGame);
|
|
}
|