Name | Type | is_array | initial_value |
//TESH.scrollpos=0
//TESH.alwaysfold=0
library Table /* made by Bribe, special thanks to Vexorian & Nestharus, version 3.1.0.1
One map, one hashtable. Welcome to NewTable 3.1
This library was originally called NewTable so it didn't conflict with
the API of Table by Vexorian. However, the damage is done and it's too
late to change the library name now. To help with damage control, I
have provided an extension library called TableBC, which bridges all
the functionality of Vexorian's Table except for 2-D string arrays &
the ".flush(integer)" method. I use ".flush()" to flush a child hash-
table, because I wanted the API in NewTable to reflect the API of real
hashtables (I thought this would be more intuitive).
API
------------
struct Table
| static method create takes nothing returns Table
| create a new Table
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush all stored values inside of it
|
| method remove takes integer key returns nothing
| remove the value at index "key"
|
| method operator []= takes integer key, $TYPE$ value returns nothing
| assign "value" to index "key"
|
| method operator [] takes integer key returns $TYPE$
| load the value at index "key"
|
| method has takes integer key returns boolean
| whether or not the key was assigned
|
----------------
struct TableArray
| static method operator [] takes integer array_size returns TableArray
| create a new array of Tables of size "array_size"
|
| method destroy takes nothing returns nothing
| destroy it
|
| method flush takes nothing returns nothing
| flush and destroy it
|
| method operator size takes nothing returns integer
| returns the size of the TableArray
|
| method operator [] takes integer key returns Table
| returns a Table accessible exclusively to index "key"
*/
globals
private integer less = 0 //Index generation for TableArrays (below 0).
private integer more = 8190 //Index generation for Tables.
//Configure it if you use more than 8190 "key" variables in your map (this will never happen though).
private hashtable ht = InitHashtable()
private key sizeK
private key listK
endglobals
private struct dex extends array
static method operator size takes nothing returns Table
return sizeK
endmethod
static method operator list takes nothing returns Table
return listK
endmethod
endstruct
private struct handles extends array
method has takes integer key returns boolean
return HaveSavedHandle(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSavedHandle(ht, this, key)
endmethod
endstruct
private struct agents extends array
method operator []= takes integer key, agent value returns nothing
call SaveAgentHandle(ht, this, key, value)
endmethod
endstruct
//! textmacro NEW_ARRAY_BASIC takes SUPER, FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$(ht, this, key, value)
endmethod
method has takes integer key returns boolean
return HaveSaved$SUPER$(ht, this, key)
endmethod
method remove takes integer key returns nothing
call RemoveSaved$SUPER$(ht, this, key)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//! textmacro NEW_ARRAY takes FUNC, TYPE
private struct $TYPE$s extends array
method operator [] takes integer key returns $TYPE$
return Load$FUNC$Handle(ht, this, key)
endmethod
method operator []= takes integer key, $TYPE$ value returns nothing
call Save$FUNC$Handle(ht, this, key, value)
endmethod
endstruct
private module $TYPE$m
method operator $TYPE$ takes nothing returns $TYPE$s
return this
endmethod
endmodule
//! endtextmacro
//Run these textmacros to include the entire hashtable API as wrappers.
//Don't be intimidated by the number of macros - Vexorian's map optimizer is
//supposed to kill functions which inline (all of these functions inline).
//! runtextmacro NEW_ARRAY_BASIC("Real", "Real", "real")
//! runtextmacro NEW_ARRAY_BASIC("Boolean", "Boolean", "boolean")
//! runtextmacro NEW_ARRAY_BASIC("String", "Str", "string")
//! runtextmacro NEW_ARRAY("Player", "player")
//! runtextmacro NEW_ARRAY("Widget", "widget")
//! runtextmacro NEW_ARRAY("Destructable", "destructable")
//! runtextmacro NEW_ARRAY("Item", "item")
//! runtextmacro NEW_ARRAY("Unit", "unit")
//! runtextmacro NEW_ARRAY("Ability", "ability")
//! runtextmacro NEW_ARRAY("Timer", "timer")
//! runtextmacro NEW_ARRAY("Trigger", "trigger")
//! runtextmacro NEW_ARRAY("TriggerCondition", "triggercondition")
//! runtextmacro NEW_ARRAY("TriggerAction", "triggeraction")
//! runtextmacro NEW_ARRAY("TriggerEvent", "event")
//! runtextmacro NEW_ARRAY("Force", "force")
//! runtextmacro NEW_ARRAY("Group", "group")
//! runtextmacro NEW_ARRAY("Location", "location")
//! runtextmacro NEW_ARRAY("Rect", "rect")
//! runtextmacro NEW_ARRAY("BooleanExpr", "boolexpr")
//! runtextmacro NEW_ARRAY("Sound", "sound")
//! runtextmacro NEW_ARRAY("Effect", "effect")
//! runtextmacro NEW_ARRAY("UnitPool", "unitpool")
//! runtextmacro NEW_ARRAY("ItemPool", "itempool")
//! runtextmacro NEW_ARRAY("Quest", "quest")
//! runtextmacro NEW_ARRAY("QuestItem", "questitem")
//! runtextmacro NEW_ARRAY("DefeatCondition", "defeatcondition")
//! runtextmacro NEW_ARRAY("TimerDialog", "timerdialog")
//! runtextmacro NEW_ARRAY("Leaderboard", "leaderboard")
//! runtextmacro NEW_ARRAY("Multiboard", "multiboard")
//! runtextmacro NEW_ARRAY("MultiboardItem", "multiboarditem")
//! runtextmacro NEW_ARRAY("Trackable", "trackable")
//! runtextmacro NEW_ARRAY("Dialog", "dialog")
//! runtextmacro NEW_ARRAY("Button", "button")
//! runtextmacro NEW_ARRAY("TextTag", "texttag")
//! runtextmacro NEW_ARRAY("Lightning", "lightning")
//! runtextmacro NEW_ARRAY("Image", "image")
//! runtextmacro NEW_ARRAY("Ubersplat", "ubersplat")
//! runtextmacro NEW_ARRAY("Region", "region")
//! runtextmacro NEW_ARRAY("FogState", "fogstate")
//! runtextmacro NEW_ARRAY("FogModifier", "fogmodifier")
//! runtextmacro NEW_ARRAY("Hashtable", "hashtable")
struct Table extends array
// Implement modules for intuitive syntax (tb.handle; tb.unit; etc.)
implement realm
implement booleanm
implement stringm
implement playerm
implement widgetm
implement destructablem
implement itemm
implement unitm
implement abilitym
implement timerm
implement triggerm
implement triggerconditionm
implement triggeractionm
implement eventm
implement forcem
implement groupm
implement locationm
implement rectm
implement boolexprm
implement soundm
implement effectm
implement unitpoolm
implement itempoolm
implement questm
implement questitemm
implement defeatconditionm
implement timerdialogm
implement leaderboardm
implement multiboardm
implement multiboarditemm
implement trackablem
implement dialogm
implement buttonm
implement texttagm
implement lightningm
implement imagem
implement ubersplatm
implement regionm
implement fogstatem
implement fogmodifierm
implement hashtablem
method operator handle takes nothing returns handles
return this
endmethod
method operator agent takes nothing returns agents
return this
endmethod
//set this = tb[GetSpellAbilityId()]
method operator [] takes integer key returns Table
return LoadInteger(ht, this, key)
endmethod
//set tb[389034] = 8192
method operator []= takes integer key, Table tb returns nothing
call SaveInteger(ht, this, key, tb)
endmethod
//set b = tb.has(2493223)
method has takes integer key returns boolean
return HaveSavedInteger(ht, this, key)
endmethod
//call tb.remove(294080)
method remove takes integer key returns nothing
call RemoveSavedInteger(ht, this, key)
endmethod
//Remove all data from a Table instance
method flush takes nothing returns nothing
call FlushChildHashtable(ht, this)
endmethod
//local Table tb = Table.create()
static method create takes nothing returns Table
local Table this = dex.list[0]
if this == 0 then
set this = more + 1
set more = this
else
set dex.list[0] = dex.list[this]
call dex.list.remove(this) //Clear hashed memory
endif
debug set dex.list[this] = -1
return this
endmethod
// Removes all data from a Table instance and recycles its index.
//
// call tb.destroy()
//
method destroy takes nothing returns nothing
debug if dex.list[this] != -1 then
debug call BJDebugMsg("Table Error: Tried to double-free instance: " + I2S(this))
debug return
debug endif
call this.flush()
set dex.list[this] = dex.list[0]
set dex.list[0] = this
endmethod
//! runtextmacro optional TABLE_BC_METHODS()
endstruct
//! runtextmacro optional TABLE_BC_STRUCTS()
struct TableArray extends array
//Returns a new TableArray to do your bidding. Simply use:
//
// local TableArray ta = TableArray[array_size]
//
static method operator [] takes integer array_size returns TableArray
local Table tb = dex.size[array_size] //Get the unique recycle list for this array size
local TableArray this = tb[0] //The last-destroyed TableArray that had this array size
debug if array_size <= 0 then
debug call BJDebugMsg("TypeError: Invalid specified TableArray size: " + I2S(array_size))
debug return 0
debug endif
if this == 0 then
set this = less - array_size
set less = this
else
set tb[0] = tb[this] //Set the last destroyed to the last-last destroyed
call tb.remove(this) //Clear hashed memory
endif
set dex.size[this] = array_size //This remembers the array size
return this
endmethod
//Returns the size of the TableArray
method operator size takes nothing returns integer
return dex.size[this]
endmethod
//This magic method enables two-dimensional[array][syntax] for Tables,
//similar to the two-dimensional utility provided by hashtables them-
//selves.
//
//ta[integer a].unit[integer b] = unit u
//ta[integer a][integer c] = integer d
//
//Inline-friendly when not running in debug mode
//
method operator [] takes integer key returns Table
static if DEBUG_MODE then
local integer i = this.size
if i == 0 then
call BJDebugMsg("IndexError: Tried to get key from invalid TableArray instance: " + I2S(this))
return 0
elseif key < 0 or key >= i then
call BJDebugMsg("IndexError: Tried to get key [" + I2S(key) + "] from outside TableArray bounds: " + I2S(i))
return 0
endif
endif
return this + key
endmethod
//Destroys a TableArray without flushing it; I assume you call .flush()
//if you want it flushed too. This is a public method so that you don't
//have to loop through all TableArray indices to flush them if you don't
//need to (ie. if you were flushing all child-keys as you used them).
//
method destroy takes nothing returns nothing
local Table tb = dex.size[this.size]
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to destroy an invalid TableArray: " + I2S(this))
debug return
debug endif
if tb == 0 then
//Create a Table to index recycled instances with their array size
set tb = Table.create()
set dex.size[this.size] = tb
endif
call dex.size.remove(this) //Clear the array size from hash memory
set tb[this] = tb[0]
set tb[0] = this
endmethod
private static Table tempTable
private static integer tempEnd
//Avoids hitting the op limit
private static method clean takes nothing returns nothing
local Table tb = .tempTable
local integer end = tb + 0x1000
if end < .tempEnd then
set .tempTable = end
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
else
set end = .tempEnd
endif
loop
call tb.flush()
set tb = tb + 1
exitwhen tb == end
endloop
endmethod
//Flushes the TableArray and also destroys it. Doesn't get any more
//similar to the FlushParentHashtable native than this.
//
method flush takes nothing returns nothing
debug if this.size == 0 then
debug call BJDebugMsg("TypeError: Tried to flush an invalid TableArray instance: " + I2S(this))
debug return
debug endif
set .tempTable = this
set .tempEnd = this + this.size
call ForForce(bj_FORCE_PLAYER[0], function thistype.clean)
call this.destroy()
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
/**************************************************************
*
* RegisterPlayerUnitEvent
* v5.1.0.0
* By Magtheridon96
*
* I would like to give a special thanks to Bribe, azlier
* and BBQ for improving this library. For modularity, it only
* supports player unit events.
*
* Functions passed to RegisterPlayerUnitEvent must either
* return a boolean (false) or nothing. (Which is a Pro)
*
* Warning:
* --------
*
* - Don't use TriggerSleepAction inside registered code.
* - Don't destroy a trigger unless you really know what you're doing.
*
* API:
* ----
*
* - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
* - Registers code that will execute when an event fires.
* - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
* - Registers code that will execute when an event fires for a certain player.
* - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
* - Returns the trigger corresponding to ALL functions of a playerunitevent.
*
**************************************************************/
library RegisterPlayerUnitEvent // Special Thanks to Bribe and azlier
globals
private trigger array t
endglobals
function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
local integer i = GetHandleId(p)
local integer k = 15
if t[i] == null then
set t[i] = CreateTrigger()
loop
call TriggerRegisterPlayerUnitEvent(t[i], Player(k), p, null)
exitwhen k == 0
set k = k - 1
endloop
endif
call TriggerAddCondition(t[i], Filter(c))
endfunction
function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
local integer i = 260 + 16 * GetHandleId(p) + GetPlayerId(pl)
if t[i] == null then
set t[i] = CreateTrigger()
call TriggerRegisterPlayerUnitEvent(t[i], pl, p, null)
endif
call TriggerAddCondition(t[i], Filter(c))
endfunction
function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
return t[GetHandleId(p)]
endfunction
endlibrary
//TESH.scrollpos=128
//TESH.alwaysfold=0
library ShopSystem requires Table, RegisterPlayerUnitEvent
/*
Version 1.2 by Adiktuz
Credits to Bribe and Magtheridon96 for Table and RegisterPlayerUnitEvent
A simple shop system which allows you to have multiple shops/shopping category inside a single shop.
If you've read my tutorial about sub-shops maybe you'll be asking what is the difference of this
to the system in the tutorial. Well, this one uses only one shop per player. Also with this one, all
items sold should be set via the trigger editor.
You can create an infinite-level shop with this one.
You can only register 11 items per catergory
Requirements:
JNGP with the latest jass helper
knowledge on how to call functions
knowledge on how to obtain rawcodes
knowledge on copying object data and adjusting them
Make sure that the shops you will register with this system doesn't have any items sold
on its object data
How to use:
1) Copy the dummy shop, the dummy select unit ability and the dummy click ability from the object editor into your map.
2) Make sure that you adjust the settings of the dummy shop in case it cannot recognize the dummy
select unit ability.
3) Copy this system and Bribe's Table library into your map.
4) Adjust some of the globals to fit your new map.
4) Start registering your main shops and items. See attached example trigger for better understanding.
Creating category items
1) Make a new item based on power ups
2) Add the Dummy Click ability
3) Make sure you set gold cost to 0 and set model used to none
Functions to toggle shop/item availability
shopAvailableForPlayer(player p, boolean what)
shopAvailableForPlayerId(integer id, boolean what)
--> Enables or disables the shop system for the player p, or the player with
PlayerId of id, depending on the setting of boolean what
itemAvailableForPlayer(integer itemid, player p, boolean what)
itemAvailableForPlayerId(integer itemid, player p, boolean what)
--> Enables or disables the item with the rawcode itemid for player p or the player with
PlayerId of id, depending on the setting of boolean what
*/
globals
private Table ShopTable
private Table WhichShop
private Table MainShop
private unit array DummyShop
private boolean array CanShop
// The next constant is required in order to avoid conflict when more than one player
// is buying from the same shop
private constant integer DUMMY_SHOP_ID = 'n000'
private hashtable ItemHash = InitHashtable()
endglobals
private module init
static method onInit takes nothing returns nothing
local integer i = 0
set ShopTable = Table.create()
set WhichShop = Table.create()
set MainShop = Table.create()
set thistype.items = Table.create()
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SELECTED, function MainShops.selectShop)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SELL_ITEM, function thistype.changeItems)
loop
if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
set DummyShop[i] = CreateUnit(Player(i),DUMMY_SHOP_ID, 0.0, 0.0, 0.0)
// I make the player own it because if not and they are not visible to the player
// the selection action actually runs before the dummy gets moved so the selection becomes empty
set CanShop[i] = true
endif
set i = i + 1
exitwhen i > 11
endloop
endmethod
endmodule
struct Shops extends array
//integer array items [11]
static Table items
integer itemnum
private static integer instanceCount = 0
private static thistype recycle = 0
private thistype recycleNext
static method clearShop takes integer id returns nothing
local integer i = 0
local thistype this = WhichShop[id]
loop
call RemoveItemFromStock(DummyShop[id], thistype.items[this*11 + i])
set i = i + 1
exitwhen i > this.itemnum
endloop
endmethod
static method changeItems takes nothing returns boolean
local player p = GetOwningPlayer(GetBuyingUnit())
local integer i = 0
local thistype this = ShopTable[GetItemTypeId(GetSoldItem())]
local integer id = GetPlayerId(p)
if this == 0 then
set p = null
return false
endif
call thistype.clearShop(id)
set WhichShop[id] = this
loop
if not LoadBoolean(ItemHash, id, thistype.items[this*11 + i]) then
call AddItemToStock(DummyShop[id], thistype.items[this*11 + i], 1,1)
endif
set i = i + 1
exitwhen i == this.itemnum
endloop
set p = null
return false
endmethod
static method forceChangeItems takes integer baseItem, integer id returns boolean
local integer i = 0
local thistype this = ShopTable[baseItem]
call thistype.clearShop(id)
set WhichShop[id] = this
loop
if not LoadBoolean(ItemHash, id, thistype.items[this*11 + i]) then
call AddItemToStock(DummyShop[id], thistype.items[this*11 + i], 1,1)
endif
set i = i + 1
exitwhen i == this.itemnum
endloop
return false
endmethod
static method addItemLink takes integer itemBaseId, integer itemAddId returns nothing
local thistype this = ShopTable[itemBaseId]
if this.itemnum < 11 then
set thistype.items[this*11 + this.itemnum] = itemAddId
set this.itemnum = this.itemnum + 1
else
debug BJDebugMsg("Item list already full")
endif
endmethod
static method removeItem takes integer baseItemId, integer itemId returns nothing
local thistype this = ShopTable[baseItemId]
local integer i = 0
loop
if this.items[this*11 + i] == itemId then
set this.itemnum = this.itemnum - 1
set thistype.items[this*11 + i] = thistype.items[this*11 + this.itemnum]
debug BJDebugMsg("REMOVED " + I2S(thistype.items[this*11 + i]))
return
endif
set i = i + 1
exitwhen i == this.itemnum
endloop
endmethod
static method duplicate takes integer baseItemId, integer itemId returns nothing
local thistype base = ShopTable[baseItemId]
local thistype that //= .allocate()
local integer i = 0
if (recycle == 0) then
set instanceCount = instanceCount + 1
set that = instanceCount
else
set that = recycle
set recycle = recycle.recycleNext
endif
set ShopTable[itemId] = that
set that.itemnum = base.itemnum
loop
set thistype.items[that*11 + i] = thistype.items[base*11 + i]
set i = i + 1
exitwhen i == base.itemnum
endloop
endmethod
static method register takes integer itemId returns nothing
local thistype this //= .allocate()
if (recycle == 0) then
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = recycle
set recycle = recycle.recycleNext
endif
set ShopTable[itemId] = this
endmethod
implement init
endstruct
struct MainShops extends array
private static integer instanceCount = 0
private static thistype recycle = 0
private thistype recycleNext
integer baseItem
static method selectShop takes nothing returns boolean
local unit u = GetTriggerUnit()
local thistype this = MainShop[GetHandleId(u)]
local player p = GetTriggerPlayer()
local integer id = GetPlayerId(p)
if this == 0 or not CanShop[id] then
set p = null
return false
endif
// If you click the portait, it will still point your camera towards the main shop
call SetUnitX(DummyShop[id], GetUnitX(u))
call SetUnitY(DummyShop[id], GetUnitY(u))
call Shops.forceChangeItems(this.baseItem, id)
if GetLocalPlayer() == p then
call ClearSelection()
call SelectUnit(DummyShop[id], true)
endif
return false
endmethod
static method register takes unit shop, integer baseItem returns nothing
local thistype this //= .allocate()
if (recycle == 0) then
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = recycle
set recycle = recycle.recycleNext
endif
set MainShop[GetHandleId(shop)] = this
set this.baseItem = baseItem
endmethod
endstruct
function shopAvailableForPlayer takes player p, boolean what returns nothing
set CanShop[GetPlayerId(p)] = what
endfunction
function shopAvailableForPlayerId takes integer id, boolean what returns nothing
set CanShop[id] = what
endfunction
function itemAvailableForPlayer takes integer itemid, player p, boolean what returns nothing
call SaveBoolean(ItemHash, GetPlayerId(p), itemid, not what)
endfunction
function itemAvailableForPlayerId takes integer itemid, integer id, boolean what returns nothing
call SaveBoolean(ItemHash, id, itemid, not what)
endfunction
endlibrary
//TESH.scrollpos=42
//TESH.alwaysfold=0
library Sample initializer init requires ShopSystem
/*
In this example, we will be making shop which is both a two-level and a three-level shop.
Here you will be familiarized to the method of registering main shops and linking items to the
system. We will also make another shop which sells directly what the second level of the other
shop sells. We will also make a return to upper category dummy item. This was made so that you'll have a better grasp on how to use the system.
Theoretically, you can create an infinite-level shop using the ShopSystem.
*/
private function create takes nothing returns nothing
// Create the shop on top
local unit shop = CreateUnit(Player(15), 'n001', 983.0, -892.0, 270.0)
/* Register the created shop as a main shop
I000 is the rawcode of the dummy item which we will use in order to determine
which items should the shop initialy have
*/
call MainShops.register(shop, 'I000')
/* We register I000 to the shop system so that we can now link items to be sold when I000 is sold.
and since our shop is linked with I000, whenever we click the shop, it will show the items linked
to I000
*/
call Shops.register('I000')
/* Now we will link items to I000, take note that this items will also be used as categories
so we will register them to the system too
*/
call Shops.addItemLink('I000', 'I001') // Equipments
call Shops.addItemLink('I000', 'I002') // Potions
// Now it's time to register those two items too
call Shops.register('I001') //Equipments
call Shops.register('I002') //Potions
// Now we will register items to I002 first since we will no longer have any category below potions
call Shops.addItemLink('I002', 'pgma') //Potion of Mana
call Shops.addItemLink('I002', 'pghe') //Potion of Health
// Now we setup another set of links with the equipments category
call Shops.addItemLink('I001', 'I003') //Weapons
call Shops.addItemLink('I001', 'I004') //Armor
call Shops.register('I003') //Weapons
call Shops.register('I004') //Armor
// Now we link items to the weapons category
call Shops.addItemLink('I003', 'ratf') //Blades of attack + 15
call Shops.addItemLink('I003', 'ofro') //Orb of Frost
call Shops.addItemLink('I003', 'ssil') //Staff of silence
call Shops.addItemLink('I003', 'stel') //Staff of teleportation
call Shops.addItemLink('I003', 'rat6') //Blades of attack + 6
call Shops.addItemLink('I003', 'rat9') //Blades of attack + 9
call Shops.addItemLink('I003', 'desc') //Dagger of escape
call Shops.addItemLink('I003', 'frgd') //Frostgard
call Shops.addItemLink('I003', 'klmm') //Kilmaim
call Shops.addItemLink('I003', 'ofir') //Orb of Fire
call itemAvailableForPlayerId('ofir', 0, false) //we disable selling og Orb of fire for Player Red
// Now we link items to the armor category
call Shops.addItemLink('I004', 'ckng') //Crown of kings
call Shops.addItemLink('I004', 'modt') //Mask of Death
call Shops.addItemLink('I004', 'rde3') //Ring of protection
// Now we will create a return to upper category button for the weapons category
call Shops.duplicate('I001', 'I005')
// the .duplicate method automatically registers the new item so no need to call .register
// Upper category of weapons is equipments so we duplicate its instance into the upper category dummy
call Shops.addItemLink('I003', 'I005') // Don't forget to link it to the weapons dummy
// Now we will create another shop at the left which sells the items in the weapons category of our
// previous shop.
set shop = CreateUnit(Player(15), 'n001', 93.4, -1430.0, 270.0)
call MainShops.register(shop, 'I006')
// We actually created a new dummy item since this shop cannot have the return to upper
// category option since it sells the items from the weapons category directly
// to easily do this:
call Shops.duplicate('I003', 'I006') // we duplicate the weapons category
call Shops.removeItem('I006', 'I005') // but remove the return to upper category item from the duplicated instance.
endfunction
private function init takes nothing returns nothing
call TimerStart(CreateTimer(), 2.00, false, function create)
endfunction
endlibrary
//TESH.scrollpos=210
//TESH.alwaysfold=0
library ShopSystem requires Table, RegisterPlayerUnitEvent
/*
Version 1.2 by Adiktuz
Credits:
Bribe for Table
Magtheridon96 for RegisterPlayerUnitEvent
Cohadar for his JassHelper update
A simple shop system which allows you to have multiple shops/shopping category inside a single shop.
If you've read my tutorial about sub-shops maybe you'll be asking what is the difference of this
to the system in the tutorial. Well, this one uses only one shop per player. Also with this one, all
items sold should be set via the trigger editor.
You can create an infinite-level shop with this one.
You can only register 11 items per catergory
Requirements:
JNGP with the Cohadar's latest jass helper
knowledge on how to call functions
knowledge on how to obtain rawcodes
knowledge on copying object data and adjusting them
Make sure that the shops you will register with this system doesn't have any items sold
on its object data
How to use:
1) Copy the dummy shop, the dummy select unit ability and the dummy click ability from the object editor into your map.
2) Make sure that you adjust the settings of the dummy shop in case it cannot recognize the dummy
select unit ability.
3) Copy this system and Bribe's Table library into your map.
4) Adjust some of the globals to fit your new map.
4) Start registering your main shops and items. See attached example trigger for better understanding.
Creating category items
1) Make a new item based on power ups
2) Add the Dummy Click ability
3) Make sure you set gold cost to 0 and set model used to none
Functions to toggle shop/item availability
shopAvailableForPlayer(player p, boolean what)
shopAvailableForPlayerId(integer id, boolean what)
--> Enables or disables the shop system for the player p, or the player with
PlayerId of id, depending on the setting of boolean what
itemAvailableForPlayer(integer itemid, player p, boolean what)
itemAvailableForPlayerId(integer itemid, integer id, boolean what)
--> Enables or disables the item with the rawcode itemid for player p or the player with
PlayerId of id, depending on the setting of boolean what
*/
globals
private Table ShopTable
private Table WhichShop
private Table MainShop
private unit array DummyShop
private boolean array CanShop
// The next constant is required in order to avoid conflict when more than one player
// is buying from the same shop
private constant integer DUMMY_SHOP_ID = 'n000'
private hashtable ItemHash = InitHashtable()
endglobals
private module init
static method onInit takes nothing returns nothing
local integer i = 0
set ShopTable = Table.create()
set WhichShop = Table.create()
set MainShop = Table.create()
set thistype.items = Table.create()
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SELECTED, function MainShops.selectShop)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SELL_ITEM, function thistype.changeItems)
for i = 0 to 11
if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
set DummyShop[i] = CreateUnit(Player(i),DUMMY_SHOP_ID, 0.0, 0.0, 0.0)
// I make the player own it because if not and they are not visible to the player
// the selection action actually runs before the dummy gets moved so the selection becomes empty
set CanShop[i] = true
endif
endfor
endmethod
endmodule
struct Shops extends array
//integer array items [11]
static Table items
integer itemnum
private static integer instanceCount = 0
private static thistype recycle = 0
private thistype recycleNext
static method clearShop takes integer id returns nothing
local integer i = 0
local thistype this = WhichShop[id]
for i = 0 to this.itemnum
call RemoveItemFromStock(DummyShop[id], thistype.items[this*11 + i])
endfor
endmethod
static method changeItems takes nothing returns boolean
local player p = GetOwningPlayer(GetBuyingUnit())
local integer i = 0
local thistype this = ShopTable[GetItemTypeId(GetSoldItem())]
local integer id = GetPlayerId(p)
if this == 0 then
set p = null
return false
endif
call thistype.clearShop(id)
set WhichShop[id] = this
for i = 0 to this.itemnum
if not LoadBoolean(ItemHash, id, thistype.items[this*11 + i]) then
call AddItemToStock(DummyShop[id], thistype.items[this*11 + i], 1,1)
endif
endfor
set p = null
return false
endmethod
static method forceChangeItems takes integer baseItem, integer id returns boolean
local integer i = 0
local thistype this = ShopTable[baseItem]
call thistype.clearShop(id)
set WhichShop[id] = this
for i = 0 to (this.itemnum - 1)
if not LoadBoolean(ItemHash, id, thistype.items[this*11 + i]) then
call AddItemToStock(DummyShop[id], thistype.items[this*11 + i], 1,1)
endif
endfor
debug BJDebugMsg(I2S(this.itemnum))
return false
endmethod
static method addItemLink takes integer itemBaseId, integer itemAddId returns nothing
local thistype this = ShopTable[itemBaseId]
if this.itemnum < 11 then
set thistype.items[this*11 + this.itemnum] = itemAddId
set this.itemnum = this.itemnum + 1
else
debug BJDebugMsg("Item list already full")
endif
endmethod
static method removeItem takes integer baseItemId, integer itemId returns nothing
local thistype this = ShopTable[baseItemId]
local integer i = 0
for i = 0 to (this.itemnum - 1)
if this.items[this*11 + i] == itemId then
set this.itemnum = this.itemnum - 1
set thistype.items[this*11 + i] = thistype.items[this*11 + this.itemnum]
//debug BJDebugMsg("REMOVED " + I2S(thistype.items[this*11 + i]))
return
endif
endfor
endmethod
static method duplicate takes integer baseItemId, integer itemId returns nothing
local thistype base = ShopTable[baseItemId]
local thistype that //= .allocate()
local integer i = 0
if (recycle == 0) then
set instanceCount = instanceCount + 1
set that = instanceCount
else
set that = recycle
set recycle = recycle.recycleNext
endif
set ShopTable[itemId] = that
set that.itemnum = base.itemnum
for i = 0 to (base.itemnum - 1)
set thistype.items[that*11 + i] = thistype.items[base*11 + i]
endfor
endmethod
static method register takes integer itemId returns nothing
local thistype this //= .allocate()
if (recycle == 0) then
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = recycle
set recycle = recycle.recycleNext
endif
set ShopTable[itemId] = this
endmethod
implement init
endstruct
struct MainShops extends array
private static integer instanceCount = 0
private static thistype recycle = 0
private thistype recycleNext
integer baseItem
static method selectShop takes nothing returns boolean
local unit u = GetTriggerUnit()
local thistype this = MainShop[GetHandleId(u)]
local player p = GetTriggerPlayer()
local integer id = GetPlayerId(p)
if this == 0 or not CanShop[id] then
set p = null
return false
endif
// If you click the portait, it will still point your camera towards the main shop
call SetUnitX(DummyShop[id], GetUnitX(u))
call SetUnitY(DummyShop[id], GetUnitY(u))
call Shops.forceChangeItems(this.baseItem, id)
if GetLocalPlayer() == p then
call ClearSelection()
call SelectUnit(DummyShop[id], true)
endif
return false
endmethod
static method register takes unit shop, integer baseItem returns nothing
local thistype this //= .allocate()
if (recycle == 0) then
set instanceCount = instanceCount + 1
set this = instanceCount
else
set this = recycle
set recycle = recycle.recycleNext
endif
set MainShop[GetHandleId(shop)] = this
set this.baseItem = baseItem
endmethod
endstruct
function shopAvailableForPlayer takes player p, boolean what returns nothing
set CanShop[GetPlayerId(p)] = what
endfunction
function shopAvailableForPlayerId takes integer id, boolean what returns nothing
set CanShop[id] = what
endfunction
function itemAvailableForPlayer takes integer itemid, player p, boolean what returns nothing
call SaveBoolean(ItemHash, GetPlayerId(p), itemid, not what)
endfunction
function itemAvailableForPlayerId takes integer itemid, integer id, boolean what returns nothing
call SaveBoolean(ItemHash, id, itemid, not what)
endfunction
endlibrary