This system allows you to get gold/lumber cost of items (in Hero's inventory or on the ground). Takes into account the number of charges.
The map is for demonstration purposes only.
The map is for demonstration purposes only.
Writing same sentence twice makes people less likely to ignore it.
How to use:
Code:
function GetItemTypeIdCharges takes integer itemTypeId returns integer
function GetItemTypeIdGoldCost takes integer itemTypeId returns integer
function GetItemTypeIdLumberCost takes integer itemTypeId returns integer
function GetItemGoldCost takes item whichItem returns integer
function GetItemLumberCost takes item whichItem returns integer
How to add to your map:
1. Create Trigger
2. Convert it to Custom Text
3. Copy and paste the following code:
[jass=the following code]library GetItemCost
//================================================================
// Version 1.3
//
//================================================================
//Changelog
//V1.3 - Removed fail-safe check in favor of (ab)using Locust ability
//
//V1.2 - Added fail-safe check (whether global dummies are dead)
// - Changed initializer (now code can be called from inside a module initializer
// or struct initializer thread)
//
//V1.1 - Added global dummies
//
//V1.0 - Created
//================================================================
// Provides functions for getting gold/lumber costs of the item,
// in other words amount of gold or lumber you'd get for selling
// that item to a vendor.
// By default, it's 50% of the price you'd pay for buying the item.
// It can be changed here:
// Gameplay Constant -> Inventory - Sell Item Return Rate
//================================================================
// The API provides following functions:
//
// GetItemTypeIdCharges takes integer itemTypeId returns integer
// GetItemTypeIdGoldCost takes integer itemTypeId returns integer
// GetItemTypeIdLumberCost takes integer itemTypeId returns integer
// GetItemGoldCost takes item whichItem returns integer
// GetItemLumberCost takes item whichItem returns integer
//================================================================
// Constant "dummy" used in this library is standard Sentry Ward.
// You can (and should) replace it with your own dummy.
//================================================================
// This library cannot determine cost of items classified as
// "Power Up" and will always return 0 for them.
//================================================================
globals
//Hashtable for storing item costs and charges
private hashtable ItemCost = InitHashtable()
//Constants to be used as hash keys
private constant integer ITEM_COST_GOLD = 0
private constant integer ITEM_COST_LUMBER = 1
private constant integer ITEM_DEFAULT_CHARGES = 3
//Player (Neutral Passive) for buying/selling
private constant player NP = Player(PLAYER_NEUTRAL_PASSIVE)
//Dummy unit - feel free to replace it
private constant integer dummy = 'oeye'
//A vendor (shop) and a customer
private unit vendor = null
private unit seller = null
endglobals
//Initializer (vendor)
private function CreateVendor takes nothing returns nothing
//Create vendor
set vendor = CreateUnit(NP, dummy, 0, 0, 0)
//Hide vendor
call ShowUnit(vendor, false)
call SetUnitPathing(vendor, false)
//Give vendor ability to buy items
call UnitAddAbility(vendor, 'Apit')
//Give vendor locust
call UnitAddAbility(vendor, 'Aloc')
endfunction
//Initializer (seller)
private function CreateSeller takes nothing returns nothing
//Create seller
set seller = CreateUnit(NP, dummy, 0, 0, 0)
//Move it to the vendor
call SetUnitPathing(seller, false)
call SetUnitX(seller, GetUnitX(vendor))
call SetUnitY(seller, GetUnitY(vendor))
//Hide seller
call ShowUnit(seller, false)
//Give seller ability to carry items
call UnitAddAbility(seller, 'AInv')
//Give seller locust
call UnitAddAbility(seller, 'Aloc')
endfunction
//Initializer (module)
private module Initialize
private static method onInit takes nothing returns nothing
call CreateVendor()
call CreateSeller()
endmethod
endmodule
//Initializer (struct)
struct Initializer extends array
implement Initialize
endstruct
//======================Rather self-explanatory===================
function GetItemTypeIdCharges takes integer itemTypeId returns integer
local item whichItem
local integer charges
//load default number of charges from hashtable
if (HaveSavedInteger(ItemCost, itemTypeId, ITEM_DEFAULT_CHARGES)) then
set charges = LoadInteger(ItemCost, itemTypeId, ITEM_DEFAULT_CHARGES)
else
//or create new item of the same type and get it's number of charges
set whichItem = CreateItem(itemTypeId, 0, 0)
set charges = GetItemCharges(whichItem)
call SaveInteger(ItemCost, itemTypeId, ITEM_DEFAULT_CHARGES, charges)
call RemoveItem(whichItem)
set whichItem = null
endif
return charges
endfunction
//=======================Calculate Cost===========================
private function CalculateItemCost takes integer itemTypeId returns nothing
local integer gold
local integer lumber
local integer goldcost
local integer lumbercost
//Creates new instance of the item
local item goods = CreateItem(itemTypeId, 0, 0)
//Detects if the item is power-up.
if (GetItemType(goods) == ITEM_TYPE_POWERUP) then
call RemoveItem(goods)
set goods = null
//Save both costs in the Hashtable
call SaveInteger(ItemCost, itemTypeId, ITEM_COST_GOLD, 0)
call SaveInteger(ItemCost, itemTypeId, ITEM_COST_LUMBER, 0)
return
endif
//Store Neutral Passive's gold and lumber
set gold = GetPlayerState(NP, PLAYER_STATE_RESOURCE_GOLD)
set lumber = GetPlayerState(NP, PLAYER_STATE_RESOURCE_LUMBER)
//Give dummy the item
call UnitAddItem(seller, goods)
//Make dummy sell item
call ShowUnit(vendor, true)
call UnitDropItemTarget(seller, goods, vendor)
call ShowUnit(vendor, false)
set goods = null
//Calculate cost (Neutral Passive's gold/lumber)
set goldcost = GetPlayerState(NP, PLAYER_STATE_RESOURCE_GOLD) - gold
set lumbercost = GetPlayerState(NP, PLAYER_STATE_RESOURCE_LUMBER) - lumber
//Save both costs in the Hashtable
call SaveInteger(ItemCost, itemTypeId, ITEM_COST_GOLD, goldcost)
call SaveInteger(ItemCost, itemTypeId, ITEM_COST_LUMBER, lumbercost)
//Also save charges, while you're at it
call GetItemTypeIdCharges(itemTypeId)
//Restore Neutral Passive's gold and lumber
call SetPlayerState(NP, PLAYER_STATE_RESOURCE_GOLD, gold)
call SetPlayerState(NP, PLAYER_STATE_RESOURCE_LUMBER, lumber)
endfunction
//==================Load Cost (or calculate)======================
private function GetItemCost takes integer itemTypeId, integer costType returns integer
//Store item data in hashtable if its not there already
if (not(HaveSavedInteger(ItemCost, itemTypeId, costType))) then
call CalculateItemCost(itemTypeId)
endif
//Read cost from hashtable
return LoadInteger(ItemCost, itemTypeId, costType)
endfunction
//===========================API==================================
function GetItemTypeIdGoldCost takes integer itemTypeId returns integer
return GetItemCost(itemTypeId, ITEM_COST_GOLD)
endfunction
function GetItemTypeIdLumberCost takes integer itemTypeId returns integer
return GetItemCost(itemTypeId, ITEM_COST_LUMBER)
endfunction
function GetItemGoldCost takes item whichItem returns integer
local integer cost = GetItemTypeIdGoldCost(GetItemTypeId(whichItem))
if (GetItemCharges(whichItem) > 0) then
set cost = cost*GetItemCharges(whichItem)/GetItemTypeIdCharges(GetItemTypeId(whichItem))
endif
return cost
endfunction
function GetItemLumberCost takes item whichItem returns integer
local integer cost = GetItemTypeIdLumberCost(GetItemTypeId(whichItem))
if (GetItemCharges(whichItem) > 0) then
set cost = cost*GetItemCharges(whichItem)/GetItemTypeIdCharges(GetItemTypeId(whichItem))
endif
return cost
endfunction
endlibrary[/code]
This system uses hashtable. Idea is, if it already calculated item's value, it doesn't have to do that ever again because it cannot be changed ingame.
Keywords:
Item, ItemCost, Cost, GetItemCost, Gold, Lumber, Price, Value