-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
319 lines (262 loc) · 8.57 KB
/
init.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
--[[ variables and constants ]]--
local debugLog = false
local aliases = {
[0 ] = "rat" ,
[1 ] = "snake" ,
[2 ] = "pig" ,
[3 ] = "wolf" ,
[4 ] = "saladin" ,
[5 ] = "caliph" ,
[6 ] = "sultan" ,
[7 ] = "richard" ,
[8 ] = "frederick" ,
[9 ] = "phillip" ,
[10] = "wazir" ,
[11] = "emir" ,
[12] = "nizar" ,
[13] = "sheriff" ,
[14] = "marshal" ,
[15] = "abbot" ,
}
local aiWithCastleTable = {
[0 ] = false ,
[1 ] = false ,
[2 ] = false ,
[3 ] = false ,
[4 ] = false ,
[5 ] = false ,
[6 ] = false ,
[7 ] = false ,
[8 ] = false ,
[9 ] = false ,
[10] = false ,
[11] = false ,
[12] = false ,
[13] = false ,
[14] = false ,
[15] = false ,
}
local REPLACEMENTS = {}
--[[ init module ]]--
local init = false
local aivInitFuncAddr = core.AOBScan("83 ec 0c 53 55 56 57 8b f9 89", 0x400000)
local ptrGenerateAivFileStatCallPos = aivInitFuncAddr + 123
local ptrGenerateAivFileStatFunc = core.readInteger(ptrGenerateAivFileStatCallPos + 1) + ptrGenerateAivFileStatCallPos + 5
local ptrAIWithAvailableCastleArray = core.readInteger(aivInitFuncAddr + 179)
local ptrAivPerAIArray = nil
local ptrAivStatArray = nil
local ptrNumberOfAIsWithCastle = nil
local vanillaAIV = {
aiWithAvailableCastle = {}, -- unused
aivPerAIArray = {}, -- unused
aivStatArray = {},
numberOfAIsWithCastle = 0, -- unused
}
-- should only be called once
local aivInitFunc = nil
aivInitFunc = core.hookCode(function(this)
aivInitFunc(this)
ptrAivPerAIArray = this + 0x3f05c
ptrAivStatArray = this + 0x3da5c
ptrNumberOfAIsWithCastle = this + 0x3f0ac
-- save vanilla (NOTE: since it does not use the functions of the AI setters, this might be a bug source)
for i = 0, 15 do
local aiWithCastle = core.readInteger(ptrAIWithAvailableCastleArray + i * 4)
vanillaAIV.aiWithAvailableCastle[i] = aiWithCastle
if aiWithCastle > 0 then
aiWithCastleTable[aiWithCastle - 1] = true
end
vanillaAIV.aivPerAIArray[i] = core.readInteger(ptrAivPerAIArray + i * 4)
local aivStatIndexBase = i * 8
for j = 0, 7 do
local aivStatIndex = aivStatIndexBase + j
vanillaAIV.aivStatArray[aivStatIndex] = core.readInteger(ptrAivStatArray + aivStatIndex * 4)
end
end
vanillaAIV.numberOfAIsWithCastle = core.readInteger(ptrNumberOfAIsWithCastle)
init = true
end, aivInitFuncAddr, 1, 1, 7)
local generateAivFileStatFunc = core.exposeCode(ptrGenerateAivFileStatFunc, 2, 1)
local stringBuffer = core.allocate(1001)
--[[ functions ]]--
local function overwriteTooLong(overwrite)
if overwrite:len() > 1000 then
log(WARNING, "Path to long. Max length is 1000 chars. Can not set overwrite: " .. overwrite)
return true
end
return false
end
local function writeCString(address, str)
core.writeString(address, str)
core.writeByte(address + str:len(), 0)
end
local function validateAiAndCastleValues(ai, castle)
local aiIndex, aiName = nil
if type(ai) == "number" then
if aliases[ai] == nil then
error("Invalid ai argument: " .. ai)
end
aiIndex = ai
aiName = aliases[ai]
else
local ok = false
for index, name in pairs(aliases) do
local isAi = name == ai
if isAi then
ok = true
aiName = ai
aiIndex = index
end
end
if not ok then
error("invalid ai argument: " .. ai)
end
end
if type(castle) ~= "number" then
if tonumber(castle) == nil then
error("invalid castle argument: " .. castle)
end
castle = tonumber(castle)
end
if castle < 1 or castle > 8 then
error("invalid castle argument: out of bounds [1-8]: " .. tostring(castle))
end
return aiIndex, aiName, castle
end
local function createDefaultAIVPath(ai, castle)
return string.format("aiv\\%s%d.aiv", ai, castle)
end
local function getNumberOfCastlesPerAiPtr(aiIndex)
return ptrAivPerAIArray + aiIndex * 4
end
local function getCastleStatIndex(aiIndex, castleIndex)
return aiIndex * 8 + castleIndex - 1 -- because castle ptr is castleIndex - 1
end
local function getVanillaCastleStat(castleStatIndex)
return vanillaAIV.aivStatArray[castleStatIndex]
end
local function getCastleStatPtr(castleStatIndex)
return ptrAivStatArray + castleStatIndex * 4
end
local function doesCastleExist(castleStatPtr)
return core.readInteger(castleStatPtr) > -1
end
local function doesVanillaCastleExist(vanillaCastleValue)
return vanillaCastleValue > -1
end
local function setAIWithCastleActive(index, active)
if aiWithCastleTable[index] == nil or aiWithCastleTable[index] == active then
return -- no action needed
end
aiWithCastleTable[index] = active
local num = 0
for i = 0, 15 do
if aiWithCastleTable[i] then
core.writeInteger(ptrAIWithAvailableCastleArray + num * 4, i + 1)
num = num + 1
end
end
for i = num, 15 do
core.writeInteger(ptrAIWithAvailableCastleArray + num * 4, 0)
end
core.writeInteger(ptrNumberOfAIsWithCastle, num)
end
local function setAivForAi(ai, castle, newFileName)
local aiIndex, aiName, castleIndex = validateAiAndCastleValues(ai, castle)
local defaultAiPath = createDefaultAIVPath(aiName, castleIndex)
local castleStatIndex = getCastleStatIndex(aiIndex, castleIndex)
local castleStatPtr = getCastleStatPtr(castleStatIndex)
local currentPresent = doesCastleExist(castleStatPtr)
local newCastleStat = nil
local setToExistingCastle = nil
if newFileName == "" then -- used for disabling castle
setToExistingCastle = false
newCastleStat = -1
elseif newFileName == nil then -- resets
newCastleStat = getVanillaCastleStat(castleStatIndex)
setToExistingCastle = doesVanillaCastleExist(newCastleStat)
else
if overwriteTooLong(newFileName) then
return
end
writeCString(stringBuffer, newFileName)
newCastleStat = generateAivFileStatFunc(0, stringBuffer) -- does not use this ptr
if newCastleStat < 0 then
log(WARNING, string.format("Unable to set castle for AI '%s': Castle file not loadable.", aiName))
return
end
setToExistingCastle = true
end
local castlePresenceChanged = setToExistingCastle ~= currentPresent
REPLACEMENTS[defaultAiPath] = newFileName
core.writeInteger(castleStatPtr, newCastleStat)
if not castlePresenceChanged then
return -- nothing else to change
end
local numberOfCastlesPtr = getNumberOfCastlesPerAiPtr(aiIndex)
local currentNumberOfThisAIsCastles = core.readInteger(numberOfCastlesPtr)
if newCastleStat < 0 then
currentNumberOfThisAIsCastles = currentNumberOfThisAIsCastles - 1
else
currentNumberOfThisAIsCastles = currentNumberOfThisAIsCastles + 1
end
core.writeInteger(numberOfCastlesPtr, currentNumberOfThisAIsCastles)
setAIWithCastleActive(aiIndex, currentNumberOfThisAIsCastles > 0)
end
local function isInit()
if not init then
log(WARNING, "aivloader not initialized yet. Ignoring request.")
end
return init
end
return {
enable = function(self, config)
debugLog = config.debugLog == true
modules.files:registerOverrideFunction(function(fileName)
if fileName:match("aiv\\.+.aiv$") then
if REPLACEMENTS[fileName] ~= nil then
if debugLog then
log(DEBUG, string.format("Processing AIV override for: '%s'", fileName))
log(DEBUG, string.format("\t replacement: '%s'", REPLACEMENTS[fileName]))
end
return REPLACEMENTS[fileName]
end
end
return nil
end)
hooks.registerHookCallback("afterInit", function()
for ai, v in pairs(config) do
for castle, fileName in pairs(v) do
setAivForAi(ai, castle, fileName)
end
end
end)
end,
disable = function(self, config)
end,
-- ReadOnlyTable is preventing this from being prettier (we cannot expose 'api' because it becomes frozen)
setAIVFileForAI = function(self, ai, castle, fileName)
if not isInit() then return end
setAivForAi(ai, castle, fileName)
end,
disableAIVForAi = function(self, ai, castle)
if not isInit() then return end
setAivForAi(ai, castle, "")
end,
resetAIVForAi = function(self, ai, castle)
if not isInit() then return end
setAivForAi(ai, castle, nil)
end,
resetAllAIVForAi = function(self, ai)
if not isInit() then return end
for i = 1, 8 do
setAivForAi(ai, i, nil)
end
end,
setMultipleAIVForAi = function(self, ai, castleToPathTable)
if not isInit() then return end
for castleIndex, filePath in pairs(castleToPathTable) do
setAivForAi(ai, castleIndex, filePath)
end
end,
}