Wc3C.net

Wc3C.net (http://www.wc3c.net/forums.php)
-   Scripts (http://www.wc3c.net/forumdisplay.php?f=737)
-   -   GetItemCostById (http://www.wc3c.net/showthread.php?t=84044)

weaaddar 06-08-2006 05:57 AM

GetItemCostById
 
2 Attachment(s)
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

Blade.dk 06-08-2006 07:07 AM

Very nice, approved :).

BertTheJasser 06-08-2006 08:19 AM

That's juts really nice. +Rep

aquilla 06-08-2006 08:26 AM

I've seen this function on wc3j but this is better. A little note though; items that cost lumber won't be sold (if anyone has that). Oh, and you forgot to null player p

nice work :)

PipeDream 06-08-2006 08:49 AM

Ah glad to see it got solved.
Quote:

you forgot to null player p
No, only leaks if you try to deallocate.

Anitarf 06-08-2006 09:22 AM

As far as I was told it leaks a bit anyway, the big problem is the handle index, which doesn't matter in this case since players don't get removed, but the pointer itself supposedly leaks a few bytes as well.

Is it neccesary to cache items? I understand getting it's cost is a complicated procedure, but since it probably isn't used too often, it shouldn't be such a performance drain. Not that making a gc variable is soo hard, but... Is it at least compatible with CSCache, for example, so that they could use the same game cahce?

Other than these cople of questions, it's a really cool function.

Chuckle_Brother 06-08-2006 02:43 PM

Super useful stuff right there.

Would rep, but it goes against my beliefs.

weaaddar 06-08-2006 03:56 PM

Right, I'm making a pocket shop and so I need this to help me do selling/buying. I didn't realize there are any melee items that cost wood but if needed I could make a version that gets both wood and gold and returns that in a location list or something.

emjlr3 06-08-2006 04:15 PM

neato, nicely done

weaaddar 06-08-2006 05:01 PM

Uploaded version 1.1 gets lumber costs too.

weaaddar 06-11-2006 12:55 AM

1 Attachment(s)
sheep is still visible :?

cohadar 12-15-2008 06:31 PM

[Sample]GetItemCostById
 
Just a fresh remake of an old script.
You can attach it to the original submission.
I wish no credit for this.
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

Pyrogasm 12-16-2008 01:15 AM

Why is H2I in there? You never use it.

Other than that I see this being fine, though I would also add:
Collapse JASS:
globals
    boolean COST_TYPE_GOLD = true
    boolean COST_TYPE_LUMBER = false
endglobals
For the sake of clarity and ease of understanding in code that uses this function.

Ammorth 12-16-2008 03:12 AM

Only thing I would do is move to Tables so that you don't have a gamecache just for this script.

Pyrogasm 12-16-2008 05:36 AM

Table uses gamecache...


All times are GMT. The time now is 01:29 PM.

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services