Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Sorrontis on July 31, 2015, 02:34:51 PM

Title: Defending friendly RPLCore unit
Post by: Sorrontis on July 31, 2015, 02:34:51 PM
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?
Title: Re: Defending friendly RPLCore unit
Post by: Lost in Nowhere on July 31, 2015, 05:22:16 PM
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.
Title: Re: Defending friendly RPLCore unit
Post by: 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?
Title: Re: Defending friendly RPLCore unit
Post by: Grayzzur on July 31, 2015, 08:06:03 PM
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).
Title: Re: Defending friendly RPLCore unit
Post by: Sorrontis on July 31, 2015, 08:55:07 PM
That makes a lot of sense. Hopefully we'd never need more than 3 nested loops.

Title: Re: Defending friendly RPLCore unit
Post by: warren on July 31, 2015, 11:02:38 PM
... The self 2 destroy is before the FailMission and pauseGame, so neither will ever happen.
Title: Re: Defending friendly RPLCore unit
Post by: Sorrontis on July 31, 2015, 11:20:21 PM
shouldn't the rpl core finish it's iteration first? and then stop working
Title: Re: Defending friendly RPLCore unit
Post by: kwinse on July 31, 2015, 11:23:28 PM
I'm not sure if it stops execution at that point, but you could always put the failmission call in the :destroyed (http://knucklecracker.com/wiki/doku.php?id=crpl:docs:func_destroyed) function.
Title: Re: Defending friendly RPLCore unit
Post by: Sorrontis on August 01, 2015, 01:01:24 AM
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!
Title: Re: Defending friendly RPLCore unit
Post by: Sorrontis on August 01, 2015, 01:55:43 AM
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
Title: Re: Defending friendly RPLCore unit
Post by: warren on August 01, 2015, 03:18:05 AM
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.