Skip to content

Commit

Permalink
feat: basic things attributes modification talkaction
Browse files Browse the repository at this point in the history
  • Loading branch information
omarcopires committed Oct 30, 2024
1 parent 6c00cc0 commit fbf5ef3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 301 deletions.
335 changes: 35 additions & 300 deletions data/scripts/talkactions/god/attributes.lua
Original file line number Diff line number Diff line change
@@ -1,315 +1,50 @@
local itemFunctions = {
["actionid"] = {
isActive = true,
targetFunction = function(item, target)
return item:setActionId(target)
end,
},
["action"] = {
isActive = true,
targetFunction = function(item, target)
return item:setActionId(target)
end,
},
["aid"] = {
isActive = true,
targetFunction = function(item, target)
return item:setActionId(target)
end,
},
["uniqueid"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_UNIQUEID, target)
end,
},
["unique"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_UNIQUEID, target)
end,
},
["uid"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_UNIQUEID, target)
end,
},
["description"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, target)
end,
},
["desc"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, target)
end,
},
["name"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_NAME, target)
end,
},
["remove"] = {
isActive = true,
targetFunction = function(item, target)
return item:remove()
end,
},
["decay"] = {
isActive = true,
targetFunction = function(item, target)
return item:decay()
end,
},
["transform"] = {
isActive = true,
targetFunction = function(item, target)
return item:transform(target)
end,
},
["clone"] = {
isActive = true,
targetFunction = function(item, target)
return item:clone()
end,
},
["attack"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_ATTACK, target)
end,
},
["defense"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_DEFENSE, target)
end,
},
["extradefense"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_EXTRADEFENSE, target)
end,
},
["charge"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_CHARGES, target)
end,
},
["armor"] = {
isActive = true,
targetFunction = function(item, target)
return item:setAttribute(ITEM_ATTRIBUTE_ARMOR, target)
end,
},
}

local creatureFunctions = {
["health"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:addHealth(target)
end,
},
["mana"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:addMana(target)
end,
},
["speed"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:changeSpeed(target)
end,
},
["droploot"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:setDropLoot(target)
end,
},
["skull"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:setSkull(target)
end,
},
["direction"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:setDirection(target)
end,
},
["maxHealth"] = {
isActive = true,
targetFunction = function(creature, target)
return creature:setMaxHealth(target)
end,
},
["say"] = {
isActive = true,
targetFunction = function(creature, target)
creature:say(target, TALKTYPE_SAY)
end,
},
}

local playerFunctions = {
["fyi"] = {
isActive = true,
targetFunction = function(player, target)
return player:popupFYI(target)
end,
},
["tutorial"] = {
isActive = true,
targetFunction = function(player, target)
return player:sendTutorial(target)
end,
},
["guildnick"] = {
isActive = true,
targetFunction = function(player, target)
return player:setGuildNick(target)
end,
},
["group"] = {
isActive = true,
targetFunction = function(player, target)
return player:setGroup(Group(target))
end,
},
["vocation"] = {
isActive = true,
targetFunction = function(player, target)
return player:setVocation(Vocation(target))
end,
},
["stamina"] = {
isActive = true,
targetFunction = function(player, target)
return player:setStamina(target)
end,
},
["town"] = {
isActive = true,
targetFunction = function(player, target)
return player:setTown(Town(target))
end,
},
["balance"] = {
isActive = true,
targetFunction = function(player, target)
return player:setBankBalance(target + player:getBankBalance())
end,
},
["save"] = {
isActive = true,
targetFunction = function(player, target)
return player:save()
end,
},
["type"] = {
isActive = true,
targetFunction = function(player, target)
return player:setAccountType(target)
end,
},
["skullTime"] = {
isActive = true,
targetFunction = function(player, target)
return player:setSkullTime(target)
end,
},
["maxMana"] = {
isActive = true,
targetFunction = function(player, target)
return player:setMaxMana(target)
end,
},
["maxHealth"] = {
isActive = true,
targetFunction = function(player, target)
return player:setMaxHealth(target)
end,
},
["addItem"] = {
isActive = true,
targetFunction = function(player, target)
return player:addItem(target, 1)
end,
},
["removeItem"] = {
isActive = true,
targetFunction = function(player, target)
return player:removeItem(target, 1)
end,
},
["premium"] = {
isActive = true,
targetFunction = function(player, target)
return player:addPremiumDays(target)
end,
},
}

local attributes = TalkAction("/attr")

function attributes.onSay(player, words, param)
-- create log
logCommand(player, words, param)

if param == "" then
player:sendCancelMessage("Command param required.")
return true
local position = player:getPosition()
position:getNextPosition(player:getDirection())

local tile = Tile(position)
if not tile then
player:sendTextMessage(MESSAGE_STATUS, "There is no tile in front of you.")
return false
end

local position = player:getPosition()
position:getNextPosition(player:getDirection(), 1)
local thing = tile:getTopVisibleThing(player)
if not thing then
player:sendTextMessage(MESSAGE_STATUS, "There is an empty tile in front of you.")
return false
end

local split = param:split(",")
local itemFunction, creatureFunction, playerFunction = itemFunctions[split[1]:lower()], creatureFunctions[split[1]:lower()], playerFunctions[split[1]:lower()]
if itemFunction and itemFunction.isActive then
local item = Tile(position):getTopVisibleThing(player)
if not item or not item:isItem() then
player:sendCancelMessage("Item not found.")
return true
end
if itemFunction.targetFunction(item, split[2]) then
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
else
player:sendCancelMessage("You cannot add that attribute to this item.")
end
elseif creatureFunction and creatureFunction.isActive then
local creature = Tile(position):getTopCreature()
if not creature or not creature:isCreature() then
player:sendCancelMessage("Creature not found.")
return true
end
if creatureFunction.targetFunction(creature, split[2]) then
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
else
player:sendCancelMessage("You cannot add that attribute to this creature.")
end
elseif playerFunction and playerFunction.isActive then
local targetPlayer = Tile(position):getTopCreature()
if not targetPlayer or not targetPlayer:getPlayer() then
player:sendCancelMessage("Player not found.")
return true
local separatorPos = param:find(",")
if not separatorPos then
player:sendTextMessage(MESSAGE_STATUS, string.format("Usage: %s attribute, value.", words))
return false
end

local attribute = string.trim(param:sub(0, separatorPos - 1))
local value = string.trim(param:sub(separatorPos + 1))

if thing:isItem() then
local attributeId = Game.getItemAttributeByName(attribute)
if attributeId == ITEM_ATTRIBUTE_NONE then
player:sendTextMessage(MESSAGE_STATUS, "Invalid attribute name.")
return false
end
if playerFunction.targetFunction(targetPlayer, split[2]) then
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
else
player:sendCancelMessage("You cannot add that attribute to this player.")

if not thing:setAttribute(attribute, value) then
player:sendTextMessage(MESSAGE_STATUS, "Could not set attribute.")
return false
end

player:sendTextMessage(MESSAGE_STATUS, string.format("Attribute %s set to: %s", attribute, thing:getAttribute(attributeId)))
position:sendMagicEffect(CONST_ME_MAGIC_GREEN)
else
player:sendCancelMessage("Unknown attribute.")
player:sendTextMessage(MESSAGE_STATUS, "Thing in front of you is not supported.")
return false
end
return true
end
Expand Down
6 changes: 6 additions & 0 deletions src/lua/functions/core/game/game_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ int GameFunctions::luaGameSetWorldType(lua_State* L) {
return 1;
}

int GameFunctions::luaGameGetItemAttributeByName(lua_State* L) {
// Game.getItemAttributeByName(name)
lua_pushnumber(L, static_cast<lua_Number>(stringToItemAttribute(getString(L, 1))));
return 1;
}

int GameFunctions::luaGameGetReturnMessage(lua_State* L) {
// Game.getReturnMessage(value)
const ReturnValue value = getNumber<ReturnValue>(L, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/core/game/game_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class GameFunctions final : LuaScriptInterface {
registerMethod(L, "Game", "getWorldType", GameFunctions::luaGameGetWorldType);
registerMethod(L, "Game", "setWorldType", GameFunctions::luaGameSetWorldType);

registerMethod(L, "Game", "getItemAttributeByName", GameFunctions::luaGameGetItemAttributeByName);
registerMethod(L, "Game", "getReturnMessage", GameFunctions::luaGameGetReturnMessage);

registerMethod(L, "Game", "createItem", GameFunctions::luaGameCreateItem);
Expand Down Expand Up @@ -125,6 +126,7 @@ class GameFunctions final : LuaScriptInterface {
static int luaGameGetWorldType(lua_State* L);
static int luaGameSetWorldType(lua_State* L);

static int luaGameGetItemAttributeByName(lua_State* L);
static int luaGameGetReturnMessage(lua_State* L);

static int luaGameCreateItem(lua_State* L);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ ItemAttribute_t stringToItemAttribute(const std::string &str) {
return ItemAttribute_t::LOOTMESSAGE_SUFFIX;
}

g_logger().error("[{}] attribute type {} is not registered", __FUNCTION__, str);
g_logger().warn("[{}] attribute type {} is not registered", __FUNCTION__, str);
return ItemAttribute_t::NONE;
}

Expand Down

0 comments on commit fbf5ef3

Please sign in to comment.