IsCreeperInRange

Started by Hubs, February 18, 2015, 01:40:33 PM

Previous topic - Next topic

Hubs

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

Grayzzur

#1
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.
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker

knucracker

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

Hubs


kwinse

Can someone create the page on the wiki?

pawel345


BTUx9

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.

Grayzzur

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).
"Fate. It protects fools, little children, and ships named 'Enterprise.'" -William T. Riker