library NoItemSharing /*
No Item Sharing v1.11 by Flux
-------------------------------------------------------------------------
This System disable players from selling other player's item.
It also allows other players from not using a certain player's item
and/or disable its Stats Bonus.
It also allows to change icons and tooltips of Shared Items for easily
indicating the difference in-game.
-------------------------------------------------------------------------
*/ requires /*
*/ optional Table /* http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
If not found, this system will use a hashtable instead. Hashtables is limited to
256 per map. If your map is close to the hashtable limit, it is recommended
to use Table
NOTES:
- Dropping a Shared Item in the ground will turn it into the Original Item
thus Shared Item only appear in Inventory. That way, you don't need to edit the Text
Description (ides) of the Shared Version decreasing the overall purple data fields
of the map.
- The Original Version and the Shared Version must be manually created at Object Editor
- Useful in AoS, Hero Arena and Hero Defense Maps by not allowing team feeding to a certain
player who will carry the whole team.
- Added Bonus: Coincidentally make the Treasure Chest Play an opening animation when dropped.
- To fully understand how the system works, test with DEBUG_SYSTEM = true and run in Debug Mode.
-----------------------------------------------------------------------------------------
Item Shared Version can either be:
- Disabled Shared: The Shared Item is unuseable/provides no stats and cannot be sold
It is recommended to use a Disabled Icon for items like this.
Demo Example: Boots of Speed, Claws of Attack.
- Usabled Shared: The Shared Item can be used but cannot be sold.
Demo Example: Clarity Potion
-----------------------------------------------------------------------------------------
******************************************************************
***************************** API: *******************************
******************************************************************
function ItemShare.data takes integer origId, integer shareId returns nothing
- Tells the system the the shared item type of origId is sharedId.
function ItemShare.getOwner takes item it returns player
- Returns the owner of a certain item, returns null if item has no owner.
- Returns null if item type is not in Database.
function ItemShare.setOwner item it, player p returns nothing
- Set the new owner of an item.
- It automatically transform the items based on the new owner. If the new
owner is null, the item is automatically dropped and the first player to
pick it will be the new owner.
******************************************************************
CREDITS:
Bribe - Table
*/
//===============================================================================
//============================= CONFIGURATION ===================================
//===============================================================================
//! textmacro ITEM_SHARING_INITIAL_DATA
//*** Input Item rawcodes here **
//call ItemShare.data(original Item Type, shared Item Type)
call ItemShare.data('I000', 'I001')
call ItemShare.data('I002', 'I003')
call ItemShare.data('I004', 'I005')
call ItemShare.data('I006', 'I007')
//** Add more here **
//call ItemShare.data(original Item Type, shared Item Type)
//........
//! endtextmacro
globals
//Show Debug Messages to help you understand how the system works.
private constant boolean DEBUG_SYSTEM = true
endglobals
//===============================================================================
//=========================== END CONFIGURATION =================================
//===============================================================================
struct ItemShare extends array
//Integer: alternate type (uses itemTypeId)
//Player: owner (uses GetHandleId)
//Boolean: status (uses itemTypeId) - true if original, false if shared
static if LIBRARY_Table then
private static Table tb
private static Table holder
else
private static hashtable hash = InitHashtable()
endif
private static trigger pickTrg = CreateTrigger()
private static trigger dropTrg = CreateTrigger()
static if DEBUG_SYSTEM then
private static constant string prefix = "|cffffcc00[NoItemSharing]|r: "
endif
static method data takes integer origId, integer shareId returns nothing
static if LIBRARY_Table then
set tb[origId] = shareId
set tb[shareId] = origId
set tb.boolean[origId] = true
set tb.boolean[shareId] = false
else
call SaveInteger(hash, origId, 0, shareId)
call SaveInteger(hash, shareId, 0, origId)
call SaveBoolean(hash, origId, 0, true)
call SaveBoolean(hash, shareId, 0, false)
endif
endmethod
private static method convert takes nothing returns nothing
local timer t = GetExpiredTimer()
local integer handleId = GetHandleId(t)
local real x
local real y
static if LIBRARY_Table then
local item it = tb.item[handleId]
local integer itemType1 = GetItemTypeId(it)
local integer itemType2 = tb[itemType1]
local boolean shared = not tb.boolean[itemType1] and tb.boolean[itemType2]
local integer id = GetHandleId(it)
local player owner = tb.player[id]
else
local item it = LoadItemHandle(hash, handleId, 0)
local integer itemType1 = GetItemTypeId(it)
local integer itemType2 = LoadInteger(hash, itemType1, 0)
local boolean shared = not LoadBoolean(hash, itemType1, 0) and LoadBoolean(hash, itemType2, 0)
local integer id = GetHandleId(it)
local player owner = LoadPlayerHandle(hash, id, 0)
endif
if shared and not IsItemOwned(it) then
set x = GetItemX(it)
set y = GetItemY(it)
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + "|cffffcc00" + GetItemName(it) + "|r is converted to its original form")
endif
call RemoveItem(it)
set it = CreateItem(itemType2, x, y)
static if LIBRARY_Table then
call tb.player.remove(id)
set tb.player[GetHandleId(it)] = owner
else
call RemoveSavedHandle(hash, id, 0)
call SavePlayerHandle(hash, GetHandleId(it), 0, owner)
endif
endif
static if LIBRARY_Table then
call tb.item.remove(handleId)
else
call RemoveSavedHandle(hash, handleId, 0)
endif
set it = null
call DestroyTimer(t)
set t = null
set owner = null
endmethod
private static method onDrop takes nothing returns boolean
local item it = GetManipulatedItem()
local integer itemType1 = GetItemTypeId(it)
local integer id = GetHandleId(it)
local timer t
static if LIBRARY_Table then
local integer itemType2 = tb[itemType1]
if not tb.boolean[itemType1] and tb.boolean[itemType2] then
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + GetPlayerName(GetTriggerPlayer()) + " has dropped a shared Item belonging to " + GetPlayerName(tb.player[GetHandleId(it)]))
endif
set t = CreateTimer()
set tb.item[GetHandleId(t)] = it
call TimerStart(t, 0.0, false, function thistype.convert)
set t = null
else
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + GetPlayerName(GetTriggerPlayer()) + " has dropped an original Item belonging to " + GetPlayerName(tb.player[GetHandleId(it)]))
endif
endif
else
local integer itemType2 = LoadInteger(hash, itemType1, 0)
if not LoadBoolean(hash, itemType1, 0) and LoadBoolean(hash, itemType2, 0) then
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + GetPlayerName(GetTriggerPlayer()) + " has dropped a shared Item belonging to " + GetPlayerName(LoadPlayerHandle(hash, GetHandleId(it), 0)))
endif
set t = CreateTimer()
call SaveItemHandle(hash, GetHandleId(t), 0, it)
call TimerStart(t, 0.0, false, function thistype.convert)
set t = null
else
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + GetPlayerName(GetTriggerPlayer()) + " has dropped an original Item belonging to " + GetPlayerName(LoadPlayerHandle(hash, GetHandleId(it), 0)))
endif
endif
endif
static if LIBRARY_Table then
call holder.unit.remove(id)
else
call RemoveSavedHandle(hash, id, 1)
endif
set it = null
return false
endmethod
private static method onPick takes nothing returns boolean
local item it = GetManipulatedItem()
local unit u = GetTriggerUnit()
local integer id = GetHandleId(it)
local player trigPlayer = GetTriggerPlayer()
local integer itemType1 = GetItemTypeId(it)
static if LIBRARY_Table then
local integer itemType2 = tb[itemType1]
local boolean orig = tb.boolean[itemType1] and not tb.boolean[itemType2]
local boolean shared = not tb.boolean[itemType1] and tb.boolean[itemType2]
local player prevPlayer = tb.player[id]
else
local integer itemType2 = LoadInteger(hash, itemType1, 0)
local boolean orig = LoadBoolean(hash, itemType1, 0) and not LoadBoolean(hash, itemType2, 0)
local boolean shared = not LoadBoolean(hash, itemType1, 0) and LoadBoolean(hash, itemType2, 0)
local player prevPlayer = LoadPlayerHandle(hash, id, 0)
endif
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + GetPlayerName(trigPlayer) + " has picked |cffffcc00" + GetItemName(it) + "|r which originally belongs to " + GetPlayerName(prevPlayer))
endif
//If item is in the database
if (orig or shared) then
//If item has no owner
if prevPlayer == null then
static if LIBRARY_Table then
set tb.player[id] = trigPlayer
else
call SavePlayerHandle(hash, id, 0, trigPlayer)
endif
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + GetPlayerName(trigPlayer) + " is the now the owner of |cffffcc00" + GetItemName(it) + "|r")
endif
endif
//orig -> shared will happen if a player happens to pick an item belonging to other players
//shared -> orig will happen if you're the owner or if there is no owner or if you're the owner
if (orig and trigPlayer != prevPlayer and prevPlayer != null) or (shared and (prevPlayer == null or trigPlayer == prevPlayer)) then
//Convert
call DisableTrigger(pickTrg)
call DisableTrigger(dropTrg)
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + "|cffffcc00" + GetItemName(it) + "|r is converted")
endif
call RemoveItem(it)
set it = CreateItem(itemType2, 0, 0)
call UnitAddItem(u, it)
static if LIBRARY_Table then
call tb.player.remove(id)
set tb.player[GetHandleId(it)] = prevPlayer
else
call RemoveSavedHandle(hash, id, 0)
call SavePlayerHandle(hash, GetHandleId(it), 0, prevPlayer)
endif
call EnableTrigger(pickTrg)
call EnableTrigger(dropTrg)
endif
//Save Unit holding the item to have O(1) operation for unit dropping the item
static if LIBRARY_Table then
set holder.unit[GetHandleId(it)] = u
else
call SaveUnitHandle(hash, GetHandleId(it), 1, u)
endif
else
static if DEBUG_SYSTEM then
call BJDebugMsg(prefix + "|cffffcc00" + GetItemName(it) + "|r is not in the database. It will treate it as a normal item")
endif
endif
set it = null
set u = null
set trigPlayer = null
set prevPlayer = null
return false
endmethod
private static method onInit takes nothing returns nothing
local integer i = 0
loop
call TriggerRegisterPlayerUnitEvent(pickTrg, Player(i), EVENT_PLAYER_UNIT_PICKUP_ITEM, null)
call TriggerRegisterPlayerUnitEvent(dropTrg, Player(i), EVENT_PLAYER_UNIT_DROP_ITEM, null)
set i = i + 1
exitwhen i == bj_MAX_PLAYER_SLOTS
endloop
call TriggerAddCondition(pickTrg, Condition(function thistype.onPick))
call TriggerAddCondition(dropTrg, Condition(function thistype.onDrop))
static if LIBRARY_Table then
set tb = Table.create()
set holder = Table.create()
endif
//! runtextmacro ITEM_SHARING_INITIAL_DATA()
endmethod
static method getOwner takes item it returns player
static if LIBRARY_Table then
return tb.player[GetHandleId(it)]
else
return LoadPlayerHandle(hash, GetHandleId(it), 0)
endif
endmethod
static method setOwner takes item it, player p returns nothing
local integer itemType1 = GetItemTypeId(it)
local unit u
static if LIBRARY_Table then
local integer itemType2 = tb[itemType1]
local boolean orig = tb.boolean[itemType1] and not tb.boolean[itemType2]
local boolean shared = not tb.boolean[itemType1] and tb.boolean[itemType2]
else
local integer itemType2 = LoadInteger(hash, itemType1, 0)
local boolean orig = LoadBoolean(hash, itemType1, 0) and not LoadBoolean(hash, itemType2, 0)
local boolean shared = not LoadBoolean(hash, itemType1, 0) and LoadBoolean(hash, itemType2, 0)
endif
if orig or shared then
if p == null then
static if LIBRARY_Table then
call tb.player.remove(GetHandleId(it))
else
call RemoveSavedHandle(hash, GetHandleId(it), 0)
endif
else
static if LIBRARY_Table then
set tb.player[GetHandleId(it)] = p
else
call SavePlayerHandle(hash, GetHandleId(it), 0, p)
endif
endif
//Refresh Item Status
if IsItemOwned(it) then
static if LIBRARY_Table then
set u = holder.unit[GetHandleId(it)]
else
set u = LoadUnitHandle(hash, GetHandleId(it), 1)
endif
call UnitRemoveItem(u, it)
if p != null then
call UnitAddItem(u, it)
endif
set u = null
endif
endif
endmethod
endstruct
endlibrary