Skip to content

Commit a44adde

Browse files
authored
Followup improvement for the LuaAI AI Control Helper (#4422)
* Followup improvement for the LuaAI AI Control Helper * Update docs
1 parent e885425 commit a44adde

File tree

3 files changed

+15
-55
lines changed

3 files changed

+15
-55
lines changed

code/ai/ai.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ extern ai_flag_name Ai_flag_names[];
7272
#define AITTV_VIA_SEXP (1<<1) // Goober5000 - via sexp
7373
#define AITTV_IGNORE_BANK (1<<2) // Goober5000 - ignore bank when turning
7474
#define AITTV_SLOW_BANK_ACCEL (1<<3) // Asteroth - used by formation flying
75+
#define AITTV_FORCE_DELTA_BANK (1<<4) // Lafiel - Always use the provided delta bank and override calculated values
7576

7677
#define KAMIKAZE_HULL_ON_DEATH -1000.0f // Hull strength ship gets set to if it crash-dies.
7778

code/ai/aicode.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,9 @@ void ai_turn_towards_vector(vec3d* dest, object* objp, vec3d* slide_vec, vec3d*
12881288

12891289
// Should be more general case here. Currently, anything that is not a weapon will bank when it turns.
12901290
// Goober5000 - don't bank if sexp or ship says not to
1291-
if ( (objp->type == OBJ_WEAPON) || (flags & AITTV_IGNORE_BANK ) )
1291+
if (flags & AITTV_FORCE_DELTA_BANK)
1292+
bank = bank_override;
1293+
else if ( (objp->type == OBJ_WEAPON) || (flags & AITTV_IGNORE_BANK ) )
12921294
bank = 0.0f;
12931295
else if (objp->type == OBJ_SHIP && Ship_info[Ships[objp->instance].ship_info_index].flags[Ship::Info_Flags::Dont_bank_when_turning])
12941296
bank = 0.0f;
@@ -14816,12 +14818,12 @@ void ai_process( object * obj, int ai_index, float frametime )
1481614818

1481714819
memset( &AI_ci, 0, sizeof(AI_ci) );
1481814820

14819-
ai_frame(OBJ_INDEX(obj));
14820-
1482114821
AI_ci.pitch = 0.0f;
1482214822
AI_ci.bank = 0.0f;
1482314823
AI_ci.heading = 0.0f;
1482414824

14825+
ai_frame(OBJ_INDEX(obj));
14826+
1482514827
// the ships maximum velocity now depends on the energy flowing to engines
1482614828
obj->phys_info.max_vel.xyz.z = shipp->current_max_speed;
1482714829

code/scripting/api/objs/ai_helper.cpp

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,65 +41,19 @@ inline int aici_getset_helper(lua_State* L, float control_info::* value) {
4141
return ade_set_args(L, "f", AI_ci.*value);
4242
}
4343

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-
9044
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")
9145
{
92-
return pi_rotation_getset_helper(L, 0);
46+
return aici_getset_helper(L, &control_info::pitch);
9347
}
9448

9549
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")
9650
{
97-
return pi_rotation_getset_helper(L, 2);
51+
return aici_getset_helper(L, &control_info::bank);
9852
}
9953

10054
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")
10155
{
102-
return pi_rotation_getset_helper(L, 1);
56+
return aici_getset_helper(L, &control_info::heading);
10357
}
10458

10559
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")
@@ -119,7 +73,7 @@ ADE_VIRTVAR(SidewaysThrust, l_AI_Helper, "number", "The sideways thrust rate for
11973

12074
ADE_FUNC(turnTowardsPoint,
12175
l_AI_Helper,
122-
"vector target, [boolean respectDifficulty = true, vector turnrateModifier /* 100% of tabled values in all rotation axes by default */]",
76+
"vector target, [boolean respectDifficulty = true, vector turnrateModifier /* 100% of tabled values in all rotation axes by default */, number bank /* native bank-on-heading by default */ ]",
12377
"turns the ship towards the specified point during this frame",
12478
nullptr,
12579
nullptr)
@@ -128,11 +82,14 @@ ADE_FUNC(turnTowardsPoint,
12882
vec3d* target;
12983
bool diffTurn = true;
13084
vec3d* modifier = nullptr;
131-
if (!ade_get_args(L, "oo|bo", l_AI_Helper.Get(&ship), l_Vector.GetPtr(&target), &diffTurn, l_Vector.GetPtr(&modifier))) {
85+
float bank = 0.0f;
86+
87+
int argnum = ade_get_args(L, "oo|bof", l_AI_Helper.Get(&ship), l_Vector.GetPtr(&target), &diffTurn, l_Vector.GetPtr(&modifier), &bank);
88+
if (argnum == 0) {
13289
return ADE_RETURN_NIL;
13390
}
13491

135-
ai_turn_towards_vector(target, ship.objp, nullptr, nullptr, 0.0f, diffTurn ? 0 : AITTV_FAST, nullptr, modifier);
92+
ai_turn_towards_vector(target, ship.objp, nullptr, nullptr, bank, (diffTurn ? 0 : AITTV_FAST) | (argnum >= 5 ? AITTV_FORCE_DELTA_BANK : 0), nullptr, modifier);
13693
return ADE_RETURN_NIL;
13794
}
13895

0 commit comments

Comments
 (0)