Encounter.lua
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- ToME - Tales of Maj'Eyal
-- Copyright (C) 2009, 2010, 2011 Nicolas Casalini
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
--
-- Nicolas Casalini "DarkGod"
-- darkgod@te4.org
require "engine.class"
require "engine.Entity"
module(..., package.seeall, class.inherit(engine.Entity))
function _M:init(t, no_default)
assert(t.on_encounter, "no encounter on_encounter")
engine.Entity.init(self, t, no_default)
if self.coords then self:parseCoords() end
end
function _M:parseCoords()
self.on_map = {}
for i, coord in ipairs(self.coords) do
if coord.likelymap then
for y, line in ipairs(coord.likelymap) do
local i = 1
for c in line:gmatch(".") do
if c ~= ' ' then
self.on_map[(coord.x+i-1).."x"..(coord.y+y-1)] = tonumber(c)
print("coords", (coord.x+i-1).."x"..(coord.y+y-1), tonumber(c))
end
i = i + 1
end
end
elseif coord.w and coord.h then
for y = 1, coord.h do
for i = 1, coord.w do
self.on_map[(coord.x+i-1).."x"..(coord.y+y-1)] = 1
end
end
end
end
end
function _M:checkFilter(filter)
if self.special_filter and not self.special_filter(self) then return false end
if filter.mapx and filter.mapy and self.on_map then
if not self.on_map[filter.mapx.."x"..filter.mapy] then return false end
end
if filter.mapx and filter.mapy and self.on_world_encounter then
local we = game.level.map.attrs(filter.mapx, filter.mapy, "world-encounter")
if not we or not we[self.on_world_encounter] then return false end
end
if self.min_level and game.player.level < self.min_level then return false end
return true
end
function _M:findSpotGeneric(who, fct)
local spots = {}
for i = -1, 1 do for j = -1, 1 do if i ~= 0 or j ~= 0 then
if fct(game.level.map, who.x + i, who.y + j) then
spots[#spots+1] = {who.x + i, who.y + j}
end
end end end
if #spots > 0 then
local s = rng.table(spots)
return s[1], s[2]
end
end
function _M:findSpot(who, what)
what = what or "block_move"
local spots = {}
for i = -1, 1 do for j = -1, 1 do if i ~= 0 or j ~= 0 then
if not game.level.map:checkAllEntities(who.x + i, who.y + j, what, who) and game.level.map:checkAllEntities(who.x + i, who.y + j, "can_encounter", who) and not game.level.map:checkAllEntities(who.x + i, who.y + j, "change_level") then
spots[#spots+1] = {who.x + i, who.y + j}
end
end end end
if #spots > 0 then
local s = rng.table(spots)
return s[1], s[2]
end
end