#innclude "otpch.h"
#include "const.h"
#include "actions.h"
#include "tools.h"
#include "player.h"
#include "monster.h"
#include "npc.h"
#include "item.h"
#include "container.h"
#include "game.h"
#include "configmanager.h"
#include "combat.h"
#include "spells.h"
#include "house.h"
#include "beds.h"
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
extern Game g_game;
extern Spells* g_spells;
extern Actions* g_actions;
extern ConfigManager g_config;
Actions::Actions():
m_interface("Action Interface")
{
m_interface.initState();
}
Actions::~Actions()
{
clear();
}
inline void Actions::clearMap(ActionUseMap& map)
{
for(ActionUseMap::iterator it = map.begin(); it != map.end(); ++it)
delete it->second;
map.clear();
}
void Actions::clear()
{
clearMap(useItemMap);
clearMap(uniqueItemMap);
clearMap(actionItemMap);
m_interface.reInitState();
}
Event* Actions::getEvent(const std::string& nodeName)
{
if(asLowerCaseString(nodeName) == "action")
return new Action(&m_interface);
return NULL;
}
bool Actions::registerEvent(Event* event, xmlNodePtr p, bool override)
{
Action* action = dynamic_cast<Action*>(event);
if(!action)
return false;
StringVec strVector;
IntegerVec intVector, endIntVector;
std::string strValue, endStrValue;
int32_t tmp = 0;
bool success = true;
if(readXMLString(p, "itemid", strValue))
{
if(!parseIntegerVec(strValue, intVector))
{
std::cout << "[Warning - Actions::registerEvent] Invalid itemid - '" << strValue << "'" << std::endl;
return false;
}
if(useItemMap.find(intVector[0]) != useItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item id: " << intVector[0] << std::endl;
success = false;
}
else
delete useItemMap[intVector[0]];
}
if(success)
useItemMap[intVector[0]] = action;
for(size_t i = 1, size = intVector.size(); i < size; ++i)
{
if(useItemMap.find(intVector) != useItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item id: " << intVector << std::endl;
continue;
}
else
delete useItemMap[intVector];
}
useItemMap[intVector] = new Action(action);
}
}
if(readXMLString(p, "fromid", strValue) && readXMLString(p, "toid", endStrValue))
{
intVector = vectorAtoi(explodeString(strValue, ";"));
endIntVector = vectorAtoi(explodeString(endStrValue, ";"));
if(intVector[0] && endIntVector[0] && intVector.size() == endIntVector.size())
{
for(size_t i = 0, size = intVector.size(); i < size; ++i)
{
tmp = intVector;
while(intVector <= endIntVector)
{
if(useItemMap.find(intVector) != useItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with id: " << intVector <<
", in fromid: " << tmp << " and toid: " << endIntVector << std::endl;
intVector++;
continue;
}
else
delete useItemMap[intVector];
}
useItemMap[intVector++] = new Action(action);
}
}
}
else
std::cout << "[Warning - Actions::registerEvent] Malformed entry (from item: \"" << strValue <<
"\", to item: \"" << endStrValue << "\")" << std::endl;
}
if(readXMLString(p, "uniqueid", strValue))
{
if(!parseIntegerVec(strValue, intVector))
{
std::cout << "[Warning - Actions::registerEvent] Invalid uniqueid - '" << strValue << "'" << std::endl;
return false;
}
if(uniqueItemMap.find(intVector[0]) != uniqueItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item uid: " << intVector[0] << std::endl;
success = false;
}
else
delete uniqueItemMap[intVector[0]];
}
if(success)
uniqueItemMap[intVector[0]] = action;
for(size_t i = 1, size = intVector.size(); i < size; ++i)
{
if(uniqueItemMap.find(intVector) != uniqueItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item uid: " << intVector << std::endl;
continue;
}
else
delete uniqueItemMap[intVector];
}
uniqueItemMap[intVector] = new Action(action);
}
}
if(readXMLString(p, "fromuid", strValue) && readXMLString(p, "touid", endStrValue))
{
intVector = vectorAtoi(explodeString(strValue, ";"));
endIntVector = vectorAtoi(explodeString(endStrValue, ";"));
if(intVector[0] && endIntVector[0] && intVector.size() == endIntVector.size())
{
for(size_t i = 0, size = intVector.size(); i < size; ++i)
{
tmp = intVector;
while(intVector <= endIntVector)
{
if(uniqueItemMap.find(intVector) != uniqueItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with uid: " << intVector <<
", in fromuid: " << tmp << " and touid: " << endIntVector << std::endl;
intVector++;
continue;
}
else
delete uniqueItemMap[intVector];
}
uniqueItemMap[intVector++] = new Action(action);
}
}
}
else
std::cout << "[Warning - Actions::registerEvent] Malformed entry (from unique: \"" << strValue <<
"\", to unique: \"" << endStrValue << "\")" << std::endl;
}
if(readXMLString(p, "actionid", strValue))
{
if(!parseIntegerVec(strValue, intVector))
{
std::cout << "[Warning - Actions::registerEvent] Invalid actionid - '" << strValue << "'" << std::endl;
return false;
}
if(actionItemMap.find(intVector[0]) != actionItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item aid: " << intVector[0] << std::endl;
success = false;
}
else
delete actionItemMap[intVector[0]];
}
if(success)
actionItemMap[intVector[0]] = action;
for(size_t i = 1, size = intVector.size(); i < size; ++i)
{
if(actionItemMap.find(intVector) != actionItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item aid: " << intVector << std::endl;
continue;
}
else
delete actionItemMap[intVector];
}
actionItemMap[intVector] = new Action(action);
}
}
if(readXMLString(p, "fromaid", strValue) && readXMLString(p, "toaid", endStrValue))
{
intVector = vectorAtoi(explodeString(strValue, ";"));
endIntVector = vectorAtoi(explodeString(endStrValue, ";"));
if(intVector[0] && endIntVector[0] && intVector.size() == endIntVector.size())
{
for(size_t i = 0, size = intVector.size(); i < size; ++i)
{
tmp = intVector;
while(intVector <= endIntVector)
{
if(actionItemMap.find(intVector) != actionItemMap.end())
{
if(!override)
{
std::cout << "[Warning - Actions::registerEvent] Duplicate registered item with aid: " << intVector <<
", in fromaid: " << tmp << " and toaid: " << endIntVector << std::endl;
intVector++;
continue;
}
else
delete actionItemMap[intVector];
}
actionItemMap[intVector++] = new Action(action);
}
}
}
else
std::cout << "[Warning - Actions::registerEvent] Malformed entry (from action: \"" << strValue <<
"\", to action: \"" << endStrValue << "\")" << std::endl;
}
return success;
}
ReturnValue Actions::canUse(const Player* player, const Position& pos)
{
const Position& playerPos = player->getPosition();
if(pos.x == 0xFFFF)
return RET_NOERROR;
if(playerPos.z > pos.z)
return RET_FIRSTGOUPSTAIRS;
if(playerPos.z < pos.z)
return RET_FIRSTGODOWNSTAIRS;
if(!Position::areInRange<1,1,0>(playerPos, pos))
return RET_TOOFARAWAY;
return RET_NOERROR;
}
ReturnValue Actions::canUse(const Player* player, const Position& pos, const Item* item)
{
Action* action = NULL;
if((action = getAction(item, ACTION_UNIQUEID)))
return action->canExecuteAction(player, pos);
if((action = getAction(item, ACTION_ACTIONID)))
return action->canExecuteAction(player, pos);
if((action = getAction(item, ACTION_ITEMID)))
return action->canExecuteAction(player, pos);
if((action = getAction(item, ACTION_RUNEID)))
return action->canExecuteAction(player, pos);
return RET_NOERROR;