Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpawnShipNear velocity matching #3674

Merged

Conversation

fluffyfreak
Copy link
Contributor

Use the target bodies velocity, if it's a ship then it will inherit their velocity, otherwise the behaviour is the same as it currently is.

@walterar this is how I'd prefer to fix what you suggested in #3669, alternatively it could take an extra parameter to specify the velocity if you like.

@walterar
Copy link
Contributor

walterar commented Apr 4, 2016

Wonderful. I tried it and, as it is, significantly improves the function.

If you want, an additional parameter to set a speed_plus would be really good.

@fluffyfreak
Copy link
Contributor Author

I've added a 6th optional parameter, you can pass the 5th hyperspace information as nil like

local ship = Ship.SpawnNear("viper_police_craft", Game.player, 10, 10, nil, velocity)
or as before like
local ship = Ship.SpawnNear("viper_police_craft", Game.player, 10, 10)

Maybe @laarmen has an opinion on these changes too?

@fluffyfreak fluffyfreak added the WIP label Apr 5, 2016
@walterar
Copy link
Contributor

walterar commented Apr 5, 2016

No, What I mean is add, to your original PR, a "fine tune"; for to do, for example:

local fine_tune = 0-- < default value of fine_tune
if case1 then
    fine_tune = 450-- < vel target + 450 m/s
elseif case2 then
    fine_tune = - 450-- < vel target - 450 m/s
end
ship = Space.SpawnShipNear("viper_police_craft", target,  50, 100, nil, fine_tune)

-- * case1: the ship appears chasing the target, to initial vel = vel_target + 450 m/s

-- * case2: the ship appears chasing the target, to initial vel_target - 450 m/s

@laarmen
Copy link
Contributor

laarmen commented Apr 5, 2016

I'm not looking at the code, but from the example it isn't exactly clear:
is velocity relative to the target or to the frame?

On Tue, Apr 5, 2016 at 2:58 PM Walter Arnolfo notifications@github.com
wrote:

No, What I mean is add, to your original PR, a "fine tune"; for to do, for
example:

local fine_tune = 0-- < default value of fine_tune
if cases1 then
fine_tune = 450-- < vel target + 450 m/s
elseif cases2 then
fine_tune = - 450-- < vel target - 450 m/s
end
ship = Space.SpawnShipNear("viper_police_craft", target, 50, 100, nil, fine_tune)

-- * case1: the ship appears chasing the target, to initial vel =
vel_target + 450 m/s

-- * case2: the ship appears chasing the target, to initial vel_target -
450 m/s


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#3674 (comment)

@walterar
Copy link
Contributor

walterar commented Apr 5, 2016

@fluffyfreak I'm testing this, as you see it?

--- old/LuaSpace.cpp
+++ new/LuaSpace.cpp
@@ -89,13 +89,12 @@
  * Examples:
  *
  * > -- spawn a ship 5-6AU from the system centre
- * > local ship = Ship.Spawn("eagle_lrf", 5, 6)
+ * > local ship = Space.Spawn("eagle_lrf", 5, 6)
  *
  * > -- spawn a ship in the ~11AU hyperspace area and make it appear that it
  * > -- came from Sol and will arrive in ten minutes
- * > local ship = Ship.Spawn(
- * >     "flowerfairy", 9, 11,
- * >     { SystemPath:New(0,0,0), Game.time + 600 }
+ * > local ship = Space.Spawn("flowerfairy", 9, 11,
+ * >              { SystemPath:New(0,0,0), Game.time + 600 }
  * > )
  *
  * Availability:
@@ -152,7 +151,7 @@
  *
  * Create a ship and place it in space near the given <Body>.
  *
- * > ship = Space.SpawnShip(type, body, min, max, hyperspace)
+ * > ship = Space.SpawnShipNear(type, body, dist, tune, hyperspace)
  *
  * Parameters:
  *
@@ -160,9 +159,9 @@
  *
  *   body - the <Body> near which the ship should be spawned
  *
- *   min - minimum distance from the body to place the ship, in Km
- *
- *   max - maximum distance to place the ship
+ *   dist - distance from the body to place the ship, in Km
+ *
+ *   tune - fine tune to init vel (vel player + or - tune), in m/s
  *
  *   hyperspace - optional table containing hyperspace entry information. If
  *                this is provided the ship will not spawn directly. Instead,
@@ -178,7 +177,7 @@
  * Example:
  *
  * > -- spawn a ship 10km from the player
- * > local ship = Ship.SpawnNear("viper_police_craft", Game.player, 10, 10)
+ * > local ship = Space.SpawnShipNear("viper_police_craft", Game.player, 10, 10)
  *
  * Availability:
  *
@@ -200,8 +199,8 @@
        luaL_error(l, "Unknown ship type '%s'", type);

    Body *nearbody = LuaObject<Body>::CheckFromLua(2);
-   float min_dist = luaL_checknumber(l, 3);
-   float max_dist = luaL_checknumber(l, 4);
+   float dist = luaL_checknumber(l, 3);
+   float tune = luaL_checknumber(l, 4);

    SystemPath *path = 0;
    double due = -1;
@@ -214,7 +213,7 @@

    // XXX protect against spawning inside the body
    Frame * newframe = nearbody->GetFrame();
-   const vector3d newPosition = (MathUtil::RandomPointOnSphere(min_dist, max_dist)* 1000.0) + nearbody->GetPosition();
+   const vector3d newPosition = (dist * 1000.0) + nearbody->GetPosition();

    // If the frame is rotating and the chosen position is too far, use non-rotating parent.
    // Otherwise the ship will be given a massive initial velocity when it's bumped out of the
@@ -227,6 +226,7 @@
    thing->SetFrame(newframe);;
    thing->SetPosition(newPosition);
    thing->SetVelocity(vector3d(0,0,0));
+   thing->SetVelocity(nearbody->GetVelocity() + tune);
    Pi::game->GetSpace()->AddBody(thing);

    LuaObject<Ship>::PushToLua(ship);
@@ -461,7 +461,7 @@
  * Example:
  *
  * > -- spawn a ship 10km from the player
- * > local ship = Ship.SpawnShipLandedNear("viper_police", Game.player, 10, 10)
+ * > local ship = Space.SpawnShipLandedNear("viper_police", Game.player, 10, 10)
  *
  * Availability:
  *
pp
@@ -89,13 +89,12 @@
  * Examples:
  *
  * > -- spawn a ship 5-6AU from the system centre
- * > local ship = Ship.Spawn("eagle_lrf", 5, 6)
+ * > local ship = Space.Spawn("eagle_lrf", 5, 6)
  *
  * > -- spawn a ship in the ~11AU hyperspace area and make it appear that it
  * > -- came from Sol and will arrive in ten minutes
- * > local ship = Ship.Spawn(
- * >     "flowerfairy", 9, 11,
- * >     { SystemPath:New(0,0,0), Game.time + 600 }
+ * > local ship = Space.Spawn("flowerfairy", 9, 11,
+ * >              { SystemPath:New(0,0,0), Game.time + 600 }
  * > )
  *
  * Availability:
@@ -152,7 +151,7 @@
  *
  * Create a ship and place it in space near the given <Body>.
  *
- * > ship = Space.SpawnShip(type, body, min, max, hyperspace)
+ * > ship = Space.SpawnShipNear(type, body, dist, tune, hyperspace)
  *
  * Parameters:
  *
@@ -160,9 +159,9 @@
  *
  *   body - the <Body> near which the ship should be spawned
  *
- *   min - minimum distance from the body to place the ship, in Km
- *
- *   max - maximum distance to place the ship
+ *   dist - distance from the body to place the ship, in Km
+ *
+ *   tune - fine tune to init vel (vel player + or - tune), in m/s
  *
  *   hyperspace - optional table containing hyperspace entry information. If
  *                this is provided the ship will not spawn directly. Instead,
@@ -178,7 +177,7 @@
  * Example:
  *
  * > -- spawn a ship 10km from the player, to vel player + 10 m/s
- * > local ship = Ship.SpawnNear("viper_police_craft", Game.player, 10, 10)
+ * > local ship = Space.SpawnShipNear("viper_police_craft", Game.player, 10, 10)
  *
  * Availability:
  *
@@ -200,8 +199,8 @@
        luaL_error(l, "Unknown ship type '%s'", type);

    Body *nearbody = LuaObject<Body>::CheckFromLua(2);
-   float min_dist = luaL_checknumber(l, 3);
-   float max_dist = luaL_checknumber(l, 4);
+   float dist = luaL_checknumber(l, 3);
+   float tune = luaL_checknumber(l, 4);

    SystemPath *path = 0;
    double due = -1;
@@ -214,7 +213,7 @@

    // XXX protect against spawning inside the body
    Frame * newframe = nearbody->GetFrame();
-   const vector3d newPosition = (MathUtil::RandomPointOnSphere(min_dist, max_dist)* 1000.0) + nearbody->GetPosition();
+   const vector3d newPosition = (dist * 1000.0) + nearbody->GetPosition();

    // If the frame is rotating and the chosen position is too far, use non-rotating parent.
    // Otherwise the ship will be given a massive initial velocity when it's bumped out of the
@@ -227,6 +226,7 @@
    thing->SetFrame(newframe);;
    thing->SetPosition(newPosition);
    thing->SetVelocity(vector3d(0,0,0));
+   thing->SetVelocity(nearbody->GetVelocity() + tune);
    Pi::game->GetSpace()->AddBody(thing);

    LuaObject<Ship>::PushToLua(ship);
@@ -461,7 +461,7 @@
  * Example:
  *
  * > -- spawn a ship 10km from the player
- * > local ship = Ship.SpawnShipLandedNear("viper_police", Game.player, 10, 10)
+ * > local ship = Space.SpawnShipLandedNear("viper_police", Game.player, 10, 10)
  *
  * Availability:
  *

@fluffyfreak
Copy link
Contributor Author

"Target", it spawns a ship near to another object and then gives it the targets velocity.

@walterar you can do that kind of change within the Lua itself once #3669 gets merged by just gettings the targets velocity and altering it slightly.

By default it will get the targets velocity

@fluffyfreak
Copy link
Contributor Author

Sorry @walterar what am I looking for in that post?

@walterar
Copy link
Contributor

walterar commented Apr 5, 2016

I corrected some errors in the original documentation. It is not "Ship.SpawnNear (" is "Space.SpawnNear ("
The main changes I'm testing are these:

-   float min_dist = luaL_checknumber(l, 3);
-   float max_dist = luaL_checknumber(l, 4);
+   float dist = luaL_checknumber(l, 3);
+   float tune = luaL_checknumber(l, 4);

-   const vector3d newPosition = (MathUtil::RandomPointOnSphere(min_dist, max_dist)* 1000.0) + nearbody->GetPosition();
+   const vector3d newPosition = (dist * 1000.0) + nearbody->GetPosition();

    thing->SetVelocity(vector3d(0,0,0));
+   thing->SetVelocity(nearbody->GetVelocity() + tune);
    Pi::game->GetSpace()->AddBody(thing);

@walterar
Copy link
Contributor

walterar commented Apr 5, 2016

@fluffyfreak If I have to choose, I prefer the first option of this PR. It is really improved a lot, and it works without touching anything in the lua modules.
Do not worry about it, you have more important things than this to do in the Pioneer engine.
Meanwhile I'll experiment a little.

@fluffyfreak
Copy link
Contributor Author

@walterar you can use it without changing any code, the other parameters are optional so everything work just as in the first commit.

If you do want to write some new code and set a velocity though, that is when you can add the 6th parameter :)

@walterar
Copy link
Contributor

walterar commented Apr 5, 2016

@fluffyfreak
I do not need a sixth parameter because I prefer to decide if I want a random distance, or not.
I'm testing the code is above. A small mod of your original code.
I am amazed how have improved the combat scenes.
But, for Pioneer vanilla, it is better the way you did. For now. ;)

👍 :)

@fluffyfreak fluffyfreak removed the WIP label Apr 6, 2016
@fluffyfreak
Copy link
Contributor Author

Right I'll consider this review ready then.

@laarmen
Copy link
Contributor

laarmen commented Apr 6, 2016

Will try to do it tonight (or this afternoon if I can catch 20mn, although
it will be a read-only review, no hands-on testing), but no promises.

On Wed, Apr 6, 2016 at 2:34 PM Andrew Copland notifications@github.com
wrote:

Right I'll consider this review ready then.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#3674 (comment)

@laarmen
Copy link
Contributor

laarmen commented Apr 6, 2016

Allright. I haven't even tested it but AFAIUI you wanted the 6th argument to be optional, right? Then you need to check for the existence of the said 6th argument. Otherwise apocalypse, mayhem, looting corpses and application crashes will likely occur (in that specific order) with legacy code.

Further more, as the author of the pi_lua_strict_pull family of methods, I would advise you NOT to use them directly in "application" code, they're more intended for use in higher abstraction parts.

So, all in all I'd replace the argument parsing part by something along the lines of

    vector3d newVelocity;
    if (!lua_isnone(l, 6))
        newVelocity = *LuaVector::CheckFromLua(l, 6); // If we have a 6th argument, it better be a vector, otherwise ERROR!!! Hence Check and not Get
    else
        newVelocity = nearbody->GetVelocity();

Cheers mate!

@laarmen laarmen added the WIP label Apr 6, 2016
@fluffyfreak
Copy link
Contributor Author

Thanks @laarmen made that change

@laarmen
Copy link
Contributor

laarmen commented Apr 7, 2016

I'll get back to that when we get the #3678 sorted out.

@fluffyfreak fluffyfreak merged commit 07c16d8 into pioneerspacesim:master Apr 20, 2016
@fluffyfreak fluffyfreak deleted the SpawnShipNearWithVelocity branch April 20, 2016 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants