- Joined
- Mar 10, 2009
- Messages
- 5,016
TrainSystem v1.3
This system is used in my map and works quite well, I want to test it here...
What the hell is this?
- Auto trains a unit one at a time
- This is more dynamic than GUI AI editor as it doesnt recognize priorities, it will train random units
- If the unit is not yet available or needs food/gold/lumber, it will not count the limit
- Limits the number of units trained by a particular structure
v1.3
- Added 3 more API's
- Fixed a big bug that death event wont subtract correctly
v1.1
- Added 1 API to remove structure from auto train
This system is used in my map and works quite well, I want to test it here...
What the hell is this?
- Auto trains a unit one at a time
- This is more dynamic than GUI AI editor as it doesnt recognize priorities, it will train random units
- If the unit is not yet available or needs food/gold/lumber, it will not count the limit
- Limits the number of units trained by a particular structure
JASS:
/*
=====Train System v1.3
=====By Mckill2009
This is a system in which your building will automatically train a unit, this is good for
AI's or computer players...
REQUIRED NATIVE ABOVE MAP SCRIPT:
native UnitAlive takes unit u returns boolean
API:
static method register takes unit source, integer limit returns nothing
- first thing to do
- registers your source unit that will automatically train units
- sets the initial limit to train units
static method addTrain takes unit source, integer trainedID returns nothing
- second thing to do
- source - the unit that will be training
- trainedID - the unittypeID of the unit being trained
- the more trainedID you register, the higher the probability it will be trained in the list
static method setLimit takes unit source, integer limit returns nothing
- sets/adds the limit of your units being trained
- the limit must be greater than the limit from initial setup (from register)
static method removeTrain takes unit source returns nothing
- removes the source from the list or stops the auto train
static method clearAll takes nothing returns nothing
- removes all units from the list
- destroys the ID's and starts a fresh one
CREDITS:
- Table by Bribe
- RegisterPlayerUnitEvent by Magtheridon96
OTHER CREDITS:
Magtheridon96 (for suggestions)
Troll-Brain (for tests)
*/
library TrainSystem uses Table, RegisterPlayerUnitEvent
struct TrainSystem
unit source
static constant integer maxInstance = 8190
static constant integer childID = 1000
static integer instance = 0
static integer array instanceAr
static timer t = CreateTimer()
static real interval = 1.0
static Table lim //limit of units trained by a particular structure
static Table chk //checks the counter of the trained unit
static Table tra //trained unit reduce counter
static TableArray random
private static method looper takes nothing returns nothing
local thistype this
local integer looper = 0
local integer i
local integer ID
local integer ran
local integer array ar
loop
set looper = looper + 1
set this = instanceAr[looper]
set ID = GetHandleId(.source)
if UnitAlive(.source) and chk.has(ID) then
if (lim[ID]) > (chk[ID]) and chk.boolean[ID] then
set i = 0
loop
set i = i + 1
set ar[i] = random[i][ID]
exitwhen i==random[childID][ID] //maxINDEX
endloop
set ran = GetRandomInt(1,random[childID][ID])
if IssueImmediateOrderById(.source, ar[ran]) then
set chk.boolean[ID] = false
endif
endif
else
call chk.remove(ID)
set .source = null
call .destroy()
set instanceAr[looper] = instanceAr[instance]
set instanceAr[instance] = this
set looper = looper - 1
set instance = instance - 1
if instance==0 then
call PauseTimer(t)
endif
endif
exitwhen looper==instance
endloop
endmethod
private static method deathEvent takes nothing returns nothing
local unit u = GetTriggerUnit()
local unit source //the source that trains the dying unit
local integer ID = GetHandleId(u)
local integer sourceID
if u==tra.unit[ID] then
set source = chk.unit[ID]
set sourceID = GetHandleId(source)
set chk[sourceID] = chk[sourceID] - 1
set chk.boolean[sourceID] = true
call tra.remove(ID)
set source = null
endif
set u = null
endmethod
private static method trainCancel takes nothing returns nothing
local unit u = GetTriggerUnit()
local unit trained = GetTrainedUnit()
local integer ID = GetHandleId(u)
if not chk.boolean[ID] then
set chk.boolean[ID] = true
set chk[ID] = chk[ID] - 1
endif
set u = null
set trained = null
endmethod
private static method trainEnd takes nothing returns nothing
local unit u = GetTriggerUnit()
local unit trained = GetTrainedUnit()
local integer ID = GetHandleId(u)
local integer trID = GetHandleId(trained)
if chk.has(ID) then
set chk[ID] = chk[ID] + 1
set chk.boolean[ID] = true
set chk.unit[trID] = u
set tra.unit[trID] = trained
endif
set u = null
set trained = null
endmethod
private static method onInit takes nothing returns nothing
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_TRAIN_CANCEL, function thistype.trainCancel)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_TRAIN_FINISH, function thistype.trainEnd)
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function thistype.deathEvent)
set lim = Table.create()
set chk = Table.create()
set tra = Table.create()
set random = TableArray[0x2000]
endmethod
//API's==============================
static method register takes unit source, integer limit returns nothing
local integer ID = GetHandleId(source)
local thistype this
if chk.has(ID) then
debug call BJDebugMsg("register ERROR: "+GetUnitName(source)+" is already registered!")
else
if instance==maxInstance then
debug call BJDebugMsg("register ERROR: Unable to allocate more than "+I2S(maxInstance)+" instance.")
call DestroyTimer(t)
else
set this = allocate()
set .source = source
set lim[ID] = limit
set chk[ID] = 0 //checks the limit
set chk.boolean[ID] = true //cheks if registered or not
if instance==0 then
set t = CreateTimer()
call TimerStart(t,interval,true,function thistype.looper)
endif
set instance = instance + 1
set instanceAr[instance] = this
endif
endif
endmethod
static method addTrain takes unit source, integer trainedID returns nothing
local integer ID = GetHandleId(source)
local integer index
if chk.has(ID) then
set random[childID][ID] = random[childID][ID]+1 //maxINDEX
set index = random[childID][ID]
set random[index][ID] = trainedID //so that all trainedID will be saved in a unique index
else
debug call BJDebugMsg("addTrain ERROR: Please register "+GetUnitName(source)+" first.")
endif
endmethod
static method setLimit takes unit source, integer limit returns nothing
local integer ID = GetHandleId(source)
if chk.has(ID) then
if (limit) > (lim[ID]) then
set lim[ID] = limit
else
debug call BJDebugMsg("setLimit ERROR: Limit must be greater than registered value!")
endif
else
debug call BJDebugMsg("addLimit ERROR: "+GetUnitName(source)+" is not registered!")
endif
endmethod
static method removeTrain takes unit source returns nothing
local integer ID = GetHandleId(source)
if chk.has(ID) then
call lim.remove(ID)
call chk.remove(ID)
set random[childID][ID] = 0
else
debug call BJDebugMsg("removeTrain ERROR: "+GetUnitName(source)+" is not registered!")
endif
endmethod
static method clear takes nothing returns nothing
call PauseTimer(t)
call DestroyTimer(t)
call random.destroy()
call lim.destroy()
call chk.destroy()
call tra.destroy()
set instance = 0
set lim = Table.create()
set chk = Table.create()
set tra = Table.create()
set random = TableArray[0x2000]
endmethod
endstruct
endlibrary
-
DEMO
-
Events
- Time - Elapsed game time is 0.00 seconds
- Conditions
-
Actions
- Set U = Barracks 0035 <gen>
- Custom script: call TrainSystem.register(udg_U, 5)
- Custom script: call TrainSystem.addTrain(udg_U, 'hfoo')
- Custom script: call TrainSystem.addTrain(udg_U, 'hrif')
- Set U = Barracks 0036 <gen>
- Custom script: call TrainSystem.register(udg_U, 5)
- Custom script: call TrainSystem.addTrain(udg_U, 'hfoo')
- Custom script: call TrainSystem.addTrain(udg_U, 'hrif')
- Set U = Arcane Sanctum 0039 <gen>
- Custom script: call TrainSystem.register(udg_U, 5)
- Custom script: call TrainSystem.addTrain(udg_U, 'hmpr')
- Custom script: call TrainSystem.addTrain(udg_U, 'hsor')
- Set U = Arcane Sanctum 0040 <gen>
- Custom script: call TrainSystem.register(udg_U, 5)
- Custom script: call TrainSystem.addTrain(udg_U, 'hmpr')
- Custom script: call TrainSystem.addTrain(udg_U, 'hsor')
- Set U = Gryphon Aviary 0037 <gen>
- Custom script: call TrainSystem.register(udg_U, 5)
- Custom script: call TrainSystem.addTrain(udg_U, 'hgry')
- Custom script: call TrainSystem.addTrain(udg_U, 'hdhw')
- Set U = Gryphon Aviary 0038 <gen>
- Custom script: call TrainSystem.register(udg_U, 5)
- Custom script: call TrainSystem.addTrain(udg_U, 'hgry')
- Custom script: call TrainSystem.addTrain(udg_U, 'hdhw')
-
Events
v1.3
- Added 3 more API's
- Fixed a big bug that death event wont subtract correctly
v1.1
- Added 1 API to remove structure from auto train
Attachments
Last edited: