-
-
Notifications
You must be signed in to change notification settings - Fork 638
/
bosslever.lua
281 lines (261 loc) · 7.4 KB
/
bosslever.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
---@class BossLever
---@field private name string
---@field private bossPosition Position
---@field private createBoss function
---@field private timeToFightAgain number
---@field private timeToDefeat number
---@field private timeAfterKill number
---@field private requiredLevel number
---@field private disabled boolean
---@field private onUseExtra function
---@field private _position Position
---@field private _uid number
---@field private _aid number
---@field private playerPositions {pos: Position, teleport: Position}[]
---@field private area {from: Position, to: Position}
---@field private monsters {name: string, pos: Position}[]
---@field private exitTeleporter Position
---@field private exit Position
---@field private encounter Encounter
---@field private timeoutEvent Event
---@field private testMode boolean
BossLever = {}
--[[
local config = {
boss = {
name = "Faceless Bane",
position = Position(33617, 32561, 13)
}
requiredLevel = 250,
timeToFightAgain = 10 * 60 * 60, -- In seconds
playerPositions = {
{ pos = Position(33638, 32562, 13), teleport = Position(33617, 32567, 13) },
{ pos = Position(33639, 32562, 13), teleport = Position(33617, 32567, 13) },
{ pos = Position(33640, 32562, 13), teleport = Position(33617, 32567, 13) },
{ pos = Position(33641, 32562, 13), teleport = Position(33617, 32567, 13) },
{ pos = Position(33642, 32562, 13), teleport = Position(33617, 32567, 13) },
},
specPos = {
from = Position(33607, 32553, 13),
to = Position(33627, 32570, 13)
},
onUseExtra = function(player)
player:teleportTo(Position(33618, 32523, 15))
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
end,
exit = Position(33618, 32523, 15),
}
]]
setmetatable(BossLever, {
---@param self BossLever
---@param config table
__call = function(self, config)
local boss = config.boss
if not boss then
error("BossLever: boss is required")
end
return setmetatable({
name = boss.name,
encounter = config.encounter,
bossPosition = boss.position,
timeToFightAgain = config.timeToFightAgain or configManager.getNumber(configKeys.BOSS_DEFAULT_TIME_TO_FIGHT_AGAIN),
timeToDefeat = config.timeToDefeat or configManager.getNumber(configKeys.BOSS_DEFAULT_TIME_TO_DEFEAT),
timeAfterKill = config.timeAfterKill or 0,
requiredLevel = config.requiredLevel or 0,
createBoss = boss.createFunction,
disabled = config.disabled,
playerPositions = config.playerPositions,
onUseExtra = config.onUseExtra or function() end,
exitTeleporter = config.exitTeleporter,
exit = config.exit,
area = config.specPos,
monsters = config.monsters or {},
testMode = config.testMode,
_position = nil,
_uid = nil,
_aid = nil,
}, { __index = BossLever })
end,
})
---@param self BossLever
---@param position Position
---@return BossLever
function BossLever:position(position)
self._position = position
return self
end
---@param self BossLever
---@param uid number
---@return BossLever
function BossLever:uid(uid)
self._uid = uid
return self
end
---@param self BossLever
---@param aid number
---@return BossLever
function BossLever:aid(aid)
self._aid = aid
return self
end
function BossLever:kvScope()
local mType = MonsterType(self.name)
if not mType then
error("BossLever: boss name is invalid")
end
return "boss.cooldown." .. toKey(tostring(mType:raceId()))
end
---@param self BossLever
---@param player Player
---@return number
function BossLever:lastEncounterTime(player)
if not player or self.testMode then
return 0
end
return player:getBossCooldown(self.name)
end
---@param self BossLever
---@param time number
---@return boolean
function BossLever:setLastEncounterTime(time)
local info = self.lever:getInfoPositions()
if not info then
logger.error("BossLever:setLastEncounterTime - lever:getInfoPositions() returned nil")
return false
end
for _, v in pairs(info) do
if v.creature then
local player = v.creature:getPlayer()
if player then
player:setBossCooldown(self.name, time)
end
end
end
return true
end
---@param player Player
---@return boolean
function BossLever:onUse(player)
local isParticipant = false
for _, v in ipairs(self.playerPositions) do
if Position(v.pos) == player:getPosition() then
isParticipant = true
end
end
if not isParticipant then
return false
end
if self.disabled then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The boss is temporarily disabled.")
return true
end
local zone = self:getZone()
if zone:countPlayers(IgnoredByMonsters) > 0 then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "There's already someone fighting with " .. self.name .. ".")
return true
end
self.lever = Lever()
local lever = self.lever
lever:setPositions(self.playerPositions)
lever:setCondition(function(creature)
if not creature or not creature:isPlayer() then
return true
end
if creature:getLevel() < self.requiredLevel then
creature:sendTextMessage(MESSAGE_EVENT_ADVANCE, "All the players need to be level " .. self.requiredLevel .. " or higher.")
return false
end
if not lever:canUseLever(player, self.name, self.timeToFightAgain / 60 / 60) then
return false
end
self.onUseExtra(creature)
return true
end)
lever:checkPositions()
if lever:checkConditions() then
zone:removeMonsters()
for _, monster in pairs(self.monsters) do
Game.createMonster(monster.name, monster.pos, true, true)
end
if self.createBoss then
if not self.createBoss() then
return true
end
elseif self.bossPosition then
logger.debug("BossLever:onUse - creating boss: {}", self.name)
local monster = Game.createMonster(self.name, self.bossPosition, true, true)
if not monster then
return true
end
monster:registerEvent("BossLeverOnDeath")
end
lever:teleportPlayers()
if self.encounter then
local encounter = Encounter(self.encounter)
encounter:reset()
encounter:start()
end
self:setLastEncounterTime(os.time() + self.timeToFightAgain)
if self.timeoutEvent then
stopEvent(self.timeoutEvent)
self.timeoutEvent = nil
end
self.timeoutEvent = addEvent(function(zone)
zone:refresh()
zone:removePlayers()
end, self.timeToDefeat * 1000, zone)
end
return true
end
---@param Zone
function BossLever:getZone()
return Zone("boss." .. toKey(self.name))
end
---@param self BossLever
---@return boolean
function BossLever:register()
local missingParams = {}
if not self.name then
table.insert(missingParams, "boss.name")
end
if not self.playerPositions then
table.insert(missingParams, "playerPositions")
end
if not self.area then
table.insert(missingParams, "specPos")
end
if not self.exit then
table.insert(missingParams, "exit")
end
if not self._position and not self._uid and not self._aid then
table.insert(missingParams, "position or uid or aid")
end
if #missingParams > 0 then
local name = self.name or "unknown"
logger.error("BossLever:register() - boss with name {} missing parameters: {}", name, table.concat(missingParams, ", "))
return false
end
local zone = self:getZone()
zone:addArea(self.area.from, self.area.to)
zone:blockFamiliars()
zone:setRemoveDestination(self.exit)
local action = Action()
action.onUse = function(player)
self:onUse(player)
end
if self._position then
action:position(self._position)
end
if self._uid then
action:uid(self._uid)
end
if self._aid then
action:aid(self._aid)
end
action:register()
BossLever[self.name] = self
if self.exitTeleporter then
SimpleTeleport(self.exitTeleporter, self.exit)
end
return true
end