]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Lua can now modify world size and world data
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 14 Sep 2019 04:30:05 +0000 (00:30 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 14 Sep 2019 04:30:05 +0000 (00:30 -0400)
Scripts/world.lua
src/script.cpp
src/world.cpp
src/world.hpp

index 655cdab95e491043577f231fb94c977eec37196d..f87f1087ca262ada0639aabf937394395cd1c34a 100644 (file)
@@ -26,14 +26,15 @@ world = {
 
     Generate = function(self)
         math.randomseed(self.Seed)
+        xsize, ysize, zsize = self:setSize(250, 128, 3)
         --self.data = {}
-        for Z = 0,self.Layers do
+        for Z = 0,zsize-1 do
             --self.data[Z] = {}
-            for X = 0,250 do
+            for X = 0,xsize-1 do
                 --self.data[Z][X] = {}
                 YGen = math.floor(6*math.sin(X/20) + Z) + 64
                 YDepth = math.random(2,5)
-                for Y = 0,128 do
+                for Y = 0,ysize-1 do
                     if Y == YGen then
                         --self.data[Z][X][Y] = 0
                         self:setData(X, Y, Z, "grass");
@@ -48,6 +49,7 @@ world = {
                 end
             end
         end
+        self:setData(1000, 1345, 5, "grass"); -- Test error checking
         print("Done with world gen");
     end
 }
index da365e1d39c9efbe67acb004ec621a2d5c8c6991..cf5b2ddee1c7e4868a2f886668e27e2296438278 100644 (file)
@@ -131,9 +131,10 @@ void ScriptSystem::scriptExport(void)
             sol::constructors<World(sol::object), World(void)>(),
             "Generate", &World::generate,
             "Seed", sol::property(&World::setSeed, &World::getSeed),
-            "Layers", sol::property(&World::setLayers, &World::getLayers),
             "setData", &World::setData,
-            "registerMaterial", &World::registerMaterial);
+            "registerMaterial", &World::registerMaterial,
+            "setSize", &World::setSize,
+            "getSize", &World::getSize);
 
 
     auto gamespace = lua["game"].get_or_create<sol::table>();
index dc9f75b01c36cac8e83732a20f6eee2a48caff45..eb101f28e23af5e13e46df5cdce39f0115815a2b 100644 (file)
@@ -22,6 +22,7 @@
 /*****************
 *  WORLD CLASS  *
 *****************/
+/* CONSTRUCTORS */
 World::World(sol::object param)
 {
     if (param.get_type() == sol::type::table) {
@@ -45,23 +46,13 @@ World::World(sol::object param)
         std::cerr << "World paramaters must be stored in a table" << std::endl;
     }
 
-    registerMat(this);
-    generate(this);
-}
-
-void World::setData(unsigned int x,
-                    unsigned int y,
-                    unsigned int z,
-                    std::string d)
-{
-    (void)x;
-    (void)y;
-    (void)z;
-    (void)d;
-
-    // TODO actually do stuff here
+    if (registerMat != sol::nil)
+        registerMat(this);
+    if (generate != sol::nil)
+        generate(this);
 }
 
+/* REGISTRY */
 void World::registerMaterial(std::string name, sol::object data)
 {
     if (data.get_type() == sol::type::table) {
@@ -83,6 +74,75 @@ void World::registerMaterial(std::string name, sol::object data)
     }
 }
 
+/* DATA */
+void World::setData(unsigned int x,
+                    unsigned int y,
+                    unsigned int z,
+                    std::string d)
+{
+    unsigned int discovered = -1;
+
+    auto found = string_registry.find(d);
+    if (found != string_registry.end())
+        discovered = found->second;
+
+    try {
+        data.at(z).at(x).at(y) = discovered;
+    } catch (std::out_of_range &oor) {
+    // Make sure any assignments that are outsize specified world size are
+    //  caught to avoid any seg faults
+        std::cerr << "Unable to set data at: "
+                  << x << "," << y << "," << z
+                  << " Exception: " << oor.what() << std::endl;
+    }
+}
+
+/* SIZE */
+std::tuple<unsigned int, unsigned int, unsigned int>
+World::setSize(unsigned int x, unsigned int y, unsigned int z)
+{
+    width = x;
+    height = y;
+    layers = z;
+
+    data = std::vector<std::vector<std::vector<unsigned int>>>
+        (z, std::vector<std::vector<unsigned int>>
+            (x,std::vector<unsigned int>
+                (y, -1)
+            )
+        );
+
+    return {width, height, layers};
+}
+
+std::tuple<unsigned int, unsigned int, unsigned int>
+World::getSize()
+{
+    return {width, height, layers};
+}
+
+/* RENDERING */
+void World::generateMesh()
+{
+    const unsigned int coordLength = 3 + // x, y, z
+                                     2 + // texture coords
+                                     1;  // transparency
+
+    (void)coordLength;
+}
+
+/* SEED */
+unsigned int World::getSeed()
+{
+    return seed;
+}
+
+unsigned int World::setSeed(unsigned int s)
+{
+    seed = s;
+    return seed;
+}
+
 
 /******************
 *  WORLD SYSTEM  *
index cb9995e2a2e545e0da119d9966e3add243c08e50..7a7461fef1cd92a80ecef71c017d4c1b24bf0bb6 100644 (file)
@@ -44,7 +44,6 @@ struct WorldMaterial
             std::string nor = tab["normal"];
             normal = Texture(nor);
         }
-
         if (tab["passable"] == sol::type::boolean) {
             passable = tab["passable"];
         }
@@ -57,12 +56,20 @@ private:
     unsigned int seed;
     unsigned int layers;
 
+    unsigned int height;
+    unsigned int width;
+
     std::vector<std::vector<std::vector<unsigned int>>> data;
 
     std::unordered_map<std::string, unsigned int> string_registry;
     std::vector<WorldMaterial> registry;
 
+    std::vector<float> mesh;
 public:
+    /* VARS */
+    sol::function generate;
+    sol::function registerMat;
+
     World() {}
     World(sol::object ref);
     ~World() {
@@ -72,26 +79,24 @@ public:
         data.clear();
     }
 
-    sol::function generate;
-    sol::function registerMat;
-
     /* SEED */
-    unsigned int getSeed() {return seed;}
-    void setSeed(unsigned int s) {seed = s;}
-
-    /* LAYERS */
-    unsigned int getLayers() {return layers;}
-    // If we change the amount of layers, we have to regenerate the world
-    void setLayers(unsigned int l) {
-        layers = l;
-        generate();
-    }
+    unsigned int getSeed();
+    unsigned int setSeed(unsigned int);
+
+    /* REGISTRY */
+    void registerMaterial(std::string, sol::object);
 
     /* DATA */
     void setData(unsigned int, unsigned int, unsigned int, std::string);
 
-    /* REGISTRY */
-    void registerMaterial(std::string, sol::object);
+    /* SIZE */
+    std::tuple<unsigned int, unsigned int, unsigned int> setSize(unsigned int,
+                                                                 unsigned int,
+                                                                 unsigned int);
+    std::tuple<unsigned int, unsigned int, unsigned int> getSize();
+
+    /* RENDERING */
+    void generateMesh();
 };
 
 /**