-
-
Notifications
You must be signed in to change notification settings - Fork 664
/
Copy pathhazard.lua
209 lines (179 loc) · 5.06 KB
/
hazard.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
Hazard = {
areas = {},
}
function Hazard.new(prototype)
local instance = {}
instance.name = prototype.name
instance.from = prototype.from
instance.to = prototype.to
instance.minLevel = prototype.minLevel or 1
instance.maxLevel = prototype.maxLevel
instance.storageMax = prototype.storageMax ---@deprecated
instance.storageCurrent = prototype.storageCurrent ---@deprecated
instance.crit = prototype.crit
instance.dodge = prototype.dodge
instance.damageBoost = prototype.damageBoost
instance.defenseBoost = prototype.defenseBoost
instance.zone = Zone(instance.name)
if instance.from and instance.to then
instance.zone:addArea(instance.from, instance.to)
end
setmetatable(instance, { __index = Hazard })
return instance
end
function Hazard:getHazardPlayerAndPoints(damageMap)
local hazardPlayer = nil
local hazardPoints = -1
for key, _ in pairs(damageMap) do
local player = Player(key)
if player then
local playerHazardPoints = self:getPlayerCurrentLevel(player)
if playerHazardPoints < hazardPoints or hazardPoints == -1 then
hazardPlayer = player
hazardPoints = playerHazardPoints
end
end
end
if hazardPoints == -1 then
hazardPoints = self.minLevel
end
return hazardPlayer, hazardPoints
end
function Hazard:getCurrentLevel(players)
local hazardPlayer = nil
local hazardPoints = -1
for _, player in ipairs(players) do
local playerHazardPoints = self:getPlayerCurrentLevel(player)
if playerHazardPoints < hazardPoints or hazardPoints == -1 then
hazardPlayer = player
hazardPoints = playerHazardPoints
end
end
if hazardPoints == -1 then
hazardPoints = self.minLevel
end
return hazardPoints
end
function Hazard:getPlayerCurrentLevel(player)
if self.storageCurrent then
local fromStorage = player:getStorageValue(self.storageCurrent)
return fromStorage <= 0 and self.minLevel or fromStorage
end
local fromKV = player:kv():scoped(self.name):get("current-level") or self.minLevel
return fromKV <= 0 and self.minLevel or fromKV
end
function Hazard:getPlayerMaxLevelEver(player)
local fromKV = player:kv():scoped(self.name):get("max-level-set") or self.minLevel
return fromKV <= 0 and self.minLevel or fromKV
end
function Hazard:setPlayerCurrentLevel(player, level)
local max = self:getPlayerMaxLevel(player)
if level > max then
return false
end
if self.storageCurrent then
player:setStorageValue(self.storageCurrent, level)
else
player:kv():scoped(self.name):set("current-level", level)
local maxEver = player:kv():scoped(self.name):get("max-level-set") or self.minLevel
if level > maxEver then
player:kv():scoped(self.name):set("max-level-set", level)
end
end
local zones = player:getZones()
if not zones then
return true
end
player:updateHazard()
return true
end
function Hazard:getPlayerMaxLevel(player)
if self.storageMax then
local fromStorage = player:getStorageValue(self.storageMax)
return fromStorage <= 0 and self.minLevel or fromStorage
end
local fromKV = player:kv():scoped(self.name):get("max-level") or self.minLevel
return fromKV <= 0 and self.minLevel or fromKV
end
function Hazard:levelUp(player)
local current = self:getPlayerCurrentLevel(player)
local max = self:getPlayerMaxLevel(player)
if current == max then
self:setPlayerMaxLevel(player, max + 1)
end
end
function Hazard:setPlayerMaxLevel(player, level)
if level > self.maxLevel then
level = self.maxLevel
end
if self.storageMax then
player:setStorageValue(self.storageMax, level)
return
end
player:kv():scoped(self.name):set("max-level", level)
end
function Hazard:isInZone(position)
local zones = position:getZones()
if not zones then
return false
end
for _, zone in ipairs(zones) do
local hazard = Hazard.getByName(zone:getName())
if hazard then
return hazard == self
end
end
return false
end
function Hazard:register()
if not configManager.getBoolean(configKeys.TOGGLE_HAZARDSYSTEM) then
return
end
local event = ZoneEvent(self.zone)
function event.afterEnter(zone, creature)
local player = creature:getPlayer()
if not player then
return
end
logger.debug("Player {} entered hazard zone {}", player:getName(), zone:getName())
player:setHazardSystemPoints(self:getPlayerCurrentLevel(player))
end
function event.afterLeave(zone, creature)
local player = creature:getPlayer()
if not player then
return
end
player:setHazardSystemPoints(0)
end
Hazard.areas[self.name] = self
event:register()
end
function Hazard.getByName(name)
return Hazard.areas[name]
end
if not HazardMonster then
HazardMonster = { eventName = "HazardMonster" }
end
function HazardMonster.onSpawn(monster, position)
local monsterType = monster:getType()
if not monsterType then
return false
end
local zones = position:getZones()
if not zones then
return true
end
for _, zone in ipairs(zones) do
local hazard = Hazard.getByName(zone:getName())
if hazard then
monster:hazard(true)
if hazard then
monster:hazardCrit(hazard.crit)
monster:hazardDodge(hazard.dodge)
monster:hazardDamageBoost(hazard.damageBoost)
monster:hazardDefenseBoost(hazard.defenseBoost)
end
end
end
return true
end