182 lines
5.1 KiB
C++
182 lines
5.1 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 "world.hpp"
|
|
#include "traceback.hpp"
|
|
#include "textgame.hpp"
|
|
#include "luaconsole.hpp"
|
|
|
|
// Add another error status.
|
|
|
|
static lua_State *globalL = NULL;
|
|
|
|
|
|
static void lstop(lua_State *L, lua_Debug *ar)
|
|
{
|
|
(void)ar; /* unused arg. */
|
|
lua_sethook(L, NULL, 0, 0);
|
|
/* Avoid luaL_error -- a C hook doesn't add an extra frame. */
|
|
luaL_where(L, 0);
|
|
lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
|
|
lua_error(L);
|
|
}
|
|
|
|
static void laction(int i)
|
|
{
|
|
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
|
terminate process (default action) */
|
|
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
|
}
|
|
|
|
static void l_message(const char *msg)
|
|
{
|
|
fputs(msg, stderr);
|
|
fputc('\n', stderr);
|
|
fflush(stderr);
|
|
}
|
|
|
|
void TextGame::do_lua(const std::string &exp) {
|
|
lua_State *L = world_->state();
|
|
int status = luaL_loadbuffer(L, exp.c_str(), exp.size(), "=stdin");
|
|
assert(status == LUA_OK);
|
|
globalL = L;
|
|
signal(SIGINT, laction);
|
|
status = traceback_pcall(L, 0, LUA_MULTRET);
|
|
signal(SIGINT, SIG_DFL);
|
|
if (status == LUA_OK) {
|
|
if (lua_gettop(L) > 0) {
|
|
lua_getglobal(L, "pprint");
|
|
if (lua_isnil(L, -1)) {
|
|
lua_pop(L, 1);
|
|
lua_getglobal(L, "print");
|
|
}
|
|
lua_insert(L, 1);
|
|
if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0) {
|
|
l_message(
|
|
lua_pushfstring(L, "error calling 'print' (%s)",
|
|
lua_tostring(L, -1)));
|
|
}
|
|
}
|
|
} else {
|
|
const char *msg = lua_tostring(L, -1);
|
|
if (msg == NULL) {
|
|
msg = "(error object is not a string)";
|
|
}
|
|
l_message(msg);
|
|
lua_pop(L, 1);
|
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
|
}
|
|
}
|
|
|
|
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(1, 100)) {
|
|
const Tangible *tan = world_->tangible_get(id);
|
|
const AnimQueue &aq = tan->anim_queue_;
|
|
std::cerr << id << ": " << aq.get_graphic() << " " << aq.get_plane() << " " << aq.get_xyz() << std::endl;
|
|
}
|
|
}
|
|
|
|
void TextGame::do_menu_command(const StringVec &cmd) {
|
|
int64_t id;
|
|
if (cmd.size() == 1) {
|
|
id = 1;
|
|
} else if (cmd.size() == 2) {
|
|
id = util::strtoint(cmd[1], -1);
|
|
} else {
|
|
std::cerr << "m command (menu) expects a tangible ID or defaults to 1" << std::endl;
|
|
return;
|
|
}
|
|
gui_place_ = id;
|
|
world_->update_gui(1, 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;
|
|
world_->invoke_plan(1, gui_place_, action);
|
|
}
|
|
|
|
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_quit_command(const StringVec &cmd) {
|
|
if (cmd.size() != 1) {
|
|
std::cerr << "q command (quit) takes no arguments" << std::endl;
|
|
return;
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
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 (util::validinteger(words[0])) do_choose_command(words);
|
|
else {
|
|
std::cerr << "Unknown command: " << words[0] << std::endl;
|
|
}
|
|
}
|
|
|
|
void TextGame::run()
|
|
{
|
|
world_.reset(new World);
|
|
world_->init_standalone();
|
|
console_.clear();
|
|
while (true) {
|
|
console_.add_stdin();
|
|
int action = console_.action();
|
|
if (action == LuaConsole::DO_LUA) {
|
|
do_lua(console_.lua_expression());
|
|
} else if (action == LuaConsole::DO_COMMAND) {
|
|
do_command(console_.words());
|
|
} else if (action == LuaConsole::DO_SYNTAX) {
|
|
std::cerr << console_.syntax() << std::endl;
|
|
}
|
|
}
|
|
}
|
|
|