diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-27 11:35:22 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-27 11:35:22 -0400 |
commit | 48cd8419bb274345fe386d47843f9aa16910e090 (patch) | |
tree | a0e31b43b0f38d6de8d3879b29563cfee1b19ced | |
parent | 8be1f74e878950cbbac3f05451341fc18892518b (diff) |
lua-scripted wandering
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | include/components/wander.hpp | 12 | ||||
-rw-r--r-- | include/quest.hpp | 3 | ||||
-rw-r--r-- | include/systems/lua.hpp | 68 | ||||
-rw-r--r-- | lib/libgif.a | bin | 144192 -> 0 bytes | |||
-rwxr-xr-x | lib/libgif.la | 41 | ||||
-rwxr-xr-x | lib/libgif.so | bin | 121392 -> 0 bytes | |||
-rwxr-xr-x | lib/libgif.so.7 | bin | 121392 -> 0 bytes | |||
-rwxr-xr-x | lib/libgif.so.7.0.0 | bin | 121392 -> 0 bytes | |||
-rw-r--r-- | main.cpp | 3 | ||||
-rw-r--r-- | src/inventory.cpp | 2 | ||||
-rw-r--r-- | src/quest.cpp | 38 | ||||
-rw-r--r-- | src/systems/lua.cpp | 2 | ||||
-rw-r--r-- | src/systems/movement.cpp | 6 | ||||
-rw-r--r-- | src/ui.cpp | 4 | ||||
-rw-r--r-- | src/world.cpp | 7 | ||||
-rw-r--r-- | xml/entities.xml | 14 |
18 files changed, 150 insertions, 60 deletions
@@ -4,11 +4,11 @@ CC = gcc CXX = g++ ifeq ($(TARGET_OS),linux) - LIBS = -Llib -lgif -lentityx -lpthread -lGL -lGLEW -lfreetype \ + LIBS = -Llib -lgif -llua -lentityx -lpthread -lGL -lGLEW -lfreetype \ -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2main endif ifeq ($(TARGET_OS),win32) - LIBS = -lgif -Llib -lentityx -lopengl32 -lglew32 -lmingw32 \ + LIBS = -Llib -lgif -llua -lentityx -lopengl32 -lglew32 -lmingw32 \ -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer -lfreetype endif @@ -14,10 +14,10 @@ The 'gamedev' project can be built on both Linux-based and Windows operating sys * SDL2, including SDL2_image and SDL2_mixer * FreeType 2 * GLEW -* libgif if not on 64-bit linux - -Windows builds are done with msys2, 64-bit. The compiler must support C++17. libgif for amd64 linux is currently included in the lib/ folder, so any other build set-up must supply their own copy, and change the Makefile accordingly (see what is done for win32 builds, for example). +* giflib +* lua (liblua linked into program) +Windows builds are done with msys2, 64-bit. The compiler must support C++17. Build Preparation ----------------- diff --git a/include/components/wander.hpp b/include/components/wander.hpp index 2001e89..d275158 100644 --- a/include/components/wander.hpp +++ b/include/components/wander.hpp @@ -1,16 +1,18 @@ #ifndef COMPONENTS_WANDER_HPP_ #define COMPONENTS_WANDER_HPP_ +#include <string> + +#include <systems/lua.hpp> + /** * Causes the entity to wander about. */ struct Wander { - Wander(float ix = 0, float r = 0) - : initialX(ix), range(r), countdown(0) {} + Wander(const std::string& s = "") + : script(LuaSystem::makeScript(s)) {} - float initialX; - float range; - int countdown; + LuaScript script; }; #endif // COMPONENTS_WANDER_HPP_ diff --git a/include/quest.hpp b/include/quest.hpp index 3358d8f..b42f756 100644 --- a/include/quest.hpp +++ b/include/quest.hpp @@ -78,6 +78,9 @@ public: titles.emplace_front(q.name); return titles;
} + + static void save(void); + static void load(void); }; #endif // QUEST_HPP_ diff --git a/include/systems/lua.hpp b/include/systems/lua.hpp new file mode 100644 index 0000000..0d8528b --- /dev/null +++ b/include/systems/lua.hpp @@ -0,0 +1,68 @@ +#ifndef SYSTEMS_LUA_HPP_ +#define SYSTEMS_LUA_HPP_ + +#include <string> +#include <lua.hpp> + +#include <vector2.hpp> + +class LuaScript { +private: + lua_State* state; + std::string script; + +public: + LuaScript(const std::string& sc = "") + : script(sc) { + state = luaL_newstate(); + luaL_openlibs(state); + luaL_loadstring(state, script.c_str()); + lua_pcall(state, 0, 0, 0); + } + + inline auto operator()(void) { + lua_getglobal(state, "update"); + lua_pcall(state, 0, LUA_MULTRET, 0); + if (lua_gettop(state) != 2) + return vec2(); + vec2 ret (lua_tonumber(state, 1), lua_tonumber(state, 2)); + lua_pop(state, 2); + return ret; + } +}; + +class LuaSystem { +private: + class LuaInterpreter { + private: + lua_State* state; + + public: + LuaInterpreter(void) { + state = luaL_newstate(); + luaL_openlibs(state); + } + + void registerFunc(const std::string& name, lua_CFunction func) { + lua_pushcclosure(state, func, 0); + lua_setglobal(state, name.c_str()); + } + + void loadFile(const std::string& name) { + luaL_dofile(state, name.c_str()); + lua_pcall(state, 0, 0, 0); // 'prime' the file + } + + void runFunc(const std::string& name) { + lua_getglobal(state, name.c_str()); + lua_pcall(state, 0, 0, 0); + } + }; + +public: + inline static LuaScript makeScript(const std::string& s) { + return LuaScript(s); + } +}; + +#endif // SYSTEMS_LUA_HPP_ diff --git a/lib/libgif.a b/lib/libgif.a Binary files differdeleted file mode 100644 index 0a53285..0000000 --- a/lib/libgif.a +++ /dev/null diff --git a/lib/libgif.la b/lib/libgif.la deleted file mode 100755 index 6338897..0000000 --- a/lib/libgif.la +++ /dev/null @@ -1,41 +0,0 @@ -# libgif.la - a libtool library file -# Generated by libtool (GNU libtool) 2.4.2 Debian-2.4.2-1.11 -# -# Please DO NOT delete this file! -# It is necessary for linking the library. - -# The name that we can dlopen(3). -dlname='libgif.so.7' - -# Names of this library. -library_names='libgif.so.7.0.0 libgif.so.7 libgif.so' - -# The name of the static archive. -old_library='libgif.a' - -# Linker flags that can not go in dependency_libs. -inherited_linker_flags='' - -# Libraries that this one depends upon. -dependency_libs='' - -# Names of additional weak libraries provided by this library -weak_library_names='' - -# Version information for libgif. -current=7 -age=0 -revision=0 - -# Is this an already installed library? -installed=yes - -# Should we warn about portability when linking against -modules? -shouldnotlink=no - -# Files to dlopen/dlpreopen -dlopen='' -dlpreopen='' - -# Directory that this library needs to be installed in: -libdir='/home/clyne/Downloads/giflib-5.1.4/out/lib' diff --git a/lib/libgif.so b/lib/libgif.so Binary files differdeleted file mode 100755 index 0511815..0000000 --- a/lib/libgif.so +++ /dev/null diff --git a/lib/libgif.so.7 b/lib/libgif.so.7 Binary files differdeleted file mode 100755 index 0511815..0000000 --- a/lib/libgif.so.7 +++ /dev/null diff --git a/lib/libgif.so.7.0.0 b/lib/libgif.so.7.0.0 Binary files differdeleted file mode 100755 index 0511815..0000000 --- a/lib/libgif.so.7.0.0 +++ /dev/null @@ -24,6 +24,7 @@ using namespace std::literals::chrono_literals; #include <render.hpp> #include <ui.hpp> #include <inventory.hpp> +#include <quest.hpp> std::atomic_bool GameThread::pause; @@ -107,6 +108,7 @@ int main(int argc, char *argv[]) WorldSystem::loader(); InventorySystem::load(); + QuestSystem::load(); ///////////////////////////// // // @@ -172,6 +174,7 @@ int main(int argc, char *argv[]) // save game::briceSave(); + QuestSystem::save(); InventorySystem::save(); WorldSystem::save(); diff --git a/src/inventory.cpp b/src/inventory.cpp index 36cc041..41fe661 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -421,7 +421,7 @@ void InventorySystem::load(void) // check signature if (std::stoi(lines[0]) != 831998) - UserError("Save file signature is invalid... (delete it)"); + UserError("Inventory save file signature is invalid... (delete it)"); for (unsigned int i = 1; i < lines.size(); i += 3) { if (lines[i].size() > 0) { diff --git a/src/quest.cpp b/src/quest.cpp index 6017247..489d71e 100644 --- a/src/quest.cpp +++ b/src/quest.cpp @@ -1,8 +1,12 @@ #include <quest.hpp> #include <algorithm> +#include <iostream> +#include <fstream> #include <engine.hpp> +#include <error.hpp> +#include <fileio.hpp> #include <inventory.hpp> #include <tokens.hpp> @@ -82,3 +86,37 @@ bool QuestSystem::hasQuest(std::string title) return (std::find_if(std::begin(current), std::end(current), [&title](const Quest& q) { return (q.name == title); }) != std::end(current)); } + +void QuestSystem::save(void) +{ + std::ofstream s (game::config::xmlFolder + "quest.dat"); + + // signature? + s << "831998\n"; + + for (const auto& q : current) { + s << q.name << '\n' << q.desc << '\n'; + for (const auto& r : q.reqs) + s << r.first << ',' << std::to_string(r.second) << ','; + s << "Reward,"; + for (const auto& r : q.rewards) + s << r.first << ',' << std::to_string(r.second) << ','; + s << '\n'; + } +} + +void QuestSystem::load(void) +{ + std::ifstream sf (game::config::xmlFolder + "quest.dat"); + if (sf.good()) { + sf.close(); + auto lines = readFileA(game::config::xmlFolder + "quest.dat"); + + // check signature + if (std::stoi(lines[0]) != 831998) + UserError("Quest save file signature is invalid... (delete it)"); + + for (unsigned int i = 1; i < lines.size(); i += 3) + assign(lines[i], lines[i + 1], lines[i + 2]); + } +} diff --git a/src/systems/lua.cpp b/src/systems/lua.cpp new file mode 100644 index 0000000..b093aff --- /dev/null +++ b/src/systems/lua.cpp @@ -0,0 +1,2 @@ +#include <systems/lua.hpp> + diff --git a/src/systems/movement.cpp b/src/systems/movement.cpp index 7ff9966..4eb574d 100644 --- a/src/systems/movement.cpp +++ b/src/systems/movement.cpp @@ -75,14 +75,16 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e // make the entity wander // TODO initialX and range? if (entity.has_component<Wander>()) { - auto& countdown = entity.component<Wander>()->countdown; + auto vel = entity.component<Wander>()->script(); + direction.x = vel.x; + /*auto& countdown = entity.component<Wander>()->countdown; if (countdown > 0) { countdown--; } else { countdown = 5000 + randGet() % 10 * 100; direction.x = (randGet() % 3 - 1) * 0.004f; - } + }*/ } } }); @@ -793,8 +793,8 @@ void UISystem::putString(const vec2& p, const std::string& s, float wrap) for (auto c : word) { switch (c) { case '\n': - //offset.y -= FontSystem::getSize() * 1.05f; - //offset.x = p.x; + offset.y -= FontSystem::getSize() * 1.05f; + offset.x = p.x; break; case '\b': //offset.x -= add.x; diff --git a/src/world.cpp b/src/world.cpp index 7904c4f..9f57b87 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -350,9 +350,10 @@ void WorldSystem::loader(void) entity.assign<Dialog>(wxml, abcd); else if (tname == "Grounded") entity.assign<Grounded>(); // no need to pass xmls... - else if (tname == "Wander") - entity.assign<Wander>(); - else if (tname == "Hop") + else if (tname == "Wander") { + auto script = abcd->GetText(); + entity.assign<Wander>(script != nullptr ? script : ""); + } else if (tname == "Hop") entity.assign<Hop>(); else if (tname == "Health") entity.assign<Health>(wxml, abcd); diff --git a/xml/entities.xml b/xml/entities.xml index 72431dc..aecae45 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -50,7 +50,19 @@ <Physics /> <Name value="Daddy" /> <Dialog /> - <Wander /> + <Wander> + countdown = 0 + velx = 0 + + update = function() + if (countdown == 0) then + countdown = 5000 + math.random(0, 2000) + velx = (math.random(0, 3) - 1) * 0.004 + end + countdown = countdown - 1 + return velx, 0 + end + </Wander> </npc> <skirl> |