Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize: Move some arrays to std::array #412

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Descent3/AIMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
#define AI_SOUND_SHORT_DIST 60.0f

extern int AI_NumRendered;
extern int AI_RenderedList[MAX_OBJECTS];
extern std::array<int, MAX_OBJECTS> AI_RenderedList;

extern int AI_NumHostileAlert; // A rough number of alert/hostile robots
// that have seen the player recently
Expand Down
6 changes: 3 additions & 3 deletions Descent3/AImain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ const char *const Ai_movement_subtype_walking_strings[MAX_AI_INIT_MOVEMENT_SUBTY
#define AI_MAX_MELEE_RANGE 5.0f

int AI_NumRendered;
int AI_RenderedList[MAX_OBJECTS];
std::array<int, MAX_OBJECTS> AI_RenderedList;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as we're doing these conversions, let's default-initialize the elements in the arrays:

Suggested change
std::array<int, MAX_OBJECTS> AI_RenderedList;
std::array<int, MAX_OBJECTS> AI_RenderedList{};

Both T[N] and std::array<T, N> default-initialize only the array object, not the array elements. IE, this gives an indeterminate value:

std::array<int, 4> arr;
int val = arr[0];
// val is indeterminate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean... OK but I was expecting most of these to eventually be changed to std::vector.

Copy link
Contributor

@tophyr tophyr Jun 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vectors would be preferable, definitely. I'm fine with not adding the explicit initialization in this changeset.


int AI_NumHostileAlert = 0;

Expand Down Expand Up @@ -3684,7 +3684,7 @@ bool AIInit(object *obj, uint8_t ai_class, uint8_t ai_type, uint8_t ai_movement)
((ai_info->flags & AIF_TEAM_MASK) == AIF_TEAM_REBEL);

ai_info->mem_time_till_next_update = 3.0f + (float)ps_rand() / (float)D3_RAND_MAX * 2.0f;
memset(ai_info->memory, 0, sizeof(ai_mem) * AI_MEM_DEPTH);
ai_info->memory.fill(ai_mem{});
for (i = 0; i < AI_MEM_DEPTH; i++) {
ai_info->memory[0].shields = obj->shields;
}
Expand Down Expand Up @@ -5990,7 +5990,7 @@ void AIDetermineTarget(object *obj) {
// also current emotional levels should influence the percent chance of the check being successful
void AIDoFreud(object *obj) {
ai_frame *ai_info = obj->ai_info;
ai_mem *mem = ai_info->memory;
auto& mem = ai_info->memory;

int fear_depth = (ai_info->life_preservation) * (AI_MEM_DEPTH - 1) + 1;
int anger_depth = (ai_info->agression) * (AI_MEM_DEPTH - 1) + 1;
Expand Down
22 changes: 11 additions & 11 deletions Descent3/Game2DLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@

void SelectNextCameraView(int window);
#define NUM_CAMERA_VIEWS 3
extern int Camera_view_mode[NUM_CAMERA_VIEWS];
extern std::array<int, NUM_CAMERA_VIEWS> Camera_view_mode;

// Osiris_CreateModuleInitStruct
// Purpose:
// This function initializes a Module Init Struct with all the needed data to get sent
// to the module during initialization.
extern void Osiris_CreateModuleInitStruct(tOSIRISModuleInit *mi);
module GameDLLHandle = {NULL};
extern ddgr_color Player_colors[];
extern std::array<ddgr_color, 32> Player_colors;
GravisZro marked this conversation as resolved.
Show resolved Hide resolved
struct game_api {
int *objs;
int *rooms;
Expand Down Expand Up @@ -136,16 +136,16 @@ static void DUMMYrend_DrawScaledBitmap(int x1, int y1, int x2, int y2, int bm, f
}

void GetGameAPI(game_api *api) {
api->objs = (int *)Objects;
api->rooms = (int *)Rooms;
api->objs = (int *)std::data(Objects);
GravisZro marked this conversation as resolved.
Show resolved Hide resolved
api->rooms = (int *)std::data(Rooms);
api->terrain = (int *)Terrain_seg;
api->players = (int *)Players;
api->players = (int *)std::data(Players);
api->netgame = (int *)&Netgame;
api->netplayers = (int *)&NetPlayers;
api->ships = (int *)&Ships;
api->weapons = (int *)&Weapons;
api->Current_mission = (int *)&Current_mission;
api->GameTextures = (int *)GameTextures;
api->GameTextures = (int *)std::data(GameTextures);
api->GameVClips = (int *)GameVClips;
// Fill in function pointers here. The order here must match the order on the
// DLL side
Expand Down Expand Up @@ -534,10 +534,10 @@ void GetGameAPI(game_api *api) {
api->vp[8] = (int *)&Game_interface_mode;
api->vp[9] = (int *)LocalD3Dir;
api->vp[10] = (int *)&Game_is_master_tracker_game;
api->vp[11] = (int *)Local_object_list;
api->vp[12] = (int *)Server_object_list;
api->vp[11] = (int *)std::data(Local_object_list);
api->vp[12] = (int *)std::data(Server_object_list);
api->vp[13] = (int *)&Dedicated_server;
api->vp[14] = (int *)Player_colors;
api->vp[14] = (int *)std::data(Player_colors);
api->vp[15] = (int *)&Hud_aspect_x;
api->vp[16] = (int *)&Hud_aspect_y;
api->vp[17] = (int *)&Viewer_object;
Expand All @@ -550,8 +550,8 @@ void GetGameAPI(game_api *api) {
api->vp[24] = (int *)&Multi_next_level;
api->vp[25] = (int *)&Level_info;
api->vp[26] = (int *)GameArgs;
api->vp[27] = (int *)Camera_view_mode;
api->vp[28] = (int *)Object_info;
api->vp[27] = (int *)std::data(Camera_view_mode);
api->vp[28] = (int *)std::data(Object_info);

api->osiris_functions = &Multi_d3m_osiris_funcs;
Osiris_CreateModuleInitStruct(&Multi_d3m_osiris_funcs);
Expand Down
12 changes: 6 additions & 6 deletions Descent3/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ void ShrinkWindow() {

#define NUM_CAMERA_VIEWS 3

int Camera_view_mode[NUM_CAMERA_VIEWS];
std::array<int, NUM_CAMERA_VIEWS> Camera_view_mode;

// Selects the next mode for this view
void SelectNextCameraView(int window) {
Expand Down Expand Up @@ -1385,7 +1385,7 @@ void ProcessNormalKey(int key) {
switch (key) {
case KEY_C: {
// goto next player
int viewer_objnum = Viewer_object - Objects;
int viewer_objnum = OBJNUM(Viewer_object);
int found = 0;

for (int i = viewer_objnum + 1; i <= Highest_object_index && !found; i++, i %= (Highest_object_index + 1)) {
Expand All @@ -1397,7 +1397,7 @@ void ProcessNormalKey(int key) {
} break;
case KEY_P: {
// goto next player
int viewer_objnum = Viewer_object - Objects;
int viewer_objnum = OBJNUM(Viewer_object);
int found = 0;

for (int i = viewer_objnum + 1; i <= Highest_object_index && !found; i++, i %= (Highest_object_index + 1)) {
Expand All @@ -1409,7 +1409,7 @@ void ProcessNormalKey(int key) {
} break;
case KEY_R: {
// goto next robot
int viewer_objnum = Viewer_object - Objects;
int viewer_objnum = OBJNUM(Viewer_object);
int found = 0;

for (int i = viewer_objnum + 1; i <= Highest_object_index && !found; i++, i %= (Highest_object_index + 1)) {
Expand Down Expand Up @@ -2076,7 +2076,7 @@ void ProcessTestKeys(int key) {

case KEY_1: {
// goto prev viewer
int viewer_objnum = Viewer_object - Objects;
int viewer_objnum = OBJNUM(Viewer_object);
int found = 0;

for (i = viewer_objnum - 1; i >= 0 && !found; i--, i < 0 ? i = Highest_object_index : i = i) {
Expand All @@ -2099,7 +2099,7 @@ void ProcessTestKeys(int key) {

case KEY_3: {
// goto next viewer
int viewer_objnum = Viewer_object - Objects;
int viewer_objnum = OBJNUM(Viewer_object);
int found = 0;

for (i = viewer_objnum + 1; i <= Highest_object_index && !found; i++, i %= (Highest_object_index + 1)) {
Expand Down
16 changes: 8 additions & 8 deletions Descent3/LoadLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ int ReadRoom(CFILE *ifile, room *rp, int version) {

// Check for bad normal
if (!t) {
mprintf(1, "WARNING: Room %d face %d has bad normal!\n", rp - Rooms, i);
mprintf(1, "WARNING: Room %d face %d has bad normal!\n", rp - std::data(Rooms), i);
}
}

Expand Down Expand Up @@ -3492,8 +3492,8 @@ void ReadPlayerStarts(CFILE *infile, int fileversion) {
void VerifyObjectList() {
int i;

uint8_t already_listed[MAX_OBJECTS];
memset(already_listed, 0, MAX_OBJECTS);
std::array<uint8_t, MAX_OBJECTS> already_listed;
already_listed.fill(0);

for (i = 0; i <= Highest_room_index; i++) {
room *rp = &Rooms[i];
Expand Down Expand Up @@ -3790,8 +3790,8 @@ int LoadLevel(char *filename, void (*cb_fn)(const char *, int, int)) {
} else if (ISCHUNK(CHUNK_OBJECT_HANDLES)) { // Read in any non-zero handles for deleted objects
int handle, objnum;

uint8_t already_loaded[MAX_OBJECTS];
memset(already_loaded, 0, MAX_OBJECTS);
std::array<uint8_t, MAX_OBJECTS> already_loaded;
already_loaded.fill(0);

// Get the number of handles in the file
n = cf_ReadInt(ifile);
Expand Down Expand Up @@ -5118,7 +5118,7 @@ int SaveLevel(char *filename, bool f_save_room_AABB) {
room *rp;
int nrooms = 0, nverts = 0, nfaces = 0, nfaceverts = 0, nportals = 0;
extern int CountRoomFaceVerts(room * rp); // TEMP: move to room.h
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) // Count the number of faces, verts, etc. in the level
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) // Count the number of faces, verts, etc. in the level
if (rp->used) {
nrooms++;
nverts += rp->num_verts;
Expand All @@ -5144,13 +5144,13 @@ int SaveLevel(char *filename, bool f_save_room_AABB) {
EndChunk(ofile, chunk_start_pos);

// Write room wind, if any rooms have wind
for (i = nrooms = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) // Count the number of rooms with wind
for (i = nrooms = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) // Count the number of rooms with wind
if ((rp->wind.x != 0.0) || (rp->wind.y != 0.0) || (rp->wind.z != 0.0))
nrooms++;
if (nrooms) {
chunk_start_pos = StartChunk(ofile, CHUNK_ROOM_WIND);
cf_WriteInt(ofile, nrooms);
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) { // write the wind values
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) { // write the wind values
if ((rp->wind.x != 0.0) || (rp->wind.y != 0.0) || (rp->wind.z != 0.0)) {
cf_WriteShort(ofile, i);
cf_WriteVector(ofile, &rp->wind);
Expand Down
6 changes: 3 additions & 3 deletions Descent3/ObjInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,8 @@ int ObjInitGeneric(object *objp, bool reinit) {
// Deal with deleted type
if (obj_info->type == OBJ_NONE) {
int i;
for (i = 0, obj_info = Object_info; i < MAX_OBJECT_IDS; i++, obj_info) { // find other object of same type
if (Object_info[i].type == objp->type)
for (i = 0, obj_info = std::data(Object_info); i < MAX_OBJECT_IDS; i++, obj_info++) { // find other object of same type
GravisZro marked this conversation as resolved.
Show resolved Hide resolved
if (obj_info->type == objp->type)
break;
}
ASSERT(i < MAX_OBJECT_IDS); // There should (in real life) always be at least one of each type
Expand Down Expand Up @@ -1205,7 +1205,7 @@ int ObjInit(object *objp, int type, int id, int handle, vector *pos, float creat
void ObjReInitAll() {
int objnum;
object *objp;
for (objnum = 0, objp = Objects; objnum <= Highest_object_index; objnum++, objp++)
for (objnum = 0, objp = std::data(Objects); objnum <= Highest_object_index; objnum++, objp++)
if (objp->type != OBJ_NONE)
ObjInitTypeSpecific(objp, 1);
}
12 changes: 6 additions & 6 deletions Descent3/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,15 +1135,15 @@

#include <algorithm>

player Players[MAX_PLAYERS];
std::array<player, MAX_PLAYERS> Players;
int Player_num;

int Num_teams = 0;
int Team_game = 0;
int Default_ship_permission = 0x01;
float HudNameTan = -1;

team Teams[MAX_TEAMS];
std::array<team, MAX_TEAMS> Teams;

static int Highest_player_start = 0;

Expand Down Expand Up @@ -2717,7 +2717,7 @@ void DeadPlayerShipHit(object *obj, int hit_room, vector *hitpt, float magnitude
fq.p1 = &obj->pos;
fq.startroom = hit_room;
fq.rad = obj->size;
fq.thisobjnum = obj - Objects;
fq.thisobjnum = OBJNUM(obj);
fq.ignore_obj_list = NULL;
fq.flags = FQ_CHECK_OBJS | FQ_OBJ_BACKFACE | FQ_ONLY_PLAYER_OBJ;

Expand Down Expand Up @@ -3549,7 +3549,7 @@ void PlayerSwitchToObserver(int slot, int observer_mode, int piggy_objnum) {
SetObjectControlType(obj, CT_NONE);
Players[slot].piggy_objnum = piggy_objnum;
Players[slot].piggy_sig = Objects[piggy_objnum].handle & HANDLE_COUNT_MASK;
mprintf(0, "Object %d is observing object %d!\n", obj - Objects, piggy_objnum);
mprintf(0, "Object %d is observing object %d!\n", OBJNUM(obj), piggy_objnum);
}
}

Expand Down Expand Up @@ -3800,11 +3800,11 @@ void MakeAtuoWaypointList() {
Num_waypoints = 0;

// Clear room waypoint flags
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++)
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++)
rp->flags &= ~RF_WAYPOINT;

// Look through objects for waypoints
for (i = 0, objp = Objects; i <= Highest_object_index; i++, objp++) {
for (i = 0, objp = std::data(Objects); i <= Highest_object_index; i++, objp++) {
if (objp->type == OBJ_WAYPOINT) {
if (Num_waypoints < MAX_WAYPOINTS) {
Waypoints[Num_waypoints].pos = objp->pos;
Expand Down
12 changes: 6 additions & 6 deletions Descent3/WeaponFire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ void AquireElectricalTarget(object *obj) {
fq.startroom = obj->roomnum;
fq.p1 = &dest;
fq.rad = .0001f;
fq.thisobjnum = Objects - obj;
fq.thisobjnum = OBJNUM(obj); // bugfix?
GravisZro marked this conversation as resolved.
Show resolved Hide resolved
fq.ignore_obj_list = NULL;
fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS | FQ_IGNORE_WEAPONS;

Expand Down Expand Up @@ -1214,7 +1214,7 @@ void AquireElectricalTarget(object *obj) {
int CreateAndFireWeapon(vector *pos, vector *dir, object *parent, int weapon_num) {
int objnum;
object *obj;
int parentnum = parent - Objects;
int parentnum = OBJNUM(parent);
uint8_t terrain = 0;

ASSERT(Weapons[weapon_num].used);
Expand Down Expand Up @@ -2514,7 +2514,7 @@ void DrawPolygonalWeaponRing(object *obj) {
int i;
g3Point *pntlist[4];
float inner_size = (obj->size * .8);
int objnum = obj - Objects;
int objnum = OBJNUM(obj);
int odd = objnum % 2;

int num_segments = 20;
Expand Down Expand Up @@ -2597,7 +2597,7 @@ void DrawWeaponObject(object *obj) {
DrawElectricalWeapon(obj);
} else if ((Weapons[obj->id].flags & WF_IMAGE_BITMAP) || (Weapons[obj->id].flags & WF_IMAGE_VCLIP)) {
int bm_handle;
int objnum = obj - Objects;
int objnum = OBJNUM(obj);
float rot_temp = Weapons[obj->id].phys_info.rotvel.z / 65536.0;
int int_game = Gametime / rot_temp;
float diff = Gametime - (int_game * rot_temp);
Expand Down Expand Up @@ -2726,7 +2726,7 @@ void DoZoomEffect(player_weapon *pw, uint8_t clear) {
fq.startroom = obj->roomnum;
fq.p1 = &dest;
fq.rad = .0001f;
fq.thisobjnum = Objects - obj;
fq.thisobjnum = OBJNUM(obj); // bugfix?
fq.ignore_obj_list = NULL;
fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS | FQ_IGNORE_WEAPONS;

Expand Down Expand Up @@ -3395,7 +3395,7 @@ void CreateCountermeasureFromObject(object *parent, int weapon_id) {
ASSERT(Weapons[weapon_id].used);

if (Game_mode & GM_MULTI)
MultiSendRequestCountermeasure(parent - Objects, weapon_id);
MultiSendRequestCountermeasure(OBJNUM(parent), weapon_id);
else
FireWeaponFromObject(parent, weapon_id, 5);
}
Expand Down
6 changes: 4 additions & 2 deletions Descent3/aipath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <array>

#include "aipath.h"
#include "gamepath.h"
#include "AIGoal.h"
Expand All @@ -33,8 +35,8 @@
#define MAX_DYNAMIC_PATHS 50
#define MAX_NODES 50

ai_dynamic_path AIDynamicPath[MAX_DYNAMIC_PATHS];
int AIAltPath[MAX_ROOMS];
std::array<ai_dynamic_path, MAX_DYNAMIC_PATHS> AIDynamicPath;
std::array<int, MAX_ROOMS> AIAltPath;
int AIAltPathNumNodes;

static void AIUpdatePathInfo(q_item **node_list, int start, int end) {
Expand Down
Loading