CPRL Movement Error

Started by Jacobwde12, February 27, 2015, 07:27:29 AM

Previous topic - Next topic

Jacobwde12

I have been trying to make a CRPL tower patrol however as the tower is at the x4, it starts glitching out and goes to the bottom left of my screen. The map is a 256x256 map.
Any suggestions on how to fix this? Here's the code:

#Makes the satellite move between multiple points.
#The points are defined once and assigned to local variables.

once
   12 ->x1
   46 ->y1
   13 ->x2
   255 ->y2
   84 ->x3
   247 ->y3
   124 ->x4
   206 ->y4
   188 ->x5
   253 ->y5
   246 ->x6
   249 ->y6
endonce

#Check to see if we are moving. If not queue up a movement
GetQueuedMoveCount eq0 if
   #If we are at the first position, move to the second, then move to the third, etc.. 
   #Otherwise move to the first position.
   CurrentX <-x5 eq if
      <-x1 <-y1 1 QueueMove
   CurrentX <-x1 eq else
      <-x2 <-y2 1 QueueMove
   CurrentX <-x2 eq else
  <-x3 <-y3 1 QueueMove
   CurrentX <-x3 eq else
      <-x4 <-y4 1 QueueMove
   CurrentX <-x4 eq else
  <-x5 <-y5 1 QueueMove
   CurrentX <-x5 eq else
      <-x6 <-y6 eq QueueMove
   endif
endif

Grayzzur

Your first check is for position 5 instead of 6 (the last spot). Your last move command has "eq" instead of "1" before "QueueMove" -- but you never hit that piece of code as-is.

The big problem is that "if-else-endif" doesn't work the way you have it. You can't go "if-else-else-else-else-endif", you have to nest individual if statements.


GetQueuedMoveCount eq0 if
   #If we are at the first position, move to the second, then move to the third, etc.. 
   #Otherwise move to the first position.
CurrentX <-x6 eq if
<-x1 <-y1 1 QueueMove
else
CurrentX <-x1 eq if
<-x2 <-y2 1 QueueMove
else
CurrentX <-x2 eq if
<-x3 <-y3 1 QueueMove
else
CurrentX <-x3 eq if
<-x4 <-y4 1 QueueMove
else
CurrentX <-x4 eq if
<-x5 <-y5 1 QueueMove
else
CurrentX <-x5 eq if
<-x6 <-y6 1 QueueMove
endif
endif
endif
endif
endif
endif
endif
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

kwinse

#2
Why not just QueueMove all the points of patrol all at once? The movement queue is built for that purpose after all.

EDIT: The only immediate reason I can think not to is if some script changes the patrol coordinates.

warren

That script will not work if two coordinates have the same x value.

I would use the pattern:

else CurrentX <-x_ eq CurrentY <-y_ eq or if

I would also consider switching to a list to store an arbitrary number of points.
I would then also consider simply storing the current coordinate as a variable.

Grayzzur

kwinse and warren both have excellent points. I sort of had my blinders on and was just answering the initial question.. why doesn't your script work.. as opposed to how to best make a core follow a patrol path.

First step per kwinse is to realize that you can queue multiple moves at once, and plug in the entire patrol path -- then plug it in again when the queued count hits zero. This also addresses warren's concern about having multiple points with the same X location.

GetQueuedMoveCount eq0 if
<-x1 <-y1 1 QueueMove
<-x2 <-y2 1 QueueMove
<-x3 <-y3 1 QueueMove
<-x4 <-y4 1 QueueMove
<-x5 <-y5 1 QueueMove
<-x6 <-y6 1 QueueMove
endif


However, using x1, x2, x3... y1, y2, y3... is messy, and as warren suggests, using a list is a better way to go. Assuming you just want the core to follow a patrol path repeatedly, you could do something like this:


once
   CreateList ->patrolPath
   ClearStack #just in case
   12 46
   13 255
   84 247
   124 206
   188 253
   246 249
   <-patrolPath PrependStackToList
   
   1 ->patrolSpeed
endonce

GetQueuedMoveCount eq0 if
0 ->i
while <-i <-patrolPath GetListCount lt repeat
<-patrolPath <-i GetListElement
<-patrolPath add(<-i 1) GetListElement
<-patrolSpeed QueueMove

<-i 2 add ->i
endwhile
endif


If lists are too much to deal with, go back to the first suggestion, but you really don't need to put the values in variables.

GetQueuedMoveCount eq0 if
12 46 1 QueueMove
13 255 1 QueueMove
84 247 1 QueueMove
124 206 1 QueueMove
188 253 1 QueueMove
246 249 1 QueueMove
endif
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

Xalos

Quote from: warren on February 27, 2015, 09:03:32 PM
That script will not work if two coordinates have the same x value.

I would use the pattern:

else CurrentX <-x_ eq CurrentY <-y_ eq or if

I would also consider switching to a list to store an arbitrary number of points.
I would then also consider simply storing the current coordinate as a variable.

It would need to be and, wouldn't it? or would execute the if block if either coordinate matched, creating a plus shape across the map of regions that would match the current point.

Xeos

Quote from: Grayzzur on February 27, 2015, 11:27:56 PM
kwinse and warren both have excellent points. I sort of had my blinders on and was just answering the initial question.. why doesn't your script work.. as opposed to how to best make a core follow a patrol path.

First step per kwinse is to realize that you can queue multiple moves at once, and plug in the entire patrol path -- then plug it in again when the queued count hits zero. This also addresses warren's concern about having multiple points with the same X location.

GetQueuedMoveCount eq0 if
<-x1 <-y1 1 QueueMove
<-x2 <-y2 1 QueueMove
<-x3 <-y3 1 QueueMove
<-x4 <-y4 1 QueueMove
<-x5 <-y5 1 QueueMove
<-x6 <-y6 1 QueueMove
endif


However, using x1, x2, x3... y1, y2, y3... is messy, and as warren suggests, using a list is a better way to go. Assuming you just want the core to follow a patrol path repeatedly, you could do something like this:


once
   CreateList ->patrolPath
   ClearStack #just in case
   12 46
   13 255
   84 247
   124 206
   188 253
   246 249
   <-patrolPath PrependStackToList
   
   1 ->patrolSpeed
endonce

GetQueuedMoveCount eq0 if
0 ->i
while <-i <-patrolPath GetListCount lt repeat
<-patrolPath <-i GetListElement
<-patrolPath add(<-i 1) GetListElement
<-patrolSpeed QueueMove

<-i 2 add ->i
endwhile
endif


If lists are too much to deal with, go back to the first suggestion, but you really don't need to put the values in variables.

GetQueuedMoveCount eq0 if
12 46 1 QueueMove
13 255 1 QueueMove
84 247 1 QueueMove
124 206 1 QueueMove
188 253 1 QueueMove
246 249 1 QueueMove
endif

Great post and very well explained for a noob like me - thanks :)
A child of 5 could beat this map ... Go fetch a child of 5 !