Skip to content

Commit

Permalink
Merge branch 'main' into ajustmentPlayerEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas authored Nov 28, 2023
2 parents 56b4e79 + 970b309 commit 86a09fa
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 66 deletions.
27 changes: 18 additions & 9 deletions data-otservbr-global/scripts/spells/attack/divine_grenade.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local combatGrenade = Combat()
combatGrenade:setParameter(COMBAT_PARAM_TYPE, COMBAT_HOLYDAMAGE)
combatGrenade:setArea(createCombatArea(AREA_CIRCLE2X2))
combatGrenade:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_HOLYDAMAGE)

function onGetFormulaValues(player, level, maglevel)
local min = (level / 5) + (maglevel * 4)
local max = (level / 5) + (maglevel * 6)

local grade = player:upgradeSpellsWOD("Divine Grenade")

local multiplier = 1.0
Expand All @@ -15,6 +17,7 @@ function onGetFormulaValues(player, level, maglevel)

min = min * multiplier
max = max * multiplier

return -min, -max
end

Expand All @@ -37,32 +40,36 @@ local explodeGrenade = function(position, playerId)
var.type = 2 -- VARIANT_POSITION
var.pos = position
combatGrenade:execute(player, var)
player:getPosition():removeMagicEffect(CONST_ME_DIVINE_GRENADE)
end

local combatCast = Combat()
combatCast:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_HOLY)
local function removeGrenadeEffect(position)
position:removeMagicEffect(CONST_ME_DIVINE_GRENADE)
end

function onTargetCreature(creature, target)
if not creature and target and creature:isPlayer() then
if not (creature and target and creature:isPlayer()) then
return false
end

local position = target:getPosition()
local position = creature:getPosition():getWithinRange(target:getPosition(), 4)
addEvent(explodeGrenade, 3000, position, creature:getId())
addEvent(removeGrenadeEffect, 3000, position)
return true
end

local combatCast = Combat()
combatCast:setCallback(CALLBACK_PARAM_TARGETCREATURE, "onTargetCreature")

local spell = Spell("instant")

function spell.onCastSpell(creature, var)
if not (creature and creature:isPlayer()) then
if not creature or not creature:isPlayer() then
return false
end
local grade = creature:upgradeSpellsWOD("Divine Grenade")
if grade == WHEEL_GRADE_NONE then

local grade = creature:revelationStageWOD("Divine Grenade")

if grade == 0 then
creature:sendCancelMessage("You cannot cast this spell")
creature:getPosition():sendMagicEffect(CONST_ME_POFF)
return false
Expand All @@ -73,7 +80,9 @@ function spell.onCastSpell(creature, var)

var.instantName = "Divine Grenade Cast"
if combatCast:execute(creature, var) then
creature:getPosition():sendMagicEffect(CONST_ME_DIVINE_GRENADE)
local target = Creature(var:getNumber())
local position = creature:getPosition():getWithinRange(target:getPosition(), 4)
position:sendMagicEffect(CONST_ME_DIVINE_GRENADE)
local condition = Condition(CONDITION_SPELLCOOLDOWN, CONDITIONID_DEFAULT, 258)
condition:setTicks((cooldown * 1000) / configManager.getFloat(configKeys.RATE_SPELL_COOLDOWN))
creature:addCondition(condition)
Expand Down
18 changes: 17 additions & 1 deletion data/libs/functions/position.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function Position:moveUpstairs()
return self
end

-- Functions from OTServBR-Global
function Position:isInRange(from, to)
-- No matter what corner from and to is, we want to make
-- life easier by calculating north-west and south-east
Expand Down Expand Up @@ -373,3 +372,20 @@ function Position:isProtectionZoneTile()
end
return tile:hasFlag(TILESTATE_PROTECTIONZONE)
end

--- Calculates and returns a position based on a specified range.
-- This method determines which position (self or the other) is returned based on the provided range.
-- If the distance between self and the other position is greater than the specified range,
-- it returns the self position. Otherwise, it returns the other position.
-- @param self The position object calling the method.
-- @param otherPosition Position The other position to compare with.
-- @param range number The range to compare the distance against.
-- @return Position The position within the specified range (either self or otherPosition).
function Position.getWithinRange(self, otherPosition, range)
local distance = math.max(math.abs(self.x - otherPosition.x), math.abs(self.y - otherPosition.y))
if distance > range then
return self
end

return otherPosition
end
34 changes: 18 additions & 16 deletions src/lua/functions/core/game/bank_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int BankFunctions::luaBankCredit(lua_State* L) {
// Bank.credit(playerOrGuild, amount)
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 2);
Expand All @@ -19,7 +19,7 @@ int BankFunctions::luaBankDebit(lua_State* L) {
// Bank.debit(playerOrGuild, amount)
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 2);
Expand All @@ -31,7 +31,7 @@ int BankFunctions::luaBankBalance(lua_State* L) {
// Bank.balance(playerOrGuild[, amount]])
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
if (lua_gettop(L) == 1) {
Expand All @@ -47,7 +47,7 @@ int BankFunctions::luaBankHasBalance(lua_State* L) {
// Bank.hasBalance(playerOrGuild, amount)
auto bank = getBank(L, 1);
if (bank == nullptr) {
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 2);
Expand All @@ -60,13 +60,13 @@ int BankFunctions::luaBankTransfer(lua_State* L) {
auto source = getBank(L, 1);
if (source == nullptr) {
g_logger().debug("BankFunctions::luaBankTransfer: source is null");
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
std::shared_ptr<Bank> destination = getBank(L, 2);
if (destination == nullptr) {
g_logger().debug("BankFunctions::luaBankTransfer: destination is null");
lua_pushnil(L);
reportErrorFunc("Bank is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 3);
Expand All @@ -78,12 +78,12 @@ int BankFunctions::luaBankTransferToGuild(lua_State* L) {
// Bank.transfer(fromPlayerOrGuild, toGuild, amount)
auto source = getBank(L, 1);
if (source == nullptr) {
lua_pushnil(L);
reportErrorFunc("Source is nullptr");
return 1;
}
std::shared_ptr<Bank> destination = getBank(L, 2, true /* isGuild */);
if (destination == nullptr) {
lua_pushnil(L);
reportErrorFunc("Destination is nullptr");
return 1;
}
uint64_t amount = getNumber<uint64_t>(L, 3);
Expand All @@ -93,20 +93,21 @@ int BankFunctions::luaBankTransferToGuild(lua_State* L) {

int BankFunctions::luaBankWithdraw(lua_State* L) {
// Bank.withdraw(player, amount[, source = player])
auto player = getPlayer(L, 1);
const auto &player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

uint64_t amount = getNumber<uint64_t>(L, 2);
if (lua_gettop(L) == 2) {
if (!player) {
return 1;
}

const auto bank = std::make_shared<Bank>(player);
pushBoolean(L, bank->withdraw(player, amount));
return 1;
}
auto source = getBank(L, 3);
if (source == nullptr) {
lua_pushnil(L);
reportErrorFunc("Source is nullptr");
return 1;
}
pushBoolean(L, source->withdraw(player, amount));
Expand All @@ -115,8 +116,9 @@ int BankFunctions::luaBankWithdraw(lua_State* L) {

int BankFunctions::luaBankDeposit(lua_State* L) {
// Bank.deposit(player, amount[, destination = player])
auto player = getPlayer(L, 1);
const auto &player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}
const auto bank = std::make_shared<Bank>(player);
Expand All @@ -134,7 +136,7 @@ int BankFunctions::luaBankDeposit(lua_State* L) {
}
auto destination = getBank(L, 3);
if (destination == nullptr) {
lua_pushnil(L);
reportErrorFunc("Destination is nullptr");
return 1;
}
pushBoolean(L, g_game().removeMoney(player, amount) && destination->credit(amount));
Expand Down
8 changes: 4 additions & 4 deletions src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,16 +741,16 @@ int GlobalFunctions::luaDebugPrint(lua_State* L) {

int GlobalFunctions::luaIsInWar(lua_State* L) {
// isInWar(cid, target)
std::shared_ptr<Player> player = getPlayer(L, 1);
const auto &player = getPlayer(L, 1);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
reportErrorFunc(fmt::format("{} - Player", getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)));
pushBoolean(L, false);
return 1;
}

std::shared_ptr<Player> targetPlayer = getPlayer(L, 2);
const auto &targetPlayer = getPlayer(L, 2);
if (!targetPlayer) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
reportErrorFunc(fmt::format("{} - TargetPlayer", getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND)));
pushBoolean(L, false);
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/lua/functions/core/game/modal_window_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ int ModalWindowFunctions::luaModalWindowSetPriority(lua_State* L) {

int ModalWindowFunctions::luaModalWindowSendToPlayer(lua_State* L) {
// modalWindow:sendToPlayer(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
lua_pushnil(L);
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

Expand Down
10 changes: 5 additions & 5 deletions src/lua/functions/core/network/network_message_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ int NetworkMessageFunctions::luaNetworkMessageSendToPlayer(lua_State* L) {
}

const auto &player = getPlayer(L, 2);
if (player) {
player->sendNetworkMessage(*message);
pushBoolean(L, true);
} else {
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushnil(L);
return 1;
}

player->sendNetworkMessage(*message);
pushBoolean(L, true);
return 1;
}
14 changes: 10 additions & 4 deletions src/lua/functions/creatures/npc/npc_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ int NpcFunctions::luaNpcOpenShopWindow(lua_State* L) {
return 1;
}

std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
Expand All @@ -360,7 +360,7 @@ int NpcFunctions::luaNpcOpenShopWindow(lua_State* L) {

int NpcFunctions::luaNpcCloseShopWindow(lua_State* L) {
// npc:closeShopWindow(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
Expand Down Expand Up @@ -452,7 +452,13 @@ int NpcFunctions::luaNpcFollow(lua_State* L) {
return 1;
}

pushBoolean(L, npc->setFollowCreature(getPlayer(L, 2)));
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

pushBoolean(L, npc->setFollowCreature(player));
return 1;
}

Expand All @@ -478,7 +484,7 @@ int NpcFunctions::luaNpcSellItem(lua_State* L) {
return 1;
}

std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
Expand Down
37 changes: 31 additions & 6 deletions src/lua/functions/creatures/player/party_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ int PartyFunctions::luaPartyGetLeader(lua_State* L) {

int PartyFunctions::luaPartySetLeader(lua_State* L) {
// party:setLeader(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
if (party) {
pushBoolean(L, party->passPartyLeadership(player));
} else {
lua_pushnil(L);
Expand Down Expand Up @@ -139,7 +144,12 @@ int PartyFunctions::luaPartyGetInviteeCount(lua_State* L) {

int PartyFunctions::luaPartyAddInvite(lua_State* L) {
// party:addInvite(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->invitePlayer(player));
Expand All @@ -151,7 +161,12 @@ int PartyFunctions::luaPartyAddInvite(lua_State* L) {

int PartyFunctions::luaPartyRemoveInvite(lua_State* L) {
// party:removeInvite(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->removeInvite(player));
Expand All @@ -163,7 +178,12 @@ int PartyFunctions::luaPartyRemoveInvite(lua_State* L) {

int PartyFunctions::luaPartyAddMember(lua_State* L) {
// party:addMember(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->joinParty(player));
Expand All @@ -175,7 +195,12 @@ int PartyFunctions::luaPartyAddMember(lua_State* L) {

int PartyFunctions::luaPartyRemoveMember(lua_State* L) {
// party:removeMember(player)
std::shared_ptr<Player> player = getPlayer(L, 2);
const auto &player = getPlayer(L, 2);
if (!player) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
return 1;
}

std::shared_ptr<Party> party = getUserdataShared<Party>(L, 1);
if (party && player) {
pushBoolean(L, party->leaveParty(player));
Expand Down
Loading

0 comments on commit 86a09fa

Please sign in to comment.