]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
lua-scripted wandering
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 27 Sep 2017 15:35:22 +0000 (11:35 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 27 Sep 2017 15:35:22 +0000 (11:35 -0400)
18 files changed:
Makefile
README.md
include/components/wander.hpp
include/quest.hpp
include/systems/lua.hpp [new file with mode: 0644]
lib/libgif.a [deleted file]
lib/libgif.la [deleted file]
lib/libgif.so [deleted file]
lib/libgif.so.7 [deleted file]
lib/libgif.so.7.0.0 [deleted file]
main.cpp
src/inventory.cpp
src/quest.cpp
src/systems/lua.cpp [new file with mode: 0644]
src/systems/movement.cpp
src/ui.cpp
src/world.cpp
xml/entities.xml

index b48a4acbd4148506cc0bfcfe91d4e122ae143b42..2700c0d6bb60d7c9c591d0ddadd81894ce4e3329 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -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
 
index f6b12c8082f072290ca854993b6d501d0e31c6dd..cf797c7f5485e25d9d916b48f32c406a345783f0 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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
 -----------------
 
index 2001e89a1f198dc059da8f5fe08c6d4219313913..d2751583f2458c9a79bf2321066d0a227cbbec5c 100644 (file)
@@ -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_
index 3358d8f8489230de75551fd5d04070fefb88eae7..b42f75644392f0ddb530ba8c862a936cdb5498cb 100644 (file)
@@ -78,6 +78,9 @@ public:
                        titles.emplace_front(q.name);
                return titles;\r
        }
+
+       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 (file)
index 0000000..0d8528b
--- /dev/null
@@ -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
deleted file mode 100644 (file)
index 0a53285..0000000
Binary files a/lib/libgif.a and /dev/null differ
diff --git a/lib/libgif.la b/lib/libgif.la
deleted file mode 100755 (executable)
index 6338897..0000000
+++ /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
deleted file mode 100755 (executable)
index 0511815..0000000
Binary files a/lib/libgif.so and /dev/null differ
diff --git a/lib/libgif.so.7 b/lib/libgif.so.7
deleted file mode 100755 (executable)
index 0511815..0000000
Binary files a/lib/libgif.so.7 and /dev/null differ
diff --git a/lib/libgif.so.7.0.0 b/lib/libgif.so.7.0.0
deleted file mode 100755 (executable)
index 0511815..0000000
Binary files a/lib/libgif.so.7.0.0 and /dev/null differ
index 49d1dd9fd72f2ee62502d7a04dce3d0792664974..c05a7873a6cf1517d8eb0d0d3d0a0af746c4ab4f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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();
 
index 36cc0419ae2d0071683ea4006cda12bbd1ad2897..41fe6615a7adc8182e35df36684472c489b7187c 100644 (file)
@@ -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) {
index 60172479dd1954f25a521f262b4c1b9aa7d88419..489d71eede8e5787f7cf4bc80507625900095e3e 100644 (file)
@@ -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 (file)
index 0000000..b093aff
--- /dev/null
@@ -0,0 +1,2 @@
+#include <systems/lua.hpp>
+
index 7ff9966e5e5017ecf4b2f8c42dfa2be01dd9a94d..4eb574d3cd0af8636c6522e486adf87c2c9ee25a 100644 (file)
@@ -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;
-                               }
+                               }*/
                        }
                }
        });
index dea2f8e14ee6abfa1f2765d6076f89d864d06d1c..7af77f0c58dd21e62186142abc82de02c1766ec0 100644 (file)
@@ -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;
index 7904c4fcffcf50c208d4c460f98e4847a042ab5b..9f57b87bdee4a5c6511343fb018c46b6e1e4f00b 100644 (file)
@@ -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);
index 72431dc0c6e96fc65d9ac5ce7181b137051959db..aecae45c6ef43552507e632da9f01497b982e28f 100644 (file)
        <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>