Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Atrex on April 30, 2014, 06:44:49 PM

Title: Make Enemies Respawn
Post by: Atrex on April 30, 2014, 06:44:49 PM
This script will make enemy structures within the specified range of the CRPL tower respawn when killed. All enemies will create an emitter on death, and you can set the power and interval of this emitter. SpawnRange controls how far from the dead unit the new one can spawn. Also, this script will not work well with moving enemies. If anything moves, the script will think it was destroyed.
# NewRespawn.crpl
# Created on: 4/29/2014 7:08:45 PM
# ------------------------------------------
$boostNumChance:0.2
$power:25
$interval:10
$range:200
$spawnRange:20

once
GetEnemyUnitsInRange(CurrentCoords <-range) ->total
CreateList ->enemies
<-enemies AppendStackToList
CreateList ->enemyX
CreateList ->enemyY
<-enemies GetListCount 0 do
<-enemyX <-enemies I GetListElement CONST_COORDX GetUnitAttribute AppendToList
<-enemyY <-enemies I GetListElement CONST_COORDY GetUnitAttribute AppendToList
loop
endonce
<-enemies GetListCount 0 do
<-enemyX I GetListElement <-enemyY I GetListElement GetUnitAt GetUnitType "POWERZONE" eq if
  CreateUnit("EMITTER" RandCoordsInRange(<-enemyX I GetListElement <-enemyY I GetListElement <-spawnRange)) ->child
  SetScriptVar(<-child 0 "productionInterval" <-interval)
  SetScriptVar(<-child 0 "productionAmt" <-power 1000000 mul)
  <-enemies I <-child SetListElement
  <-enemyX I <-child CONST_COORDX GetUnitAttribute SetListElement
  <-enemyY I <-child CONST_COORDY GetUnitAttribute SetListElement
  randfloat <-boostNumChance lt if
     CreateUnit("EMITTER" RandCoordsInRange(CurrentCoords <-spawnRange)) ->child
     SetScriptVar(<-child 0 "productionInterval" <-interval)
     SetScriptVar(<-child 0 "productionAmt" <-power 1000000 mul)
     <-enemies <-child AppendToList
     <-enemyX <-child CONST_COORDX GetUnitAttribute AppendToList
     <-enemyY <-child CONST_COORDY GetUnitAttribute AppendToList
  endif
endif
loop
Title: Re: Make Enemies Respawn
Post by: Michionlion on April 30, 2014, 07:27:36 PM
One should use the [code] tags to encase CRPL.  Otherwise, looks cool.[/code]
Title: Re: Make Enemies Respawn
Post by: ZackNAttack on April 30, 2014, 11:01:56 PM
Quote from: Atrex on April 30, 2014, 06:44:49 PM
This script will make enemy structures within the specified range of the CRPL tower respawn when killed. All enemies will create an emitter on death, and you can set the power and interval of this emitter. SpawnRange controls how far from the dead unit the new one can spawn. Also, this script will not work well with moving enemies. If anything moves, the script will think it was destroyed.
# NewRespawn.crpl
# Created on: 4/29/2014 7:08:45 PM
# ------------------------------------------
$boostNumChance:0.2
$power:25
$interval:10
$range:200
$spawnRange:20

once
GetEnemyUnitsInRange(CurrentCoords <-range) ->total
CreateList ->enemies
<-enemies AppendStackToList
CreateList ->enemyX
CreateList ->enemyY
<-enemies GetListCount 0 do
<-enemyX <-enemies I GetListElement CONST_COORDX GetUnitAttribute AppendToList
<-enemyY <-enemies I GetListElement CONST_COORDY GetUnitAttribute AppendToList
loop
endonce
<-enemies GetListCount 0 do
<-enemyX I GetListElement <-enemyY I GetListElement GetUnitAt GetUnitType "POWERZONE" eq if
  CreateUnit("EMITTER" RandCoordsInRange(<-enemyX I GetListElement <-enemyY I GetListElement <-spawnRange)) ->child
  SetScriptVar(<-child 0 "productionInterval" <-interval)
  SetScriptVar(<-child 0 "productionAmt" <-power 1000000 mul)
  <-enemies I <-child SetListElement
  <-enemyX I <-child CONST_COORDX GetUnitAttribute SetListElement
  <-enemyY I <-child CONST_COORDY GetUnitAttribute SetListElement
  randfloat <-boostNumChance lt if
     CreateUnit("EMITTER" RandCoordsInRange(CurrentCoords <-spawnRange)) ->child
     SetScriptVar(<-child 0 "productionInterval" <-interval)
     SetScriptVar(<-child 0 "productionAmt" <-power 1000000 mul)
     <-enemies <-child AppendToList
     <-enemyX <-child CONST_COORDX GetUnitAttribute AppendToList
     <-enemyY <-child CONST_COORDY GetUnitAttribute AppendToList
  endif
endif
loop

To fix the moving error, use :destroyed
Title: Re: Make Enemies Respawn
Post by: Lost in Nowhere on April 30, 2014, 11:24:30 PM
The only thing is is that :destroyed is only called when the tower with the script is destroyed, so that wouldn't work...
Title: Re: Make Enemies Respawn
Post by: Annonymus on May 01, 2014, 04:14:18 PM
To fix the problem with moving enemies the script of the moving unit would have to be modified, either by using :destroyed or by telling the re-spawner it's alive every time it moves.
Title: Re: Make Enemies Respawn
Post by: Mikoz on May 02, 2014, 12:15:16 AM
Don't save CONST_COORDX and CONST_COORDY to separate lists. If you use list of unit ID's to receive them each time, then it will work with moving units as well.
Or just check CONST_ISDESTROYED instead of position.