Name | Type | is_array | initial_value |
TempInt | integer | No | |
UTBC_ConfigUnitType | unitcode | Yes | |
UTBC_ConfigUnitTypeCount | integer | No | |
UTBC_InsufficientFood | boolean | No | |
UTBC_InsufficientGold | boolean | No | |
UTBC_InsufficientLumber | boolean | No | |
UTBC_OrderSuccessful | boolean | No | |
UTBC_TrainButtonPlayer | player | No | |
UTBC_TrainButtonUnitType | unitcode | No | UnitTypeNull |
/*****************************************************************/
/* You can delete these native declarations here */
/* if they are already present elsewhere in your map */
/*****************************************************************/
native GetUnitGoldCost takes integer unitId returns integer
native GetUnitWoodCost takes integer unitId returns integer
/****************************************************************************************/
/* */
/* UnitTrainButtonClick v1.0 by Derdan */
/* Requirements: None */
/* Optional: Table by Bribe */
/* https://www.hiveworkshop.com/threads/snippet-new-table.188084/ */
/* */
/* Motivation: */
/* The new train button events cannot catch what */
/* unit type was ordered to be trained. This */
/* system handles that for you. */
/* */
/* The new events are useful because they fire */
/* even if you can't afford the unit's gold, lumber, */
/* or food cost. Regular train orders do not do this. */
/* */
/* Usage: */
/* In the config trigger, register all the unit types */
/* whose train button click you want to be able to */
/* catch. This might be a lot of units but for now */
/* I don't know how it can be done automatically. */
/* */
/* In your triggers using this system: */
/* Events: New train button events. */
/* Conditions: Cannot use the UTBC_ variables! */
/* Actions: (Wait 0.00 seconds) must come before using */
/* the UTBC_ variables! */
/* */
/* Example trigger: */
/* Events */
/* Player - Button for train Footman pressed. */
/* (Footman must be in config trigger!) */
/* Conditions */
/* Actions */
/* Wait 0.00 seconds */
/* (Now you can use UTBC_ variables) */
/* UTBC_TrainButtonUnitType - Unit type the button issues a train order for */
/* UTBC_TrainButtonPlayer - Owner of the barracks */
/* UTBC_InsufficientGold - true if player cannot afford the unit's gold cost */
/* UTBC_InsufficientLumber - same as above but for lumber */
/* UTBC_InsufficientFood - same as above but for food */
/* UTBC_OrderSuccessful - true if the unit is now being trained */
/* (Triggering unit) - the barracks */
/* */
/****************************************************************************************/
library UnitTrainButtonClick uses optional Table
/*****************************************************************/
/* Use these functions over the natives to prevent game crashes! */
/*****************************************************************/
function GetUnitTypeGoldCost takes integer unitId returns integer
if (IsUnitIdType(unitId, UNIT_TYPE_HERO)) then
return 0
endif
return GetUnitGoldCost(unitId)
endfunction
function GetUnitTypeWoodCost takes integer unitId returns integer
if (IsUnitIdType(unitId, UNIT_TYPE_HERO)) then
return 0
endif
return GetUnitWoodCost(unitId)
endfunction
/*****************************************************************/
private struct S extends array
static if LIBRARY_Table then
static Table unitIdTable
static Table registeredUnitIds
else
static hashtable ht
endif
private static method onUnitTrainButtonClick takes nothing returns nothing
local player p = GetOwningPlayer(GetTriggerUnit())
static if LIBRARY_Table then
set udg_UTBC_TrainButtonUnitType = unitIdTable[GetHandleId(GetTriggeringTrigger())]
else
set udg_UTBC_TrainButtonUnitType = LoadInteger(ht, 0, GetHandleId(GetTriggeringTrigger()))
endif
set udg_UTBC_TrainButtonPlayer = p
set udg_UTBC_InsufficientGold = GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD) < GetUnitTypeGoldCost(udg_UTBC_TrainButtonUnitType)
set udg_UTBC_InsufficientLumber = GetPlayerState(p, PLAYER_STATE_RESOURCE_LUMBER) < GetUnitTypeWoodCost(udg_UTBC_TrainButtonUnitType)
set udg_UTBC_InsufficientFood = GetPlayerState(p, PLAYER_STATE_RESOURCE_FOOD_USED) + GetFoodUsed(udg_UTBC_TrainButtonUnitType) > GetPlayerState(p, PLAYER_STATE_RESOURCE_FOOD_CAP)
set udg_UTBC_OrderSuccessful = not (udg_UTBC_InsufficientGold or udg_UTBC_InsufficientLumber or udg_UTBC_InsufficientFood)
endmethod
private static method isUnitIdRegistered takes integer unitId returns boolean
static if LIBRARY_Table then
return registeredUnitIds.has(unitId)
else
return HaveSavedBoolean(ht, 1, unitId)
endif
endmethod
private static method registerUnitId takes integer unitId returns nothing
local trigger t
// Make sure not to register the train button event more than once for each unit type
if (not isUnitIdRegistered(unitId)) then
set t = CreateTrigger()
// This new event is handy. It fires even if the player is unable to afford the unit.
// Therefore we can decide what to do after checking whether they can afford the unit or not.
// Unfortunately it cannot be determined through event response what unit type is to be trained.
// This information needs to be linked to a trigger for each individual unit type when
// the event is registered to be able to access it.
call TriggerRegisterTrainCommandEventBJ(t, unitId)
call TriggerAddCondition(t, function thistype.onUnitTrainButtonClick)
static if LIBRARY_Table then
set unitIdTable[GetHandleId(t)] = unitId
set registeredUnitIds.boolean[unitId] = true
else
call SaveInteger(ht, 0, GetHandleId(t), unitId)
call SaveBoolean(ht, 1, unitId, true)
endif
endif
set t = null
endmethod
static method registerUnitTypes takes nothing returns nothing
local integer i = 0
loop
exitwhen (i == udg_UTBC_ConfigUnitTypeCount)
call registerUnitId(udg_UTBC_ConfigUnitType[i])
set i = i + 1
endloop
// Get rid of space we don't need anymore...
static if LIBRARY_Table then
call registeredUnitIds.destroy()
else
call FlushChildHashtable(ht, 1)
endif
endmethod
private static method onInit takes nothing returns nothing
static if LIBRARY_Table then
set unitIdTable = Table.create()
set registeredUnitIds = Table.create()
else
set ht = InitHashtable()
endif
endmethod
endstruct
function UTBC_RegisterUnitTypes takes nothing returns nothing
call S.registerUnitTypes()
endfunction
endlibrary