Skip to content
33 changes: 25 additions & 8 deletions code/iff_defs/iff_defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ int IFF_COLOR_TAGGED = 2;
// global only to file
SCP_vector <std::pair<color, color>> Iff_colors; // AL 1-2-97: Create two IFF colors, regular and bright

typedef struct {
int r;
int g;
int b;
} temp_color_t;

// parallel to Iff_colors
static SCP_vector<temp_color_t> temp_colors;

flag_def_list rti_flags[] = {
{ "crosshairs", RTIF_CROSSHAIRS, 0 },
{ "blink", RTIF_BLINK, 0 },
Expand Down Expand Up @@ -82,16 +91,8 @@ int iff_get_alpha_value(bool is_bright)
*/
int iff_init_color(int r, int g, int b)
{
typedef struct {
int r;
int g;
int b;
} temp_color_t;

int i, idx;

static SCP_vector<temp_color_t> temp_colors;

Assert(r >= 0 && r <= 255);
Assert(g >= 0 && g <= 255);
Assert(b >= 0 && b <= 255);
Expand Down Expand Up @@ -662,6 +663,22 @@ void iff_init()
resolve_iff_data();
}

void iff_close()
{
Iff_info.clear();
Iff_info_names.clear();
Iff_colors.clear();
temp_colors.clear();

attack_names.clear();
observed_color_table.clear();
accessibility_observed_color_table.clear();

strcpy_s(traitor_name, "Traitor");
Iff_traitor = 0;
AccessiblitySupported = false;
}

/**
* Find the iff name
*
Expand Down
3 changes: 3 additions & 0 deletions code/iff_defs/iff_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ extern int iff_init_color(int r, int g, int b);
// load the iff table
extern void iff_init();

// free the iff table
extern void iff_close();

// search for iff
extern int iff_lookup(const char *iff_name);

Expand Down
12 changes: 7 additions & 5 deletions code/model/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ class model_subsystem { /* contains rotation rate info */
float turret_fov; // dot of turret_norm:vec_to_enemy > this means can see
float turret_max_fov; // dot of turret_norm:vec_to_enemy <= this means barrels can elevate up to the target
float turret_base_fov; // turret's base's fov, -1 means no restriction
float pof_turret_fov; // turret_fov as read from the model, before any ships.tbl override
float pof_turret_max_fov; // turret_max_fov as read from the model, before any ships.tbl override
float pof_turret_base_fov; // turret_base_fov as read from the model, before any ships.tbl override
int turret_num_firing_points; // number of firing points on this turret
vec3d turret_firing_point[MAX_TFP]; // in parent object's reference frame, point from which to fire.
int turret_gun_sobj; // Which subobject in this model the firing points are linked to.
Expand All @@ -276,7 +279,7 @@ class model_subsystem { /* contains rotation rate info */
gamesnd_id rotation_snd; //Sound to make when the subsystem is rotating. (ie turrets)

// engine wash info
struct engine_wash_info *engine_wash_pointer; // index into Engine_wash_info
int engine_wash_index; // index into Engine_wash_info, or -1

// movement specific info
int weapon_rotation_pbank; // weapon-controlled rotation - Goober5000
Expand Down Expand Up @@ -603,8 +606,7 @@ typedef struct thruster_bank {
int num_points;
std::shared_ptr<glow_point[]> points;

// Engine wash info
struct engine_wash_info *wash_info_pointer; // index into Engine_wash_info
int subsys_num; // engine subsystem for this bank, used for engine wash; index to ship_info->subsystems of the class that loaded the model
int obj_num; // what subsystem number this bank is on; index to ship_info->subsystems
int submodel_num; // what submodel number this bank is on; index to polymodel->submodel/polymodel_instance->submodel
} thruster_bank;
Expand Down Expand Up @@ -1078,8 +1080,8 @@ void model_instance_snapshot_prev_all();
// Flags a submodel, and with it all its children, as Can_move
void model_set_submodel_can_move(polymodel* pm, int submodel_num);

// routine to copy subsystems. Must be called when subsystems sets are the same -- see ship.cpp
void model_copy_subsystems(int n_subsystems, model_subsystem *d_sp, model_subsystem *s_sp);
// routine to copy subsystems, matched by name, between ship classes that share a model -- see ship.cpp
void model_copy_subsystems(int n_dest, model_subsystem *d_sp, int n_source, const model_subsystem *s_sp);

// Flags you can pass to model_render
#define MR_NORMAL (0) // Draw a normal object
Expand Down
4 changes: 3 additions & 1 deletion code/model/modelinterp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,9 @@ int model_should_render_engine_glow(int objnum, int bank_obj)
ship *shipp = &Ships[obj->instance];
ship_info *sip = &Ship_info[shipp->ship_info_index];

Assert( bank_obj < sip->n_subsystems );
// a class whose subsystems differ from those of the class that loaded the model may not have this subsystem
if (bank_obj >= sip->n_subsystems)
return 1;

char subname[MAX_NAME_LEN];
// shipp->subsystems isn't always valid here so don't use it
Expand Down
124 changes: 71 additions & 53 deletions code/model/modelread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,21 +605,38 @@ bool in(char *&p, char *str, const char *substr)
return p != nullptr;
}

// copy the model's turret FOVs into the live values, except for any that ships.tbl has overridden
static void model_apply_pof_turret_fovs(model_subsystem *subsystemp)
{
if (!subsystemp->flags[Model::Subsystem_Flags::Turret_barrel_fov_overridden])
subsystemp->turret_fov = subsystemp->pof_turret_fov;
if (!subsystemp->flags[Model::Subsystem_Flags::Turret_base_fov_overridden])
subsystemp->turret_base_fov = subsystemp->pof_turret_base_fov;
if (!subsystemp->flags[Model::Subsystem_Flags::Turret_max_fov_overridden])
subsystemp->turret_max_fov = subsystemp->pof_turret_max_fov;
}

const Model::Subsystem_Flags carry_flags[] = { Model::Subsystem_Flags::Crewpoint, Model::Subsystem_Flags::Rotates, Model::Subsystem_Flags::Translates, Model::Subsystem_Flags::Triggered, Model::Subsystem_Flags::Artillery, Model::Subsystem_Flags::Stepped_rotate, Model::Subsystem_Flags::Stepped_translate };
// Function to copy model data from one subsystem set to another subsystem set. This function
// is called when two ships use the same model data, but since the model only gets read in one time,
// the subsystem data is only present in one location. The ship code will call this routine to fix
// this situation by copying stuff from the source subsystem set to the dest subsystem set.
void model_copy_subsystems( int n_subsystems, model_subsystem *d_sp, model_subsystem *s_sp )
void model_copy_subsystems( int n_dest, model_subsystem *d_sp, int n_source, const model_subsystem *s_sp )
{
int i, j;
model_subsystem *source, *dest;

for (i = 0; i < n_subsystems; i++ ) {
source = &s_sp[i];
for ( j = 0; j < n_subsystems; j++ ) {
dest = &d_sp[j];
const model_subsystem *source;
model_subsystem *dest;

// a destination subsystem with no match in the source stays unlinked, which is reported when a ship of that class is created
for (i = 0; i < n_dest; i++ ) {
dest = &d_sp[i];
for ( j = 0; j < n_source; j++ ) {
source = &s_sp[j];
if ( !subsystem_stricmp( source->subobj_name, dest->subobj_name) ) {
// an unlinked source has nothing to copy, and copying it would unlink a destination that is already linked
if ( source->model_num == -1 )
break;

for (auto const &flag : carry_flags) {
if (source->flags[flag])
dest->flags.set(flag);
Expand All @@ -631,13 +648,21 @@ void model_copy_subsystems( int n_subsystems, model_subsystem *d_sp, model_subsy
dest->radius = source->radius;
dest->type = source->type;
dest->turret_gun_sobj = source->turret_gun_sobj;
dest->path_num = source->path_num;
dest->weapon_rotation_pbank = source->weapon_rotation_pbank;
dest->stepped_rotation = source->stepped_rotation;
dest->stepped_translation = source->stepped_translation;

strcpy_s(dest->name, source->name);

if ( dest->type == SUBSYSTEM_TURRET ) {
int nfp;

dest->turret_fov = source->turret_fov;
dest->pof_turret_fov = source->pof_turret_fov;
dest->pof_turret_base_fov = source->pof_turret_base_fov;
dest->pof_turret_max_fov = source->pof_turret_max_fov;
model_apply_pof_turret_fovs(dest);

dest->turret_num_firing_points = source->turret_num_firing_points;
dest->turret_norm = source->turret_norm;

Expand All @@ -650,13 +675,6 @@ void model_copy_subsystems( int n_subsystems, model_subsystem *d_sp, model_subsy
break;
}
}
if ( j == n_subsystems )
Error(LOCATION, "Subsystem '%s' could not be matched between two ship classes that share a model. "
"The destination ship will be missing this subsystem at runtime. "
"Check that subsystem names are spelled identically on both ship classes, "
"and that any modular table extensions to one ship are mirrored on the other.",
source->subobj_name);

}
}

Expand Down Expand Up @@ -691,39 +709,34 @@ void set_subsystem_info(int model_num, model_subsystem *subsystemp, char *props,

subsystemp->type = SUBSYSTEM_TURRET;

// don't set the turret FOV values if they have already been set (e.g. through ships.tbl)
if (!subsystemp->flags[Model::Subsystem_Flags::Turret_barrel_fov_overridden]) {
if (in(p, props, "$fov")) {
get_user_prop_value(p + 4, buf); // get the value of the fov
float value = (float)atoi(buf);
CLAMP(value, 0.0f, 360.0f);
float angle = fl_radians(value) / 2.0f;
subsystemp->turret_fov = cosf(angle);
} else
subsystemp->turret_fov = 0.0f;
}

if (!subsystemp->flags[Model::Subsystem_Flags::Turret_base_fov_overridden]) {
if (in(p, props, "$base_fov")) {
get_user_prop_value(p + 9, buf); // get the value of the fov
float value = (float)atoi(buf);
CLAMP(value, 0.0f, 360.0f);
float angle = fl_radians(value) / 2.0f;
subsystemp->turret_base_fov = cosf(angle);
} else
subsystemp->turret_base_fov = -1.0f;
}

if (!subsystemp->flags[Model::Subsystem_Flags::Turret_max_fov_overridden]) {
if (in(p, props, "$max_fov")) {
get_user_prop_value(p + 8, buf); // get the value of the fov
float value = (float)atoi(buf);
CLAMP(value, 0.0f, 90.0f);
float angle = PI_2 - fl_radians(value);
subsystemp->turret_max_fov = cosf(angle);
} else
subsystemp->turret_max_fov = 1.0f;
}
if (in(p, props, "$fov")) {
get_user_prop_value(p + 4, buf); // get the value of the fov
float value = i2fl(atoi(buf));
CLAMP(value, 0.0f, 360.0f);
float angle = fl_radians(value) / 2.0f;
subsystemp->pof_turret_fov = cosf(angle);
} else
subsystemp->pof_turret_fov = 0.0f;

if (in(p, props, "$base_fov")) {
get_user_prop_value(p + 9, buf); // get the value of the fov
float value = i2fl(atoi(buf));
CLAMP(value, 0.0f, 360.0f);
float angle = fl_radians(value) / 2.0f;
subsystemp->pof_turret_base_fov = cosf(angle);
} else
subsystemp->pof_turret_base_fov = -1.0f;

if (in(p, props, "$max_fov")) {
get_user_prop_value(p + 8, buf); // get the value of the fov
float value = i2fl(atoi(buf));
CLAMP(value, 0.0f, 90.0f);
float angle = PI_2 - fl_radians(value);
subsystemp->pof_turret_max_fov = cosf(angle);
} else
subsystemp->pof_turret_max_fov = 1.0f;

model_apply_pof_turret_fovs(subsystemp);

subsystemp->turret_num_firing_points = 0;

Expand Down Expand Up @@ -2554,9 +2567,9 @@ modelread_status read_model_file_no_subsys(polymodel * pm, const char* filename,
if (bank->num_points > 0)
bank->points = make_shared<glow_point[]>(bank->num_points);

bank->subsys_num = -1;
bank->obj_num = -1;
bank->submodel_num = -1;
bank->wash_info_pointer = nullptr;

if (pm->version >= 2117) {
cfread_string_len( props, MAX_PROP_LEN, fp );
Expand Down Expand Up @@ -3168,13 +3181,15 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename
// start off assuming the subsys is invalid
int table_error = 1;
auto bank = &pm->thrusters[subsystem.first];
int wash_index = -1;

for (int k = 0; k < n_subsystems; k++) {
if (!subsystem_stricmp(subsystems[k].subobj_name, subsystem.second.subsystem_name.c_str())) {
bank->submodel_num = subsystems[k].subobj_num;
bank->subsys_num = k;

bank->wash_info_pointer = subsystems[k].engine_wash_pointer;
if (bank->wash_info_pointer != nullptr) {
wash_index = subsystems[k].engine_wash_index;
if (wash_index >= 0) {
table_error = 0;
}
// also set what subsystem this is attached to but not if we only have one thruster bank
Expand All @@ -3186,7 +3201,7 @@ modelread_status read_and_process_model_file(polymodel* pm, const char* filename
}
}

if ((bank->wash_info_pointer == nullptr) && (n_subsystems > 0)) {
if ((wash_index < 0) && (n_subsystems > 0)) {
if (table_error) {
// Warning(LOCATION, "No engine wash table entry in ships.tbl for ship model %s", filename);
}
Expand Down Expand Up @@ -6223,6 +6238,9 @@ void model_subsystem::reset()
turret_fov = 0;
turret_max_fov = 1;
turret_base_fov = -1;
pof_turret_fov = 0;
pof_turret_max_fov = 1;
pof_turret_base_fov = -1;
turret_num_firing_points = 0;
for (auto it = std::begin(turret_firing_point); it != std::end(turret_firing_point); ++it)
it->xyz.x = it->xyz.y = it->xyz.z = 0.0f;
Expand All @@ -6237,7 +6255,7 @@ void model_subsystem::reset()
dead_snd = gamesnd_id();
rotation_snd = gamesnd_id();

engine_wash_pointer = NULL;
engine_wash_index = -1;

weapon_rotation_pbank = 0;
stepped_rotation.reset();
Expand Down
Loading
Loading