Refactor the Lua code for the default game.

* Split the models and block definitions into two folders
* Gave some models more generic names
master
aurailus 2019-04-21 15:25:06 -07:00
parent 91e3cc81ca
commit 4078e29440
20 changed files with 581 additions and 535 deletions

View File

@ -11,6 +11,9 @@
<file path="$PROJECT_DIR$/lib" />
</excludeRoots>
</component>
<component name="GCoverageShowInEditor">
<option name="showInEditor" value="false" />
</component>
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>

View File

@ -0,0 +1,23 @@
local path = "/home/aurailus/C++/GlProject/res/lua/default/";
-- Temporarily generate Air in the lua file
-- TODO: Move this to C++
-- Air [ID 0]
zeus.register_block("_:air", {
visible = false,
culls = false,
solid = false,
name = "Air",
model = "default:block",
textures = {"_missing"},
})
--Grass, dirt, stone
dofile(path .. "blocks/ground.lua");
--Leaves, wood
dofile(path .. "blocks/tree.lua");
--Tallgrass, Flowers
dofile(path .. "blocks/foliage.lua");
--Sand, sandstone
dofile(path .. "blocks/desert.lua");

View File

@ -0,0 +1,22 @@
-- Sand [ID 19]
zeus.register_block('default:sand', {
name = "Sand",
model = "default:block",
textures = {"default_sand"},
toughness = {
hand = 3,
shovel = 1,
pick = 2
},
})
-- Sandstone [ID 20]
zeus.register_block('default:sandstone', {
name = "Sandstone",
model = "default:block",
textures = {"default_sandstone"},
toughness = {
hand = 14,
pick = 3
},
})

View File

@ -0,0 +1,57 @@
-- TallGrass [ID 6..10]
for i=1,5,1 do
zeus.register_block("default:tallgrass_" .. i, {
culls = false,
solid = false,
name = "Tall Grass",
model = "default:cross",
textures = {
"default_tallgrass_" .. i,
},
lowdef_render = false,
selection_box = {
{1/16, 0, 1/16, 15/16, 0.30 + i * 0.1, 15/16}
},
toughness = {
hand = 0
}
})
end
-- Flowers [ID 11..18]
local flowers = {
"rose",
"tulip",
"viola",
"geranium",
"mushroom_red",
"mushroom_brown",
"dandelion_white",
"dandelion_yellow"
}
for _,flower in ipairs(flowers) do
local function tchelper(first, rest)
return first:upper()..rest:lower()
end
local name = flower:gsub("_", " "):gsub("(%a)([%w_']*)", tchelper)
zeus.register_block("default:flower_" .. flower, {
culls = false,
solid = false,
name = name,
model = "default:cross",
textures = {
"flowers_" .. flower
},
lowdef_render = false,
selection_box = {
{4/16, 0, 4/16, 12/16, 14/16, 12/16}
},
toughness = {
hand = 0
}
})
end

View File

@ -0,0 +1,47 @@
-- Grass [ID 1]
zeus.register_block("default:grass", {
name = "Grass",
model = "default:block_foliage",
textures = {
"default_grass_top",
"default_dirt",
"default_grass_side",
"default_grass_side",
"default_grass_side",
"default_grass_side",
"default_grass_float",
},
lowdef_textures = {
"default_grass_top",
"default_dirt",
"default_grass_side"
},
toughness = {
hand = 3,
shovel = 1,
pick = 2
},
})
-- Dirt [ID 2]
zeus.register_block('default:dirt', {
name = "Dirt",
model = "default:block",
textures = {"default_dirt"},
toughness = {
hand = 3,
shovel = 1,
pick = 2
},
})
-- Stone [ID 3]
zeus.register_block('default:stone', {
name = "Stone",
model = "default:block",
textures = {"default_stone"},
toughness = {
hand = 14,
pick = 3
},
})

View File

@ -0,0 +1,33 @@
-- Leaves [ID 4]
zeus.register_block('default:leaves', {
visible = true,
culls = false,
name = "Log",
model = "default:leaflike",
textures = {
"default_leaves",
"default_leaves_puff"
},
lowdef_textures = {
"default_leaves_opaque",
},
toughness = {
hand = 1,
axe = 0.2,
},
})
-- Wood [ID 5]
zeus.register_block('default:wood', {
name = "Log",
model = "default:block",
textures = {
"default_log_top",
"default_log_top",
"default_log_side"
},
toughness = {
hand = 5,
axe = 3,
},
})

View File

@ -0,0 +1,6 @@
local path = "/home/aurailus/C++/GlProject/res/lua/default/";
dofile(path .. "models/block.lua");
dofile(path .. "models/block_foliage.lua");
dofile(path .. "models/cross.lua");
dofile(path .. "models/leaflike.lua");

View File

@ -0,0 +1,62 @@
--
-- Basic 'block' model, represents a cube.
-- Texture order is: top, bottom, left, right, front, back.
--
zeus.register_blockmodel("default:block", {
{
face = "left",
tex = 3,
points = {
0, 0, 0, 0, 1,
0, 0, 1, 1, 1,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0
}
}, {
face = "right",
tex = 4,
points = {
1, 1, 1, 1, 0,
1, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 1, 0, 0, 0
}
}, {
face = "top",
tex = 1,
points = {
0, 1, 0, 0, 0,
0, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0
}
}, {
face = "bottom",
tex = 2,
points = {
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 0, 1
}
}, {
face = "front",
tex = 5,
points = {
0, 0, 1, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 0,
0, 1, 1, 0, 0
}
}, {
face = "back",
tex = 6,
points = {
0, 0, 0, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, 0, 0, 1, 1
}
}
})

View File

@ -0,0 +1,100 @@
--
-- Cube model with extra side faces that pop out of the model.
-- Useful for making grass, snow, textures that have a raised material on top of them.
-- Texture order is: top, bottom, left, right, front, back, raised.
--
zeus.register_blockmodel("default:block_foliage", {
{
face = "left",
tex = 3,
points = {
0, 0, 0, 0, 1,
0, 0, 1, 1, 1,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0
}
}, {
face = "right",
tex = 4,
points = {
1, 1, 1, 1, 0,
1, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 1, 0, 0, 0
}
}, {
face = "top",
tex = 1,
points = {
0, 1, 0, 0, 0,
0, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0
}
}, {
face = "bottom",
tex = 2,
points = {
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 0, 1
}
}, {
face = "front",
tex = 5,
points = {
0, 0, 1, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 0,
0, 1, 1, 0, 0
}
}, {
face = "back",
tex = 6,
points = {
0, 0, 0, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, 0, 0, 1, 1
}
}, {
--Floats begin here
face = "front",
tex = 7,
points = {
0, 1, 1, 0, 0,
0, 0.2, 1.2, 0, 1,
1, 0.2, 1.2, 1, 1,
1, 1, 1, 1, 0
}
}, {
face = "back",
tex = 7,
points = {
1, 0.2, -0.2, 1, 1,
0, 0.2, -0.2, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0
}
}, {
face = "right",
tex = 7,
points = {
1.2, 0.2, 1, 1, 1,
1.2, 0.2, 0, 0, 1,
1, 1, 0, 0, 0,
1, 1, 1, 1, 0
}
}, {
face = "left",
tex = 7,
points = {
0, 1, 0, 0, 0,
-0.2, 0.2, 0, 0, 1,
-0.2, 0.2, 1, 1, 1,
0, 1, 1, 1, 0
}
}
})

View File

@ -0,0 +1,45 @@
--
-- Diagonal cross-shaped model. Has 2 vertical faces that make an X pattern when looked at from above.
-- Useful for representing plants, grass, etc.
-- Only takes one texture, which is displayed on all faces.
--
zeus.register_blockmodel("default:cross", {
{
face = "nocull",
tex = 1,
points = {
0.1, 0, 0.1, 0, 1,
0.9, 0, 0.9, 1, 1,
0.9, 0.9, 0.9, 1, 0,
0.1, 0.9, 0.1, 0, 0
}
}, {
face = "nocull",
tex = 1,
points = {
0.9, 0.9, 0.9, 1, 0,
0.9, 0, 0.9, 1, 1,
0.1, 0, 0.1, 0, 1,
0.1, 0.9, 0.1, 0, 0
}
}, {
face = "nocull",
tex = 1,
points = {
0.9, 0.9, 0.1, 1, 0,
0.9, 0, 0.1, 1, 1,
0.1, 0, 0.9, 0, 1,
0.1, 0.9, 0.9, 0, 0
}
}, {
face = "nocull",
tex = 1,
points = {
0.1, 0, 0.9, 0, 1,
0.9, 0, 0.1, 1, 1,
0.9, 0.9, 0.1, 1, 0,
0.1, 0.9, 0.9, 0, 0
}
}
})

View File

@ -0,0 +1,87 @@
--
-- Cube model with extra inner faces that pop out of the model.
-- Useful for making bushy leaves.
-- Texture order is: cube textures, pop textures.
--
zeus.register_blockmodel("default:leaflike", {
{
face = "left",
tex = 1,
points = {
0, 0, 0, 0, 1,
0, 0, 1, 1, 1,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0
}
}, {
face = "right",
tex = 1,
points = {
1, 1, 1, 1, 0,
1, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 1, 0, 0, 0
}
}, {
face = "top",
tex = 1,
points = {
0, 1, 0, 0, 0,
0, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0
}
}, {
face = "bottom",
tex = 1,
points = {
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 0, 1
}
}, {
face = "front",
tex = 1,
points = {
0, 0, 1, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 0,
0, 1, 1, 0, 0
}
}, {
face = "back",
tex = 1,
points = {
0, 0, 0, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, 0, 0, 1, 1
}
}, {
face = "nocull",
tex = 2,
points = {
-0.31, 1.30, -0.3, 0, 0,
-0.31, -0.30, -0.31, 0, 1,
1.3, -0.30, 1.3, 1, 1,
1.3, 1.30, 1.29, 1, 0,
1.3, -0.30, 1.3, 1, 1,
-0.31, -0.30, -0.31, 0, 1,
-0.31, 1.30, -0.3, 0, 0,
1.3, 1.30, 1.29, 1, 0,
-0.31, 1.30, 1.29, 0, 0,
-0.31, -0.30, 1.3, 0, 1,
1.3, -0.30, -0.31, 1, 1,
1.3, 1.30, -0.3, 1, 0,
1.3, -0.30, -0.31, 1, 1,
-0.31, -0.30, 1.3, 0, 1,
-0.31, 1.30, 1.29, 0, 0,
1.3, 1.30, -0.3, 1, 0
}
}
})

View File

@ -1,488 +1,5 @@
-- Dump function
function dump(tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
if type(k) == "number" then
k = "[" .. k .. "]"
end
local indentString = string.rep(" ", indent)
local formatting = indentString .. k .. " = "
if type(v) == "table" then
print(formatting .. "{")
dump(v, indent+1)
print(indentString .. "}")
elseif type(v) == 'boolean' then
print(formatting .. tostring(v))
else
print(formatting .. v)
end
end
end
local path = "/home/aurailus/C++/GlProject/res/lua/";
-- Create cube model
zeus.register_blockmodel("default:block", {
{
face = "left",
tex = 3,
points = {
0, 0, 0, 0, 1,
0, 0, 1, 1, 1,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0
}
}, {
face = "right",
tex = 4,
points = {
1, 1, 1, 1, 0,
1, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 1, 0, 0, 0
}
}, {
face = "top",
tex = 1,
points = {
0, 1, 0, 0, 0,
0, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0
}
}, {
face = "bottom",
tex = 2,
points = {
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 0, 1
}
}, {
face = "front",
tex = 5,
points = {
0, 0, 1, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 0,
0, 1, 1, 0, 0
}
}, {
face = "back",
tex = 6,
points = {
0, 0, 0, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, 0, 0, 1, 1
}
}
})
-- Create plantlike model
zeus.register_blockmodel("default:plantlike", {
{
face = "nocull",
tex = 1,
points = {
0.1, 0, 0.1, 0, 1,
0.9, 0, 0.9, 1, 1,
0.9, 0.9, 0.9, 1, 0,
0.1, 0.9, 0.1, 0, 0
}
}, {
face = "nocull",
tex = 1,
points = {
0.9, 0.9, 0.9, 1, 0,
0.9, 0, 0.9, 1, 1,
0.1, 0, 0.1, 0, 1,
0.1, 0.9, 0.1, 0, 0
}
}, {
face = "nocull",
tex = 1,
points = {
0.9, 0.9, 0.1, 1, 0,
0.9, 0, 0.1, 1, 1,
0.1, 0, 0.9, 0, 1,
0.1, 0.9, 0.9, 0, 0
}
}, {
face = "nocull",
tex = 1,
points = {
0.1, 0, 0.9, 0, 1,
0.9, 0, 0.1, 1, 1,
0.9, 0.9, 0.1, 1, 0,
0.1, 0.9, 0.9, 0, 0
}
}
})
-- Create leaves model
zeus.register_blockmodel("default:block_poof", {
{
face = "left",
tex = 1,
points = {
0, 0, 0, 0, 1,
0, 0, 1, 1, 1,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0
}
}, {
face = "right",
tex = 1,
points = {
1, 1, 1, 1, 0,
1, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 1, 0, 0, 0
}
}, {
face = "top",
tex = 1,
points = {
0, 1, 0, 0, 0,
0, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0
}
}, {
face = "bottom",
tex = 1,
points = {
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 0, 1
}
}, {
face = "front",
tex = 1,
points = {
0, 0, 1, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 0,
0, 1, 1, 0, 0
}
}, {
face = "back",
tex = 1,
points = {
0, 0, 0, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, 0, 0, 1, 1
}
}, {
face = "nocull",
tex = 2,
points = {
-0.31, 1.30, -0.3, 0, 0,
-0.31, -0.30, -0.31, 0, 1,
1.3, -0.30, 1.3, 1, 1,
1.3, 1.30, 1.29, 1, 0,
1.3, -0.30, 1.3, 1, 1,
-0.31, -0.30, -0.31, 0, 1,
-0.31, 1.30, -0.3, 0, 0,
1.3, 1.30, 1.29, 1, 0,
-0.31, 1.30, 1.29, 0, 0,
-0.31, -0.30, 1.3, 0, 1,
1.3, -0.30, -0.31, 1, 1,
1.3, 1.30, -0.3, 1, 0,
1.3, -0.30, -0.31, 1, 1,
-0.31, -0.30, 1.3, 0, 1,
-0.31, 1.30, 1.29, 0, 0,
1.3, 1.30, -0.3, 1, 0
}
}
})
zeus.register_blockmodel("default:block_side_foliage", {
{
face = "left",
tex = 3,
points = {
0, 0, 0, 0, 1,
0, 0, 1, 1, 1,
0, 1, 1, 1, 0,
0, 1, 0, 0, 0
}
}, {
face = "right",
tex = 4,
points = {
1, 1, 1, 1, 0,
1, 0, 1, 1, 1,
1, 0, 0, 0, 1,
1, 1, 0, 0, 0
}
}, {
face = "top",
tex = 1,
points = {
0, 1, 0, 0, 0,
0, 1, 1, 0, 1,
1, 1, 1, 1, 1,
1, 1, 0, 1, 0
}
}, {
face = "bottom",
tex = 2,
points = {
0, 0, 0, 0, 0,
1, 0, 0, 1, 0,
1, 0, 1, 1, 1,
0, 0, 1, 0, 1
}
}, {
face = "front",
tex = 5,
points = {
0, 0, 1, 0, 1,
1, 0, 1, 1, 1,
1, 1, 1, 1, 0,
0, 1, 1, 0, 0
}
}, {
face = "back",
tex = 6,
points = {
0, 0, 0, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0,
1, 0, 0, 1, 1
}
}, {
--Floats begin here
face = "front",
tex = 7,
points = {
0, 1, 1, 0, 0,
0, 0.2, 1.2, 0, 1,
1, 0.2, 1.2, 1, 1,
1, 1, 1, 1, 0
}
}, {
face = "back",
tex = 7,
points = {
1, 0.2, -0.2, 1, 1,
0, 0.2, -0.2, 0, 1,
0, 1, 0, 0, 0,
1, 1, 0, 1, 0
}
}, {
face = "right",
tex = 7,
points = {
1.2, 0.2, 1, 1, 1,
1.2, 0.2, 0, 0, 1,
1, 1, 0, 0, 0,
1, 1, 1, 1, 0
}
}, {
face = "left",
tex = 7,
points = {
0, 1, 0, 0, 0,
-0.2, 0.2, 0, 0, 1,
-0.2, 0.2, 1, 1, 1,
0, 1, 1, 1, 0
}
}
})
--[[
**Block Creation**
The function zeus.register_block creates a block in the scene.
The first argument is the name of the block, which should be prefixed by the mod name and a colon.
The next argument is a table of properties. These are the following accepted properties:
name: The display name of the block
model: The blockModel of the block, will default to "default:cube"
textures: The list of textures for the blockmodel. Which textures go where are defined by the blockmodel.
--]]
-- Ignore this, it is temporary
-- Air [ID 0]
zeus.register_block("_:air", {
visible = false,
culls = false,
solid = false,
name = "Air",
model = "default:block",
textures = {"_missing"},
})
-- Grass [ID 1]
zeus.register_block("default:grass", {
name = "Grass",
model = "default:block_side_foliage",
textures = {
"default_grass_top",
"default_dirt",
"default_grass_side",
"default_grass_side",
"default_grass_side",
"default_grass_side",
"default_grass_float",
},
lowdef_textures = {
"default_grass_top",
"default_dirt",
"default_grass_side"
},
toughness = {
hand = 3,
shovel = 1,
pick = 2
},
})
-- Dirt [ID 2]
zeus.register_block('default:dirt', {
name = "Dirt",
model = "default:block",
textures = {"default_dirt"},
toughness = {
hand = 3,
shovel = 1,
pick = 2
},
})
-- Stone [ID 3]
zeus.register_block('default:stone', {
name = "Stone",
model = "default:block",
textures = {"default_stone"},
toughness = {
hand = 14,
pick = 3
},
})
-- Leaves [ID 4]
zeus.register_block('default:leaves', {
visible = true,
culls = false,
name = "Log",
model = "default:block_poof",
textures = {
"default_leaves",
"default_leaves_puff"
},
lowdef_textures = {
"default_leaves_opaque",
},
toughness = {
hand = 1,
axe = 0.2,
},
})
-- Wood [ID 5]
zeus.register_block('default:wood', {
name = "Log",
model = "default:block",
textures = {
"default_log_top",
"default_log_top",
"default_log_side"
},
toughness = {
hand = 5,
axe = 3,
},
})
-- TallGrass [ID 6..10]
for i=1,5,1 do
zeus.register_block("default:tallgrass_" .. i, {
culls = false,
solid = false,
name = "Tall Grass",
model = "default:plantlike",
textures = {
"default_tallgrass_" .. i,
},
lowdef_render = false,
selection_box = {
{1/16, 0, 1/16, 15/16, 0.30 + i * 0.1, 15/16}
},
toughness = {
hand = 0
}
})
end
-- Flowers [ID 11..18]
local flowers = {
"rose",
"tulip",
"viola",
"geranium",
"mushroom_red",
"mushroom_brown",
"dandelion_white",
"dandelion_yellow"
}
for _,flower in ipairs(flowers) do
local function tchelper(first, rest)
return first:upper()..rest:lower()
end
local name = flower:gsub("_", " "):gsub("(%a)([%w_']*)", tchelper)
zeus.register_block("default:flower_" .. flower, {
culls = false,
solid = false,
name = name,
model = "default:plantlike",
textures = {
"flowers_" .. flower
},
lowdef_render = false,
selection_box = {
{4/16, 0, 4/16, 12/16, 14/16, 12/16}
},
toughness = {
hand = 0
}
})
end
-- Sand [ID 19]
zeus.register_block('default:sand', {
name = "Sand",
model = "default:block",
textures = {"default_sand"},
toughness = {
hand = 3,
shovel = 1,
pick = 2
},
})
-- Sandstone [ID 20]
zeus.register_block('default:sandstone', {
name = "Sandstone",
model = "default:block",
textures = {"default_sandstone"},
toughness = {
hand = 14,
pick = 3
},
})
dofile(path .. "util.lua");
dofile(path .. "default/models.lua");
dofile(path .. "default/blocks.lua");

20
res/lua/util.lua Normal file
View File

@ -0,0 +1,20 @@
-- Dump function
function dump(tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
if type(k) == "number" then
k = "[" .. k .. "]"
end
local indentString = string.rep(" ", indent)
local formatting = indentString .. k .. " = "
if type(v) == "table" then
print(formatting .. "{")
dump(v, indent+1)
print(indentString .. "}")
elseif type(v) == 'boolean' then
print(formatting .. tostring(v))
else
print(formatting .. v)
end
end
end

View File

@ -3,7 +3,6 @@
//
#include "LuaApi.h"
#include "../def/GameDefs.h"
void LuaApi::init(GameDefs& defs) {

View File

@ -55,7 +55,9 @@ LModuleRegister::LModuleRegister(sol::state &lua, sol::table &zeus, GameDefs &de
//
// # Register Block
// `zeus.register_block(string identifier, table definition)`
//
//
//
zeus.set_function("register_block", [&](std::string identifier, sol::table data) {
try {
if (identifier.length() == 0) throw "No Identifier";
@ -65,25 +67,37 @@ LModuleRegister::LModuleRegister(sol::state &lua, sol::table &zeus, GameDefs &de
auto modelStr = data.get_or<std::string>("model", "default:cube");
auto selection = data.get<sol::optional<sol::table>>("selection_box");
if (!name) throw "No name property";
if (!textures) throw "No textures table";
bool visible = data.get_or("visible", true);
bool culls = data.get_or("culls", true);
bool solid = data.get_or("solid", true);
if (!name) throw "No name property";
if (!textures) throw "No textures table";
//Convert textures to a std::vector
std::vector<std::string> texturesVector;
textures->for_each([&](sol::object key, sol::object value) {
if (!value.is<std::string>()) throw "Non string texture value.";
std::string str = value.as<std::string>();
texturesVector.push_back(str);
});
if (texturesVector.size() == 0) texturesVector.push_back("_missing");
//TODO: In register_blockmodel, store these models in C++ somewhere so that I don't need to reference the
//lua data to register a model.
sol::table models = zeus["registered_blockmodels"];
auto model = models.get<sol::optional<sol::table>>(modelStr);
if (!model) throw "Undefined model";
//TODO: Validate and allow multiple selection boxes
SelectionBox sbox = {{0, 0, 0}, {1, 1, 1}};
if (selection) {
sol::table def = (*selection)[1];
sbox = {{def[1], def[2], def[3]}, {def[4], def[5], def[6]}};
}
BlockModel blockModel = BlockModel::from_lua_def(*model, *textures, defs.textures(), visible, culls);
BlockModel blockModel = BlockModel::create(*model, texturesVector, defs.textures(), visible, culls);
BlockDef def(identifier, std::move(blockModel), solid, sbox);
defs.blocks().registerBlock(std::move(def));

View File

@ -6,6 +6,8 @@
#define ZEUS_LREGISTERBLOCK_H
#include "../../def/block/graph/SelectionBox.h"
#include "../../def/block/BlockDef.h"
#include "../../def/block/BlockAtlas.h"
#include <sol.hpp>
class GameDefs;

View File

@ -4,8 +4,6 @@
#include "GameDefs.h"
#include "../api/func/LModuleRegister.h"
GameDefs::GameDefs(std::string tex_path) {
textureAtlas = TextureAtlas(512);
textureAtlas.loadFromDirectory(std::move(tex_path));

View File

@ -5,58 +5,69 @@
#include "BlockModel.h"
//TODO: Refactor to not take lua properties to allow for unit tests
BlockModel BlockModel::from_lua_def(sol::table model, sol::table textures, TextureAtlas& atlas, bool visible, bool culls) {
BlockModel BlockModel::create(sol::table model, std::vector<std::string> textures, TextureAtlas& atlas, bool visible, bool culls) {
BlockModel blockModel;
blockModel.culls = culls;
blockModel.visible = visible;
try {
blockModel.culls = culls;
blockModel.visible = visible;
for (auto meshPartPair : model) {
auto meshPart = (sol::table)meshPartPair.second;
model.for_each([&](sol::object key, sol::object value) {
if (!value.is<sol::table>()) throw "Meshpart is not a table";
sol::table meshPart = value.as<sol::table>();
std::string face = meshPart.get_or<std::string>("face", "nocull");
int tex = (int)meshPart.get_or<float>("tex", 0);
//This value is sanitized later
std::string face = meshPart.get_or<std::string>("face", "nocull");
auto pointsOptional = meshPart.get<sol::optional<sol::table>>("points");
if (!pointsOptional) break;
sol::table points = *pointsOptional;
//TODO: Validate that tex is greater than 0
int tex = (int)meshPart.get_or<float>("tex", 1);
int texturesLength = textures.size();
auto oPoints = meshPart.get<sol::optional<sol::table>>("points");
if (!oPoints) throw "Meshpart is missing points field";
sol::table points = *oPoints;
std::string texture = textures.get_or<std::string>(std::min(tex, texturesLength), "_missing");
int texturesLength = textures.size();
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
for (int i = 1; i <= points.size()/5; i++) {
int o = (i-1) * 5 + 1;
if (points.size() % 20 != 0) throw "Points array is not well formed. (Not a multiple of 20 values)";
glm::vec3 pos((float)points[o], (float)points[o+1], (float)points[o+2]);
glm::vec2 tex((float)points[o+3], (float)points[o+4]);
for (int i = 1; i <= points.size()/5; i++) {
int offset = (i - 1) * 5 + 1;
vertices.push_back({pos, {0, 0, 0}, tex, {0, 0}});
}
glm::vec3 pos(points[offset], points[offset + 1], points[offset + 2]);
glm::vec2 tex(points[offset + 3], points[offset + 4]);
int ind = 0;
for (int i = 1; i <= points.size()/20; i++) {
indices.push_back(ind);
indices.push_back(ind + 1);
indices.push_back(ind + 2);
indices.push_back(ind + 2);
indices.push_back(ind + 3);
indices.push_back(ind);
ind += 4;
}
vertices.push_back({pos, {0, 0, 0}, tex, {0, 0}});
}
auto mp = MeshPart(std::move(vertices), std::move(indices), texture, atlas);
int ind = 0;
for (int i = 1; i <= points.size() / 20; i++) {
indices.push_back(ind);
indices.push_back(ind + 1);
indices.push_back(ind + 2);
indices.push_back(ind + 2);
indices.push_back(ind + 3);
indices.push_back(ind);
ind += 4;
}
if (face == "top") blockModel.topFaces.push_back(mp);
else if (face == "bottom") blockModel.bottomFaces.push_back(mp);
else if (face == "left") blockModel.leftFaces.push_back(mp);
else if (face == "right") blockModel.rightFaces.push_back(mp);
else if (face == "front") blockModel.frontFaces.push_back(mp);
else if (face == "back") blockModel.backFaces.push_back(mp);
else if (face == "nocull") blockModel.noCulledFaces.push_back(mp);
MeshPart partObject(std::move(vertices), std::move(indices),
textures[std::min(tex - 1, (int)textures.size() - 1)], atlas);
if (face == "top") blockModel.topFaces.push_back(partObject);
else if (face == "bottom") blockModel.bottomFaces.push_back(partObject);
else if (face == "left") blockModel.leftFaces.push_back(partObject);
else if (face == "right") blockModel.rightFaces.push_back(partObject);
else if (face == "front") blockModel.frontFaces.push_back(partObject);
else if (face == "back") blockModel.backFaces.push_back(partObject);
else if (face == "nocull") blockModel.noCulledFaces.push_back(partObject);
else throw "Face value is not one of 'top', 'bottom', 'left', 'right', 'front', 'back', 'nocull'.";
});
}
catch (const std::string& e) {
std::cerr << "Exception on BlockModel constructor: " << e << std::endl;
}
return std::move(blockModel);

View File

@ -22,7 +22,7 @@ struct BlockModel {
bool culls;
bool visible;
static BlockModel from_lua_def(sol::table model, sol::table textures, TextureAtlas& atlas, bool visible, bool culls);
static BlockModel create(sol::table model, std::vector<std::string> textures, TextureAtlas& atlas, bool visible, bool culls);
};

View File

@ -157,7 +157,7 @@ Texture &TextureAtlas::getTexture() {
glm::vec4 TextureAtlas::getTextureUVs(std::string& name) {
if (!textures.count(name)) {
std::cerr << "Invalid texture name (at TextureAtlas.cpp line " << __LINE__ << "): " << name << std::endl;
return {0, 0, 0, 0};
return textures.at("_missing")->uv;
}
return textures.at(name)->uv;
}