-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKatiaBuildUtils.lua
454 lines (419 loc) · 11.9 KB
/
KatiaBuildUtils.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
local MAJOR, MINOR = "Module:KatiaBuildUtils-1.0", 1
local APkg = Apollo.GetPackage(MAJOR)
if APkg and (APkg.nVersion or 0) >= MINOR then
return -- no upgrade needed
end
local KatiaBuildUtils = APkg and APkg.tPackage or {}
local _ENV = nil -- blocking globals in Lua 5.2
KatiaBuildUtils.null = setmetatable ({}, {
__toinn = function () return "null" end
})
local Batcher = Apollo.GetPackage("Module:KatiaBatcher-1.0").tPackage
-- Display screen message to user
local function FloatText(strMessage)
local tTextOption = {
strFontFace = "CRB_FloaterLarge",
fDuration = 3.5,
fScale = 1,
fExpand = 1,
fVibrate = 0,
fSpinAroundRadius = 0,
fFadeInDuration = 0.2,
fFadeOutDuration = 0.5,
fVelocityDirection = 0,
fVelocityMagnitude = 0,
fAccelDirection = 0,
fAccelMagnitude = 0,
fEndHoldDuration = 1,
eLocation = CombatFloater.CodeEnumFloaterLocation.Bottom,
fOffsetDirection = 0,
fOffset = 0,
eCollisionMode = CombatFloater.CodeEnumFloaterCollisionMode.Horizontal,
fExpandCollisionBoxWidth = 1,
fExpandCollisionBoxHeight = 1,
nColor = 0xFF0000,
iUseDigitSpriteSet = nil,
bUseScreenPos = true,
bShowOnTop = true,
fRotation = 0,
fDelay = 0,
nDigitSpriteSpacing = 0,
}
CombatFloater.ShowTextFloater(GameLib.GetControlledUnit(), strMessage, tTextOption)
end
-- Check if all strings in matchers are in check
local function CheckMatchers(check, matchers)
for _,i in pairs(matchers) do
if string.find(string.lower(check), string.lower(i)) == nil then
return false
end
end
return true
end
function KatiaBuildUtils:OnLoad()
self.xmlDoc = XmlDoc.CreateFromFile("KatiaBuildUtils.xml")
self.xmlDoc:RegisterCallback("OnDocLoaded", self)
end
function KatiaBuildUtils:OnDocLoaded()
if self.xmlDoc ~= nil and self.xmlDoc:IsLoaded() then
self.wndBack = Apollo.LoadForm(self.xmlDoc, "Background", "InWorldHudStratum", self)
if self.wndBack == nil then
Apollo.AddAddonErrorText(self, "Could not load the background window for some reason.")
return
end
end
end
-- Save the info of a decor item into a pack
function KatiaBuildUtils:DecorToPack(decor)
local tDecorInfo = decor:GetDecorIconInfo()
if tDecorInfo == nil then
return nil
end
local newPack = {}
newPack.N = decor:GetName()
newPack.I = decor:GetDecorInfoId()
newPack.X = tDecorInfo.fWorldPosX
newPack.Y = tDecorInfo.fWorldPosY
newPack.Z = tDecorInfo.fWorldPosZ
newPack.S = tDecorInfo.fScaleCurrent
newPack.P = tDecorInfo.fPitch
newPack.R = tDecorInfo.fRoll
newPack.Yaw = tDecorInfo.fYaw
newPack.C = decor:GetSavedDecorColor()
return newPack
end
-- Preview a pack from crate or vendor
function KatiaBuildUtils:ClonePack(pack, neutralize)
local colorMatch
if neutralize or not pack.C then
colorMatch = 0
else
colorMatch = pack.C
end
-- resolve matchers if necessary
if pack.matchers ~= nil then
local tDecorCrateList = HousingLib.GetResidence():GetDecorCrateList()
if tDecorCrateList ~= nil then
for idx = 1, #tDecorCrateList do
local tCratedDecorData = tDecorCrateList[idx]
if (pack.N == nil or string.len(pack.N) > string.len(tCratedDecorData.strName)) and
CheckMatchers(tCratedDecorData.strName, pack.matchers) then
pack.N = tCratedDecorData.strName
end
end
end
local tDecorVendorList = HousingLib.GetDecorCatalogList()
if tDecorVendorList ~= nil then
for idx = 1, #tDecorVendorList do
local tVendorDecorData = tDecorVendorList[idx]
if (pack.N == nil or string.len(pack.N) > string.len(tVendorDecorData.strName)) and
CheckMatchers(tVendorDecorData.strName, pack.matchers) then
pack.N = tVendorDecorData.strName
end
end
end
end
-- Check crate first
local foundMismatch = false
local tDecorCrateList = HousingLib.GetResidence():GetDecorCrateList()
if tDecorCrateList ~= nil then
for idx = 1, #tDecorCrateList do
local tCratedDecorData = tDecorCrateList[idx]
if pack.I ~= nil and tCratedDecorData.nId == pack.I or pack.N ~= nil and tCratedDecorData.strName == pack.N then
if pack.I == nil then pack.I = tCratedDecorData.nId end
for _, specific in pairs(tCratedDecorData.tDecorItems) do
if colorMatch == nil or specific.idColorShift == colorMatch then
local dec = HousingLib.PreviewCrateDecorAtLocation(
specific.nDecorId,
specific.nDecorIdHi,
pack.X, pack.Y, pack.Z,
pack.P, pack.R, pack.Yaw,
pack.S)
local tDecorInfo = dec:GetDecorIconInfo()
local newX = tDecorInfo.fWorldPosX
local newY = tDecorInfo.fWorldPosY
local newZ = tDecorInfo.fWorldPosZ
-- Deshift if necessary
dec = HousingLib.PreviewCrateDecorAtLocation(
specific.nDecorId,
specific.nDecorIdHi,
2*pack.X - newX, 2*pack.Y - newY, 2*pack.Z - newZ,
pack.P, pack.R, pack.Yaw,
pack.S)
return dec
else
foundMismatch = true
end
end
end
end
end
-- Go to vendor otherwise
if colorMatch == 0 or colorMatch == nil then
local decorId = pack.I
if decorId == nil then
local tDecorVendorList = HousingLib.GetDecorCatalogList()
if tDecorVendorList ~= nil then
for idx = 1, #tDecorVendorList do
local tVendorDecorData = tDecorVendorList[idx]
if pack.N ~= nil and tVendorDecorData.strName == pack.N then
decorId = tVendorDecorData.nId
pack.I = decorId
end
end
end
end
if decorId ~= nil then
local dec = HousingLib.PreviewVendorDecorAtLocation(decorId,
pack.X, pack.Y, pack.Z,
pack.P, pack.R, pack.Yaw,
pack.S)
local tDecorInfo = dec:GetDecorIconInfo()
local newX = tDecorInfo.fWorldPosX
local newY = tDecorInfo.fWorldPosY
local newZ = tDecorInfo.fWorldPosZ
-- Deshift if necessary
dec = HousingLib.PreviewVendorDecorAtLocation(decorId,
2*pack.X - newX, 2*pack.Y - newY, 2*pack.Z - newZ,
pack.P, pack.R, pack.Yaw,
pack.S)
return dec
end
end
if foundMismatch then
FloatText(string.format("No matching colors found for %s", pack.N))
elseif pack.N ~= nil then
FloatText(string.format("%s not available in crate or vendor", pack.N))
else
FloatText("No matches found")
end
return nil
end
function KatiaBuildUtils:findDecorInCrate(id, color)
local tDecorCrateList = HousingLib.GetResidence():GetDecorCrateList()
for _, v in ipairs(tDecorCrateList) do
if(id ~= nil and v.nId == id) then
if colorMatch == nil or v.idColorShift == colorMatch then
Print(self:dumpTable(v))
return v.nDecorId, v.nDecorIdHi
end
end
end
return nil, nil
end
function KatiaBuildUtils:makeShoppingList(set, colorMatch, ignoreDefaultColor)
local list = {}
for _, v in ipairs(set) do
color = 0
if(colorMatch and v.C ~= nil) then
color = v.C
end
if(not(color == 0 and ignoreDefaultColor)) then
if(list[v.I] == nil) then
list[v.I] = {}
end
local li = list[v.I]
if(li[color] == nil) then
li[color] = 0
end
li[color] = li[color] + 1
end
end
local tDecorCrateList = HousingLib.GetResidence():GetDecorCrateList()
for _, v in ipairs(tDecorCrateList) do
local ld = list[v.nId]
if (ld ~= nil) then
for _, v in ipairs(v.tDecorItems) do
if(ld[v.idColorShift] ~= nil) then
ld[v.idColorShift] = ld[v.idColorShift] - 1
if(ld[v.idColorShift] <= 0) then
ld[v.idColorShift] = nil
end
end
end
end
end
return list
end
function KatiaBuildUtils:flattenShoppingList(list)
flat = {}
for id, clist in pairs(list) do
for col, amount in pairs(clist) do
entry = {}
entry.id = id
entry.col = col
entry.amount = amount
table.insert(flat, entry)
end
end
return flat
end
function KatiaBuildUtils:printShoppingList(set, colorMatch, ignoreDefaultColor)
local list = self:makeShoppingList(set, colorMatch, ignoreDefaultColor)
Print(self:dumpTable(list))
end
function KatiaBuildUtils:dumpTable(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. self:dumpTable(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
-- Conversion between player coordinates and decor coordinates
local world_offsets = {
[1136] = { -- non community housing plot
x = 1472,
y = -715,
z = 1440
},
[1265] = { -- community common area
x = 528,
y = -715,
z = -464
},
-- community plots below
[1161] = {
x = 352,
y = -715,
z = -640
},
[1162] = {
x = 640,
y = -715,
z = -640
},
[1163] = {
x = 224,
y = -715,
z = -256
},
[1164] = {
x = 512,
y = -715,
z = -256
},
[1165] = {
x = 800,
y = -715,
z = -256
},
}
function KatiaBuildUtils:DecorToPlayerPos(x, y, z)
local offsets = world_offsets[GameLib.GetCurrentZoneId()]
local result = {}
result.x = z + offsets.x
result.y = y + offsets.y
result.z = (-1 * x) + offsets.z
return result
end
function KatiaBuildUtils:PlayerToDecorPos(tPos)
local offsets = world_offsets[GameLib.GetCurrentZoneId()]
local result = {}
result.X = -1 * (tPos.z - offsets.z)
result.Y = tPos.y - offsets.y
result.Z = tPos.x - offsets.x
return result
end
-- Pull a matching item from crate/vendor and place it above player head
function KatiaBuildUtils:Summon(strArg)
local tPos = GameLib.GetPlayerUnit():GetPosition()
local dPos = self:PlayerToDecorPos(tPos)
dPos.Y = dPos.Y + 3
dPos.P = 0
dPos.R = 0
dPos.Yaw = 0
dPos.S = 1
dPos.matchers = {}
string.gsub(strArg, "([^ ]+)", function(c) table.insert(dPos.matchers, c) end)
local dec = self:ClonePack(dPos)
Event_FireGenericEvent("HousingFreePlaceDecorQuery", dec)
end
-- Round all stats to the nearest hundredth
function KatiaBuildUtils:Round()
local res = HousingLib.GetResidence()
if res == nil then return end
local selDecor = res:GetSelectedDecor()
if selDecor == nil then return end
local pack = self:DecorToPack(selDecor)
selDecor:SetPosition(ToHundredth(pack.X), ToHundredth(pack.Y), ToHundredth(pack.Z))
selDecor:SetRotation(ToHundredth(pack.P), ToHundredth(pack.R), ToHundredth(pack.Yaw))
selDecor:SetScale(ToHundredth(pack.S))
end
-- Draw a line to selected decor on screen
function KatiaBuildUtils:Locate()
local res = HousingLib.GetResidence()
if res == nil then return end
local selDecor = res:GetSelectedDecor()
if selDecor == nil then
self:FloatText("No decor selected")
return
end
local pack = self:DecorToPack(selDecor)
self.dPos = self:DecorToPlayerPos(pack.X, pack.Y, pack.Z)
self.count = 100
self.locateTimer = ApolloTimer.Create(0.03, true, "OnLocateTick", self)
end
function KatiaBuildUtils:OnLocateTick()
self.wndBack:DestroyAllPixies()
self.count = self.count - 1
if self.dPos == nil or self.count <= 0 then
self.locateTimer:Stop()
return
end
local player = GameLib.GetPlayerUnit()
if player == nil then
return
end
local pPos = player:GetPosition()
local pScreen = GameLib.WorldLocToScreenPoint(Vector3.New(pPos.x, pPos.y, pPos.z))
local dScreen = GameLib.WorldLocToScreenPoint(Vector3.New(self.dPos.x, self.dPos.y, self.dPos.z))
self.wndBack:AddPixie( {
bLine = true, fWidth = 2, cr = {a=1,r=1,g=0,b=0},
loc = { fPoints = { 0, 0, 0, 0 }, nOffsets = { dScreen.x, dScreen.y, pScreen.x, pScreen.y } }
} )
end
function KatiaBuildUtils:Place()
local selDecor = HousingLib.GetResidence():GetSelectedDecor()
if selDecor ~= nil then
selDecor:Place()
end
end
function KatiaBuildUtils:Crate()
local selDecor = HousingLib.GetResidence():GetSelectedDecor()
if selDecor ~= nil then
selDecor:Crate()
end
end
function KatiaBuildUtils:CrateLinkedSet()
local root = HousingLib.GetResidence():GetSelectedDecor()
if root == nil then
self:FloatText("No decor selected.")
return
end
local todo = Batcher:Prepare(
function (_, dec)
dec:Crate()
end,
nil,
nil
)
if todo == nil then return end
self:CratePlusChildren(root, todo)
Batcher:StartDecorDriven()
end
-- Recursive helper for crating linked sets
function KatiaBuildUtils:CratePlusChildren(node, todo)
local children = node:GetChildren()
if children ~= nil then
for i, j in pairs(node:GetChildren()) do
self:CratePlusChildren(j, todo)
end
end
table.insert(todo, node)
end
Apollo.RegisterPackage(KatiaBuildUtils, MAJOR, MINOR, {})