Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Hubs on February 18, 2015, 01:40:33 PM

Title: IsCreeperInRange
Post by: Hubs on February 18, 2015, 01:40:33 PM
Has anyone used IsCreeperInRange? I've tried to use it in various ways and can't figure out what the variables required are. The wiki page is blank for it. I know the first two are X and Y, and I've read on the forums that there are 5 more variables (7 total).

I found an update from a beta build: "July 22, 2013: Added IsCreeperInRange CRPL API (threshold, greater/less than support, AC or C, square or circle)"

My guess was the variables are X, Y, distance, threshhold, above/below threshhold, AC/C, Square/Circle.  I tried many different variations, but couldn't get it to work as expected. If anyone has had more luck than me, let me know!

Thanks,
Hubs
Title: Re: IsCreeperInRange
Post by: Grayzzur on February 18, 2015, 02:57:10 PM
So far it looks like:
X Y Distance ? ? IsSquare DetectAC

The 6th parameter seems to change the range between square (true) and circle (false). The 7th seems to make it look for AC (when true) instead of C. That's with "1 0" in for the two questions marks. So far, any other values I try in those slots make the function always return 1.

Still playing with it, but that's where I'm stuck.
Title: Re: IsCreeperInRange
Post by: knucracker on February 18, 2015, 04:06:32 PM
Looks like the Vapen mission uses it.  I've included the example below.  As for the function itself, these are the arguments:
[int x, int y, int range, int threshold, bool lessThan, bool square, bool AC]

The internal function looks like this:

private bool IsCreeperInRange(int x, int y, int range, int threshold, bool lessThan, bool square, bool AC) {
int leftX = x-range; if ( leftX < 0) leftX = 0;
int rightX = x+range; if (rightX >= GameSpace.GAMESPACE_WIDTH) rightX = GameSpace.GAMESPACE_WIDTH-1;
int topY = y-range; if ( topY < 0) topY = 0;
int bottomY = y+range; if (bottomY >= GameSpace.GAMESPACE_HEIGHT) bottomY = GameSpace.GAMESPACE_HEIGHT-1;

for (int j = topY; j <= bottomY; j++) {
for (int i = leftX; i <= rightX; i++) {
int d2 = (x-i)*(x-i) + (y-j)*(y-j);
if (square || range < 0 || d2 < range*range) {
int c = GameSpace.instance.creeperSimulation.GetCreeper(i, j);
if (AC && c > 0) continue;
if (!AC && c < 0) continue;
if (AC) c = -c;
if (lessThan) {
if (c <= threshold) return true;
} else {
if (c >= threshold) {
return true;
}
}
}
}
}
return false;
}



The CRPL Example:

$powerLineUnitX:84
$powerLineUnitY:81

$beamUnitX:84
$beamUnitY:63

$initialDelay:0

once
GetUnitAt(<-powerLineUnitX <-powerLineUnitY) ->powerLineUID
GetUnitAt(<-beamUnitX <-beamUnitY) ->beamUID
endonce


if (not(<-alreadyDelayed) and (<-initialDelay gt(0)))
Delay(<-initialDelay)
TRUE ->alreadyDelayed
endif

@ActivatePowerPlant(not(IsCreeperInRange(CurrentX CurrentY 5 1 FALSE TRUE FALSE)))


:ActivatePowerPlant
->activate
if (<-activate and (not(<-active)))
TRUE ->active
SetImageColor(Self "Main" 255 255 255 255)
SetImageColor(<-powerLineUID "Main" 0 255 0 255)
SetScriptVar(<-beamUID "BeamWeapon.crpl" "enabled" TRUE)
PlaySound("Misc15")
else if (not(<-activate) and (<-active))
FALSE ->active
SetImageColor(Self "Main" 255 128 200 255)
SetImageColor(<-powerLineUID "Main" 255 0 0 255)
SetScriptVar(<-beamUID "BeamWeapon.crpl" "enabled" FALSE)
PlaySound("Misc27")
endif endif
Title: Re: IsCreeperInRange
Post by: Hubs on February 19, 2015, 08:23:35 AM
Sweet, thanks V!
Title: Re: IsCreeperInRange
Post by: kwinse on February 21, 2015, 01:21:00 AM
Can someone create the page on the wiki?
Title: Re: IsCreeperInRange
Post by: pawel345 on February 22, 2015, 06:16:15 AM
Page created :D
Title: Re: IsCreeperInRange
Post by: BTUx9 on February 26, 2015, 12:59:20 AM
I updated the wiki with the following info:
GetCreeper returns a float value, but threshold is an integer.  So, to test for a creeper level of 1.0, you'd use a threshold of 1,000,000.
Title: Re: IsCreeperInRange
Post by: Grayzzur on February 26, 2015, 09:46:57 AM
That's the case with most "floats" in the game. Everything is integer math with 6 digits. That's why the max creeper level is 2147 -- maxint is 2147483647 (yielding 2147.483647 in creeper math).