File: erm.lua

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (252 lines) | stat: -rw-r--r-- 4,092 bytes parent folder | download | duplicates (2)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
DATA = DATA or {}
local DATA = DATA
local GAME = GAME

local TurnStarted = require("events.TurnStarted")

DATA.ERM = DATA.ERM or {}

local ERM_D = DATA.ERM

ERM_D.F = ERM_D.F or {}
ERM_D.MDATA = ERM_D.MDATA or {}
ERM_D.Q = ERM_D.Q or {}
ERM_D.v = ERM_D.v or {}
ERM_D.z = ERM_D.z or {}

local INT_MT =
{
	__index = function(t, key)
		return rawget(t, key) or 0
	end
}

local STR_MT =
{
	__index = function(t, key)
		return rawget(t, key) or ""
	end
}

setmetatable(DATA.ERM.Q, INT_MT)
setmetatable(DATA.ERM.v, INT_MT)
setmetatable(DATA.ERM.z, STR_MT)

local ERM =
{
	w = {},
	M = {},
	subs = {}
}

local ERM_MT =
{
	__index = DATA.ERM
}

setmetatable(ERM, ERM_MT)

local MACROS_MT =
{
	__index = function(M, key)
		address = rawget(ERM.MDATA, key)
		assert(address, "Macro "..key.." is not defined")
		return ERM[address.name][address.index]
	end,
	__newindex = function(M, key, value)
		address = rawget(ERM.MDATA, key)
		assert(address, "Macro "..key.." is not defined")
		ERM[address.name][address.index] = value
	end
}

setmetatable(ERM.M, MACROS_MT)

function ERM:addMacro(name, varName, varIndex)
	assert(varName == "v" or varName == "z", "Macro for "..varName.. "[...] is not allowed")
	rawset(self.MDATA, name, {name = varName, index = varIndex})
end

function ERM:getZVar(p1)
	if type(p1) == "number" then
		return self.z[tostring(p1)]
	else
		return p1
	end
end

local HERO_VAR_MT =
{
	__index = function(t, key)
		ERM_D.WDATA = ERM_D.WDATA or {}
		local wKey = ERM_D.wKey

		if not wKey then
			error("Hero variable set is not selected")
		end

		ERM_D.WDATA[wKey] = ERM_D.WDATA[wKey] or {}

		return ERM_D.WDATA[wKey][key] or 0
	end,
	__newindex = function(t, key, value)
		ERM_D.WDATA = ERM_D.WDATA or {}
		local wKey = ERM_D.wKey

		if not wKey then
			error("Hero variable set is not selected")
		end

		ERM_D.WDATA[wKey] = ERM_D.WDATA[wKey] or {}
		ERM_D.WDATA[wKey][key] = value
	end
}

setmetatable(ERM.w, HERO_VAR_MT)

local function dailyUpdate(event)
	ERM.Q.c = GAME:getDate(0)
end

table.insert(ERM.subs, TurnStarted.subscribeAfter(EVENT_BUS, dailyUpdate))

local Receivers = {}

local function createReceiverLoader(name)
	local loader = function(...)
		Receivers[name] = Receivers[name] or require("core:erm."..name)
		return Receivers[name]:new(ERM, ...)
	end
	return loader
end

--[[

AR
BA
BF
BG
BH
CA
CD
CE
CM
DL
CO
EA
EX
GE
HL
HO
HT
LE
MO
MR
MW
OB
PM
PO
QW
SS
TL
TR

]]

local supportedReceivers = {"BM", "BU", "DO", "FU", "HE", "IF", "DO", "MA", "MF", "OW", "TM", "UN", "VR"}

for _, v in pairs(supportedReceivers) do ERM[v] = createReceiverLoader(v) end

local Triggers = {}

local function createTriggerLoader(name)
	local loader = function(id_list, id_key)

		local t = Triggers[id_key]

		if not t then
			local p = require("core:erm."..name.."_T")
			t = p:new{ERM=ERM, id=id_list}
			Triggers[id_key] = t
		end

		return t
	end
	return loader
end

--[[
!?AE
!?BA
!?BF
!?BG
!?BR
!?CM client only?
!?CO
!?DL client only?
!?GE
!?HE
!?HL
!?HM
!?LE (!$LE)
!?MG
!?MM client only?
!?MR
!?MW
!?OB (!$OB)
!?SN client only?
!?SN (ERA)
!?TH client only?
!?TL client only? depends on time limit feature
]]

local supportedTriggers = {"PI", "FU", "GM", "MF", "OB", "TM"}

local TriggerLoaders = {}

for _, v in pairs(supportedTriggers) do TriggerLoaders[v] = createTriggerLoader(v) end

local function makeIdKey(name, id_list)
	if id_list == nil then
		id_list = {}
	elseif type(id_list) ~= "table" then
		id_list = {id_list}
	end

	return name .. table.concat(id_list, "|")
end

function ERM:addTrigger(t)
	local name = t.name
	local fn = t.fn
	local id_list = t.id or {}

	local id_key = makeIdKey(name, id_list)

	local trigger = TriggerLoaders[name](id_list, id_key)

	trigger:addHandler(fn)
end

function ERM:callFunction(id, x)
	local id_key = makeIdKey("FU", id)

	local t = Triggers[id_key]
	if t then
		t:call({}, x)
	end
end

function ERM:callInstructions(cb)
	if not DATA.ERM.instructionsCalled then
		cb()
		local id_key = makeIdKey("PI")
		local pi = Triggers[id_key]
		if pi then
			pi:call()
		end
		DATA.ERM.instructionsCalled = true
	end
end

return ERM