progress on /apps/demos/crawl

This commit is contained in:
veclavtalica 2025-02-20 22:20:02 +03:00
parent bd3b090f6f
commit 80a4ae3d0e
2 changed files with 36 additions and 18 deletions

View File

@ -1,24 +1,27 @@
@map
-- Grid of entities defined by @classes.
-- Grid of symbols defined by @classes section.
#####
#.@.#
#...#
##.##
#...#
#...#
##.######
#...X...#
#./.#####
#####
@classes
-- Defines classes under symbols, which could have properties.
-- Defines classes under symbols, which could have properties attached.
# wall
. void
test : this
. floor
@ player_spawn
unique :
face : south
hold : torch
X door
open_on : sg_torch0
/ torch
emit : sg_torch0
@meta
-- Arbitrary sections could be defined with values pairs.
description : Test Level! Just two square rooms.
-- Arbitrary sections could be defined with value pairs.
description : Test Level! Just two square rooms and a tunnel opened by lever.

View File

@ -4,10 +4,15 @@ function load_level(file)
local f = file_read { file = file }
local result = {
classes = {},
glossary = {},
classes = {
void = { },
},
glossary = {
[" "] = "void",
},
grid = {},
map = {},
size = { x = 0, y = 0 },
}
-- iterate over lines
@ -20,21 +25,23 @@ function load_level(file)
if #line == 0 or line:find("^%-%-%s*") then
goto skip
-- start new section
elseif line:find("^@%a+") then
elseif line:find("^@%g+") then
section = line:sub(2); subsection = "none"
-- decode map one line at a time
elseif section == "map" then
local l = #result.map + 1
if result.size.x < #line then
result.size.x = #line
end
result.map[l] = {}
for i = 1, #line do
result.map[l][i] = line:sub(i,i)
end
assert(#result.map[1] == #result.map[l])
-- templates to expand
elseif section == "classes" then
-- properties
if line:find("^ %a+") then
local _, _, property, value = line:find("^ (%a+)%s?:%s?(.*)")
if line:find("^ %g+") then
local _, _, property, value = line:find("^ (%g+)%s?:%s?(.*)")
result.classes[subsection][property] = value
goto skip
end
@ -45,7 +52,7 @@ function load_level(file)
result.glossary[symbol] = classname
subsection = classname
elseif section ~= "none" then
local _, _, property, value = line:find("^(%a+)%s?:%s?(.*)")
local _, _, property, value = line:find("^(%g+)%s?:%s?(.*)")
if result[section] == nil then
result[section] = {}
end
@ -58,15 +65,23 @@ function load_level(file)
-- post process
for y = 1, #result.map do
result.grid[y] = {}
for x = 1, #result.map[y] do
local class = result.classes[result.glossary[result.map[y][x]]]
for x = 1, result.size.x do
-- past defined for line
local symbol
if x > #result.map[y] then symbol = " "
else symbol = result.map[y][x]
end
local class = result.classes[result.glossary[symbol]]
if class["unique"] ~= nil then
class.position = { x = x, y = y }
end
result.grid[y][x] = class
::continue::
end
end
result.size.y = #result.map
print(result.meta.description)
return result