From da4175cdcac7c12ad1cb59cd0072e63eba89813c Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 17 Nov 2009 23:08:53 +0000
Subject: [PATCH] luaaa

git-svn-id: http://svn.net-core.org/repos/t-engine4@4 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engine/Actor.lua             | 10 +++++++
 game/engine/Entity.lua            |  8 ++++--
 game/engine/Map.lua               |  8 +++---
 game/engine/class.lua             |  4 +--
 game/engine/init.lua              | 45 ++++++++-----------------------
 game/modules/tome/class/Actor.lua | 12 +++++++++
 game/modules/tome/init.lua        |  4 +++
 game/modules/tome/load.lua        | 30 +++++++++++++++++++++
 src/main.c                        |  1 +
 9 files changed, 81 insertions(+), 41 deletions(-)
 create mode 100644 game/modules/tome/class/Actor.lua
 create mode 100644 game/modules/tome/init.lua
 create mode 100644 game/modules/tome/load.lua

diff --git a/game/engine/Actor.lua b/game/engine/Actor.lua
index bb1c04d652..6f4087d9bf 100644
--- a/game/engine/Actor.lua
+++ b/game/engine/Actor.lua
@@ -4,10 +4,20 @@ local Map = require "engine.Map"
 
 module(..., package.seeall, class.inherit(Entity))
 
+function _M:init(t)
+	t = t or {}
+	self.name = t.name
+	Entity.init(self, t)
+end
+
 function _M:move(map, x, y)
 	if self.x and self.y then
 		map:remove(self.x, self.y, Map.ACTOR)
 	end
+	if x < 0 then x = 0 end
+	if x >= map.w then x = map.w - 1 end
+	if y < 0 then y = 0 end
+	if y >= map.h then y = map.h - 1 end
 	self.x, self.y = x, y
 	map(x, y, Map.ACTOR, self)
 end
diff --git a/game/engine/Entity.lua b/game/engine/Entity.lua
index 96b64d08ff..e5bae5244d 100644
--- a/game/engine/Entity.lua
+++ b/game/engine/Entity.lua
@@ -6,6 +6,7 @@ local next_uid = 1
 setmetatable(__uids, {__mode="v"})
 
 function _M:init(t)
+	print("entity init")
 	t = t or {}
 	self.uid = next_uid
 	__uids[self.uid] = self
@@ -15,6 +16,7 @@ function _M:init(t)
 	self.color_g = t.color_g or 0
 	self.color_b = t.color_b or 0
 	self.block_sight = t.block_sight
+	self.block_move = t.block_move
 
 	next_uid = next_uid + 1
 end
@@ -26,6 +28,8 @@ function _M:cloned()
 	next_uid = next_uid + 1
 end
 
-function _M:display()
-	return self.display, self.color_r, self.color_g, self.color_b
+function _M:check(prop, ...)
+	if type(self[prop]) == "function" then return self[prop](...)
+	else return self[prop]
+	end
 end
diff --git a/game/engine/Map.lua b/game/engine/Map.lua
index 2b9babcf96..910af5d77c 100644
--- a/game/engine/Map.lua
+++ b/game/engine/Map.lua
@@ -7,6 +7,8 @@ TERRAIN = 1
 OBJECT = 10
 ACTOR = 20
 
+displayOrder = { ACTOR, OBJECT, TERRAIN }
+
 function _M:init(w, h)
 	self.w, self.h = w, h
 	self.map = {}
@@ -46,10 +48,10 @@ function _M:display()
 	player:move(self, player.x+1, player.y)
 
 	for i = 0, self.w - 1 do for j = 0, self.h - 1 do
-		local e = self(i, j, TERRAIN)
+		local e, si = nil, 1
+		while not e and si <= #displayOrder do e = self(i, j, displayOrder[si]) si = si + 1 end
 		local z = i + j * self.w
 		if e then
---		print("grid", i, j, z, self.seens[z])
 			if self.seens[z] then
 				engine.display.char(e.display, i, j, e.color_r, e.color_g, e.color_b)
 			elseif self.remembers[z] then
@@ -68,7 +70,7 @@ end
 function _M:opaque(x, y)
 	if x < 0 or x >= self.w or y < 0 or y >= self.h then return false end
 	local e = self(x, y, TERRAIN)
-	if e and e.block_sight then return true end
+	if e and e:check("block_sight") then return true end
 end
 
 function _M:apply(x, y)
diff --git a/game/engine/class.lua b/game/engine/class.lua
index ed2f457a7a..3229280b27 100644
--- a/game/engine/class.lua
+++ b/game/engine/class.lua
@@ -3,9 +3,9 @@ module("class", package.seeall)
 local base = _G
 
 function make(c)
+	setmetatable(c, {__index=_M})
 	c.new = function(...)
 		local obj = {}
-		setmetatable(c, {__index=_M})
 		setmetatable(obj, {__index=c})
 		if obj.init then obj:init(...) end
 		return obj
@@ -15,9 +15,9 @@ end
 
 function inherit(base)
 	return function(c)
+		setmetatable(c, {__index=base})
 		c.new = function(...)
 			local obj = {}
-			setmetatable(c, {__index=base})
 			setmetatable(obj, {__index=c})
 			if obj.init then obj:init(...) end
 			return obj
diff --git a/game/engine/init.lua b/game/engine/init.lua
index b592b6c84b..751de46355 100644
--- a/game/engine/init.lua
+++ b/game/engine/init.lua
@@ -1,34 +1,11 @@
-local Map = require "engine.Map"
-local Entity = require "engine.Entity"
-local Actor = require "engine.Actor"
-
-map = Map.new(20, 20)
-
-local floor = Entity.new{display='#', color_r=100, color_g=100, color_b=100}
-local e1 = Entity.new{display='#', color_r=255, block_sight=true}
-local e2 = Entity.new{display='#', color_g=255, block_sight=true}
-local e3 = Entity.new{display='#', color_b=255, block_sight=true}
-local e4 = e3:clone{color_r=255}
-
-for i = 0, 19 do for j = 0, 19 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)
-
-player = Actor.new{display='#', color_r=125, color_g=125, color_b=0}
-player:move(map, 2, 3)
-
-map:setCurrent()
-
-print("map is ", map)
-
---dofile("/game/modules/tome/")
+local mod_def = loadfile("/tome/init.lua")
+if mod_def then
+	local mod = {}
+	setfenv(mod_def, mod)
+	mod_def()
+
+	if not mod.name or not mod.short_name or not mod.version or not mod.starter then os.exit() end
+	require(mod.starter)
+else
+	os.exit()
+end
diff --git a/game/modules/tome/class/Actor.lua b/game/modules/tome/class/Actor.lua
new file mode 100644
index 0000000000..71a59cb60d
--- /dev/null
+++ b/game/modules/tome/class/Actor.lua
@@ -0,0 +1,12 @@
+require "engine.class"
+require "engine.Actor"
+
+module(..., package.seeall, class.inherit(engine.Actor))
+
+function _M:init(t)
+	engine.Actor.init(self, t)
+end
+
+function _M:move(map, x, y)
+	engine.Actor.move(self, map, x, y)
+end
diff --git a/game/modules/tome/init.lua b/game/modules/tome/init.lua
new file mode 100644
index 0000000000..5cb83b42d4
--- /dev/null
+++ b/game/modules/tome/init.lua
@@ -0,0 +1,4 @@
+name = "Tales of Middle Earth"
+short_name = "tome"
+version = {4,0,0}
+starter = "tome.load"
diff --git a/game/modules/tome/load.lua b/game/modules/tome/load.lua
new file mode 100644
index 0000000000..55951483f5
--- /dev/null
+++ b/game/modules/tome/load.lua
@@ -0,0 +1,30 @@
+local Map = require "engine.Map"
+local Entity = require "engine.Entity"
+local Actor = require "tome.class.Actor"
+
+local map = Map.new(20, 20)
+
+local floor = Entity.new{display='#', color_r=100, color_g=100, color_b=100}
+local e1 = Entity.new{display='#', color_r=255, block_sight=true}
+local e2 = Entity.new{display='#', color_g=255, block_sight=true}
+local e3 = Entity.new{display='#', color_b=255, block_sight=true}
+local e4 = e3:clone{color_r=255}
+
+for i = 0, 19 do for j = 0, 19 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)
+
+player = Actor.new{name="player!", display='#', color_r=125, color_g=125, color_b=0}
+player:move(map, 2, 3)
+
+map:setCurrent()
diff --git a/src/main.c b/src/main.c
index 58af58b99b..9ddd1a1254 100644
--- a/src/main.c
+++ b/src/main.c
@@ -129,6 +129,7 @@ int main (int argc, char *argv[])
 {
 	PHYSFS_init(argv[0]);
 	PHYSFS_mount("game/", "/", 1);
+	PHYSFS_mount("game/modules/tome", "/tome", 1);
 
 	TTF_Init();
 
-- 
GitLab