aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-14 00:30:05 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-14 00:30:05 -0400
commit0e19992b820158f8e9c0fc16ddc372a5ea596f53 (patch)
tree222d9256992f0107db694a8246aed471b1d7301b
parentc89b9aad8eadb3bfebaf4e6ae65ee6650e313fc7 (diff)
Lua can now modify world size and world data
-rw-r--r--Scripts/world.lua8
-rw-r--r--src/script.cpp5
-rw-r--r--src/world.cpp90
-rw-r--r--src/world.hpp37
4 files changed, 104 insertions, 36 deletions
diff --git a/Scripts/world.lua b/Scripts/world.lua
index 655cdab..f87f108 100644
--- a/Scripts/world.lua
+++ b/Scripts/world.lua
@@ -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
}
diff --git a/src/script.cpp b/src/script.cpp
index da365e1..cf5b2dd 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -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>();
diff --git a/src/world.cpp b/src/world.cpp
index dc9f75b..eb101f2 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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 *
diff --git a/src/world.hpp b/src/world.hpp
index cb9995e..7a7461f 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -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();
};
/**