/
items.lua
115 lines (102 loc) · 2.84 KB
/
items.lua
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
local helper = wesnoth.require "helper"
local wml_actions = wesnoth.wml_actions
local scenario_items = {}
local next_item_name = 0
local function add_overlay(x, y, cfg)
wesnoth.add_tile_overlay(x, y, cfg)
local items = scenario_items[x * 10000 + y]
if not items then
items = {}
scenario_items[x * 10000 + y] = items
end
table.insert(items,
{
x = x, y = y,
image = cfg.image,
halo = cfg.halo,
team_name = cfg.team_name,
visible_in_fog = cfg.visible_in_fog,
redraw = cfg.redraw,
name = cfg.name
})
end
local function remove_overlay(x, y, name)
local items = scenario_items[x * 10000 + y]
if not items then return end
wesnoth.remove_tile_overlay(x, y, name)
if name then
for i = #items,1,-1 do
local item = items[i]
if item.image == name or item.halo == name or item.name == name then
table.remove(items, i)
end
end
end
if not name or #items == 0 then
scenario_items[x * 10000 + y] = nil
end
end
function wesnoth.persistent_tags.item.write(add)
for i,v in pairs(scenario_items) do
for j,w in ipairs(v) do
add(w)
end
end
end
function wesnoth.persistent_tags.next_item_name.write(add)
add{next_item_name = next_item_name}
end
function wesnoth.persistent_tags.item.read(cfg)
add_overlay(cfg.x, cfg.y, cfg)
end
function wesnoth.persistent_tags.next_item_name.read(cfg)
next_item_name = cfg.next_item_name or next_item_name
end
function wml_actions.item(cfg)
local locs = wesnoth.get_locations(cfg)
cfg = wml.parsed(cfg)
if not cfg.name then
cfg.name = "item_" .. tostring(next_item_name)
next_item_name = next_item_name + 1
end
if not cfg.image and not cfg.halo then
helper.wml_error "[item] missing required image= and halo= attributes."
end
for i, loc in ipairs(locs) do
add_overlay(loc[1], loc[2], cfg)
end
local redraw = cfg.redraw
if redraw == nil then redraw = true end
if redraw then wml_actions.redraw {} end
if cfg.write_name then wml.variables[cfg.write_name] = cfg.name end
return cfg.name
end
function wml_actions.remove_item(cfg)
local locs = wesnoth.get_locations(cfg)
for i, loc in ipairs(locs) do
remove_overlay(loc[1], loc[2], cfg.image)
end
end
function wml_actions.store_items(cfg)
local variable = cfg.variable or "items"
variable = tostring(variable or helper.wml_error("invalid variable= in [store_items]"))
wml.variables[variable] = nil
local index = 0
for i, loc in ipairs(wesnoth.get_locations(cfg)) do
local items = scenario_items[loc[1] * 10000 + loc[2]]
if items then
for j, item in ipairs(items) do
wml.variables[string.format("%s[%u]", variable, index)] = item
index = index + 1
end
end
end
end
local methods = { remove = remove_overlay }
function methods.place_image(x, y, name)
add_overlay(x, y, { x = x, y = y, image = name })
end
function methods.place_halo(x, y, name)
add_overlay(x, y, { x = x, y = y, halo = name })
end
return methods