Skip to content
Snippets Groups Projects
Commit d044ee3e authored by dg's avatar dg
Browse files

zones

git-svn-id: http://svn.net-core.org/repos/t-engine4@14 51575b47-30f0-44d4-a5cc-537603b46e54
parent 5eb93829
No related branches found
No related tags found
No related merge requests found
Showing
with 345 additions and 66 deletions
......@@ -7,7 +7,9 @@ module(..., package.seeall, class.inherit(Entity))
function _M:init(t)
t = t or {}
self.name = t.name
self.energy = { value=0, mod=1 }
self.energy = t.energy or { value=0, mod=1 }
self.energy.value = self.energy.value or 0
self.energy.mod = self.energy.mod or 0
Entity.init(self, t)
end
......
......@@ -36,3 +36,19 @@ function _M:check(prop, ...)
else return self[prop]
end
end
function _M:loadList(...)
local res = {}
for i, file in ipairs{...} do
local f, err = loadfile(file)
if err then error(err) end
local data = f()
for i, a in ipairs(data) do
local e = self.new(a)
res[#res+1] = e
if a.define_as then res[a.define_as] = e end
end
end
return res
end
......@@ -7,6 +7,9 @@ function _M:init(keyhandler)
self.w, self.h = core.display.size()
end
function _M:run()
end
function _M:setLevel(level)
self.level = level
end
......
......@@ -11,12 +11,14 @@ function _M:tick()
engine.Game.tick(self)
-- Give some energy to entities
for uid, e in pairs(self.level.entities) do
if e.energy and e.energy.value < self.energy_to_act then
e.energy.value = (e.energy.value or 0) + self.energy_per_tick * (e.energy.mod or 1)
-- print(e.uid, e.energy.value)
if e.energy.value >= self.energy_to_act and e.act then
e:act(self)
if self.level then
for uid, e in pairs(self.level.entities) do
if e.energy and e.energy.value < self.energy_to_act then
e.energy.value = (e.energy.value or 0) + self.energy_per_tick * (e.energy.mod or 1)
-- print(e.uid, e.energy.value)
if e.energy.value >= self.energy_to_act and e.act then
e:act(self)
end
end
end
end
......
require "engine.class"
local Entity = require "engine.Entity"
module(..., package.seeall, class.inherit(Entity))
function _M:init(t)
t = t or {}
self.name = t.name
Entity.init(self, t)
end
......@@ -159,3 +159,9 @@ function _M:liteAll(x, y, w, h)
self.lites(i, j, true)
end end
end
function _M:rememberAll(x, y, w, h)
for i = x, x + w - 1 do for j = y, y + h - 1 do
self.remembers(i, j, true)
end end
end
require "engine.class"
module(..., package.seeall, class.make)
function _M:init(map)
self.map = map
end
function _M:generate()
end
module(..., package.seeall, class.make)
-- Static method
-- Setup classes to use for entities
function _M:setup(t)
self.npc_class = require(t.npc_class)
self.grid_class = require(t.grid_class)
self.object_class = require(t.object_class)
end
function _M:init(short_name)
self.short_name = short_name
self:load()
self.levels = self.levels or {}
self.npc_list = self.npc_class:loadList("/data/zones/"..self.short_name.."/npcs.lua")
self.grid_list = self.grid_class:loadList("/data/zones/"..self.short_name.."/grids.lua")
self.object_list = self.object_class:loadList("/data/zones/"..self.short_name.."/objects.lua")
end
function _M:load()
local f, err = loadfile("/data/zones/"..self.short_name.."/zone.lua")
if err then error(err) end
local data = f()
end
......@@ -6,6 +6,7 @@ function make(c)
setmetatable(c, {__index=_M})
c.new = function(...)
local obj = {}
obj.__CLASSNAME = c._NAME
setmetatable(obj, {__index=c})
if obj.init then obj:init(...) end
return obj
......@@ -18,6 +19,7 @@ function inherit(base)
setmetatable(c, {__index=base})
c.new = function(...)
local obj = {}
obj.__CLASSNAME = c._NAME
setmetatable(obj, {__index=c})
if obj.init then obj:init(...) end
return obj
......@@ -26,17 +28,109 @@ function inherit(base)
end
end
function _M:clone(t, deep)
function _M:getClassName()
return self.__CLASSNAME
end
function _M:getClass()
return getmetatble(self).__index
end
local function clonerecurs(d)
local n = {}
for k, e in pairs(self) do
n[k] = e
for k, e in pairs(d) do
local nk, ne = k, e
if type(k) == "table" then nk = clonerecurs(k) end
if type(e) == "table" then ne = clonerecurs(e) end
n[nk] = ne
end
return n
end
--[[
local function cloneadd(dest, src)
for k, e in pairs(src) do
local nk, ne = k, e
if type(k) == "table" then nk = cloneadd(k) end
if type(e) == "table" then ne = cloneadd(e) end
dest[nk] = ne
end
end
]]
function _M:clone(t)
local n = clonerecurs(self)
if t then
for k, e in pairs(t) do
n[k] = e
end
-- error("cloning mutation not yet implemented")
-- cloneadd(n, t)
for k, e in pairs(t) do n[k] = e end
end
setmetatable(n, getmetatable(self))
if n.cloned then n:cloned(self) end
return n
end
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- LOAD & SAVE
-----------------------------------------------------------------------
-----------------------------------------------------------------------
local function basicSerialize(o)
if type(o) == "number" or type(o) == "boolean" then
return tostring(o)
elseif type(o) == "function" then
return string.format("%q", string.dump(o))
else -- assume it is a string
return string.format("%q", o)
end
end
local function serialize_data(outf, name, value, saved, filter)
saved = saved or {} -- initial value
outf(name, " = ")
if type(value) == "number" or type(value) == "string" or type(value) == "boolean" or type(value) == "function" then
outf(basicSerialize(value), "\n")
elseif type(value) == "table" then
if saved[value] then -- value already saved?
outf(saved[value], "\n") -- use its previous name
else
saved[value] = name -- save name for next time
outf("{}\n") -- create a new table
for k,v in pairs(value) do -- save its fields
if not filter[k] then
local fieldname = string.format("%s[%s]", name, basicSerialize(k))
serialize_data(outf, fieldname, v, saved, filter)
end
end
end
else
error("cannot save a " .. type(value) .. " ("..name..")")
end
end
local function serialize(data, filter)
local tbl = {}
local outf = function(...) for i,str in ipairs(arg) do table.insert(tbl, str) end end
serialize_data(outf, "data", data, nil, filter)
table.insert(tbl, "return data\n")
return tbl
end
local function deserialize(string)
local f = loadstring(string)
setfenv(f, {})
return f()
end
function _M:save(filter)
filter = filter or {}
filter.new = true
return table.concat(serialize(self, filter))
end
function load(str)
local obj = deserialize(str)
if obj then
setmetatable(obj, {__index=require(obj.__CLASSNAME)})
if obj.loaded then obj:loaded() end
end
return obj
end
require "engine.class"
require "engine.MapGenerator"
module(..., package.seeall, class.inherit(engine.MapGenerator))
function _M:init(map, splitzone, floor, wall)
engine.MapGenerator.init(self, map)
self.smallest = 100000
self.tree = {}
self.splitzone = splitzone
self.floor, self.wall = floor, wall
end
function _M:split(x, y, w, h)
local x1, y1, w1, h1
local x2, y2, w2, h2
local split, dir
if rng.chance(2) then
split = rng.range(w * self.splitzone[1], w * self.splitzone[2])
x1, y1, w1, h1 = x, y, split, h
x2, y2, w2, h2 = x + split, y, w - split, h
else
split = rng.range(h * self.splitzone[1], h * self.splitzone[2])
x1, y1, w1, h1 = x, y, w, split
x2, y2, w2, h2 = x, y + split, w, h - split
print(x1, y1, w1, h1)
print(x2, y2, w2, h2)
end
return {x1, y1, w1, h1}, {x2, y2, w2, h2}
end
function _M:fill(t)
for i = t[1], t[1] + t[3] - 1 do
for j = t[2], t[2] + t[4] - 1 do
if i == t[1] or i == t[1] + t[3] - 1 or j == t[2] or j == t[2] + t[4] - 1 then
self.map(i, j, engine.Map.TERRAIN, self.wall)
else
self.map(i, j, engine.Map.TERRAIN, self.floor)
end
end
end
end
function _M:roomSize(t)
return t[3] * t[4]
end
function _M:generate()
local process = { {0, 0, self.map.w, self.map.h} }
local rooms = {}
while #process > 0 do
local baser = table.remove(process, 1)
local r1, r2 = self:split(unpack(baser))
if self:roomSize(r1) <= 40 or self:roomSize(r2) <= 40 or r1[3] < 6 or r1[4] < 6 or r2[3] < 6 or r2[4] < 6 then
table.insert(rooms, r1)
table.insert(rooms, r2)
else
table.insert(process, r1)
table.insert(process, r2)
end
end
for i, r in ipairs(rooms) do
self:fill(r)
end
end
......@@ -14,7 +14,8 @@ key:addCommand(key._x, {"ctrl"}, function() os.exit() end)
key:addCommand(key._RETURN, {"alt"}, function() core.display.fullscreen() end)
-- Load the game module
local mod_def = loadfile("/tome/init.lua")
game = false
local mod_def = loadfile("/mod/init.lua")
if mod_def then
local mod = {}
setfenv(mod_def, mod)
......@@ -22,8 +23,9 @@ if mod_def then
if not mod.name or not mod.short_name or not mod.version or not mod.starter then os.exit() end
engine.Tiles.prefix = "/"..mod.short_name.."/data/gfx/"
require(mod.starter)
engine.Tiles.prefix = "/data/gfx/"
game = dofile("/"..mod.starter:gsub("%.", "/")..".lua")
game:run()
else
os.exit()
end
......@@ -3,29 +3,34 @@ require "engine.Actor"
module(..., package.seeall, class.inherit(engine.Actor))
function _M:init(game, t)
function _M:init(t)
t.block_move = _M.bumped
engine.Actor.init(self, t)
self.game = game
self.level = 1
self.life = 100
self.mana = 100
self.skills = {}
self.attacks = {}
engine.Actor.init(self, t)
end
self.tooltip = _M.tooltip
-- When saving, ignore some fields
function _M:save()
return engine.Actor.save(self, {game=true})
end
function _M:move(x, y, force)
local moved = false
if force or self.energy.value >= self.game.energy_to_act then
moved = engine.Actor.move(self, self.game.level.map, x, y)
if not force then self.energy.value = self.energy.value - self.game.energy_to_act end
if force or self.energy.value >= game.energy_to_act then
moved = engine.Actor.move(self, game.level.map, x, y)
if not force then self.energy.value = self.energy.value - game.energy_to_act end
end
return moved
end
function _M:bumped(x, y, e)
-- Dont bump yourself!
if e ~= self then
self.game.log("%s bumped into %s!", tostring(e.name), tostring(self.name))
if e and e ~= self then
game.log("%s bumped into %s!", tostring(e.name), tostring(self.name))
end
return true
end
......
......@@ -3,57 +3,56 @@ require "engine.GameTurnBased"
require "engine.KeyCommand"
require "engine.LogDisplay"
local Tooltip = require "engine.Tooltip"
local BST = require "engine.generator.BST"
local Zone = require "engine.Zone"
local Map = require "engine.Map"
local Level = require "engine.Level"
local Entity = require "engine.Entity"
local Player = require "tome.class.Player"
local NPC = require "tome.class.NPC"
local Grid = require "engine.Grid"
local Actor = require "mod.class.Actor"
local Player = require "mod.class.Player"
local NPC = require "mod.class.NPC"
module(..., package.seeall, class.inherit(engine.GameTurnBased))
function _M:init()
engine.GameTurnBased.init(self, engine.Key.current, 1000, 100)
end
function _M:run()
self:setupCommands()
Zone:setup{npc_class="mod.class.NPC", grid_class="engine.Grid", object_class="engine.Entity"}
self.zone = Zone.new("ancient_ruins")
self.tooltip = engine.Tooltip.new(nil, nil, {255,255,255}, {30,30,30})
self.log = engine.LogDisplay.new(self.w * 0.5, self.h * 0.20, nil, nil, nil, {255,255,255}, {30,30,30})
self.log("Welcome to #00FF00#Tales of Middle Earth!")
local map = Map.new(self.w, math.floor(self.h * 0.80), 16, 16)
-- map:liteAll(0, 0, map.w, map.h)
local floor = Entity.new{display='.', color_r=100, color_g=200, color_b=100, color_br=0, color_bg=50, color_bb=0}
local e1 = Entity.new{display='#', color_r=255, block_sight=true, block_move=true}
local e2 = Entity.new{display='#', color_g=255, block_sight=true, block_move=true}
local e3 = Entity.new{display='#', color_b=255, block_sight=true, block_move=true}
local e4 = e3:clone{color_r=255}
for i = 0, map.w-1 do for j = 0, map.h-1 do
map(i, j, 1, floor)
end end
map(8, 6, Map.TERRAIN, e4)
map(8, 7, Map.TERRAIN, e2)
map(8, 8, Map.TERRAIN, e3)
map(9, 6, Map.TERRAIN, e1)
map(9, 7, Map.TERRAIN, e2)
map(9, 8, Map.TERRAIN, e3)
map(10, 6, Map.TERRAIN, e1)
map(10, 7, Map.TERRAIN, e2)
map(10, 8, Map.TERRAIN, e3)
map:liteAll(0, 0, map.w, map.h)
map:rememberAll(0, 0, map.w, map.h)
local floor = self.zone.grid_list.GRASS
local wall = self.zone.grid_list.TREE
local generator = BST.new(map, {0.4, 0.6}, floor, wall)
generator:generate()
local level = Level.new(map)
self:setLevel(level)
self.player = Player.new(self, {name="player", image='player.png', display='@', color_r=230, color_g=230, color_b=230})
self.player = Player.new{name="player", image='player.png', display='@', color_r=230, color_g=230, color_b=230}
self.player:move(4, 3, true)
level:addEntity(self.player)
local m = NPC.new(self, {name="here be dragons", display='D', color_r=125, color_g=125, color_b=255})
m.energy.mod = 0.38
m:move(1, 3, true)
level:addEntity(m)
for i = 1, 5 do
-- local m = self.npc_list[rng.range(1, 2)]:clone()
-- level:addEntity(m)
-- local x, y = rng.range(0, map.w), rng.range(0, map.h)
-- while map:checkAllEntity(x, y, "block_move") do x, y = rng.range(0, map.w), rng.range(0, map.h) end
-- m:move(x, y, true)
end
-- Ok everything is good to go, activate the game in the engine!
self:setCurrent()
......
require "engine.class"
require "tome.class.Actor"
require "mod.class.Actor"
module(..., package.seeall, class.inherit(tome.class.Actor))
module(..., package.seeall, class.inherit(mod.class.Actor))
function _M:init(game, t)
tome.class.Actor.init(self, game, t)
function _M:init(t)
mod.class.Actor.init(self, t)
end
function _M:act()
......
require "engine.class"
require "tome.class.Actor"
require "mod.class.Actor"
module(..., package.seeall, class.inherit(tome.class.Actor))
module(..., package.seeall, class.inherit(mod.class.Actor))
function _M:init(game, t)
tome.class.Actor.init(self, game, t)
function _M:init(t)
mod.class.Actor.init(self, t)
end
function _M:move(x, y, force)
local moved = tome.class.Actor.move(self, x, y, force)
local moved = mod.class.Actor.move(self, x, y, force)
return moved
end
function _M:act()
self.game.paused = true
game.paused = true
end
return {
{
define_as = "GRASS",
name = "grass",
display = ".", color_g=255,
},
{
define_as = "TREE",
name = "tree",
display = "#", color_g=200,
block_move = true,
block_sight = true,
},
}
return {
{
name = "dragon of death",
display = "D", color_r=255,
life = 1000,
mana = 1000,
energy = { mod=0.8 },
},
{
name = "baby dragon",
display = "d", color_r=128,
life = 1000,
mana = 1000,
energy = { mod=0.3 },
},
}
\ No newline at end of file
return {
}
return {
}
name = "Tales of Middle Earth"
short_name = "tome"
version = {4,0,0}
starter = "tome.load"
starter = "mod.load"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment