Skip to content

Commit

Permalink
Initial Rough Draft w/ working compilation and startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Codinablack committed Jan 23, 2025
1 parent 54631e8 commit 910cbec
Show file tree
Hide file tree
Showing 113 changed files with 6,963 additions and 6,786 deletions.
85 changes: 42 additions & 43 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ bool Actions::registerLuaEvent(Action* event)
return false;
}

ReturnValue Actions::canUse(const Player* player, const Position& pos)
ReturnValue Actions::canUse(const PlayerConstPtr& player, const Position& pos)
{
if (pos.x != 0xFFFF) {
const Position& playerPos = player->getPosition();
Expand All @@ -239,16 +239,16 @@ ReturnValue Actions::canUse(const Player* player, const Position& pos)
return RETURNVALUE_NOERROR;
}

ReturnValue Actions::canUse(const Player* player, const Position& pos, const Item* item)
ReturnValue Actions::canUse(const PlayerConstPtr& player, const Position& pos, const ItemConstPtr& item)
{
Action* action = getAction(item);
auto action = getAction(item);
if (action) {
return action->canExecuteAction(player, pos);
}
return RETURNVALUE_NOERROR;
}

ReturnValue Actions::canUseFar(const Creature* creature, const Position& toPos, bool checkLineOfSight, bool checkFloor)
ReturnValue Actions::canUseFar(const CreatureConstPtr& creature, const Position& toPos, bool checkLineOfSight, bool checkFloor)
{
if (toPos.x == 0xFFFF) {
return RETURNVALUE_NOERROR;
Expand All @@ -270,7 +270,7 @@ ReturnValue Actions::canUseFar(const Creature* creature, const Position& toPos,
return RETURNVALUE_NOERROR;
}

Action* Actions::getAction(const Item* item)
Action* Actions::getAction(const ItemConstPtr& item)
{
if (item->hasAttribute(ITEM_ATTRIBUTE_UNIQUEID)) {
auto it = uniqueItemMap.find(item->getUniqueId());
Expand All @@ -295,15 +295,15 @@ Action* Actions::getAction(const Item* item)
return g_spells->getRuneSpell(item->getID());
}

ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
ReturnValue Actions::internalUseItem(PlayerPtr player, const Position& pos, uint8_t index, const ItemPtr& item, bool isHotkey)
{
if (Door* door = item->getDoor()) {
if (auto door = item->getDoor()) {
if (!door->canUse(player)) {
return RETURNVALUE_NOTPOSSIBLE;
}
}

Action* action = getAction(item);
auto action = getAction(item);
if (action) {
if (action->isScripted()) {
if (action->executeUse(player, item, pos, nullptr, pos, isHotkey)) {
Expand All @@ -318,7 +318,7 @@ ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_
}
}

if (BedItem* bed = item->getBed()) {
if (BedItemPtr bed = item->getBed()) {
if (!bed->canUse(player)) {
if (!bed->getHouse()) {
return RETURNVALUE_YOUCANNOTUSETHISBED;
Expand All @@ -336,7 +336,7 @@ ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_
g_game.sendOfflineTrainingDialog(player);
}
else {
BedItem* bedItem = player->getBedItem();
auto bedItem = player->getBedItem();
if (bedItem) {
bedItem->sleep(player);
}
Expand All @@ -346,28 +346,28 @@ ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_
return RETURNVALUE_NOERROR;
}

if (Container* container = item->getContainer()) {
Container* openContainer;
if (auto container = item->getContainer()) {
ContainerPtr openContainer;

//depot container
if (DepotLocker* depot = container->getDepotLocker()) {
DepotLocker& myDepotLocker = player->getDepotLocker();
myDepotLocker.setParent(depot->getParent()->getTile());
openContainer = &myDepotLocker;
if (auto depot = container->getDepotLocker()) {
DepotLockerPtr& myDepotLocker = player->getDepotLocker();
myDepotLocker->setParent(depot->getParent());
openContainer = myDepotLocker;
} else {
openContainer = container;
}

uint32_t corpseOwner = container->getCorpseOwner();
if (container->isRewardCorpse()) {
RewardChest& myRewardChest = player->getRewardChest();
auto& myRewardChest = player->getRewardChest();

for (Item* subItem : container->getItemList()) {
for (auto subItem : container->getItemList()) {
if (subItem->getID() == ITEM_REWARD_CONTAINER) {
int64_t rewardDate = subItem->getIntAttr(ITEM_ATTRIBUTE_DATE);

bool foundMatch = false;
for (Item* rewardItem : myRewardChest.getItemList()) {
for (auto rewardItem : myRewardChest->getItemList()) {
if (rewardItem->getID() == ITEM_REWARD_CONTAINER && rewardItem->getIntAttr(ITEM_ATTRIBUTE_DATE) == rewardDate) {
foundMatch = true;
break;
Expand All @@ -385,32 +385,31 @@ ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_
}

// Reward chest
if (RewardChest* rewardchest = container->getRewardChest()) {
RewardChest& myRewardChest = player->getRewardChest();
myRewardChest.setParent(rewardchest->getParent()->getTile());
if (auto rewardchest = container->getRewardChest()) {
auto& myRewardChest = player->getRewardChest();
myRewardChest->setParent(myRewardChest->getParent());

if (myRewardChest.getItemList().empty()) {
if (myRewardChest->getItemList().empty()) {
return RETURNVALUE_REWARDCHESTEMPTY;
}

for (Item* rewardItem : myRewardChest.getItemList()) {
for (auto rewardItem : myRewardChest->getItemList()) {
if (rewardItem->getID() == ITEM_REWARD_CONTAINER) {
Container* rewardContainer = rewardItem->getContainer();
auto rewardContainer = rewardItem->getContainer();
if (rewardContainer) {
rewardContainer->setParent(&myRewardChest);
rewardContainer->setParent(myRewardChest);
}
}
}
openContainer = &myRewardChest;
openContainer = myRewardChest;
}
else if (item->getID() == ITEM_REWARD_CONTAINER) {
RewardChest& myRewardChest = player->getRewardChest();
auto& myRewardChest = player->getRewardChest();
int64_t rewardDate = item->getIntAttr(ITEM_ATTRIBUTE_DATE);

for (Item* rewardItem : myRewardChest.getItemList()) {
for (auto rewardItem : myRewardChest->getItemList()) {
if (rewardItem->getID() == ITEM_REWARD_CONTAINER && rewardItem->getIntAttr(ITEM_ATTRIBUTE_DATE) == rewardDate && rewardItem->getIntAttr(ITEM_ATTRIBUTE_REWARDID) == item->getIntAttr(ITEM_ATTRIBUTE_REWARDID)) {
Container* rewardContainer = rewardItem->getContainer();
if (rewardContainer) {
if (const auto rewardContainer = rewardItem->getContainer()) {
rewardContainer->setParent(container->getRealParent());
openContainer = rewardContainer;
}
Expand All @@ -420,7 +419,7 @@ ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_
}

//open/close container
int32_t oldContainerId = player->getContainerID(openContainer);
int32_t oldContainerId = player->getContainerID(std::static_pointer_cast<const Container>(openContainer));
if (oldContainerId == -1) {
player->addContainer(index, openContainer);
player->onSendContainer(openContainer);
Expand Down Expand Up @@ -449,7 +448,7 @@ ReturnValue Actions::internalUseItem(Player* player, const Position& pos, uint8_
}


static void showUseHotkeyMessage(Player* player, const Item* item, uint32_t count)
static void showUseHotkeyMessage(const PlayerPtr& player, const ItemConstPtr& item, uint32_t count)
{
const ItemType& it = Item::items[item->getID()];
if (!it.showCount) {
Expand All @@ -461,7 +460,7 @@ static void showUseHotkeyMessage(Player* player, const Item* item, uint32_t coun
}
}

bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey)
bool Actions::useItem(PlayerPtr player, const Position& pos, uint8_t index, const ItemPtr& item, bool isHotkey)
{
player->setNextAction(OTSYS_TIME() + g_config.getNumber(ConfigManager::ACTIONS_DELAY_INTERVAL));

Expand All @@ -471,7 +470,7 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
}

if (g_config.getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(item->getTile())) {
if (const auto& houseTile = std::dynamic_pointer_cast<const HouseTile>(item->getTile())) {
if (!item->getTopParent()->getCreature() && !houseTile->getHouse()->isInvited(player)) {
player->sendCancelMessage(RETURNVALUE_PLAYERISNOTINVITED);
return false;
Expand All @@ -493,8 +492,8 @@ bool Actions::useItem(Player* player, const Position& pos, uint8_t index, Item*
return true;
}

bool Actions::useItemEx(Player* player, const Position& fromPos, const Position& toPos,
uint8_t toStackPos, Item* item, bool isHotkey, Creature* creature/* = nullptr*/)
bool Actions::useItemEx(const PlayerPtr& player, const Position& fromPos, const Position& toPos,
uint8_t toStackPos, const ItemPtr& item, bool isHotkey, const CreaturePtr& creature/* = nullptr*/)
{
player->setNextAction(OTSYS_TIME() + g_config.getNumber(ConfigManager::EX_ACTIONS_DELAY_INTERVAL));

Expand All @@ -516,7 +515,7 @@ bool Actions::useItemEx(Player* player, const Position& fromPos, const Position&
}

if (g_config.getBoolean(ConfigManager::ONLY_INVITED_CAN_MOVE_HOUSE_ITEMS)) {
if (const HouseTile* const houseTile = dynamic_cast<const HouseTile*>(item->getTile())) {
if (const auto& houseTile = std::static_pointer_cast<const HouseTile>(item->getTile())) {
if (!item->getTopParent()->getCreature() && !houseTile->getHouse()->isInvited(player)) {
player->sendCancelMessage(RETURNVALUE_PLAYERISNOTINVITED);
return false;
Expand Down Expand Up @@ -560,7 +559,7 @@ bool Action::configureEvent(const pugi::xml_node& node)

namespace {

bool enterMarket(Player* player, Item*, const Position&, Thing*, const Position&, bool)
bool enterMarket(const PlayerPtr& player, ItemPtr, const Position&, ThingPtr, const Position&, bool)
{
player->sendMarketEnter();
return true;
Expand All @@ -586,23 +585,23 @@ bool Action::loadFunction(const pugi::xml_attribute& attr, bool isScripted)
return true;
}

ReturnValue Action::canExecuteAction(const Player* player, const Position& toPos)
ReturnValue Action::canExecuteAction(const PlayerConstPtr& player, const Position& toPos)
{
if (allowFarUse) {
return g_actions->canUseFar(player, toPos, checkLineOfSight, checkFloor);
}
return g_actions->canUse(player, toPos);
}

Thing* Action::getTarget(Player* player, Creature* targetCreature, const Position& toPosition, uint8_t toStackPos) const
ThingPtr Action::getTarget(const PlayerPtr& player, const CreaturePtr& targetCreature, const Position& toPosition, uint8_t toStackPos) const
{
if (targetCreature) {
return targetCreature;
}
return g_game.internalGetThing(player, toPosition, toStackPos, 0, STACKPOS_USETARGET);
}

bool Action::executeUse(Player* player, Item* item, const Position& fromPosition, Thing* target, const Position& toPosition, bool isHotkey)
bool Action::executeUse(const PlayerPtr& player, const ItemPtr& item, const Position& fromPosition, const ThingPtr& target, const Position& toPosition, bool isHotkey)
{
//onUse(player, item, fromPosition, target, toPosition, isHotkey)
if (!scriptInterface->reserveScriptEnv()) {
Expand All @@ -617,7 +616,7 @@ bool Action::executeUse(Player* player, Item* item, const Position& fromPosition

scriptInterface->pushFunction(scriptId);

LuaScriptInterface::pushUserdata<Player>(L, player);
LuaScriptInterface::pushSharedPtr(L, player);
LuaScriptInterface::setMetatable(L, -1, "Player");

LuaScriptInterface::pushThing(L, item);
Expand Down
35 changes: 23 additions & 12 deletions src/actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class Action;
using Action_ptr = std::unique_ptr<Action>;
using ActionFunction = std::function<bool(Player* player, Item* item, const Position& fromPosition, Thing* target, const Position& toPosition, bool isHotkey)>;
using ActionFunction = std::function<bool(PlayerPtr player, ItemPtr item, const Position& fromPosition, ThingPtr target, const Position& toPosition, bool isHotkey)>;

class Action : public Event
{
Expand All @@ -21,65 +21,76 @@ class Action : public Event
bool loadFunction(const pugi::xml_attribute& attr, bool isScripted) override;

//scripting
virtual bool executeUse(Player* player, Item* item, const Position& fromPosition,
Thing* target, const Position& toPosition, bool isHotkey);
virtual bool executeUse(const PlayerPtr& player, const ItemPtr& item, const Position& fromPosition,
const ThingPtr& target, const Position& toPosition, bool isHotkey);

bool getAllowFarUse() const {
return allowFarUse;
}

void setAllowFarUse(bool v) {
allowFarUse = v;
}

bool getCheckLineOfSight() const {
return checkLineOfSight;
}

void setCheckLineOfSight(bool v) {
checkLineOfSight = v;
}

bool getCheckFloor() const {
return checkFloor;
}

void setCheckFloor(bool v) {
checkFloor = v;
}

void clearItemIdRange() {
return ids.clear();
}

const std::vector<uint16_t>& getItemIdRange() const {
return ids;
}

void addItemId(uint16_t id) {
ids.emplace_back(id);
}

void clearUniqueIdRange() {
return uids.clear();
}

const std::vector<uint16_t>& getUniqueIdRange() const {
return uids;
}

void addUniqueId(uint16_t id) {
uids.emplace_back(id);
}

void clearActionIdRange() {
return aids.clear();
}

const std::vector<uint16_t>& getActionIdRange() const {
return aids;
}

void addActionId(uint16_t id) {
aids.emplace_back(id);
}

virtual ReturnValue canExecuteAction(const Player* player, const Position& toPos);
virtual ReturnValue canExecuteAction(const PlayerConstPtr& player, const Position& toPos);

virtual bool hasOwnErrorHandler() {
return false;
}
virtual Thing* getTarget(Player* player, Creature* targetCreature, const Position& toPosition, uint8_t toStackPos) const;

virtual ThingPtr getTarget(const PlayerPtr& player, const CreaturePtr& targetCreature, const Position& toPosition, uint8_t toStackPos) const;

ActionFunction function;

Expand All @@ -104,18 +115,18 @@ class Actions final : public BaseEvents
Actions(const Actions&) = delete;
Actions& operator=(const Actions&) = delete;

bool useItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey);
bool useItemEx(Player* player, const Position& fromPos, const Position& toPos, uint8_t toStackPos, Item* item, bool isHotkey, Creature* creature = nullptr);
bool useItem(PlayerPtr player, const Position& pos, uint8_t index, const ItemPtr& item, bool isHotkey);
bool useItemEx(const PlayerPtr& player, const Position& fromPos, const Position& toPos, uint8_t toStackPos, const ItemPtr& item, bool isHotkey, const CreaturePtr& creature = nullptr);

ReturnValue canUse(const Player* player, const Position& pos);
ReturnValue canUse(const Player* player, const Position& pos, const Item* item);
ReturnValue canUseFar(const Creature* creature, const Position& toPos, bool checkLineOfSight, bool checkFloor);
ReturnValue canUse(const PlayerConstPtr& player, const Position& pos);
ReturnValue canUse(const PlayerConstPtr& player, const Position& pos, const ItemConstPtr& item);
ReturnValue canUseFar(const CreatureConstPtr& creature, const Position& toPos, bool checkLineOfSight, bool checkFloor);

bool registerLuaEvent(Action* event);
void clear(bool fromLua) override final;

private:
ReturnValue internalUseItem(Player* player, const Position& pos, uint8_t index, Item* item, bool isHotkey);
ReturnValue internalUseItem(PlayerPtr player, const Position& pos, uint8_t index, const ItemPtr& item, bool isHotkey);

LuaScriptInterface& getScriptInterface() override;
std::string_view getScriptBaseName() const override { return "actions"; }
Expand All @@ -127,7 +138,7 @@ class Actions final : public BaseEvents
ActionUseMap uniqueItemMap;
ActionUseMap actionItemMap;

Action* getAction(const Item* item);
Action* getAction(const ItemConstPtr& item);
void clearMap(ActionUseMap& map, bool fromLua);

LuaScriptInterface scriptInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/augment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "augment.h"

Augment::Augment(std::string name, std::string description) : m_name(name), m_description(description) {
Augment::Augment(const std::string& name, const std::string& description) : m_name(name), m_description(description) {

}

Expand Down
Loading

0 comments on commit 910cbec

Please sign in to comment.