获取物品出售价格

该库会创建物品并进行出售,来获取物品的出售价格(在第一次获取某种物品时)。同时记录该价格在哈希表,以便下次可以直接获取。

库来源:https://www.hiveworkshop.com

缺憾:

  • 无法获取力量提升类型的物品价格,因为该类型的物品到背包就直接使用了。
  • 需要在游戏平衡性选项中设置物品出售价格的系数。如果设置为50%这里获取的是50%的出售价格。

以下是可用的函数:

获取对应物品类型的出售价格(金币)

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

库代码

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
分享

Joy Wen

天下无人不闻君

0 条评论

发表评论