forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove bit set from icons enum and use std::bitset (opentibiabr#…
…2782) This update modifies how we manage player icons in the ProtocolGame class by transitioning from using a traditional enum to enum class. The prior method involved a static enum bitset which was directly converted to a uint32_t for protocol communication. This approach was inflexible as it required compile-time knowledge of the bitset's size, limiting our ability to dynamically handle an expanding set of player icons. By adopting enum class, we gain stronger type safety and better namespace isolation, enhancing code clarity and reducing potential errors due to implicit type conversions. We've replaced the static bitset with a std::unordered_set for storing icons, granting dynamic management capabilities without a fixed-size constraint. At runtime, this set is converted into a std::bitset and subsequently into a uint32_t when transmitting to the protocol. This refactoring not only simplifies maintenance but also scales more effectively with additions to the icon set, as demonstrated by the inclusion of new icons such as 'bakragore'. To facilitate testing and interaction with this new system, a corresponding talk action /testicon has been implemented. These changes prepare our codebase for future enhancements and ensure that icon management remains robust and adaptable as new requirements emerge.
- Loading branch information
Showing
16 changed files
with
362 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
local function convertIconsToBitValue(iconList) | ||
local bitObj = NewBit(0) | ||
for icon in string.gmatch(iconList, "%d+") do | ||
icon = tonumber(icon) | ||
if icon then | ||
local flag = bit.lshift(1, icon - 1) | ||
bitObj:updateFlag(flag) | ||
end | ||
end | ||
return bitObj:getNumber() | ||
end | ||
|
||
function Player:sendNormalIcons(icons) | ||
local msg = NetworkMessage() | ||
msg:addByte(0xA2) | ||
msg:addU32(icons) | ||
msg:addByte(0) | ||
msg:sendToPlayer(self) | ||
end | ||
|
||
function Player:sendIconBakragore(specialIcon) | ||
local msg = NetworkMessage() | ||
msg:addByte(0xA3) | ||
msg:addU32(specialIcon) | ||
msg:addByte(0) | ||
msg:sendToPlayer(self) | ||
end | ||
|
||
--[[ | ||
Usage (normal icons): | ||
/testicon 1 | ||
/testicon 2 | ||
/testicon 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 | ||
Usage (special icons): | ||
/testicon special, 1 | ||
/testicon special, 2 | ||
]] | ||
|
||
local testIcons = TalkAction("/testicon") | ||
|
||
function testIcons.onSay(player, words, param) | ||
if param == "" then | ||
player:sendCancelMessage("Icon required.") | ||
logger.error("[testIcons.onSay] - Icon number's required") | ||
return true | ||
end | ||
|
||
local split = param:split(",") | ||
local firstParam = split[1]:trim():lower() | ||
|
||
if firstParam == "special" and tonumber(split[2]) then | ||
local specialIcon = tonumber(split[2]) | ||
player:sendIconBakragore(specialIcon) | ||
else | ||
local icons = convertIconsToBitValue(param) | ||
player:sendNormalIcons(icons) | ||
end | ||
|
||
return true | ||
end | ||
|
||
testIcons:separator(" ") | ||
testIcons:setDescription("[Usage]: /testicon {icon1}, {icon2}, {icon3}, ... [Usage2]: /testicon special, {icon}") | ||
testIcons:groupType("god") | ||
testIcons:register() | ||
|
||
local condition = Condition(CONDITION_BAKRAGORE, CONDITIONID_DEFAULT, 0, true) | ||
|
||
local bakragoreIcon = TalkAction("/bakragoreicon") | ||
|
||
function bakragoreIcon.onSay(player, words, param) | ||
if param == "" then | ||
player:sendCancelMessage("Icon number required.") | ||
logger.error("[addBakragoreIcon.onSay] - Icon number's required") | ||
return true | ||
end | ||
|
||
if param == "remove" then | ||
for i = 1, 10 do | ||
if player:hasCondition(CONDITION_BAKRAGORE, i) then | ||
player:removeCondition(CONDITION_BAKRAGORE, i) | ||
end | ||
end | ||
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Removed all Bakragore icons.") | ||
return true | ||
end | ||
|
||
local numParam = tonumber(param) | ||
if numParam then | ||
if player:hasCondition(CONDITION_BAKRAGORE, numParam) then | ||
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You already have the Bakragore icon.") | ||
return true | ||
end | ||
|
||
condition:setParameter(CONDITION_PARAM_SUBID, numParam) | ||
condition:setParameter(CONDITION_PARAM_TICKS, -1) | ||
player:addCondition(condition) | ||
|
||
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Added Bakragore icon with ID: " .. numParam) | ||
end | ||
|
||
return true | ||
end | ||
|
||
bakragoreIcon:separator(" ") | ||
bakragoreIcon:groupType("god") | ||
bakragoreIcon:register() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.