forked from Farbod678/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Settlement.ttslua
152 lines (117 loc) · 5.32 KB
/
Settlement.ttslua
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
local Archive = require("Kdm/Archive")
local Check = require("Kdm/Util/Check")
local Expansion = require("Kdm/Expansion")
local log = require("Kdm/Log").ForModule("Settlement")
local Location = require("Kdm/Location")
local NamedObject = require("Kdm/NamedObject")
local Util = require("Kdm/Util/Util")
local Ui = require("Kdm/Ui")
---------------------------------------------------------------------------------------------------
local Settlement = {}
---------------------------------------------------------------------------------------------------
function Settlement.Init()
Settlement.gearbySettlementLocation = {}
for _, expansion in ipairs(Expansion.All()) do
for settlementLocation, gear in pairs(expansion.settlementLocationGear or {}) do
assert(Check(Settlement.gearbySettlementLocation[settlementLocation] == nil, "Settlement location %s already registered", settlementLocation))
log:Debugf("Adding %s -> %s", settlementLocation, gear)
Settlement.gearbySettlementLocation[settlementLocation] = gear
end
end
local ui = Ui.Create3d("SettlementBoard", NamedObject.Get("Settlement Board"), 0.61)
local width = -0.21765
local height = 0.644982
local createButton = function(name, x, y, onClick)
local topLeft = { x = x, y = y }
local bottomRight = { x = topLeft.x + width, y = topLeft.y + height }
ui:Button({ id = name, topLeft = topLeft, bottomRight = bottomRight, onClick = onClick })
end
local x = 7.031489
local y1 = 3.920273
local y3 = 5.323954
local dy = (y3 - y1) / 2
local names = { "Starting Gear", "Rare Gear", "Promo Gear" }
for i, name in ipairs(names) do
createButton("Reset "..name, x, y1 + (i - 1) * dy, function() Settlement.ResetGear(name, name) end)
end
local x1 = 5.764139
local x10 = -7.409930
local dx = (x10 - x1) / 9
local y1 = 1.971291
local y2 = 7.274027
local dy = y2 - y1
for col = 1, 10 do
for row = 1, 2 do
local i = (row - 1) * 10 + col
local name = string.format("Reset Settlement Location Gear %d", i)
createButton(name, x1 + (col - 1) * dx, y1 + (row - 1)* dy, function() Settlement.ResetSettlementLocationGear(i) end)
end
end
ui:ApplyToObject()
for i = 1, 20 do
local location = Location.Get("Settlement Location "..i)
location:AddDropHandler(function(object) Settlement.OnDrop(i, object) end)
location:AddPickUpHandler(function(object) Settlement.OnPickUp(i, object) end)
end
end
---------------------------------------------------------------------------------------------------
function Settlement.ResetGear(gear, locationName)
assert(Check.Str(gear))
local location = Location.Get(locationName)
log:Debugf("Resetting gear %s at %s", gear, locationName)
local blocking = location:RayClean({ types = { "Gear" } })
if #blocking > 0 then
log:Broadcastf("Something is blocking the gear slot. Please move the highlighted objects out of the way and try again.")
Util.HighlightAll(blocking)
return
end
Archive.Take({
name = gear,
type = "Gear",
location = location,
rotation = { x = 0, y = 180, z = 180 },
})
Archive.Clean()
end
---------------------------------------------------------------------------------------------------
function Settlement.ResetSettlementLocationGear(index)
assert(Check.Num(index))
local location = Location.Get(string.format("Settlement Location %d", index))
local object = location:FirstObject({ types = { "Settlement Locations" } })
if object == nil then
return log:Debugf("No settlement location in slot %d to reset gear for.", index)
end
log:Debugf("Found candidate settlement location [%s]%s", object.getGUID(), object.getName())
local gear = Settlement.gearbySettlementLocation[object.getName()]
if not gear then
return log:Broadcastf("%s doesn't have any gear. Maybe the gear you're looking for is in the 'Rare Gear' deck?", object.getName())
end
Settlement.ResetGear(gear, "Settlement Location Gear "..index)
end
---------------------------------------------------------------------------------------------------
function Settlement.OnDrop(index, object)
if object.getGMNotes() != "Settlement Locations" then
return
end
local gear = Settlement.gearbySettlementLocation[object.getName()]
if not gear then
return
end
log:Debugf("[%s]%s was dropped on settlement location %d to spawn gear %s", object.getGUID(), object.getName(), index, gear)
Settlement.ResetGear(gear, "Settlement Location Gear "..index)
end
---------------------------------------------------------------------------------------------------
function Settlement.OnPickUp(index, object)
if object.getGMNotes() != "Settlement Locations" then
return
end
if not Settlement.gearbySettlementLocation[object.getName()] then
return
end
log:Debugf("Settlement location [%s]%s removed from settlement location %d", object.getGUID(), object.getName(), index)
Location.Get("Settlement Location Gear "..index):RayClean({ types = { "Gear" } })
end
---------------------------------------------------------------------------------------------------
return {
Init = Settlement.Init,
}