|
6 | 6 | #include "vecmath.h" |
7 | 7 |
|
8 | 8 | #include "ai/ai.h" |
| 9 | +#include "mission/missionparse.h" |
| 10 | +#include "ship/ship.h" |
9 | 11 |
|
10 | 12 | namespace scripting { |
11 | 13 | namespace api { |
@@ -39,19 +41,65 @@ inline int aici_getset_helper(lua_State* L, float control_info::* value) { |
39 | 41 | return ade_set_args(L, "f", AI_ci.*value); |
40 | 42 | } |
41 | 43 |
|
42 | | -ADE_VIRTVAR(Pitch, l_AI_Helper, "number", "The pitch rate for the ship this frame, -1 to 1", "number", "The pitch rate, or 0 if the handle is invalid") |
| 44 | +inline int pi_rotation_getset_helper(lua_State* L, int axis) { |
| 45 | + //Use with care! This works only if, as expected, the l_AI_Helper object is the one supplied to the luaai scripts this very frame |
| 46 | + object_h ship; |
| 47 | + float f = 0.0f; |
| 48 | + if (!ade_get_args(L, "o|f", l_AI_Helper.Get(&ship), &f)) |
| 49 | + return ade_set_error(L, "f", 0.0f); |
| 50 | + |
| 51 | + if (!ship.IsValid()) |
| 52 | + return ade_set_error(L, "f", 0.0f); |
| 53 | + |
| 54 | + vec3d vel_limit; |
| 55 | + // get the turn rate if we have Use_axial_turnrate_differences |
| 56 | + if (The_mission.ai_profile->flags[AI::Profile_Flags::Use_axial_turnrate_differences]) { |
| 57 | + vel_limit = Ship_info[Ships[ship.objp->instance].ship_info_index].max_rotvel; |
| 58 | + } |
| 59 | + else { // else get the turn time |
| 60 | + float turn_time = Ship_info[Ships[ship.objp->instance].ship_info_index].srotation_time; |
| 61 | + |
| 62 | + if (turn_time > 0.0f) |
| 63 | + { |
| 64 | + vel_limit.xyz.x = PI2 / turn_time; |
| 65 | + vel_limit.xyz.y = PI2 / turn_time; |
| 66 | + vel_limit.xyz.z = PI2 / turn_time; |
| 67 | + } |
| 68 | + } |
| 69 | + vec3d acc_limit = ai_get_acc_limit(&vel_limit, ship.objp); |
| 70 | + |
| 71 | + float currentThrustRate = ((ship.objp->phys_info.desired_rotvel.a1d[axis] - ship.objp->phys_info.rotvel.a1d[axis]) / AI_frametime) / acc_limit.a1d[axis]; |
| 72 | + |
| 73 | + if (ADE_SETTING_VAR) { |
| 74 | + currentThrustRate = f; |
| 75 | + float targetVel = currentThrustRate * acc_limit.a1d[axis] * AI_frametime + ship.objp->phys_info.rotvel.a1d[axis]; |
| 76 | + CLAMP(targetVel, -vel_limit.a1d[axis], vel_limit.a1d[axis]); |
| 77 | + ship.objp->phys_info.desired_rotvel.a1d[axis] = targetVel; |
| 78 | + |
| 79 | + vec3d rotstep = ship.objp->phys_info.desired_rotvel * AI_frametime; |
| 80 | + |
| 81 | + matrix rotmat; |
| 82 | + angles rotangles{ rotstep.xyz.x, rotstep.xyz.z, rotstep.xyz.y }; |
| 83 | + vm_angles_2_matrix(&rotmat, &rotangles); |
| 84 | + vm_matrix_x_matrix(&ship.objp->phys_info.ai_desired_orient, &ship.objp->orient, &rotmat); |
| 85 | + } |
| 86 | + |
| 87 | + return ade_set_args(L, "f", currentThrustRate); |
| 88 | +} |
| 89 | + |
| 90 | +ADE_VIRTVAR(Pitch, l_AI_Helper, "number", "The pitch thrust rate for the ship this frame, -1 to 1", "number", "The pitch rate, or 0 if the handle is invalid") |
43 | 91 | { |
44 | | - return aici_getset_helper(L, &control_info::pitch); |
| 92 | + return pi_rotation_getset_helper(L, 0); |
45 | 93 | } |
46 | 94 |
|
47 | | -ADE_VIRTVAR(Bank, l_AI_Helper, "number", "The bank rate for the ship this frame, -1 to 1", "number", "The bank rate, or 0 if the handle is invalid") |
| 95 | +ADE_VIRTVAR(Bank, l_AI_Helper, "number", "The bank thrust rate for the ship this frame, -1 to 1", "number", "The bank rate, or 0 if the handle is invalid") |
48 | 96 | { |
49 | | - return aici_getset_helper(L, &control_info::bank); |
| 97 | + return pi_rotation_getset_helper(L, 2); |
50 | 98 | } |
51 | 99 |
|
52 | | -ADE_VIRTVAR(Heading, l_AI_Helper, "number", "The heading rate for the ship this frame, -1 to 1", "number", "The heading rate, or 0 if the handle is invalid") |
| 100 | +ADE_VIRTVAR(Heading, l_AI_Helper, "number", "The heading thrust rate for the ship this frame, -1 to 1", "number", "The heading rate, or 0 if the handle is invalid") |
53 | 101 | { |
54 | | - return aici_getset_helper(L, &control_info::heading); |
| 102 | + return pi_rotation_getset_helper(L, 1); |
55 | 103 | } |
56 | 104 |
|
57 | 105 | ADE_VIRTVAR(ForwardThrust, l_AI_Helper, "number", "The forward thrust rate for the ship this frame, -1 to 1", "number", "The forward thrust rate, or 0 if the handle is invalid") |
|
0 commit comments