Thread: GetItemCostById
View Single Post
Old 06-08-2006, 05:57 AM   #1
weaaddar
User


Respected User
 
Join Date: Apr 2002
Posts: 2,372

Submissions (3)

weaaddar has a spectacular aura about (131)

Default GetItemCostById

Edit: Version 1.1 Adds lumber cost getting magic.

This is a demo map to show off my work around to get an item's cost.

Picking up an item will cause a text message to appear with its name, and the items cost, after which the item is destroyed.

Code:
|----------------------------------------------------------------
| What is this map for?
|----------------------------------------------------------------
 ____________________________________________________________________________
|function GetItemCostById takes integer id,boolean whichtype returns integer |
 ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

As the name suggests, given an item id, it will get its cost.
When whichtype is true it returns gold, otherwise lumber
This is for me a very seeked functionality, so I'm very happy.
This is very effecient, and about as customizable as a standalone function gets.

Implementation is very easy:
1) Create a gamecache variable called gc
2) Copy the trigger GetItemCostById to your map.

That is it. If you can get Jesp Spells to work this function shouldn't be that hard!

|---------------------------------------------------------------
|How does it work?
|---------------------------------------------------------------

It makes a shop and a hero for player 15, and places them outside of the viewable map area.
When called with an id
1) I check in gamecache if I already stored the item's cost, if I did I return it.
2) Otherwise I add 50,000 gold and lumber to player 15's gold
3) Then I add the item's id to the shop
4) I order the hero to buy the item
5) I figure out how much the item cost, by doing 50,000-Current gold/lumber for player 15
6) I cache the cost
7) I destroy the item, and remove it from the stock of the shop
8) I return the cost

ChangeLog
==============
1.1 Added support to get an items lumber cost. Made the code a bit cleaner.

____________________

cohadar has updated this function to use the new vJASS syntax and has fixed a bug when both lumber and gold cost were 0. Thanks to him we have a new version (which uses Vexorian's Table):
Collapse JASS:
//===========================================================================
//  vJASS version of weaaddar's GetItemCostById system
//===========================================================================
//
//  * Uses Table system
//  * Fixed an issue that could happen if item cost was zero for both gold and lumber
//  * This version is compatible with original one
//  * Added some helper functions: GetItemGoldCost, GetItemLumberCost, GetItemGoldCostById, GetItemLumberCostById
//
//===========================================================================
library GetItemCostById initializer Init uses Table

globals
    private constant player  BuyerPlayer = Player(15)
    private constant integer BuyerTypeId = 'hpal'
    private constant integer ShopTypeId = 'nshe'
    private constant integer MAX_PRICE = 50000
    
    // check Init function
    private Table GoldPrice = 0
    private Table LumberPrice = 0
    private unit Buyer = null
    private unit Shop = null
endglobals

//===========================================================================
//  whichtype == true --> gold
//  whichtype == false --> lumber
//===========================================================================
function GetItemCostById takes integer id, boolean whichtype returns integer
    local integer gold
    local integer lumber

    if GoldPrice.exists(id) then
        set gold = GoldPrice[id]
        set lumber = LumberPrice[id]
    else
        call SetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_GOLD,   MAX_PRICE)
        call SetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_LUMBER, MAX_PRICE)
        call AddItemToStock(Shop, id, 1, 1)
        call IssueNeutralImmediateOrderById(BuyerPlayer, Shop, id)
        call RemoveItem(UnitItemInSlot(Buyer, 0))
        call RemoveItemFromStock(Shop, id)
        set gold   = MAX_PRICE - GetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_GOLD)
        set lumber = MAX_PRICE - GetPlayerState(BuyerPlayer, PLAYER_STATE_RESOURCE_LUMBER)
        set GoldPrice[id] = gold
        set LumberPrice[id] = lumber
    endif
    
    if whichtype then
        return gold
    else
        return lumber
    endif
endfunction

//===========================================================================
function GetItemGoldCost takes item i returns integer
    return GetItemCostById(GetItemTypeId(i), true)
endfunction

//===========================================================================
function GetItemLumberCost takes item i returns integer
    return GetItemCostById(GetItemTypeId(i), false)
endfunction

//===========================================================================
function GetItemGoldCostById takes integer id returns integer
    return GetItemCostById(id, true)
endfunction

//===========================================================================
function GetItemLumberCostById takes integer id returns integer
    return GetItemCostById(id, false)
endfunction

//===========================================================================
private function Init takes nothing returns nothing
    local rect r = GetWorldBounds()
    
    set Buyer = CreateUnit(BuyerPlayer, BuyerTypeId, 0, 0, 0)
    call SetUnitX(Buyer, GetRectMaxX(r))
    call SetUnitY(Buyer, GetRectMaxY(r))
    
    set Shop = CreateUnit(BuyerPlayer, ShopTypeId, 0, 0, 0)
    call SetUnitX(Shop,GetRectMaxX(r))
    call SetUnitY(Shop,GetRectMaxY(r))
    
    call UnitAddAbility(Shop, 'Asid')
    call UnitRemoveAbility(Shop, 'Awan')
    call UnitAddAbility(Shop, 'Aloc')

    set GoldPrice = Table.create()
    set LumberPrice = Table.create()
    
    call RemoveRect(r)
    set r = null
endfunction

endlibrary
Attached Images
File Type: jpg GetItemCostById.JPG (50.3 KB, 796 views)
Attached Files
File Type: w3x GetItemCostById.w3x (14.8 KB, 325 views)
weaaddar is offline   Reply With Quote
Sponsored Links - Login to hide this ad!