Skip to content

Commit

Permalink
feat: methods to set factions into player (opentibiabr#693)
Browse files Browse the repository at this point in the history
Fixed 2 players with the same factions can't attack.

Adding 2 methods and Lua export.

setFaction
getFaction
  • Loading branch information
carlospess0a authored Jan 13, 2023
1 parent 41a15a0 commit 21f7a17
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,17 @@ ReturnValue Combat::canDoCombat(Creature* attacker, Creature* target)
}

//nopvp-zone
const Tile* targetPlayerTile = targetPlayer->getTile();
if (targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE)) {
if (const Tile* targetPlayerTile = targetPlayer->getTile();
targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE))
{
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
} else if (attackerPlayer->getTile()->hasFlag(TILESTATE_NOPVPZONE) && !targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE | TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}

if (attackerPlayer->getFaction() != FACTION_DEFAULT && attackerPlayer->getFaction() != FACTION_PLAYER && attackerPlayer->getFaction() == targetPlayer->getFaction()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}

if (attackerMaster) {
Expand Down
6 changes: 5 additions & 1 deletion src/creatures/players/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,12 @@ class Player final : public Creature, public Cylinder
}

Faction_t getFaction() const override {
return FACTION_PLAYER;
return faction;
}

void setFaction(Faction_t factionId) {
faction = factionId;
}
//combat functions
bool setAttackedCreature(Creature* creature) override;
bool isImmune(CombatType_t type) const override;
Expand Down Expand Up @@ -2433,6 +2436,7 @@ class Player final : public Creature, public Cylinder
BlockType_t lastAttackBlockType = BLOCK_NONE;
TradeState_t tradeState = TRADE_NONE;
FightMode_t fightMode = FIGHTMODE_ATTACK;
Faction_t faction = FACTION_PLAYER;
account::AccountType accountType = account::AccountType::ACCOUNT_TYPE_NORMAL;
QuickLootFilter_t quickLootFilter;
VipStatus_t statusVipList = VIPSTATUS_ONLINE;
Expand Down
27 changes: 27 additions & 0 deletions src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3184,3 +3184,30 @@ int PlayerFunctions::luaPlayerGetForgeCores(lua_State *L) {
lua_pushnumber(L, static_cast<lua_Number>(core));
return 1;
}

int PlayerFunctions::luaPlayerSetFaction(lua_State* L) {
// player:setFaction(factionId)
Player* player = getUserdata<Player>(L, 1);
if (player == nullptr) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
return 0;
}
Faction_t factionId = getNumber<Faction_t>(L, 2);
player->setFaction(factionId);
pushBoolean(L, true);
return 1;
}

int PlayerFunctions::luaPlayerGetFaction(lua_State* L) {
// player:getFaction()
const Player* player = getUserdata<Player>(L, 1);
if (player == nullptr) {
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
pushBoolean(L, false);
return 0;
}

lua_pushnumber(L, player->getFaction());
return 1;
}
6 changes: 6 additions & 0 deletions src/lua/functions/creatures/player/player_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ class PlayerFunctions final : LuaScriptInterface {
registerMethod(L, "Player", "getForgeSlivers", PlayerFunctions::luaPlayerGetForgeSlivers);
registerMethod(L, "Player", "getForgeCores", PlayerFunctions::luaPlayerGetForgeCores);

registerMethod(L, "Player", "setFaction", PlayerFunctions::luaPlayerSetFaction);
registerMethod(L, "Player", "getFaction", PlayerFunctions::luaPlayerGetFaction);

GroupFunctions::init(L);
GuildFunctions::init(L);
MountFunctions::init(L);
Expand Down Expand Up @@ -558,6 +561,9 @@ class PlayerFunctions final : LuaScriptInterface {
static int luaPlayerGetForgeSlivers(lua_State* L);
static int luaPlayerGetForgeCores(lua_State* L);

static int luaPlayerSetFaction(lua_State* L);
static int luaPlayerGetFaction(lua_State* L);

friend class CreatureFunctions;
};

Expand Down

0 comments on commit 21f7a17

Please sign in to comment.