player.lua

main 217 lines · 5.9 KB Raw
Alex Alex Commit Server upload 18/10/2026 23:38
1local foodCondition = Condition(CONDITION_REGENERATION, CONDITIONID_DEFAULT)
2
3function Player.feed(self, food)
4 local condition = self:getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT)
5 if condition then
6 condition:setTicks(condition:getTicks() + (food * 1000))
7 else
8 local vocation = self:getVocation()
9 if not vocation then
10 return nil
11 end
12
13 foodCondition:setTicks(food * 1000)
14 foodCondition:setParameter(CONDITION_PARAM_HEALTHGAIN, vocation:getHealthGainAmount())
15 foodCondition:setParameter(CONDITION_PARAM_HEALTHTICKS, vocation:getHealthGainTicks() * 1000)
16 foodCondition:setParameter(CONDITION_PARAM_MANAGAIN, vocation:getManaGainAmount())
17 foodCondition:setParameter(CONDITION_PARAM_MANATICKS, vocation:getManaGainTicks() * 1000)
18
19 self:addCondition(foodCondition)
20 end
21 return true
22end
23
24function Player.getClosestFreePosition(self, position, extended)
25 if self:getGroup():getAccess() and self:getAccountType() >= ACCOUNT_TYPE_GOD then
26 return position
27 end
28 return Creature.getClosestFreePosition(self, position, extended)
29end
30
31function Player.getDepotItems(self, depotId)
32 return self:getDepotChest(depotId, true):getItemHoldingCount()
33end
34
35function Player.hasFlag(self, flag)
36 return self:getGroup():hasFlag(flag)
37end
38
39function Player.getLossPercent(self)
40 local blessings = 0
41 local lossPercent = {
42 [0] = 100,
43 [1] = 70,
44 [2] = 45,
45 [3] = 25,
46 [4] = 10,
47 [5] = 0
48 }
49
50 for i = 1, 5 do
51 if self:hasBlessing(i) then
52 blessings = blessings + 1
53 end
54 end
55 return lossPercent[blessings]
56end
57
58function Player.isPremium(self)
59 return self:getPremiumDays() > 0 or configManager.getBoolean(configKeys.FREE_PREMIUM)
60end
61
62function Player.sendCancelMessage(self, message)
63 if type(message) == "number" then
64 message = Game.getReturnMessage(message)
65 end
66 return self:sendTextMessage(MESSAGE_STATUS_SMALL, message)
67end
68
69function Player.isUsingOtClient(self)
70 return self:getClient().os >= CLIENTOS_OTCLIENT_LINUX
71end
72
73function Player.sendExtendedOpcode(self, opcode, buffer)
74 if not self:isUsingOtClient() then
75 return false
76 end
77
78 local networkMessage = NetworkMessage()
79 networkMessage:addByte(0x32)
80 networkMessage:addByte(opcode)
81 networkMessage:addString(buffer)
82 networkMessage:sendToPlayer(self)
83 networkMessage:delete()
84 return true
85end
86
87APPLY_SKILL_MULTIPLIER = true
88local addSkillTriesFunc = Player.addSkillTries
89function Player.addSkillTries(...)
90 APPLY_SKILL_MULTIPLIER = false
91 local ret = addSkillTriesFunc(...)
92 APPLY_SKILL_MULTIPLIER = true
93 return ret
94end
95
96local addManaSpentFunc = Player.addManaSpent
97function Player.addManaSpent(...)
98 APPLY_SKILL_MULTIPLIER = false
99 local ret = addManaSpentFunc(...)
100 APPLY_SKILL_MULTIPLIER = true
101 return ret
102end
103
104-- Always pass the number through the isValidMoney function first before using the transferMoneyTo
105function Player.transferMoneyTo(self, target, amount)
106 if not target then
107 return false
108 end
109
110 -- See if you can afford this transfer
111 local balance = self:getBankBalance()
112 if amount > balance then
113 return false
114 end
115
116 -- See if player is online
117 local targetPlayer = Player(target.guid)
118 if targetPlayer then
119 targetPlayer:setBankBalance(targetPlayer:getBankBalance() + amount)
120 else
121 db.query("UPDATE `players` SET `balance` = `balance` + " .. amount .. " WHERE `id` = '" .. target.guid .. "'")
122 end
123
124 self:setBankBalance(self:getBankBalance() - amount)
125 return true
126end
127
128function Player.canCarryMoney(self, amount)
129 -- Anyone can carry as much imaginary money as they desire
130 if amount == 0 then
131 return true
132 end
133
134 -- The 3 below loops will populate these local variables
135 local totalWeight = 0
136 local inventorySlots = 0
137
138 -- Add crystal coins to totalWeight and inventorySlots
139 local type_crystal = ItemType(ITEM_CRYSTAL_COIN)
140 local crystalCoins = math.floor(amount / 10000)
141 if crystalCoins > 0 then
142 amount = amount - (crystalCoins * 10000)
143 while crystalCoins > 0 do
144 local count = math.min(100, crystalCoins)
145 totalWeight = totalWeight + type_crystal:getWeight(count)
146 crystalCoins = crystalCoins - count
147 inventorySlots = inventorySlots + 1
148 end
149 end
150
151 -- Add platinum coins to totalWeight and inventorySlots
152 local type_platinum = ItemType(ITEM_PLATINUM_COIN)
153 local platinumCoins = math.floor(amount / 100)
154 if platinumCoins > 0 then
155 amount = amount - (platinumCoins * 100)
156 while platinumCoins > 0 do
157 local count = math.min(100, platinumCoins)
158 totalWeight = totalWeight + type_platinum:getWeight(count)
159 platinumCoins = platinumCoins - count
160 inventorySlots = inventorySlots + 1
161 end
162 end
163
164 -- Add gold coins to totalWeight and inventorySlots
165 local type_gold = ItemType(ITEM_GOLD_COIN)
166 if amount > 0 then
167 while amount > 0 do
168 local count = math.min(100, amount)
169 totalWeight = totalWeight + type_gold:getWeight(count)
170 amount = amount - count
171 inventorySlots = inventorySlots + 1
172 end
173 end
174
175 -- If player don't have enough capacity to carry this money
176 if self:getFreeCapacity() < totalWeight then
177 return false
178 end
179
180 -- If player don't have enough available inventory slots to carry this money
181 local backpack = self:getSlotItem(CONST_SLOT_BACKPACK)
182 if not backpack or backpack:getEmptySlots(true) < inventorySlots then
183 return false
184 end
185 return true
186end
187
188function Player.withdrawMoney(self, amount)
189 local balance = self:getBankBalance()
190 if amount > balance or not self:addMoney(amount) then
191 return false
192 end
193
194 self:setBankBalance(balance - amount)
195 return true
196end
197
198function Player.depositMoney(self, amount)
199 if not self:removeMoney(amount) then
200 return false
201 end
202
203 self:setBankBalance(self:getBankBalance() + amount)
204 return true
205end
206
207function Player.JumpCreature(self, creatureID, jumpHeight, jumpDuration, jumpStraight)
208 local networkMessage = NetworkMessage()
209 networkMessage:addByte(0x36) -- 54
210 networkMessage:addU32(creatureID)
211 networkMessage:addU32(jumpHeight)
212 networkMessage:addU32(jumpDuration)
213 networkMessage:addByte(jumpStraight)
214 networkMessage:sendToPlayer(self)
215 networkMessage:delete()
216 return true
217end
Top