Skip to content

Commit

Permalink
Lua stuff & other things
Browse files Browse the repository at this point in the history
Added ItemType.usesSlot to global.lua, now the slot constants actually
have a use.
Changed onChangeHealth & onChangeMana to be more flexible.
Added table.create & Item.hasProperty.
Made Game::combatChangeHealth & combatChangeMana a bit faster.
  • Loading branch information
dalkon committed Jul 8, 2014
1 parent 347b1f6 commit 4d1a293
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 422 deletions.
17 changes: 17 additions & 0 deletions data/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,20 @@ end
function Position.getTile(self)
return Tile(self)
end

local slotBits = {
[CONST_SLOT_HEAD] = SLOTP_HEAD,
[CONST_SLOT_NECKLACE] = SLOTP_NECKLACE,
[CONST_SLOT_BACKPACK] = SLOTP_BACKPACK,
[CONST_SLOT_ARMOR] = SLOTP_ARMOR,
[CONST_SLOT_RIGHT] = SLOTP_RIGHT,
[CONST_SLOT_LEFT] = SLOTP_LEFT,
[CONST_SLOT_LEGS] = SLOTP_LEGS,
[CONST_SLOT_FEET] = SLOTP_FEET,
[CONST_SLOT_RING] = SLOTP_RING,
[CONST_SLOT_AMMO] = SLOTP_AMMO
}

function ItemType.usesSlot(self, slot)
return bit.band(self:getSlotPosition(), slotBits[slot] or 0) ~= 0
end
2 changes: 1 addition & 1 deletion src/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ bool ChatChannel::executeOnSpeakEvent(const Player& player, SpeakClasses& type,
result = LuaScriptInterface::getBoolean(L, -1);
} else if (lua_isnumber(L, -1)) {
result = true;
type = static_cast<SpeakClasses>(LuaScriptInterface::getNumber<uint32_t>(L, -1));
type = LuaScriptInterface::getNumber<SpeakClasses>(L, -1);
}
lua_pop(L, 1);
}
Expand Down
1 change: 1 addition & 0 deletions src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Combat::~Combat()
CombatDamage Combat::getCombatDamage(Creature* creature, Creature* target) const
{
CombatDamage damage;
damage.origin = params.origin;
if (!creature) {
return damage;
}
Expand Down
7 changes: 7 additions & 0 deletions src/combat.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ struct CombatParams {
valueCallback = nullptr;
tileCallback = nullptr;
targetCallback = nullptr;

origin = ORIGIN_NONE;
}

std::forward_list<const Condition*> conditionList;
Expand All @@ -93,6 +95,7 @@ struct CombatParams {

ConditionType_t dispelType;
CombatType_t combatType;
CombatOrigin origin;

uint8_t impactEffect;
uint8_t distanceEffect;
Expand Down Expand Up @@ -317,6 +320,10 @@ class Combat
postCombatEffects(caster, pos, params);
}

void setOrigin(CombatOrigin origin) {
params.origin = origin;
}

protected:
static void doCombatDefault(Creature* caster, Creature* target, const CombatParams& params);

Expand Down
10 changes: 7 additions & 3 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1236,19 +1236,23 @@ bool ConditionDamage::getNextDamage(int32_t& damage)
return false;
}

bool ConditionDamage::doDamage(Creature* creature, int32_t damage)
bool ConditionDamage::doDamage(Creature* creature, int32_t healthChange)
{
if (creature->isSuppress(getType())) {
return true;
}

CombatType_t combatType = Combat::ConditionToDamageType(conditionType);
Creature* attacker = g_game.getCreatureByID(owner);
if (g_game.combatBlockHit(combatType, attacker, creature, damage, false, false, field)) {
if (g_game.combatBlockHit(combatType, attacker, creature, healthChange, false, false, field)) {
return false;
}

return g_game.combatChangeHealth(combatType, attacker, creature, damage);
CombatDamage damage;
damage.origin = ORIGIN_CONDITION;
damage.primary.value = healthChange;
damage.primary.type = combatType;
return g_game.combatChangeHealth(attacker, creature, damage);
}

void ConditionDamage::endCondition(Creature*)
Expand Down
2 changes: 1 addition & 1 deletion src/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class ConditionDamage: public Condition
std::list<IntervalInfo> damageList;

bool getNextDamage(int32_t& damage);
bool doDamage(Creature* creature, int32_t damage);
bool doDamage(Creature* creature, int32_t healthChange);
bool updateCondition(const Condition* addCondition);
};

Expand Down
82 changes: 53 additions & 29 deletions src/creatureevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,31 +433,33 @@ bool CreatureEvent::executeOnKill(Creature* creature, Creature* target)
return m_scriptInterface->callFunction(2);
}

bool CreatureEvent::executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId)
void CreatureEvent::executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId)
{
//onModalWindow(cid, modalWindowId, buttonId, choiceId)
//onModalWindow(player, modalWindowId, buttonId, choiceId)
if (!m_scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeModalWindow] Call stack overflow" << std::endl;
return false;
return;
}

ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
env->setScriptId(m_scriptId, m_scriptInterface);

lua_State* L = m_scriptInterface->getLuaState();

m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, player->getID());

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

lua_pushnumber(L, modalWindowId);
lua_pushnumber(L, buttonId);
lua_pushnumber(L, choiceId);

return m_scriptInterface->callFunction(4);
m_scriptInterface->callVoidFunction(4);
}

bool CreatureEvent::executeTextEdit(Player* player, Item* item, const std::string& text)
{
//onTextEdit(cid, item, text)
//onTextEdit(player, item, text)
if (!m_scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeTextEdit] Call stack overflow" << std::endl;
return false;
Expand All @@ -467,74 +469,96 @@ bool CreatureEvent::executeTextEdit(Player* player, Item* item, const std::strin
env->setScriptId(m_scriptId, m_scriptInterface);

lua_State* L = m_scriptInterface->getLuaState();

m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, player->getID());

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

LuaScriptInterface::pushThing(L, item, env->addThing(item));
LuaScriptInterface::pushString(L, text);

return m_scriptInterface->callFunction(3);
}

bool CreatureEvent::executeChangeHealth(Creature* creature, Creature* attacker, const CombatDamage& damage)
void CreatureEvent::executeChangeHealth(Creature* creature, Creature* attacker, CombatDamage& damage)
{
//onChangeHealth(cid, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType)
//onChangeHealth(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if (!m_scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeChangeHealth] Call stack overflow" << std::endl;
return false;
return;
}

ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
env->setScriptId(m_scriptId, m_scriptInterface);

lua_State* L = m_scriptInterface->getLuaState();

m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, creature->getID());

LuaScriptInterface::pushUserdata(L, creature);
LuaScriptInterface::setCreatureMetatable(L, -1, creature);
if (attacker) {
lua_pushnumber(L, attacker->getID());
LuaScriptInterface::pushUserdata(L, attacker);
LuaScriptInterface::setCreatureMetatable(L, -1, attacker);
} else {
lua_pushnumber(L, 0);
lua_pushnil(L);
}
lua_pushnumber(L, damage.primary.value);
lua_pushnumber(L, damage.primary.type);
lua_pushnumber(L, damage.secondary.value);
lua_pushnumber(L, damage.secondary.type);
lua_pushnumber(L, damage.origin);

return m_scriptInterface->callFunction(6);
if (m_scriptInterface->protectedCall(L, 7, 4) != 0) {
LuaScriptInterface::reportError(nullptr, LuaScriptInterface::popString(L));
} else {
damage.primary.value = LuaScriptInterface::getNumber<int32_t>(L, -4);
damage.primary.type = LuaScriptInterface::getNumber<CombatType_t>(L, -3);
damage.secondary.value = LuaScriptInterface::getNumber<int32_t>(L, -2);
damage.secondary.type = LuaScriptInterface::getNumber<CombatType_t>(L, -1);
lua_pop(L, 4);
}
m_scriptInterface->resetScriptEnv();
}

bool CreatureEvent::executeChangeMana(Creature* creature, Creature* attacker, int32_t manaChange)
void CreatureEvent::executeChangeMana(Creature* creature, Creature* attacker, int32_t& manaChange)
{
//onChangeMana(cid, attacker, manaChange)
//onChangeMana(creature, attacker, manaChange)
if (!m_scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeChangeMana] Call stack overflow" << std::endl;
return false;
return;
}

ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
env->setScriptId(m_scriptId, m_scriptInterface);

lua_State* L = m_scriptInterface->getLuaState();

m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, creature->getID());

LuaScriptInterface::pushUserdata(L, creature);
LuaScriptInterface::setCreatureMetatable(L, -1, creature);
if (attacker) {
lua_pushnumber(L, attacker->getID());
LuaScriptInterface::pushUserdata(L, attacker);
LuaScriptInterface::setCreatureMetatable(L, -1, attacker);
} else {
lua_pushnumber(L, 0);
lua_pushnil(L);
}
lua_pushnumber(L, manaChange);

return m_scriptInterface->callFunction(3);
if (m_scriptInterface->protectedCall(L, 3, 1) != 0) {
LuaScriptInterface::reportError(nullptr, LuaScriptInterface::getString(L, -1));
} else {
manaChange = LuaScriptInterface::getNumber<int32_t>(L, -1);
}
lua_pop(L, 1);
m_scriptInterface->resetScriptEnv();
}

bool CreatureEvent::executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer)
void CreatureEvent::executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer)
{
//onExtendedOpcode(cid, opcode, buffer)
//onExtendedOpcode(player, opcode, buffer)
if (!m_scriptInterface->reserveScriptEnv()) {
std::cout << "[Error - CreatureEvent::executeExtendedOpcode] Call stack overflow" << std::endl;
return false;
return;
}

ScriptEnvironment* env = m_scriptInterface->getScriptEnv();
Expand All @@ -550,5 +574,5 @@ bool CreatureEvent::executeExtendedOpcode(Player* player, uint8_t opcode, const
lua_pushnumber(L, opcode);
LuaScriptInterface::pushString(L, buffer);

return m_scriptInterface->callFunction(3);
m_scriptInterface->callVoidFunction(3);
}
8 changes: 4 additions & 4 deletions src/creatureevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ class CreatureEvent : public Event
bool executeOnDeath(Creature* creature, Item* corpse, Creature* killer, Creature* mostDamageKiller, bool lastHitUnjustified, bool mostDamageUnjustified);
bool executeOnKill(Creature* creature, Creature* target);
bool executeAdvance(Player* player, skills_t, uint32_t, uint32_t);
bool executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId);
void executeModalWindow(Player* player, uint32_t modalWindowId, uint8_t buttonId, uint8_t choiceId);
bool executeTextEdit(Player* player, Item* item, const std::string& text);
bool executeChangeHealth(Creature* creature, Creature* attacker, const CombatDamage& damage);
bool executeChangeMana(Creature* creature, Creature* attacker, int32_t manaChange);
bool executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer);
void executeChangeHealth(Creature* creature, Creature* attacker, CombatDamage& damage);
void executeChangeMana(Creature* creature, Creature* attacker, int32_t& manaChange);
void executeExtendedOpcode(Player* player, uint8_t opcode, const std::string& buffer);
//

protected:
Expand Down
14 changes: 13 additions & 1 deletion src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,26 @@ struct ModalWindow
: title(title), message(message), id(id), defaultEnterButton(0xFF), defaultEscapeButton(0xFF), priority(false) {}
};

struct CombatDamage {
enum CombatOrigin
{
ORIGIN_NONE = 0,
ORIGIN_CONDITION = 1,
ORIGIN_SPELL = 2,
ORIGIN_MELEE = 3,
ORIGIN_RANGED = 4
};

struct CombatDamage
{
struct {
CombatType_t type;
int32_t value;
} primary, secondary;

CombatOrigin origin;
CombatDamage()
{
origin = ORIGIN_NONE;
primary.type = secondary.type = COMBAT_NONE;
primary.value = secondary.value = 0;
}
Expand Down
Loading

0 comments on commit 4d1a293

Please sign in to comment.