Posté le: 22/01/10 12:20 Sujet du message: Optimisation: Pirate AI
Ce déclencheur pompe beaucoup de mémoire: il vérifie périodiquement et pour tous les joueurs le nombre d'unités joueur de niveau X avec un GroupEnum pour créer ou non des unités en réaction.
(En dehors de ça, créé une petite base, ses ouvriers et lance l'entraînement de vaisseaux selon la puissance des joueur).
Y'a-t-il un moyen de créer des forces pirates AI en fonction du niveau des joueurs sur la carte sans pour autant faire des GroupEnum volumineux?
Secret:
Jass:
library PirateBaseSystem needs GroupFunction, TimerUtils, TextDisplay,Resource
//=================================================
globals
private constant integer BASE = 'o00I'
private constant integer TURRET = 'A01A'
private constant integer MINE = 'n001'
private constant integer SHIP1 = 'h002'
private constant integer SHIP2 = 'h011'
private constant integer SHIP3 = 'h012'
private constant integer CARGO = 'h00N'
private constant integer LSHU = 'h000'
private constant integer ASHU = 'h00C'
private constant integer RFRI = 'h00L'
private constant integer LCRU = 'h00A'
private constant integer NSHU = 'h00G'
private constant integer NFRI = 'h00I'
private constant real DIS = 250.
timer PirateTimerWait
unit PirateBase
unit PirateMine
unit array PirateShipyard [3]
private trigger PirateFirstRun
private trigger PirateBaseDestruction
private trigger PirateTrain
endglobals
//=================================================
private function filter1 takes nothing returns boolean
return IsUnitType(GetFilterUnit(),UNIT_TYPE_ANCIENT)==true
endfunction
//=================================================
private function creabase takes nothing returns nothing
local integer i =0
local real x1 = GetRandomReal(GetRectMinX(bj_mapInitialPlayableArea),GetRectMaxX(bj_mapInitialPlayableArea))
local real y1 = GetRandomReal(GetRectMinY(bj_mapInitialPlayableArea),GetRectMaxY(bj_mapInitialPlayableArea))
local real x2 = 0.
local real y2 = 0.
if CreepsEnabled == true then
call DisplayAll("Radar Officer", "Pirate base detected")
call SetResource(Player(PLAYER_NEUTRAL_AGGRESSIVE),50000,50000)
call SetPlayerState( Player(PLAYER_NEUTRAL_AGGRESSIVE), PLAYER_STATE_RESOURCE_FOOD_CAP, 50 )
set PirateBase = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), BASE,x1,y1, GetRandomReal(0.,360.))
call UnitAddAbility(PirateBase,TURRET)
call UnitAddAbility(PirateBase,TURRET)
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateMine = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE),MINE,x2,y2, GetRandomReal(0.,360.))
call AddResourceAmount(PirateMine,99000 )
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateShipyard[0] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), SHIP1,x2,y2, GetRandomReal(0.,360.))
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateShipyard[1] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), SHIP2,x2,y2, GetRandomReal(0.,360.))
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateShipyard[2] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), SHIP3,x2,y2, GetRandomReal(0.,360.))
loop
set i = i + 1
exitwhen i > 6
call IssueTargetOrder( CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), CARGO,x1,y1, GetRandomReal(0.,360.)), "harvest", PirateMine)
endloop
endif
endfunction
//=================================================
private function IniRunAgain takes nothing returns nothing
set PirateTimerWait = GetExpiredTimer()
if CreepsEnabled==true then
call creabase()
endif
endfunction
//=================================================
private function LoopRun takes nothing returns boolean
local player owner = GetOwningPlayer(GetTriggerUnit())
if GetTriggerUnit() == PirateBase and CreepsEnabled==false then
set owner= GetOwningPlayer(GetKillingUnit())
call KillUnit(PirateMine)
call KillUnit(PirateShipyard[0])
call KillUnit(PirateShipyard[1])
call KillUnit(PirateShipyard[2])
call SetResource(owner,1000,500)
call DisplayAll("Game",PlayerColor[GetPlayerId(owner)] + GetPlayerName(owner) + "|r"+" has destroyed a Neutral Pirate Base.")
set PirateTimerWait = NewTimer()
call TimerStart(PirateTimerWait, 30., false, function IniRunAgain )
endif
set owner = null
return false
endfunction
//=================================================
private function TrainPirateUnits takes nothing returns nothing
local timer t = GetExpiredTimer()
local integer i = 0
local real x1 = 0.
local real y1 = 0.
local real x2 = 0.
local real y2 = 0.
local real distance = 0.
local real angle = 0.
local unit spawnedUnit = null
if CreepsEnabled==true then
loop
exitwhen i > MAXPLAYER
if CountUnitsOfPlayer(Player(PLAYER_NEUTRAL_AGGRESSIVE)) <= 40 and GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
if CountUnitsOfLevel(Player(i),3)>= 5 and GetUnitState(PirateShipyard[0], UNIT_STATE_LIFE) > 0 then
call IssueImmediateOrderById(PirateShipyard[0],NSHU)
call IssueImmediateOrderById(PirateShipyard[0],NSHU)
endif
if CountUnitsOfLevel(Player(i),5)>= 5 and GetUnitState(PirateShipyard[1], UNIT_STATE_LIFE) > 0 then
call IssueImmediateOrderById(PirateShipyard[1],NFRI)
call IssueImmediateOrderById(PirateShipyard[1],NFRI)
endif
if CountUnitsOfLevel(Player(i),10)>= 5 and GetUnitState(PirateShipyard[2], UNIT_STATE_LIFE) > 0 then
call IssueImmediateOrderById(PirateShipyard[2],LCRU)
call IssueImmediateOrderById(PirateShipyard[2],LCRU)
endif
call GroupEnumUnitsOfPlayer(ENUM_GROUP,Player(i),Condition(function filter1))
set x1 = GetUnitX(FirstOfGroup(ENUM_GROUP))
set y1 = GetUnitY(FirstOfGroup(ENUM_GROUP))
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),LSHU,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
if CountUnitsOfType(Player(i), 'h00O') >= 5 then
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),ASHU,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
endif
if CountUnitsOfType(Player(i), 'h00N') >= 15 then
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),NSHU,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
endif
if CountUnitsOfLevel(Player(i),10) >= 10 then
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),RFRI,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
endif
if CountUnitsOfType(Player(i), 'h009') >= 1 then
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),LCRU,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
endif
endif
set i = i + 1
endloop
else
call ReleaseTimer(t)
endif
set spawnedUnit = null
endfunction
//=================================================
private function IniTrainPirateUnits takes nothing returns boolean
local timer t = NewTimer()
if CreepsEnabled==true then
call TimerStart (t, 121., true, function TrainPirateUnits )
endif
call ReleaseTimer(t)
return true
endfunction
//=================================================
private function FirstRun takes nothing returns boolean
if CreepsEnabled==true then
call creabase()
endif
return true
endfunction
//=================================================
function EnablePirateBase takes nothing returns nothing
set PirateFirstRun = CreateTrigger()
call TriggerRegisterTimerEventSingle( PirateFirstRun, 200 )
call TriggerAddCondition(PirateFirstRun, Condition(function FirstRun))
//=================================================
//Count the units of a player whith a level higher than a value
//=================================================
private function ImplementCountUnitOfLevel takes nothing returns boolean
if GetOwningPlayer(GetFilterUnit())==Owner and GetUnitLevel(GetFilterUnit())>=WhichUnitId then
set Count= Count + 1
endif
return true
endfunction
//=================================================
function CountUnitsOfLevel takes player whichPlayer,integer level returns integer
set Count = 0
set WhichUnitId = level
set Owner = whichPlayer
call GroupEnumUnitsOfPlayer(ENUM_GROUP,Owner,Filter(function ImplementCountUnitOfLevel))
return Count
endfunction
Inscrit le: 23 Aoû 2007 Messages: 7143 Sujets: 147 Spécialité en worldedit: le troll, le flood, la vulgarité, mon coeur balance Médailles: 2 (En savoir plus...)
Posté le: 15/03/10 21:09 Sujet du message:
Hmm, faudrait vérifier l'ensemble ton code :
Jass:
if CreepsEnabled==true then
call TimerStart (t, 121., true, function TrainPirateUnits )
endif
call ReleaseTimer(t)
Release un timer en cours d'utilisation, mauvaise idée.
Et il me semble que tu oublies d'en release ailleurs dans ton code (bref regard survolant ton code) _________________
private function filter1 takes nothing returns boolean
return IsUnitType(GetFilterUnit(),UNIT_TYPE_ANCIENT)==true
endfunction
private function creabase takes nothing returns nothing
local integer i =0
local real x1 = GetRandomReal(GetRectMinX(bj_mapInitialPlayableArea),GetRectMaxX(bj_mapInitialPlayableArea))
local real y1 = GetRandomReal(GetRectMinY(bj_mapInitialPlayableArea),GetRectMaxY(bj_mapInitialPlayableArea))
local real x2 = 0.
local real y2 = 0.
if CreepsEnabled == true then
call DisplayAll("Radar Officer", "Pirate base detected")
call SetResource(Player(PLAYER_NEUTRAL_AGGRESSIVE),50000,50000)
call SetPlayerState( Player(PLAYER_NEUTRAL_AGGRESSIVE), PLAYER_STATE_RESOURCE_FOOD_CAP, 50 )
set PirateBase = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), BASE,x1,y1, GetRandomReal(0.,360.))
call UnitAddAbility(PirateBase,TURRET)
call UnitAddAbility(PirateBase,TURRET)
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateMine = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE),MINE,x2,y2, GetRandomReal(0.,360.))
call AddResourceAmount(PirateMine,99000 )
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateShipyard[0] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), SHIP1,x2,y2, GetRandomReal(0.,360.))
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateShipyard[1] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), SHIP2,x2,y2, GetRandomReal(0.,360.))
set x2 = x1 + GetRandomReal(-DIS,DIS)
set y2 = y1 + GetRandomReal(-DIS,DIS)
set PirateShipyard[2] = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), SHIP3,x2,y2, GetRandomReal(0.,360.))
loop
set i = i + 1
exitwhen i > 6
call IssueTargetOrder( CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE), CARGO,x1,y1, GetRandomReal(0.,360.)), "harvest", PirateMine)
endloop
endif
endfunction
private function IniRunAgain takes nothing returns nothing
set PirateTimerWait = GetExpiredTimer()
if CreepsEnabled==true then
call creabase()
endif
endfunction
private function LoopRun takes nothing returns boolean
local player owner = GetOwningPlayer(GetTriggerUnit())
if GetTriggerUnit() == PirateBase and CreepsEnabled==false then
set owner= GetOwningPlayer(GetKillingUnit())
call KillUnit(PirateMine)
call KillUnit(PirateShipyard[0])
call KillUnit(PirateShipyard[1])
call KillUnit(PirateShipyard[2])
call SetResource(owner,1000,500)
call DisplayAll("Game",PlayerColor[GetPlayerId(owner)] + GetPlayerName(owner) + "|r"+" has destroyed a Neutral Pirate Base.")
set PirateTimerWait = NewTimer()
call TimerStart(PirateTimerWait, 30., false, function IniRunAgain )
endif
set owner = null
return false
endfunction
private function TrainPirateUnits takes nothing returns nothing
local timer t = GetExpiredTimer()
local integer i = 0
local real x1 = 0.
local real y1 = 0.
local real x2 = 0.
local real y2 = 0.
local real distance = 0.
local real angle = 0.
local unit spawnedUnit = null
if CreepsEnabled==true then
loop
exitwhen i > MAXPLAYER
if CreepsPop <= 40 and GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
if UnitLevel3[i]>= 5 and GetUnitState(PirateShipyard[0], UNIT_STATE_LIFE) > 0 then
call IssueImmediateOrderById(PirateShipyard[0],NSHU)
call IssueImmediateOrderById(PirateShipyard[0],NSHU)
endif
if UnitLevel5[i]>= 5 and GetUnitState(PirateShipyard[1], UNIT_STATE_LIFE) > 0 then
call IssueImmediateOrderById(PirateShipyard[1],NFRI)
call IssueImmediateOrderById(PirateShipyard[1],NFRI)
endif
if UnitLevel10[i]>= 5 and GetUnitState(PirateShipyard[2], UNIT_STATE_LIFE) > 0 then
call IssueImmediateOrderById(PirateShipyard[2],LCRU)
call IssueImmediateOrderById(PirateShipyard[2],LCRU)
endif
call GroupEnumUnitsOfPlayer(ENUM_GROUP,Player(i),Condition(function filter1))
set x1 = GetUnitX(FirstOfGroup(ENUM_GROUP))
set y1 = GetUnitY(FirstOfGroup(ENUM_GROUP))
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),LSHU,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
if UnitLevel10[i]>= 10 then
set angle = GetRandomReal(0.,360.)
set distance = GetRandomReal(600.,1500.)
set x2 = x1+distance*Cos(angle)
set y2 = y1+distance*Sin(angle)
set spawnedUnit = CreateUnit(Player(PLAYER_NEUTRAL_AGGRESSIVE),RFRI,x2,y2,angle)
call UnitApplyTimedLife(spawnedUnit,'BTLF',120.)
call UnitAddAbility(spawnedUnit, ERRE)
endif
endif
set i = i + 1
endloop
else
call ReleaseTimer(t)
endif
set spawnedUnit = null
endfunction
private function IniTrainPirateUnits takes nothing returns boolean
local timer t = NewTimer()
if CreepsEnabled==true then
call TimerStart (t, 121., true, function TrainPirateUnits )
endif
call ReleaseTimer(t)
return true
endfunction
private function FirstRun takes nothing returns boolean
if CreepsEnabled==true then
call creabase()
endif
return true
endfunction
function EnablePirateBase takes nothing returns nothing
set PirateFirstRun = CreateTrigger()
call TriggerRegisterTimerEventSingle( PirateFirstRun, 200 )
call TriggerAddCondition(PirateFirstRun, Condition(function FirstRun))
Page 1 sur 1 La question posée dans ce topic a été résolue !
Vous ne pouvez pas poster de nouveaux sujets dans ce forum Vous ne pouvez pas répondre aux sujets dans ce forum Vous ne pouvez pas éditer vos messages dans ce forum Vous ne pouvez pas supprimer vos messages dans ce forum Vous ne pouvez pas voter dans les sondages de ce forum