wc3campaigns
WC3C Homepage - www.wc3c.netUser Control Panel (Requires Log-In)Engage in discussions with other users and join contests in the WC3C forums!Read one of our many tutorials, ranging in difficulty from beginner to advanced!Show off your artistic talents in the WC3C Gallery!Download quality models, textures, spells (vJASS/JASS), systems, and scripts!Download maps that have passed through our rigorous approval process!

Go Back   Wc3C.net > Resources > Code Resources > Scripts
User Name
Password
Register Rules Get Hosted! Chat Pastebin FAQ and Rules Members List Calendar



Reply
 
Thread Tools Search this Thread
Old 06-08-2006, 04: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, 794 views)
Attached Files
File Type: w3x GetItemCostById.w3x (14.8 KB, 324 views)

Last edited by Pyrogasm : 12-16-2008 at 09:35 PM.
weaaddar is offline   Reply With Quote
Sponsored Links - Login to hide this ad!
Old 06-08-2006, 06:07 AM   #2
Blade.dk
.
 
Blade.dk's Avatar


Respected User
 
Join Date: May 2005
Posts: 1,990

Submissions (15)

Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)Blade.dk is a glorious beacon of light (418)

Approved Map: Azeroth's Arcane ArenaSpell session 01 winner

Send a message via MSN to Blade.dk
Default

Very nice, approved :).
__________________
Spell Making Course: Part 1: Making a simple stomp spell.
I wonder if I'll ever finish part 2.
Blade.dk is offline   Reply With Quote
Old 06-08-2006, 07:19 AM   #3
BertTheJasser
xyzi - our universe
 
BertTheJasser's Avatar
 
Join Date: May 2005
Posts: 742

Submissions (2)

BertTheJasser has a spectacular aura about (111)BertTheJasser has a spectacular aura about (111)BertTheJasser has a spectacular aura about (111)BertTheJasser has a spectacular aura about (111)

Default

That's juts really nice. +Rep
__________________
Note: Bye... I had a lot of fun here!
Special thanks to Vexorian who helped me learn jass, the real jass and always helped me when problems occured, I would call him somehow my mentor. Pipedream, who made amazing Grimoire and helped me acclerating my map (currently at 99% finished, no developement atm).

Vote for Linux Ports in general of Blizzard products: http://www.PetitionOnline.com/ibpfl/
BertTheJasser is offline   Reply With Quote
Old 06-08-2006, 07:26 AM   #4
aquilla
User
 
aquilla's Avatar
 
Join Date: Mar 2003
Posts: 217

Submissions (1)

aquilla will become famous soon enough (71)aquilla will become famous soon enough (71)aquilla will become famous soon enough (71)

Send a message via MSN to aquilla
Default

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 :)
aquilla is offline   Reply With Quote
Old 06-08-2006, 07:49 AM   #5
PipeDream
Moderator
 
PipeDream's Avatar


Code Moderator
 
Join Date: Feb 2006
Posts: 1,405

Submissions (6)

PipeDream is a glorious beacon of light (463)PipeDream is a glorious beacon of light (463)PipeDream is a glorious beacon of light (463)PipeDream is a glorious beacon of light (463)

Default

Ah glad to see it got solved.
Quote:
you forgot to null player p
No, only leaks if you try to deallocate.
__________________
PipeDream is offline   Reply With Quote
Old 06-08-2006, 08:22 AM   #6
Anitarf
Procrastination Incarnate


Development Director
 
Join Date: Feb 2004
Posts: 8,190

Submissions (19)

Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)Anitarf has a brilliant future (903)

2008 Spell olympics - Fire - SilverApproved Map: Old School Alliance TacticsHero Contest #2 - 3rd PlaceSpell making session 2 winner

Default

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.
__________________
Anitarf is offline   Reply With Quote
Old 06-08-2006, 01:43 PM   #7
Chuckle_Brother
Oh for the sake of fudge
 
Chuckle_Brother's Avatar


Respected User
 
Join Date: Dec 2005
Posts: 782

Submissions (2)

Chuckle_Brother will become famous soon enough (53)Chuckle_Brother will become famous soon enough (53)

Send a message via ICQ to Chuckle_Brother Send a message via AIM to Chuckle_Brother Send a message via MSN to Chuckle_Brother Send a message via Yahoo to Chuckle_Brother
Default

Super useful stuff right there.

Would rep, but it goes against my beliefs.
__________________
"...you play a mean banjo"
Chuckle_Brother is offline   Reply With Quote
Old 06-08-2006, 02:56 PM   #8
weaaddar
User


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

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

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.
weaaddar is offline   Reply With Quote
Old 06-08-2006, 03:15 PM   #9
emjlr3
Rehabbing
 
emjlr3's Avatar
 
Join Date: Jun 2005
Posts: 1,386

Submissions (14)

emjlr3 is a jewel in the rough (151)emjlr3 is a jewel in the rough (151)

Mapping Contest First Place

Send a message via AIM to emjlr3 Send a message via MSN to emjlr3
Default

neato, nicely done
__________________
emjlr3 is offline   Reply With Quote
Old 06-08-2006, 04:01 PM   #10
weaaddar
User


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

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

Uploaded version 1.1 gets lumber costs too.
weaaddar is offline   Reply With Quote
Old 06-10-2006, 11:55 PM   #11
weaaddar
User


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

Submissions (3)

weaaddar has a spectacular aura about (131)

Default

sheep is still visible :?
Attached Images
File Type: jpg sheep.JPG (6.4 KB, 165 views)
weaaddar is offline   Reply With Quote
Old 12-15-2008, 05:31 PM   #12
cohadar
master of fugue
 
cohadar's Avatar
 
Join Date: Jun 2007
Posts: 2,453

Submissions (5)

cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)cohadar is just really nice (250)

Default [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
__________________
Omg database crash deleted my signature, as a side effect this immensely improved wc3c.

Last edited by cohadar : 12-16-2008 at 08:57 AM.
cohadar is offline   Reply With Quote
Old 12-16-2008, 12:15 AM   #13
Pyrogasm
Lackadaisically Absent.
 
Pyrogasm's Avatar


Respected User
 
Join Date: Sep 2006
Posts: 4,523

Submissions (9)

Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)

Hero Contest - Fourth place

Send a message via ICQ to Pyrogasm Send a message via AIM to Pyrogasm Send a message via MSN to Pyrogasm Send a message via Yahoo to Pyrogasm
Default

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.
__________________
Quote:
Originally posted by Rising_Dusk
Your spells are mostly ignored because they are not very cool so we aren't very excited to review/approve them, but you are incredibly persistent and won't give us an excuse to graveyard it. That is generally what results in a resource being ignored for a long time.

The Spell Request Thread — Done for, unless someone else wants to revive it...
It lasted a damn long time.

Please; Ask for Help Appropriately














Quote:
Originally posted by Kyrbi0
Huh. Almost makes me wish I had a girlfriend, to take advantage of today (wait, no, that's not what I meant... I mean, take advantage of the fact that it is international women's day... gah, never mind).
Quote:
Originally posted by Pyrogasm
Rome may not have been built in a day, but the Romans sure as hell didn't say "look at this great city we built guys!" when they had nothing more than a bit of stone and some cottages.
Pyrogasm is offline   Reply With Quote
Old 12-16-2008, 02:12 AM   #14
Ammorth
I blink, therefore I am.
 
Ammorth's Avatar
 
Join Date: Sep 2006
Posts: 1,812

Submissions (10)

Ammorth is a glorious beacon of light (461)Ammorth is a glorious beacon of light (461)Ammorth is a glorious beacon of light (461)Ammorth is a glorious beacon of light (461)

Default

Only thing I would do is move to Tables so that you don't have a gamecache just for this script.
__________________
Ammorth is offline   Reply With Quote
Old 12-16-2008, 04:36 AM   #15
Pyrogasm
Lackadaisically Absent.
 
Pyrogasm's Avatar


Respected User
 
Join Date: Sep 2006
Posts: 4,523

Submissions (9)

Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)Pyrogasm is a splendid one to behold (638)

Hero Contest - Fourth place

Send a message via ICQ to Pyrogasm Send a message via AIM to Pyrogasm Send a message via MSN to Pyrogasm Send a message via Yahoo to Pyrogasm
Default

Table uses gamecache...
__________________
Quote:
Originally posted by Rising_Dusk
Your spells are mostly ignored because they are not very cool so we aren't very excited to review/approve them, but you are incredibly persistent and won't give us an excuse to graveyard it. That is generally what results in a resource being ignored for a long time.

The Spell Request Thread — Done for, unless someone else wants to revive it...
It lasted a damn long time.

Please; Ask for Help Appropriately














Quote:
Originally posted by Kyrbi0
Huh. Almost makes me wish I had a girlfriend, to take advantage of today (wait, no, that's not what I meant... I mean, take advantage of the fact that it is international women's day... gah, never mind).
Quote:
Originally posted by Pyrogasm
Rome may not have been built in a day, but the Romans sure as hell didn't say "look at this great city we built guys!" when they had nothing more than a bit of stone and some cottages.
Pyrogasm is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 05:46 PM.


Affiliates
The Hubb The JASS Vault Clan WEnW Campaign Creations Clan CBS GamesModding Flixreel Videos

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