insect swarm: fix movement issues

directional-lights
Maikel de Vries 2016-09-30 18:55:36 +02:00
parent 732fff3029
commit b42a4b824b
2 changed files with 30 additions and 20 deletions

View File

@ -21,10 +21,10 @@ public func Place(int amount, int swarm_members, proplist area)
/** Call something for every swarm member
*/
public func SwarmCall(string func, par1, par2, par3, par4, par5, par6, par7, par8, par9)
public func SwarmCall(string fn, par1, par2, par3, par4, par5, par6, par7, par8, par9)
{
if(!lib_swarm_helper) return;
lib_swarm_helper->SwarmCall(func, par1, par2, par3, par4, par5, par6, par7, par8, par9);
if (!lib_swarm_helper) return;
lib_swarm_helper->SwarmCall(fn, par1, par2, par3, par4, par5, par6, par7, par8, par9);
}
/** Standard swarm size (utilised in Place()). Default 10.
@ -46,7 +46,7 @@ local lib_swarm_previnline;
*/
public func CreateSwarm(int amount)
{
if (!amount) return;
if (amount <= 0) return;
// Create a swarm helper
lib_swarm_helper = CreateObject(Library_Swarm_Helper, AbsX(0), AbsY(0), NO_OWNER);
@ -54,9 +54,9 @@ public func CreateSwarm(int amount)
lib_swarm_helper->SetSwarmCount(amount);
var last_created = this, insect;
while(amount)
while (amount)
{
insect = CreateObject(GetID(),0,0,GetOwner());
insect = CreateObject(GetID(), 0, 0, GetOwner());
insect->SetPreviousInLine(last_created);
insect->SetSwarmHelper(lib_swarm_helper);
insect->SetCommand("None");
@ -99,7 +99,7 @@ private func Death()
if (lib_swarm_helper->GetMaster() == this)
lib_swarm_helper->MakeNewMaster(lib_swarm_nextinline);
// Death of a slave
if(lib_swarm_previnline && lib_swarm_nextinline)
if (lib_swarm_previnline && lib_swarm_nextinline)
{
lib_swarm_nextinline->SetPreviousInLine(lib_swarm_previnline);
lib_swarm_previnline->SetNextInLine(lib_swarm_nextinline);
@ -147,10 +147,14 @@ private func MoveToTarget()
x = point.x;
y = point.y;
}
x = BoundBy(x + Random(lib_swarm_density*2) - lib_swarm_density, 10, LandscapeWidth()-10);
y = BoundBy(y + Random(lib_swarm_density*2) - lib_swarm_density, 10, LandscapeHeight()-10);
x = BoundBy(x + Random(lib_swarm_density * 2) - lib_swarm_density, 10, LandscapeWidth() - 10);
y = BoundBy(y + Random(lib_swarm_density * 2) - lib_swarm_density, 10, LandscapeHeight() - 10);
// Only move if there is no solid or liquid at this coordinate, otherwise just wait for next activity update.
if (GBackSemiSolid(x - GetX(), y - GetY()))
return;
SetCommand("MoveTo", nil, x, y, nil, true);
AppendCommand("Call", this, nil,nil,nil,nil, "MissionComplete");
AppendCommand("Call", this, nil, nil, nil, nil, "MissionComplete");
return;
}
/* Saving */

View File

@ -6,6 +6,9 @@
@author Clonkonaut
--*/
public func IsAnimal() { return true; }
public func IsInsect() { return true; }
public func Place(int amount, proplist area)
{
// No calls to objects, only definitions
@ -17,9 +20,11 @@ public func Place(int amount, proplist area)
for (var i = 0 ; i < amount ; i++)
{
position = FindLocation(Loc_InArea(area), Loc_Wall(CNAT_Bottom), Loc_Sky());
if (!position)
position = FindLocation(Loc_InArea(area), Loc_Wall(CNAT_Bottom), Loc_Tunnel());
if (position)
{
insect = CreateObjectAbove(this, position.x, position.y, NO_OWNER);
insect = CreateObjectAbove(this, position.x, position.y - 2, NO_OWNER);
if (insect->Stuck()) insect->RemoveObject();
if (insect) insects[GetLength(insects)] = insect;
}
@ -62,7 +67,7 @@ public func IsSleeping()
private func MissionComplete()
{
// Set a dummy command. After it finishes, regular Activity() behaviour will kick in.
SetCommand("Wait", nil,nil,nil,nil, 20 + Random(80));
SetCommand("Wait", nil, nil, nil, nil, 20 + Random(80));
}
/** Overload to define any kind of interesting spot for the insect. Return true if spot was found.
@ -241,25 +246,26 @@ private func MoveToTarget()
{
coordinates.x = GetX();
// Prevent insect from flying around at borders too much
if (GetX() < lib_insect_max_dist/2) coordinates.x += lib_insect_max_dist/2;
if (GetX() > LandscapeWidth() - lib_insect_max_dist/2) coordinates.x -= lib_insect_max_dist/2;
if (GetX() < lib_insect_max_dist / 2) coordinates.x += lib_insect_max_dist / 2;
if (GetX() > LandscapeWidth() - lib_insect_max_dist / 2) coordinates.x -= lib_insect_max_dist / 2;
coordinates.x = BoundBy(coordinates.x + Random(lib_insect_max_dist) - lib_insect_max_dist/2, 10, LandscapeWidth()-10);
coordinates.x = BoundBy(coordinates.x + Random(lib_insect_max_dist) - lib_insect_max_dist / 2, 10, LandscapeWidth() - 10);
}
coordinates.y = GetHorizonHeight(coordinates.x)- 30 - Random(60);
// Move to a place slightly above the surface.
coordinates.y = GetHorizonHeight(coordinates.x, GetY()) - 30 - Random(60);
}
// Insect may have died
if (!this || !GetAlive()) return;
SetCommand("MoveTo", nil, coordinates.x, coordinates.y, nil, true);
AppendCommand("Call", this, nil,nil,nil,nil, "MissionComplete");
AppendCommand("Call", this, nil, nil, nil, nil, "MissionComplete");
}
private func GetHorizonHeight(int x)
private func GetHorizonHeight(int x, int y_current)
{
var height;
var height = y_current;
while (height < LandscapeHeight() && !GBackSemiSolid(AbsX(x), AbsY(height)))
height += 10;
height += 5;
return height;
}