Main Menu

CRPL help

Started by Richard333, February 16, 2014, 01:06:05 PM

Previous topic - Next topic

Richard333

New to making CW3 maps and at coding and to the forums. I can't figure out why but the "patrolcount" never goes down when one of the units the base produced dies where as it should make/start making a new one when one of the unit's dies but it never does. I've hammered away at it for a while so i'm asking for some help fixing it.

Spoiler
$MAX_PATROLS:16
$SPAWN_DELAY:150

if (<-patrolCount lt(<-MAX_PATROLS))
if (GetTimer0 eq0)
@CreatePatrol
<-patrolCount add(1) ->patrolCount
SetTimer0(<-SPAWN_DELAY)
endif
endif

:CreatePatrol
CreateUnit("CRPLCore" CurrentX CurrentY) ->unit
AddScriptToUnit(<-unit "Evil Terrain Rail.crpl")
AddScriptToUnit(<-unit "Evil Idol.crpl")
[close]

Spoiler
$MIN_TERRAIN_HEIGHT:10
$MAX_TERRAIN_HEIGHT:10
$SPEED:1.25

once
# Initialize our last position since we don't have one.
-1 ->lastCellX
-1 ->lastCellY
endonce

#If we aren't moving, choose a new location.  The location will be a neighboring cell.
GetQueuedMoveCount eq0 if
@ChooseNewCell if
<-chosenX <-chosenY <-SPEED QueueMove
CurrentX ->lastCellX
CurrentY ->lastCellY
endif
endif


:ChooseNewCell
-1 ->chosenX
-1 ->chosenY

# Get a list of the neighbors that we can move to.
# This call returns the coordinate pairs on the stack.
@GetPossibleCells

# Choose a random location within the list
0 <-count RandInt ->randCellNumber

# Go through the list and pop all of the coordinates off the stack.
# As we pass the coordinates that we chose in our random number above, remember them.
# Those are the coordinates we will be returning.
<-count 0 do
->y
->x
I <-randCellNumber eq if
<-x ->chosenX
<-y ->chosenY
endif
loop
# Return if we chose a new location
<-chosenX -1 neq


:GetPossibleCells
#Check the four neighboring cells to see if they are acceptable terrain heights.
0 ->count
CurrentX 1 add ->cx CurrentY ->cy @CheckCell #Right
CurrentX ->cx CurrentY 1 sub ->cy @CheckCell #Up
CurrentX 1 sub ->cx CurrentY ->cy @CheckCell #Left
CurrentX ->cx CurrentY 1 add ->cy @CheckCell #Down

# By default, we won't return the last cell coordinates.  This is so the patrolling unit
# doesn't return back to where it came from immediately.  But, if the only choice is to return
# to the previous cell, then that is what we have to do.
<-count eq0 if
<-lastCellX
<-lastCellY
1 ->count
endif


:CheckCell
#Check to see if the cell we are looking at is the last cell, if so ignore.
<-cx <-lastCellX neq <-cy <-lastCellY neq or if
# Check if the target cell is an acceptable terrain height.  If so, push the
# coordinates to the stack and increment count.
<-cx <-cy GetTerrain ->terrainHeight
<-terrainHeight <-MIN_TERRAIN_HEIGHT gte <-terrainHeight <-MAX_TERRAIN_HEIGHT lte and if
<-cx
<-cy
<-count 1 add ->count
endif
endif

:Destroyed
if (<-base neq0)
<-base "Base.crpl" "patrolCount" GetScriptVar ->patrolCount
<-patrolCount 1 sub ->patrolCount
<-base "Base.crpl" "patrolCount" <-patrolCount SetScriptVar
endif
[close]

Grayzzur

First, when posting code in a spoiler tag, use the tt tag instead of the code tag. It's hard to see your code smashed into a 2-line box.

You don't mention the file names of the two bits of code above, and that can sometimes help, especially when dealing with GetScriptVar/SetScriptVar.

My first guess, though, would be that you're not setting the "base" variable on the created patrol units. Without the base unit ID, the dying patrol unit will not be able to adjust the patrol count in its :destroyed function.

:CreatePatrol
CreateUnit("CRPLCore" CurrentX CurrentY) ->unit
AddScriptToUnit(<-unit "Evil Terrain Rail.crpl")
AddScriptToUnit(<-unit "Evil Idol.crpl")
<-unit "Name of your 2nd script from above.crpl" "base" Self SetScriptVar

"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

Flabort

Quote from: Grayzzur on February 16, 2014, 02:12:34 PM
First, when posting code in a spoiler tag, use the tt tag instead of the code tag. It's hard to see your code smashed into a 2-line box.
A little off-topic, but it appears to be 12 lines for me, on Firefox, Windows 7.
I guess if it's mashing it down for some configurations, it is more polite to not do so, but I'm just sayin', it works for me.
My maps: Top scores: Sugarplum, Cryz Dal, Cryz Torri, Cryz Bohz (Click fetch scores, page courtesy of kwinse)

Richard333

Quote from: Grayzzur on February 16, 2014, 02:12:34 PM
First, when posting code in a spoiler tag, use the tt tag instead of the code tag. It's hard to see your code smashed into a 2-line box.

You don't mention the file names of the two bits of code above, and that can sometimes help, especially when dealing with GetScriptVar/SetScriptVar.

My first guess, though, would be that you're not setting the "base" variable on the created patrol units. Without the base unit ID, the dying patrol unit will not be able to adjust the patrol count in its :destroyed function.

:CreatePatrol
CreateUnit("CRPLCore" CurrentX CurrentY) ->unit
AddScriptToUnit(<-unit "Evil Terrain Rail.crpl")
AddScriptToUnit(<-unit "Evil Idol.crpl")
<-unit "Name of your 2nd script from above.crpl" "base" Self SetScriptVar


Thank you that fixed it. Also i will keep notes for future threads.

Grayzzur

Quote from: Flabort on February 16, 2014, 02:42:54 PM
Quote from: Grayzzur on February 16, 2014, 02:12:34 PM
First, when posting code in a spoiler tag, use the tt tag instead of the code tag. It's hard to see your code smashed into a 2-line box.
A little off-topic, but it appears to be 12 lines for me, on Firefox, Windows 7.
I guess if it's mashing it down for some configurations, it is more polite to not do so, but I'm just sayin', it works for me.
Huh. I tried IE and that works fine. A little digging and it looks like a bug in the SMF forum software/theme/javascript dealing with webkit browsers (Chrome, Safari).
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker