Skip to content

Commit 53bc786

Browse files
committed
sync Orientation virtvars
The two `VIRTVAR`s for Orientation in subsystems and submodel instances do the same thing, but did not have the same code - in particular, the subsystem version did not update the subsystem angle and turret angle. This syncs them up.
1 parent 6e56267 commit 53bc786

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

code/scripting/api/objs/modelinstance.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,32 +119,33 @@ ADE_FUNC(isValid, l_ModelInstance, nullptr, "True if valid, false or nil if not"
119119
return ade_set_args(L, "b", mih->IsValid());
120120
}
121121

122-
ADE_VIRTVAR(Orientation, l_SubmodelInstance, "orientation", "Gets or sets the submodel instance orientation (world orientation)", "orientation", "Orientation, or identity orientation if handle is not valid")
122+
ADE_VIRTVAR(Orientation, l_SubmodelInstance, "orientation", "Gets or sets the submodel instance orientation", "orientation", "Orientation, or identity orientation if handle is not valid")
123123
{
124124
submodelinstance_h *smih;
125125
matrix_h *mh = nullptr;
126126
if (!ade_get_args(L, "o|o", l_SubmodelInstance.GetPtr(&smih), l_Matrix.GetPtr(&mh)))
127-
return ade_set_error(L, "o", l_Matrix.Set(matrix_h(&vmd_identity_matrix)));
127+
return ade_set_error(L, "o", l_Matrix.Set(matrix_h()));
128128

129129
if (!smih->IsValid())
130-
return ade_set_error(L, "o", l_Matrix.Set(matrix_h(&vmd_identity_matrix)));
130+
return ade_set_error(L, "o", l_Matrix.Set(matrix_h()));
131+
132+
auto smi = smih->Get();
133+
if (smi == nullptr)
134+
return ade_set_error(L, "o", l_Matrix.Set(matrix_h()));
131135

132136
if (ADE_SETTING_VAR && mh != nullptr)
133137
{
134-
auto sm = smih->GetSubmodel();
135-
auto smi = smih->Get();
136-
137138
smi->canonical_prev_orient = smi->canonical_orient;
138139
smi->canonical_orient = *mh->GetMatrix();
139140

140141
float angle = 0.0f;
141-
vm_closest_angle_to_matrix(&smi->canonical_orient, &sm->rotation_axis, &angle);
142+
vm_closest_angle_to_matrix(&smi->canonical_orient, &smih->GetSubmodel()->rotation_axis, &angle);
142143

143144
smi->cur_angle = angle;
144145
smi->turret_idle_angle = angle;
145146
}
146147

147-
return ade_set_args(L, "o", l_Matrix.Set(matrix_h(&smih->Get()->canonical_orient)));
148+
return ade_set_args(L, "o", l_Matrix.Set(matrix_h(&smi->canonical_orient)));
148149
}
149150

150151
ADE_FUNC(findWorldPoint, l_SubmodelInstance, "vector", "Calculates the world coordinates of a point in a submodel's frame of reference", "vector", "Point, or empty vector if handle is not valid")

code/scripting/api/objs/subsystem.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ ADE_VIRTVAR(AWACSRadius, l_Subsystem, "number", "Subsystem AWACS radius", "numbe
101101
return ade_set_args(L, "f", sso->ss->awacs_radius);
102102
}
103103

104-
ADE_VIRTVAR(Orientation, l_Subsystem, "orientation", "Orientation of subobject or turret base", "orientation", "Subsystem orientation, or null orientation if handle is invalid")
104+
ADE_VIRTVAR(Orientation, l_Subsystem, "orientation", "Orientation of subobject or turret base", "orientation", "Subsystem orientation, or identity orientation if handle is invalid")
105105
{
106106
ship_subsys_h *sso;
107107
matrix_h *mh = nullptr;
108-
if(!ade_get_args(L, "o|o", l_Subsystem.GetPtr(&sso), l_Matrix.GetPtr(&mh)))
108+
if (!ade_get_args(L, "o|o", l_Subsystem.GetPtr(&sso), l_Matrix.GetPtr(&mh)))
109109
return ade_set_error(L, "o", l_Matrix.Set(matrix_h()));
110110

111111
if (!sso->isSubsystemValid())
@@ -115,10 +115,19 @@ ADE_VIRTVAR(Orientation, l_Subsystem, "orientation", "Orientation of subobject o
115115
if (smi == nullptr)
116116
return ade_set_error(L, "o", l_Matrix.Set(matrix_h()));
117117

118-
if(ADE_SETTING_VAR && mh)
118+
if (ADE_SETTING_VAR && mh != nullptr)
119119
{
120+
auto pm = model_get(sso->ss->system_info->model_num);
121+
auto sm = &pm->submodel[sso->ss->system_info->subobj_num];
122+
120123
smi->canonical_prev_orient = smi->canonical_orient;
121124
smi->canonical_orient = *mh->GetMatrix();
125+
126+
float angle = 0.0f;
127+
vm_closest_angle_to_matrix(&smi->canonical_orient, &sm->rotation_axis, &angle);
128+
129+
smi->cur_angle = angle;
130+
smi->turret_idle_angle = angle;
122131
}
123132

124133
return ade_set_args(L, "o", l_Matrix.Set(matrix_h(&smi->canonical_orient)));

0 commit comments

Comments
 (0)