From 476d77e47946738f5a1bbb32a9c1807476d3cc78 Mon Sep 17 00:00:00 2001 From: Paul Merrill Date: Wed, 3 Aug 2011 01:18:58 -0700 Subject: [PATCH] create Script class --- src/Makefile | 2 +- src/entity.cpp | 74 ++++--------------- src/entity.h | 5 ++ src/script.cpp | 222 ++++++++++++--------------------------------------------- src/script.h | 12 ++-- 5 files changed, 71 insertions(+), 244 deletions(-) rewrite src/script.cpp (91%) diff --git a/src/Makefile b/src/Makefile index 1d06505..33f99ef 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,7 +24,7 @@ TESTWORLD = babysfirst.world all: tsunagari $(TESTWORLD) -tsunagari: animation.o area.o cmd.o common.o entity.o log.o main.o player.o resourcer.o script.o window.o world.o +tsunagari: animation.o area.o cmd.o common.o entity.o entity-lua.o log.o main.o player.o resourcer.o script.o window.o world.o $(CXX) -o tsunagari *.o $(LDFLAGS) %.world: %/* diff --git a/src/entity.cpp b/src/entity.cpp index bdbf4eb..1f708f2 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -15,6 +15,7 @@ #include "area.h" #include "config.h" #include "entity.h" +#include "entity-lua.h" #include "log.h" #include "resourcer.h" #include "script.h" @@ -233,6 +234,11 @@ void Entity::setArea(Area* a) area = a; } +void Entity::gotoUpperLeft() +{ + setCoordsByTile(coord(1, 1, 0)); +} + SampleRef Entity::getSound(const std::string& name) { boost::unordered_map::iterator it; @@ -274,70 +280,18 @@ void Entity::preMove(coord_t delta) setPhase("moving " + facing); } -static int lua_Entity_gotoUpperLeft(lua_State* L) -{ - int n = lua_gettop(L); - if (n != 1) { - Log::err("CppFn", "CppFn needs 1 argument"); - return 0; - } - if (!lua_isuserdata(L, 1)) { - Log::err("CppFn", "CppFn's first argument needs to be a userdata"); - return 0; - } - // assert obj.type == Entity* - - Entity* obj = (Entity*)lua_touserdata(L, 1); - obj->gotoUpperLeft(); - return 0; -} - -void Entity::gotoUpperLeft() -{ - setCoordsByTile(coord(1, 1, 0)); -} - -void Entity::runScript() -{ - lua_State* L; - const char* filename = "postMove.lua"; - - // Create Lua context - L = lua_open(); - luaL_openlibs(L); - - // Provide C function to call C++ function - lua_register(L, "gotoUpperLeft", lua_Entity_gotoUpperLeft); - lua_pushlightuserdata(L, this); - lua_setglobal(L, "entity"); - - // Provide x,y coordinate variables to script - coord_t tile = getCoordsByTile(); - lua_pushinteger(L, tile.x); - lua_setglobal(L, "x"); - lua_pushinteger(L, tile.y); - lua_setglobal(L, "y"); - - // Parse script - if (luaL_loadfile(L, filename)) { - Log::err(filename, std::string("Couldn't load file: ") + - lua_tostring(L, -1)); - lua_close(L); - return; - } - - // Run script - lua_call(L, 0, 0); - - // Destroy Lua context - lua_close(L); -} - void Entity::postMove() { if (conf->movemode != TURN) setPhase(facing); - runScript(); + + coord_t tile = getCoordsByTile(); + Script script; + script.addFn("gotoUpperLeft", lua_Entity_gotoUpperLeft); + script.addInt("x", tile.x); + script.addInt("y", tile.y); + script.addData("entity", this); + script.run("postMove.lua"); } /** diff --git a/src/entity.h b/src/entity.h index 6a4feef..dde2b4e 100644 --- a/src/entity.h +++ b/src/entity.h @@ -71,6 +71,11 @@ public: // nearby Tiles. Doesn't change x,y,z position. void setArea(Area* area); + // + // Lua callback targets + // + + //! Move to the upper left corner. Sets x,y tile positions to 1,1. void gotoUpperLeft(); protected: diff --git a/src/script.cpp b/src/script.cpp dissimilarity index 91% index 0a1e377..936b21f 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -1,175 +1,47 @@ -/****************************** -** Tsunagari Tile Engine ** -** script.cpp ** -** Copyright 2011 OmegaSDG ** -******************************/ - -#include - -#include -#include - -#include "log.h" -#include "script.h" - -static void stackDump (lua_State *L) -{ - int top = lua_gettop(L); - for (int i = 1; i <= top; i++) { /* repeat for each level */ - int t = lua_type(L, i); - switch (t) { - - case LUA_TSTRING: /* strings */ - printf("ā€˜%sā€™", lua_tostring(L, i)); - break; - - case LUA_TBOOLEAN: /* booleans */ - printf(lua_toboolean(L, i) ? "true" : "false"); - break; - - case LUA_TNUMBER: /* numbers */ - printf("%g", lua_tonumber(L, i)); - break; - - default: /* other values */ - printf("%s", lua_typename(L, t)); - break; - - } - printf(" "); /* put a separator */ - } - printf("\n"); /* end the listing */ -} - -Script::Script() -{ - L = lua_open(); - luaL_openlibs(L); -} - -bool Script::init(const std::string& name) -{ - if (luaL_loadfile(L, name.c_str())) { - Log::err(name, std::string("Couldn't load file: ") + - lua_tostring(L, -1)); - return false; - } - return true; -} - -void Script::registerCppFn(const std::string& name, Entity* obj, void* fn) -{ - boost::function bound = boost::bind(&Entity::moveByPixel, obj, coord(1, 0, 0)); - int sz = sizeof(bound); - - void* ud = lua_newuserdata(L, sz); - memcpy(ud, &bound, sz); -} - -/* -void Script::registerObj(const std::string& name, void* obj) -{ - const char* key = name.c_str(); - - // registry[name] = obj - this->name = name; - this->obj = obj; - - lua_register(L, key, callCppFn); -} -*/ - -/* Lua Registry - lua_pushlightuserdata(L, this->name); - lua_pushlightuserdata(L, this->obj); - lua_settable(L, LUA_REGISTRYINDEX); -*/ - -/* Create Lua table - lua_newtable(L); - lua_setglobal(L, key); - - lua_pushnumber(L, i); // Push the table index - lua_pushnumber(L, i*2); // Push the cell value - lua_rawset(L, -3); // Stores the pair in the table -*/ - -//#include -//#include - -// void Script::createTable() -// { -// int result, i; -// double sum; - -// /* -// * Ok, now here we go: We pass data to the lua script on the stack. -// * That is, we first have to prepare Lua's virtual stack the way we -// * want the script to receive it, then ask Lua to run it. -// */ -// lua_newtable(L); /* We will pass a table */ - -// /* -// * To put values into the table, we first push the index, then the -// * value, and then call lua_rawset() with the index of the table in the -// * stack. Let's see why it's -3: In Lua, the value -1 always refers to -// * the top of the stack. When you create the table with lua_newtable(), -// * the table gets pushed into the top of the stack. When you push the -// * index and then the cell value, the stack looks like: -// * -// * <- [stack bottom] -- table, index, value [top] -// * -// * So the -1 will refer to the cell value, thus -3 is used to refer to -// * the table itself. Note that lua_rawset() pops the two last elements -// * of the stack, so that after it has been called, the table is at the -// * top of the stack. -// */ -// for (i = 1; i <= 5; i++) { -// lua_pushnumber(L, i); /* Push the table index */ -// lua_pushnumber(L, i*2); /* Push the cell value */ -// lua_rawset(L, -3); /* Stores the pair in the table */ -// } - -// /* By what name is the script going to reference our table? */ -// lua_setglobal(L, "foo"); - -// /* Ask Lua to run our little script */ -// result = lua_pcall(L, 0, LUA_MULTRET, 0); -// if (result) { -// fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); -// exit(1); -// } - -// /* Get the returned value at the top of the stack (index -1) */ -// sum = lua_tonumber(L, -1); - -// printf("Script returned: %.0f\n", sum); - -// lua_pop(L, 1); /* Take the returned value out of the stack */ -// } - -/* -void* Script::getObj(const std::string&) -{ - // variable with an unique address - static const char Key = 'k'; - - // retrieve a number - lua_pushlightuserdata(L, (void *)&Key); // push address - lua_gettable(L, LUA_REGISTRYINDEX); // retrieve value - void* obj = lua_touserdata(L, -1); // convert to number - return obj; -} -*/ - -bool Script::run() -{ - lua_call(L, 0, 0); - return true; -} - -Script::~Script() -{ - lua_close(L); -} - +/****************************** +** Tsunagari Tile Engine ** +** script.cpp ** +** Copyright 2011 OmegaSDG ** +******************************/ + +#include "log.h" +#include "script.h" + +Script::Script() +{ + L = lua_open(); + luaL_openlibs(L); +} + +void Script::addFn(const char* name, lua_CFunction fn) +{ + lua_register(L, name, fn); +} + +void Script::addInt(const char* name, lua_Integer i) +{ + lua_pushinteger(L, i); + lua_setglobal(L, name); +} + +void Script::addData(const char* name, void* data) +{ + lua_pushlightuserdata(L, data); + lua_setglobal(L, name); +} + +void Script::run(const char* fn) +{ + if (luaL_loadfile(L, fn)) { + Log::err("Script::run", std::string("Couldn't load file: ") + + lua_tostring(L, -1)); + return; + } + lua_call(L, 0, 0); +} + +Script::~Script() +{ + lua_close(L); +} + diff --git a/src/script.h b/src/script.h index 80684b9..4e40c54 100644 --- a/src/script.h +++ b/src/script.h @@ -11,24 +11,20 @@ #include -#include "entity.h" - class Script { public: Script(); ~Script(); - bool init(const std::string& filename); + void addFn(const char* name, lua_CFunction fn); + void addInt(const char* name, lua_Integer i); + void addData(const char* name, void* data); - void registerCppFn(const std::string& name, Entity* obj, void* fn); -// void* getObj(const std::string& name); - bool run(); + void run(const char* fn); private: lua_State* L; - std::string name; - void* obj; }; #endif -- 2.11.4.GIT