Skip to content

Commit

Permalink
Merge pull request scp-fs2open#6105 from Goober5000/object_h_index
Browse files Browse the repository at this point in the history
replace pointers with indexes in object_h
  • Loading branch information
Goober5000 authored Jun 9, 2024
2 parents 574c392 + d5e3874 commit c198565
Show file tree
Hide file tree
Showing 34 changed files with 509 additions and 491 deletions.
2 changes: 1 addition & 1 deletion code/actions/types/MoveToSubmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ActionResult MoveToSubmodel::execute(ProgramLocals& locals) const
// The calling code should ensure that this never happens
Assertion(locals.hostSubobject >= 0, "Did not have a valid host subobject.");

auto instance = object_get_model_instance(locals.host.objp);
auto instance = object_get_model_instance(locals.host.objp());
Assertion(instance != -1, "Model instances are required if a host subobject is specified.");

auto pmi = model_get_instance(instance);
Expand Down
4 changes: 2 additions & 2 deletions code/actions/types/ParticleEffectAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ActionResult ParticleEffectAction::execute(ProgramLocals& locals) const
vec3d local_pos;
matrix local_orient;
if (locals.hostSubobject != -1) {
auto instance = object_get_model_instance(locals.host.objp);
auto instance = object_get_model_instance(locals.host.objp());
Assertion(instance != -1, "Model instances are required if a host subobject is specified.");

auto pmi = model_get_instance(instance);
Expand All @@ -59,7 +59,7 @@ ActionResult ParticleEffectAction::execute(ProgramLocals& locals) const

auto direction = locals.variables.getValue({"locals", "direction"}).getVector();

source.moveToObject(locals.host.objp, &local_pos);
source.moveToObject(locals.host.objp(), &local_pos);
source.setOrientationFromNormalizedVec(&direction, true);

source.finish();
Expand Down
6 changes: 3 additions & 3 deletions code/actions/types/PlaySoundAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ActionResult PlaySoundAction::execute(ProgramLocals& locals) const
vec3d local_pos;
matrix local_orient;
if (locals.hostSubobject != -1) {
auto instance = object_get_model_instance(locals.host.objp);
auto instance = object_get_model_instance(locals.host.objp());
Assertion(instance != -1, "Model instances are required if a host subobject is specified.");

auto pmi = model_get_instance(instance);
Expand All @@ -47,8 +47,8 @@ ActionResult PlaySoundAction::execute(ProgramLocals& locals) const
local_pos += locals.variables.getValue({"locals", "position"}).getVector();

vec3d global_pos;
vm_vec_unrotate(&global_pos, &local_pos, &locals.host.objp->orient);
global_pos += locals.host.objp->pos;
vm_vec_unrotate(&global_pos, &local_pos, &locals.host.objp()->orient);
global_pos += locals.host.objp()->pos;

const auto soundId = gamesnd_get_by_name(m_soundIdExpression.execute(locals.variables).c_str());

Expand Down
2 changes: 1 addition & 1 deletion code/ai/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,6 @@ void do_random_sidethrust(ai_info *aip, ship_info *sip);
void ai_formation_object_recalculate_slotnums(int form_objnum, int exiting_objnum = -1);


bool test_line_of_sight(vec3d* from, vec3d* to, std::unordered_set<const object*>&& excluded_objects = {}, float threshold = 10.0f, bool test_for_shields = false, bool test_for_hull = true, float* first_intersect_dist = nullptr, object** first_intersect_obj = nullptr);
bool test_line_of_sight(vec3d* from, vec3d* to, std::unordered_set<int>&& excluded_object_ids = {}, float threshold = 10.0f, bool test_for_shields = false, bool test_for_hull = true, float* first_intersect_dist = nullptr, object** first_intersect_obj = nullptr);

#endif
6 changes: 3 additions & 3 deletions code/ai/aicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6703,7 +6703,7 @@ bool check_los(int objnum, int target_objnum, float threshold, int primary_bank,
vec3d end = Objects[target_objnum].pos;

//Don't collision check against ourselves or our target
return test_line_of_sight(&start, &end, {&Objects[objnum], &Objects[target_objnum]}, threshold);
return test_line_of_sight(&start, &end, {objnum, target_objnum}, threshold);
}

// --------------------------------------------------------------------------
Expand Down Expand Up @@ -16698,15 +16698,15 @@ void maybe_cheat_fire_synaptic(object *objp)
}
}

bool test_line_of_sight(vec3d* from, vec3d* to, std::unordered_set<const object*>&& excluded_objects, float threshold, bool test_for_shields, bool test_for_hull, float* first_intersect_dist, object** first_intersect_obj) {
bool test_line_of_sight(vec3d* from, vec3d* to, std::unordered_set<int>&& excluded_object_ids, float threshold, bool test_for_shields, bool test_for_hull, float* first_intersect_dist, object** first_intersect_obj) {
bool collides = false;

for (object* objp = GET_FIRST(&obj_used_list); objp != END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp)) {
if (objp->flags[Object::Object_Flags::Should_be_dead])
continue;

//Don't collision check against excluded objects
if (excluded_objects.count(objp) > 0)
if (excluded_object_ids.count(OBJ_INDEX(objp)) > 0)
continue;

int model_num = 0;
Expand Down
22 changes: 8 additions & 14 deletions code/camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,7 @@ void camera::do_frame(float /*in_frametime*/)

object *camera::get_object_host()
{
if(object_host.isValid())
return object_host.objp;
else
return NULL;
return object_host.objp_or_null();
}

int camera::get_object_host_submodel()
Expand All @@ -377,10 +374,7 @@ int camera::get_object_host_submodel()

object *camera::get_object_target()
{
if(object_target.isValid())
return object_target.objp;
else
return NULL;
return object_target.objp_or_null();
}

int camera::get_object_target_submodel()
Expand Down Expand Up @@ -424,7 +418,7 @@ void camera::get_info(vec3d *position, matrix *orientation, bool apply_camera_or

if(object_host.isValid())
{
object *objp = object_host.objp;
object *objp = object_host.objp();
int model_num = object_get_model(objp);
polymodel *pm = nullptr;
polymodel_instance *pmi = nullptr;
Expand All @@ -438,8 +432,8 @@ void camera::get_info(vec3d *position, matrix *orientation, bool apply_camera_or

if(object_host_submodel < 0 || pm == NULL)
{
vm_vec_unrotate(&c_pos, &pt, &object_host.objp->orient);
vm_vec_add2(&c_pos, &object_host.objp->pos);
vm_vec_unrotate(&c_pos, &pt, &object_host.objp()->orient);
vm_vec_add2(&c_pos, &object_host.objp()->pos);
}
else
{
Expand Down Expand Up @@ -488,7 +482,7 @@ void camera::get_info(vec3d *position, matrix *orientation, bool apply_camera_or
{
if(object_target.isValid())
{
object *target_objp = object_target.objp;
object *target_objp = object_target.objp();
int model_num = object_get_model(target_objp);
polymodel *target_pm = nullptr;
polymodel_instance *target_pmi = nullptr;
Expand Down Expand Up @@ -525,7 +519,7 @@ void camera::get_info(vec3d *position, matrix *orientation, bool apply_camera_or
{
if(eyep)
{
vm_vector_2_matrix(&c_ori, &host_normal, vm_vec_same(&host_normal, &object_host.objp->orient.vec.uvec)?NULL:&object_host.objp->orient.vec.uvec, NULL);
vm_vector_2_matrix(&c_ori, &host_normal, vm_vec_same(&host_normal, &object_host.objp()->orient.vec.uvec)?NULL:&object_host.objp()->orient.vec.uvec, NULL);
target_set = true;
}
else if (use_host_orient)
Expand All @@ -534,7 +528,7 @@ void camera::get_info(vec3d *position, matrix *orientation, bool apply_camera_or
}
else
{
c_ori = object_host.objp->orient;
c_ori = object_host.objp()->orient;
}
}
else
Expand Down
12 changes: 6 additions & 6 deletions code/decals/decals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ struct Decal {
if (!object.isValid()) {
return false;
}
if (object.objp->flags[Object::Object_Flags::Should_be_dead]) {
if (object.objp()->flags[Object::Object_Flags::Should_be_dead]) {
return false;
}

if (orig_obj_type != object.objp->type) {
if (orig_obj_type != object.objp()->type) {
mprintf(("Decal object type for object %d has changed from %s to %s. Please let m!m know about this\n",
OBJ_INDEX(object.objp), Object_type_names[orig_obj_type], Object_type_names[object.objp->type]));
object.objnum, Object_type_names[orig_obj_type], Object_type_names[object.objp()->type]));
return false;
}

Expand All @@ -241,7 +241,7 @@ struct Decal {
}
}

auto objp = object.objp;
auto objp = object.objp();
if (objp->type == OBJ_SHIP) {
auto shipp = &Ships[objp->instance];
auto model_instance = model_get_instance(shipp->model_instance_num);
Expand Down Expand Up @@ -393,9 +393,9 @@ void initializeMission() {
}

matrix4 getDecalTransform(Decal& decal) {
Assertion(decal.object.objp->type == OBJ_SHIP, "Only ships are currently supported for decals!");
Assertion(decal.object.objp()->type == OBJ_SHIP, "Only ships are currently supported for decals!");

auto objp = decal.object.objp;
auto objp = decal.object.objp();
auto ship = &Ships[objp->instance];
auto pmi = model_get_instance(ship->model_instance_num);
auto pm = model_get(pmi->model_num);
Expand Down
38 changes: 38 additions & 0 deletions code/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ int Object_next_signature = 1; //0 is bogus, start at 1
int Object_inited = 0;
int Show_waypoints = 0;

object_h::object_h(int in_objnum)
: objnum(in_objnum)
{
if (objnum >= 0 && objnum < MAX_OBJECTS)
sig = Objects[objnum].signature;
else
objnum = -1;
}

object_h::object_h(const object* in_objp)
{
if (in_objp)
{
objnum = OBJ_INDEX(in_objp);
sig = in_objp->signature;
}
}

object_h::object_h()
{}

bool object_h::isValid() const
{
// a signature of 0 is invalid, per obj_init()
if (objnum < 0 || sig <= 0 || objnum >= MAX_OBJECTS)
return false;
return Objects[objnum].signature == sig;
}

object* object_h::objp() const
{
return &Objects[objnum];
}

object* object_h::objp_or_null() const
{
return isValid() ? &Objects[objnum] : nullptr;
}

//WMC - Made these prettier
const char *Object_type_names[MAX_OBJECT_TYPES] = {
Expand Down
30 changes: 10 additions & 20 deletions code/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,26 +179,16 @@ extern object Objects[];

struct object_h final // prevent subclassing because classes which might use this should have their own isValid member function
{
object *objp;
int sig;

bool isValid() const {return (objp != nullptr && objp->signature == sig && sig > 0); }
object_h(object *in) {objp = in; sig = (in == nullptr) ? -1 : in->signature; }
object_h() { objp = nullptr; sig = -1; }

object_h(int objnum)
{
if (objnum >= 0 && objnum < MAX_OBJECTS)
{
objp = &Objects[objnum];
sig = objp->signature;
}
else
{
objp = nullptr;
sig = -1;
}
}
int objnum = -1;
int sig = -1;

object_h(const object* in_objp);
object_h(int in_objnum);
object_h();

bool isValid() const;
object* objp() const;
object* objp_or_null() const;

static void serialize(lua_State* L, const scripting::ade_table_entry& tableEntry, const luacpp::LuaValue& value, ubyte* data, int& packet_size);
static void deserialize(lua_State* L, const scripting::ade_table_entry& tableEntry, char* data_ptr, ubyte* data, int& offset);
Expand Down
22 changes: 11 additions & 11 deletions code/particle/ParticleSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void SourceOrigin::getGlobalPosition(vec3d* posOut) const {
vec3d offset;
switch (m_originType) {
case SourceOriginType::OBJECT: {
*posOut = m_origin.m_object.objp->pos;
vm_vec_unrotate(&offset, &m_offset, &m_origin.m_object.objp->orient);
*posOut = m_origin.m_object.objp()->pos;
vm_vec_unrotate(&offset, &m_offset, &m_origin.m_object.objp()->orient);
break;
}
case SourceOriginType::PARTICLE: {
Expand All @@ -41,7 +41,7 @@ void SourceOrigin::getGlobalPosition(vec3d* posOut) const {
break;
}
case SourceOriginType::BEAM: {
auto beam = &Beams[m_origin.m_object.objp->instance];
auto beam = &Beams[m_origin.m_object.objp()->instance];
*posOut = beam->last_start;
// weight the random points towards the start linearly
// proportion along the beam the beam stopped, of its total potential length
Expand All @@ -67,14 +67,14 @@ void SourceOrigin::getHostOrientation(matrix* matOut) const {
vec3d vec;
switch (m_originType) {
case SourceOriginType::OBJECT:
*matOut = m_origin.m_object.objp->orient;
*matOut = m_origin.m_object.objp()->orient;
break;
case SourceOriginType::PARTICLE:
vm_vector_2_matrix(matOut, &m_origin.m_particle.lock()->velocity, nullptr, nullptr);
break;
case SourceOriginType::BEAM:
vec = vmd_zero_vector;
vm_vec_normalized_dir(&vec, &Beams[m_origin.m_object.objp->instance].last_shot, &Beams[m_origin.m_object.objp->instance].last_start);
vm_vec_normalized_dir(&vec, &Beams[m_origin.m_object.objp()->instance].last_shot, &Beams[m_origin.m_object.objp()->instance].last_start);
vm_vector_2_matrix(matOut, &vec, nullptr, nullptr);
break;
case SourceOriginType::VECTOR: // Intentional fall-through, plain vectors have no orientation
Expand All @@ -90,8 +90,8 @@ void SourceOrigin::applyToParticleInfo(particle_info& info, bool allow_relative)
switch (m_originType) {
case SourceOriginType::OBJECT: {
if (allow_relative) {
info.attached_objnum = static_cast<int>(OBJ_INDEX(m_origin.m_object.objp));
info.attached_sig = m_origin.m_object.objp->signature;
info.attached_objnum = m_origin.m_object.objnum;
info.attached_sig = m_origin.m_object.sig;

info.pos = m_offset;
} else {
Expand Down Expand Up @@ -125,11 +125,11 @@ void SourceOrigin::applyToParticleInfo(particle_info& info, bool allow_relative)
vec3d SourceOrigin::getVelocity() const {
switch (this->m_originType) {
case SourceOriginType::OBJECT:
return m_origin.m_object.objp->phys_info.vel;
return m_origin.m_object.objp()->phys_info.vel;
case SourceOriginType::PARTICLE:
return m_origin.m_particle.lock()->velocity;
case SourceOriginType::BEAM: {
beam* bm = &Beams[m_origin.m_object.objp->instance];
beam* bm = &Beams[m_origin.m_object.objp()->instance];
vec3d vel;
vm_vec_normalized_dir(&vel, &bm->last_shot, &bm->last_start);
vm_vec_scale(&vel, Weapon_info[bm->weapon_info_index].max_speed);
Expand Down Expand Up @@ -214,7 +214,7 @@ bool SourceOrigin::isValid() const {
return false;
}

auto objp = m_origin.m_object.objp;
auto objp = m_origin.m_object.objp();

if (objp->type != OBJ_WEAPON && objp->type != OBJ_BEAM) {
// The following checks are only relevant for weapons
Expand Down Expand Up @@ -398,7 +398,7 @@ void ParticleSource::initializeThrusterOffset(weapon* /*wp*/, weapon_info* wip)
void ParticleSource::finishCreation() {
if (m_origin.m_originType == SourceOriginType::OBJECT) {
if (IS_VEC_NULL(&m_origin.m_offset)) {
object* obj = m_origin.m_origin.m_object.objp;
object* obj = m_origin.m_origin.m_object.objp();

if (obj->type == OBJ_WEAPON) {
weapon* wp = &Weapons[obj->instance];
Expand Down
2 changes: 1 addition & 1 deletion code/particle/ParticleSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SourceOrigin {

inline SourceOriginType getType() const { return m_originType; }

inline object* getObjectHost() const { return m_origin.m_object.objp; }
inline object* getObjectHost() const { return m_origin.m_object.objp_or_null(); }

/**
* @brief Determines if the origin is valid
Expand Down
Loading

0 comments on commit c198565

Please sign in to comment.