void LuaScript::assignCallback(Script::Ref &function) { assert(lua_isfunction(mRootState, -1)); // If there is already a callback set, replace it if (function.isValid()) luaL_unref(mRootState, LUA_REGISTRYINDEX, function.value); function.value = luaL_ref(mRootState, LUA_REGISTRYINDEX); }
static bool executeCallback(Script::Ref function, Entity &entity) { if (!function.isValid()) return false; Script *script = ScriptManager::currentState(); script->prepare(function); script->push(&entity); script->execute(entity.getMap()); return true; }
static bool executeCallback(Script::Ref function, Character *character) { if (!function.isValid()) return false; Script *script = ScriptManager::currentState(); script->prepare(function); script->push(character); script->execute(character->getMap()); return true; }
void Monster::processAttack() { if (!mTarget) { setAction(STAND); return; } if (!mCurrentAttack) return; mAttackTimeout.set(mCurrentAttack->aftDelay + mCurrentAttack->preDelay); float damageFactor = mCurrentAttack->damageFactor; Damage dmg; dmg.skill = 0; dmg.base = getModifiedAttribute(MOB_ATTR_PHY_ATK_MIN) * damageFactor; dmg.delta = getModifiedAttribute(MOB_ATTR_PHY_ATK_DELTA) * damageFactor; dmg.cth = getModifiedAttribute(ATTR_ACCURACY); dmg.element = mCurrentAttack->element; dmg.range = mCurrentAttack->range; int hit = performAttack(mTarget, dmg); if (!mCurrentAttack->scriptEvent.empty() && hit > -1) { Script::Ref function = mSpecy->getEventCallback(mCurrentAttack->scriptEvent); if (function.isValid()) { Script *script = ScriptManager::currentState(); script->setMap(getMap()); script->prepare(function); script->push(this); script->push(mTarget); script->push(hit); script->execute(); } } }
static void callVariableCallback(Script::Ref &function, const std::string &key, const std::string &value, MapComposite *map) { if (function.isValid()) { Script *s = ScriptManager::currentState(); s->prepare(function); s->push(key); s->push(value); s->execute(map); } }
bool ScriptManager::performCraft(Being *crafter, const std::list<InventoryItem> &recipe) { if (!_craftCallback.isValid()) { LOG_WARN("No crafting callback set! Crafting disabled."); return false; } _currentState->prepare(_craftCallback); _currentState->push(crafter); _currentState->push(recipe); _currentState->execute(); return true; }