Object: Variable placement for plants

RootSurface() does not yield good results for all objects, so I think the function should have a parameter for movement range. Re-declarated the variable "i" in the second if-block; I always thought we had scoped variables, it surprises me that the previous version worked.
install-platforms
Mark 2017-06-20 21:50:09 +02:00
parent 6b2cbf2ab2
commit 2553f5a388
1 changed files with 6 additions and 4 deletions

View File

@ -361,13 +361,15 @@ global func RemoveAll(p, ...)
// Pulls an object above ground if it was buried (e.g. by PlaceVegetation), mainly used by plants.
// The object must have 'Bottom' and 'Center' CNAT to use this.
// (bottom is the point which should be buried, center the lowest point that must not be buried)
global func RootSurface()
global func RootSurface(int max_movement)
{
max_movement = max_movement ?? GetObjHeight() / 2;
if (HasCNAT(CNAT_Center))
{
var i = 0;
// Move up if too far underground.
while (GetContact(-1) & CNAT_Center && i < GetObjHeight()/2)
while (GetContact(-1) & CNAT_Center && i < max_movement)
{
SetPosition(GetX(), GetY() - 1);
i++;
@ -375,9 +377,9 @@ global func RootSurface()
}
if (HasCNAT(CNAT_Bottom))
{
i = 0;
var i = 0;
// Move down if in midair.
while (!(GetContact(-1) & CNAT_Bottom) && i < GetObjHeight()/2)
while (!(GetContact(-1) & CNAT_Bottom) && i < max_movement)
{
SetPosition(GetX(), GetY() + 1);
i++;