Defending friendly RPLCore unit

Started by Sorrontis, July 31, 2015, 02:34:51 PM

Previous topic - Next topic

Sorrontis

hello, I'm making a map where one core needs to be protected, or automatic loss occurs. I've tried this code, and it simply just ends the game quickly after the start:

once
0 ->creeper
endonce

#core size 3x3
CurrentCoords ->x1 ->y1
<-x1 <-y1 GetUnitAt ->u1
<-x1 1 sub ->x0
<-x1 1 add ->x2
<-y1 1 sub ->y0
<-y1 1 add ->y2

#if any creeper reach any of the 9 squares occupied by core, core is destroyed and game ends
<-x0 <-y0 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x0 <-y1 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x0 <-y2 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x1 <-y0 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x1 <-y1 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x1 <-y2 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x2 <-y0 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x2 <-y1 GetCreeper ->c1
<-c1 <-creeper add ->creeper
<-x2 <-y2 GetCreeper ->c1
<-c1 <-creeper add ->creeper

if(<-creeper 0 gt)
<-u1 2 Destroy
FailMission
PauseGame
endif

0 ->creeper


So, I'm obviously not doing this right. Any advice?
"If you want others to be happy, practice compassion. If you want to be happy, practice compassion."

Lost in Nowhere

This should work:

-1 2 do
    -1 2 do
        I CurrentX add J CurrentY add GetCreeper 0 gt if
            Self 2 Destroy
            FailMission
            PauseGame
        endif
    loop
loop

Firstly, do loops make the code much neater, and quicker to modify.
If you want the mission to fail if there's any creeper under the core, you want to just check all cells under it if there's creeper; if there is under any cell, then you end the game.
Don't die! :)

Sorrontis

Thanks!

I was looking at do loops, but they (syntax) confuse me a bit. Why did you use I and J?
"If you want others to be happy, practice compassion. If you want to be happy, practice compassion."

Grayzzur

Quote from: Sorrontis on July 31, 2015, 06:28:14 PM
Thanks!

I was looking at do loops, but they (syntax) confuse me a bit. Why did you use I and J?
I J & K are defined CRPL keywords. I is the value of the current innermost loop. J is the value of the outer loop. K would be the next one if you nested 3 loops deep.

Also, Lost's numbers are backwards. They should be 2 -1 do for the start of both loops. It's limit first, followed by the starting index. The index is incremented by one each iteration, and when it hits the limit, it's done (does not run for the limit value itself -- which is why the example has 2 instead of 1 as the limite).
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

Sorrontis

That makes a lot of sense. Hopefully we'd never need more than 3 nested loops.

"If you want others to be happy, practice compassion. If you want to be happy, practice compassion."

warren

... The self 2 destroy is before the FailMission and pauseGame, so neither will ever happen.

Sorrontis

shouldn't the rpl core finish it's iteration first? and then stop working
"If you want others to be happy, practice compassion. If you want to be happy, practice compassion."

kwinse

#7
I'm not sure if it stops execution at that point, but you could always put the failmission call in the :destroyed function.

Sorrontis

#8
that is interesting. would I put that in a seperate script, and have both script on the core, or after the "destroy" function in my main script?

EDIT: Same script. I've played with it, works like a wonder!
"If you want others to be happy, practice compassion. If you want to be happy, practice compassion."

Sorrontis

Here's the final piece of code. It will be included in Creeper's Revenge part 5 or 6. Not sure yet.


2 -1 do
   2 -1 do
        I CurrentX add J CurrentY add GetCreeper 0 gt if
            Self 2 Destroy
:destroyed
            FailMission
            PauseGame
        endif
    loop
loop

#special thanks to: Lost in Nowhere, Grayzzur, warren, and kwinse
"If you want others to be happy, practice compassion. If you want to be happy, practice compassion."

warren

#10
You can't put function bodies inside other function bodies. Put :destroyed and contents at end of file.

edit: it is a wonder that it works like that.