• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS][Utility]Gicn- Get Item Cost

Level 31
Joined
Jul 10, 2007
Messages
6,306
[vJASS][Utility]Gcsn- Get Item Cost

Gcsn- Get Costs and States
The First Cost and State Utility Without Leaks!
Current Version- v3.0
Guide Version- (NA)



News Count- 2




#1- There's nothing out there that complies with Mfn atm.

#2- Everything that is out there leaks items >: (



The point is-
If a player leaves and you want to get the resource costs of their items
AI??

And that's about all I can think of ^^. If you find a use I didn't think of, awesome =D.

Also, the functions from common.ai (GetUnitGoldCost and GetUnitWoodCost) crash the map sometimes when used on custom units. I don't suggest using them unless you are using 0 custom units (edited normal units are fine).


Function List-
getItemChargesId(integer itemType)-
gets the default charge of an item

getItemGoldCostId(integer itemType)-
getItemWoodCostId(integer itemType)-
returns the cost of an item via it's id

getItemGoldCost(item i)-
getItemWoodCost(item i)-
returns the cost of an item (charges etc)

Same types of functions-
getUnitGoldCostId
getUnitWoodCostId
getUnitGoldCost
getUnitWoodCost

More functions-
updatePlayer(playerId)-
updates a player so that current gold and lumber can be accessed

getPlayerGold(playerId)-
gets the gold from last update of a player

getPlayerWood(playerId)-
same, but wood

getBounty(unitType)-
gets a unit's bounty via an integer. It returns an average : o.

Settings area looks like this-
JASS:
//! textmacro Gicn_USER_SETTINGS
    set COORD_X = GetRectCenterX(bj_mapInitialPlayableArea)
    set COORD_Y = GetRectMaxY(bj_mapInitialPlayableArea)
    
    set ATTACK_TYPE = ATTACK_TYPE_CHAOS
    set DAMAGE_TYPE = DAMAGE_TYPE_UNKNOWN
    set WEAPON_TYPE = WEAPON_TYPE_WHOKNOWS
//! endtextmacro


ATTACK_TYPE is an attack type that does damage to all armor types
DAMAGE_TYPE is a damage type that does damage to all armor types or w/e
WEAPON_TYPE is a weapon type that always does damage

COORD_X abd COORD_Y refer to a position on the map where you want this to be made. Making this outside of the playable map area is not advised as the items won't be removed : |. It will still work, it will just leak like the rest : (.

So put it in a nice spot at the edge of the map ^_^. If you happen to have a spot open, put it there. If there is a big empty spot, put it there. Really, you could even have it somewhere in the middle of the map because the unit doesn't show, just be warned that an item might be seen flashing or an item might "accidentally" be removed from the game. The chance of this happening if you decide to put it where players are playing is just about 0, but it's still "slightly" possible. I suggest you put it on the edge of the map ^^.


Just cnp this demonstrations into ur Mfn User Area ^_-

Demonstration-
JASS:
library a initializer ini requires RequiresMfn RequiresGcsn
    globals
        private trigger t
    endglobals
    
    private function run takes nothing returns nothing
        call Gcsn_updatePlayer(0)
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Total Gold: " + I2S(Gcsn_getPlayerGold(0)))
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Total Wood: " + I2S(Gcsn_getPlayerWood(0)))
    endfunction
    
    private function start takes nothing returns nothing
        set t = CreateTrigger()
        call TriggerRegisterTimerEvent(t, 5, true)
        call TriggerAddAction(t, function run)
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(Gcsn_getUnitGoldCostId('hpea')))
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(Gcsn_getItemGoldCostId('odef')))
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(Gcsn_getItemCharges('whwd')))
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, I2S(Gcsn_getBounty('hpea')))
    endfunction
    
    private function ini takes nothing returns nothing
        call TriggerAddAction(Mfn_ini, function start)
    endfunction
endlibrary


-News Section-


First release of Get Item Cost.

Very well tested.


The second release is now released with lots of new stuff. Array searching is now done via a hash that was made by Jesus4Lyf with a free memory command by me and a one value change, which can be found in Mfn until Jesus4Lyf updates his raw hashcode thingie. I guess both of us are authors of it now ^^.

This also includes new things for GetUnitGoldCost and GetUnitWoodCost as well as things for getting player gold and player lumber : ).

Version 3.0 will include GetUpgradeGoldCost and GetUpgradeWoodCost as well as an improved GetUnitCosts and a GetResearchCost with levels : ). With these improvements, a user will be able to get the total gold of a player that was spent through out the entirety of the game. You can also add to this value based on your own things, but everything done thru wc3 will be able to be captured.

There will also be a GetUnitsCost and GetItemsCost for getting the costs of all units owned by a player and all items owned by a player.



-Utility Section-

JASS:
//! textmacro Gicn_USER_SETTINGS
    set COORD_X = GetRectCenterX(bj_mapInitialPlayableArea)
    set COORD_Y = GetRectMaxY(bj_mapInitialPlayableArea)
    
    set ATTACK_TYPE = ATTACK_TYPE_CHAOS
    set DAMAGE_TYPE = DAMAGE_TYPE_UNKNOWN
    set WEAPON_TYPE = WEAPON_TYPE_WHOKNOWS
//! endtextmacro

JASS:
library Gcsn initializer ini requires RequiresMfn
    globals
        //Version Information
        private constant string SYSTEM = "Gcsn"
        private constant string VERSION = "3.0"
        private constant string AUTHOR = "Nestharus"
        private constant string HELPERS = "None"
        private constant string TEXTURE_PATH = "UI\\Feedback\\Resources\\ResourceGold.blp"
        private constant string DESCRIPTION = "Get Costs"
        
        private rect removalArea
        
        private constant integer UNIT_TYPE_ID = 'hpea'
        private real COORD_X
        private real COORD_Y
        private damagetype DAMAGE_TYPE
        private attacktype ATTACK_TYPE
        private weapontype WEAPON_TYPE
        
        private constant player  PLAYER_CHECKER = Player(14)
        private unit unitChecker
        
        private trigger bugFix
        
        private integer array goldResource[12]
        private integer array lumberResource[12]
    endglobals
    
    private scope ItemType
        globals
            public integer array goldCost
            public integer array woodCost
            public integer array itemCharges
            private integer currentItemType
            private integer currentHash
        endglobals
        
        private function removeItem takes nothing returns nothing
            local item i = GetEnumItem()
            local integer typeId = GetItemTypeId(i)
            if typeId == currentItemType then
                set itemCharges[currentHash] = GetItemCharges(i)
                call RemoveItem(i)
            endif
            set i = null
        endfunction
        
        private function new takes integer hash, integer itemType returns nothing
            set currentItemType = itemType
            set currentHash = hash
            call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD, 1000000)
            call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_LUMBER, 1000000)
            call AddItemToStock(unitChecker, itemType, 1, 1)
            call IssueNeutralImmediateOrderById(PLAYER_CHECKER, unitChecker, itemType)
            set goldCost[hash] = 1000000 - GetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD)
            set woodCost[hash] = 1000000 - GetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_LUMBER)
            call EnumItemsInRect(removalArea, null, function removeItem)
            call RemoveItemFromStock(unitChecker, itemType)
        endfunction
        
        //! runtextmacro Hash_CREATE("call new(hash, int)")
    endscope
    
    private scope UnitType
        globals
            public integer array goldCost
            public integer array woodCost
            private integer currentUnitType
        endglobals
        
        private function removeUnit takes nothing returns nothing
            local unit u = GetEnumUnit()
            if GetUnitTypeId(u) == currentUnitType then
                call RemoveUnit(u)
            endif
            set u = null
        endfunction
        
        private function new takes integer hash, integer unitType returns nothing
            local group g = CreateGroup()
            set currentUnitType = unitType
            call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD, 1000000)
            call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_LUMBER, 1000000)
            call AddUnitToStock(unitChecker, unitType, 1, 1)
            call IssueNeutralImmediateOrderById(PLAYER_CHECKER, unitChecker, unitType)
            set goldCost[hash] = 1000000 - GetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD)
            set woodCost[hash] = 1000000 - GetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_LUMBER)
            call GroupEnumUnitsInRange(g, COORD_X, COORD_Y-64, 256, null)
            call ForGroup(g, function removeUnit)
            call RemoveUnitFromStock(unitChecker, unitType)
            call DestroyGroup(g)
            set g = null
        endfunction
        
        //! runtextmacro Hash_CREATE("call new(hash, int)")
    endscope
    
    private scope UnitBounty
        globals
            public integer array goldBounty
        endglobals
        
        private function new takes integer hash, integer unitType returns nothing
            local unit u
            local integer average = 0
            local integer count = 1
            set u = CreateUnit(Player(12), unitType, COORD_X, COORD_Y, 270)
            call SetUnitState(u, UNIT_STATE_LIFE, 1)
            call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD, 0)
            call UnitDamageTarget(unitChecker, u, 100000000, true, false, ATTACK_TYPE_CHAOS, DAMAGE_TYPE_UNKNOWN, WEAPON_TYPE_WHOKNOWS)
            call RemoveUnit(u)
            set average = GetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD)
            loop
                set count = count + 1
                set u = CreateUnit(Player(12), unitType, COORD_X, COORD_Y, 270)
                call SetUnitState(u, UNIT_STATE_LIFE, 1)
                call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD, 0)
                call UnitDamageTarget(unitChecker, u, 100000000, true, false, ATTACK_TYPE, DAMAGE_TYPE, WEAPON_TYPE)
                call RemoveUnit(u)
                set average = (average+GetPlayerState(PLAYER_CHECKER, PLAYER_STATE_RESOURCE_GOLD))/2
                exitwhen count == 2
            endloop
            set goldBounty[hash] = average
            set u = null
        endfunction
        
        //! runtextmacro Hash_CREATE("call new(hash, int)")
    endscope
    
    public function getBounty takes integer unitType returns integer
        return UnitBounty_goldBounty[UnitBounty_Hash(unitType)]
    endfunction
    
    public function getItemCharges takes integer itemType returns integer
        return ItemType_itemCharges[ItemType_Hash(itemType)]
    endfunction

    public function getItemGoldCostId takes integer itemType returns integer
        return ItemType_goldCost[ItemType_Hash(itemType)]
    endfunction

    public function getItemWoodCostId takes integer itemType returns integer
        return ItemType_woodCost[ItemType_Hash(itemType)]
    endfunction
    
    public function getItemGoldCost takes item i returns integer
        local integer itemType = GetItemTypeId(i)
        return R2I(I2R(ItemType_goldCost[ItemType_Hash(itemType)]*(GetItemCharges(i)))/ItemType_itemCharges[ItemType_Hash(itemType)])
    endfunction
    
    public function getItemWoodCost takes item i returns integer
        local integer itemType = GetItemTypeId(i)
        return R2I(I2R(ItemType_woodCost[ItemType_Hash(itemType)]*(GetItemCharges(i)))/ItemType_itemCharges[ItemType_Hash(itemType)])
    endfunction
    
    public function getUnitGoldCostId takes integer unitType returns integer
        return UnitType_goldCost[UnitType_Hash(unitType)]
    endfunction
    
    public function getUnitWoodCostId takes integer unitType returns integer
        return UnitType_woodCost[UnitType_Hash(unitType)]
    endfunction
    
    public function getUnitGoldCost takes unit u returns integer
        local integer unitType = GetUnitTypeId(u)
        local integer cost = UnitType_goldCost[UnitType_Hash(unitType)]
        local integer x = 0
        local integer inventorySize = UnitInventorySize(u)
        local item i
        if inventorySize > 0 then
            loop
                exitwhen x == inventorySize
                set i = UnitItemInSlot(u, x)
                if GetItemTypeId(i) > 0 then
                    set cost = cost + getItemGoldCost(i)
                endif
                set x = x + 1
            endloop
        endif
        set i = null
        set u = null
        return cost
    endfunction
    
    public function getUnitWoodCost takes unit u returns integer
        local integer unitType = GetUnitTypeId(u)
        local integer cost = UnitType_woodCost[UnitType_Hash(unitType)]
        local integer x = 0
        local integer inventorySize = UnitInventorySize(u)
        local item i
        if inventorySize > 0 then
            loop
                exitwhen x == inventorySize
                set i = UnitItemInSlot(u, x)
                if GetItemTypeId(i) > 0 then
                    set cost = cost + getItemWoodCost(i)
                endif
                set x = x + 1
            endloop
        endif
        return cost    
    endfunction
    
    private function forUnit takes nothing returns nothing
        local unit u = GetEnumUnit()
        local integer pId = GetPlayerId(GetOwningPlayer(u))
        set goldResource[pId] = goldResource[pId] + getUnitGoldCost(u)
        set lumberResource[pId] = lumberResource[pId] + getUnitWoodCost(u)
        set u = null
    endfunction
    
    private function checkUnit takes nothing returns boolean
        return (GetUnitState(GetEnumUnit(), UNIT_STATE_LIFE) > 0 or IsUnitType(GetEnumUnit(), UNIT_TYPE_HERO))
    endfunction
    
    public function updatePlayer takes integer playerId returns nothing
        local group g = CreateGroup()
        local player p = Player(playerId)
        set goldResource[playerId] = GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD)
        set lumberResource[playerId] = GetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER)
        call GroupEnumUnitsOfPlayer(g, Player(playerId), Condition(function checkUnit))
        call ForGroup(g, function forUnit)
        call GroupClear(g)
        call DestroyGroup(g)
        set p = null
        set g = null
    endfunction
    
    public function getPlayerGold takes integer playerId returns integer
        return goldResource[playerId]
    endfunction
    
    public function getPlayerWood takes integer playerId returns integer
        return lumberResource[playerId]
    endfunction
    
    private function stopBug takes nothing returns nothing
        if (GetUnitX(unitChecker) != COORD_X) or (GetUnitY(unitChecker) != COORD_Y-64) then
            call IssueImmediateOrder(unitChecker, "stop")
            call IssuePointOrder(unitChecker, "move", COORD_X, COORD_Y-64)
        endif
    endfunction

    private function ini takes nothing returns nothing
        call Mfn_createCredit1(SYSTEM, VERSION, AUTHOR, HELPERS, TEXTURE_PATH, DESCRIPTION)
        //! runtextmacro Gicn_USER_SETTINGS()
        set unitChecker = CreateUnit(PLAYER_CHECKER, UNIT_TYPE_ID, Mfn_Map_centerX, Mfn_Map_centerY, 270)
        set removalArea = Rect(Mfn_Map_centerX-64, Mfn_Map_centerX-64, Mfn_Map_centerY+64, Mfn_Map_centerY+64)
        set bugFix = CreateTrigger()
        call TriggerRegisterTimerEvent(bugFix, .5, true)
        call TriggerAddAction(bugFix, function stopBug)
        call UnitAddAbility(unitChecker,'Asid')
        call UnitAddAbility(unitChecker,'Asud')
        call UnitAddAbility(unitChecker, 'Aloc')
        call UnitRemoveAbility(unitChecker, 'Awan')
        call UnitRemoveAbility(unitChecker, 'Aneu')
        call UnitRemoveAbility(unitChecker, 'Ane2')
        call ShowUnit(unitChecker, false)
        call SetUnitOwner(unitChecker, PLAYER_CHECKER, false)
        call SetUnitX(unitChecker, COORD_X)
        call SetUnitY(unitChecker, COORD_Y-64)
        call MoveRectTo(removalArea, COORD_X, COORD_Y-64)
        call SetPlayerState(PLAYER_CHECKER, PLAYER_STATE_GIVES_BOUNTY, 1)
    endfunction
endlibrary

library RequiresGcsn requires Gcsn
endlibrary
 
Last edited:
Level 14
Joined
Nov 18, 2007
Messages
816
Heres how i retrieve a units gold and lumber cost:

JASS:
    native GetUnitGoldCost      takes integer unitid                        returns integer
    native GetUnitWoodCost      takes integer unitid                        returns integer

// ...

call GetUnitGoldCost(someInt)
call GetUnitWoodCost(someInt)

AI-natives are cool.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Heres how i retrieve a units gold and lumber cost:

JASS:
    native GetUnitGoldCost      takes integer unitid                        returns integer
    native GetUnitWoodCost      takes integer unitid                        returns integer

// ...

call GetUnitGoldCost(someInt)
call GetUnitWoodCost(someInt)

AI-natives are cool.

That crashes randomly o-o. I use it on one custom unit and it's fine and then I use it on another and it's not fine : O. If it weren't for the random map crashes when using that, I wouldn't have done it for this ; ).

Yes, I do know those exist ; P
 
Top