This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
/
weapon.lua
313 lines (260 loc) · 7.87 KB
/
weapon.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
-- TODO:
-- - Distance weapons hit chance formula
-- - Wand support
otstd.weapons = {}
Weapon = {}
Weapon_mt = {__index = Weapon}
function Weapon:new(weaponID)
local weapon = {
-- Default weapon params
id = weaponID,
vocation = "any",
level = 0,
magicLevel = 0,
mana = 0,
manaPercent = 0,
soul = 0,
exhaustion = false,
premium = false,
-- Combat params
combatType = COMBAT_PHYSICALDAMAGE,
blockedByDefense = true,
blockedByArmor = true,
-- Damage formula
damageFormula = nil,
-- Callbacks
onUseWeapon = nil
}
setmetatable(weapon, Weapon_mt)
return weapon
end
-- Formula to get weapon max damage based on player's level, skill and attack factor
function otstd.getWeaponMaxDamage(level, attackSkill, attackValue, attackFactor)
return math.ceil(2 * (attackValue * (attackSkill + 5.8) / 25 + level / 10 - 0.1) / attackFactor)
end
-- Default damage formula callback
function otstd.damageFormula(player, target, weapon)
local attackValue = weapon:getAttack()
if weapon:getWeaponType() == WEAPON_AMMO then
local bow = player:getWeapon(true)
if bow and bow:getAmmoType() == weapon:getAmmoType() then
attackValue = attackValue + bow:getAttack()
end
end
local maxDamage = otstd.getWeaponMaxDamage(player:getLevel(), player:getWeaponSkill(weapon), attackValue, player:getAttackFactor())
local minDamage = 0
if weapon:getWeaponType() == WEAPON_DIST or
weapon:getWeaponType() == WEAPON_AMMO then
if typeof(target, "Player") then
minDamage = math.ceil(player:getLevel() * 0.1)
else
minDamage = math.ceil(player:getLevel() * 0.2)
end
end
-- vocation multipliers
local vocation = player:getVocation()
if vocation then
local meleeBaseDamage = vocation:getMeleeBaseDamage(weapon:getWeaponType())
if meleeBaseDamage ~= 1.0 then
maxDamage = maxDamage * meleeBaseDamage
minDamage = minDamage * meleeBaseDamage
end
end
return -math.random(minDamage, maxDamage)
end
-- Default fist formula callback
function otstd.fistDamageFormula(player, target)
return -math.random(0, otstd.getWeaponMaxDamage(player:getLevel(), player:getSkill(SKILL_FIST), 7, player:getAttackFactor()))
end
function otstd.onUseWeapon(event)
local weapon = event.weapon
if not typeof(event.player, "Player") then
error("onUseWeapon not triggered by a Player!")
end
if not weapon then
event.target = event.attacked
otstd.internalUseFist(event)
else
local internalWeapon = otstd.weapons[weapon:getItemID()]
if not internalWeapon then
error("onUseWeapon event triggered with unknown weapon. (ItemID: " .. weapon:getItemID() .. ")")
end
event.internalWeapon = internalWeapon
event.target = event.attacked
event.damageModifier = 100
if otstd.onWeaponCheck(event) then
if internalWeapon and internalWeapon.onUseWeapon then
internalWeapon:onUseWeapon(event)
else
otstd.internalUseWeapon(event)
end
end
end
return true
end
function otstd.onUsedWeapon(event)
local player = event.player
local weapon = event.weapon
local internalWeapon = event.internalWeapon
-- Add skill points
if not player:cannotGainSkill() and
player:getAddAttackSkill() then
local skillType = nil
local weaponType = weapon:getWeaponType()
if weaponType == WEAPON_SWORD then
skillType = SKILL_SWORD
elseif weaponType == WEAPON_CLUB then
skillType = SKILL_CLUB
elseif weaponType == WEAPON_AXE then
skillType = skill_AXE
elseif weaponType == WEAPON_DIST or
weaponType == WEAPON_AMMO then
skillType = SKILL_DIST
end
local blockType = player:getLastAttackBlockType()
local skillPoints = 0
if skillType == SKILL_DIST and
blockType == BLOCK_NONE then
skillPoints = 2
elseif blockType == BLOCK_DEFENSE or
blockType == BLOCK_ARMOR or
blockType == BLOCK_NONE then
skillPoints = 1
end
player:advanceSkill(skillType, skillPoints)
end
if internalWeapon then
if not player:hasInfiniteMana() then
player:spendMana(internalWeapon.mana)
end
if not player:hasInfiniteSoul() then
player:removeSoul(internalWeapon.soul)
end
if not player:canGetExhausted() and
internalWeapon.exhausted then
player:addCombatExhaustion(config["fight_exhausted"])
end
end
if not player:cannotGainInFight() then
player:addInFight(config["in_fight_duration"])
end
return true
end
function otstd.onUsedFist(event)
local player = event.player
if not player:cannotGainSkill() and
player:getAddAttackSkill() then
player:advanceSkill(SKILL_FIST, 1)
end
end
function otstd.onWeaponCheck(event)
local player = event.player
local target = event.target
local weapon = event.weapon
local internalWeapon = event.internalWeapon
local shootRange = weapon:getShootRange()
-- If is a two-handed distance weapon we may check the ammo types
-- and get the bow range
if weapon:getWeaponType() == WEAPON_AMMO then
local bow = player:getWeapon(true)
if bow:getWeaponType() ~= WEAPON_DIST or
bow:getAmmoType() ~= weapon:getAmmoType() then
return false
end
shootRange = bow:getShootRange()
end
if not areInRange(player:getPosition(), target:getPosition(), shootRange) then
return false
end
if not internalWeapon then
-- No script-specific restrictions
return true
end
if not player:ignoreWeaponCheck() then
if (internalWeapon.premium and not player:isPremium()) or
(internalWeapon.mana > player:getMana()) or
(internalWeapon.soul > player:getSoulPoints()) or
(not checkVocation(player:getVocation(), internalWeapon.vocation)) then
return false
end
-- check if wielded properly
-- level
if internalWeapon.level > player:getLevel() then
local penalty = (internalWeapon.level - player:getLevel()) * 0.02
if penalty > 0.5 then
penalty = 0.5
end
event.damageModifier = event.damageModifier - (event.damageModifier * penalty)
end
-- magic level
if internalWeapon.magicLevel > player:getMagicLevel() then
event.damageModifier = event.damageModifier / 2
end
end
return true
end
function otstd.internalUseWeapon(event)
event:skip()
local player = event.player
local target = event.target
local weapon = event.weapon
local internalWeapon = event.internalWeapon
local damage = 0
if internalWeapon and internalWeapon.damageFormula then
damage = internalWeapon:damageFormula(player, target, weapon)
else
damage = otstd.damageFormula(player, target, weapon)
end
damage = (damage * event.damageModifier) / 100
if internalWeapon then
internalCastSpell(
internalWeapon.combatType, player, target, damage,
internalWeapon.blockedByShield, internalWeapon.blockedByArmor)
else
internalCastSpell(
COMBAT_PHYSICALDAMAGE, player, target, damage,
true, true)
end
-- send effect
local shootType = weapon:getShootType()
if shootType ~= SHOOT_EFFECT_NONE then
sendDistanceEffect(player:getPosition(), target:getPosition(), shootType)
end
-- call finish handler
if internalWeapon and internalWeapon.onUsedWeapon then
internalWeapon:onUsedWeapon(event)
else
otstd.onUsedWeapon(event)
end
end
function otstd.internalUseFist(event)
event:skip()
local player = event.player
local target = event.target
-- the target must be in range
if areInRange(player:getPosition(), target:getPosition(), 1) then
local damage = otstd.fistDamageFormula(player, target)
-- do the damage
internalCastSpell(COMBAT_PHYSICALDAMAGE, player, target, damage, true, true)
-- call finish handler
otstd.onUsedFist(event)
end
end
function Weapon:register()
self:unregister()
if otstd.weapons[self.id] ~= nil then
error("Duplicate weapon id \"" .. self.id .. "\"")
else
self.onUseWeaponHandler = registerOnUseWeapon(self.id, "itemid", otstd.onUseWeapon)
otstd.weapons[self.id] = self
end
end
function Weapon:unregister()
if self.onUseWeaponHandler ~= nil then
stopListener(self.onUseWeaponHandler)
self.onUseWeaponHandler = nil
end
otstd.weapons[self.id] = nil
end
otstd.onFistHandler = registerOnUseWeapon("fist", otstd.onUseWeapon)
otstd.onDefaultWeaponHandler = registerOnUseWeapon("all", otstd.onUseWeapon)