Fix latent bugs and collapse repetition in myship/filemap

- filemap.cpp: fix inverted ternary in getkey() that dereferenced ref
  when ref==NULL; remove dead strncpy in recurse_get(); log a warning
  when addpath() silently truncates keys longer than 8 chars.
- screen.cpp: replace lone strcpy with memcpy.
- enemies.h: brace nested if/else blocks to silence dangling-else
  warnings (no behavior change).
- myship.cpp: replace three 8-case direction switches (movement and
  twice in shot_single) with dir_dx[]/dir_dy[] lookup tables;
  extract free_bolt() helper to deduplicate five copies of the
  boltst/free_obj/NULL pattern.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Ville Lindholm 2026-05-28 20:56:11 +03:00
parent 72d4cd0102
commit be85e999f0
No known key found for this signature in database
GPG Key ID: 89AE9EAA3B6FDE7C
5 changed files with 46 additions and 129 deletions

View File

@ -348,6 +348,7 @@ inline void _enemy::move()
// Handle collisions with the player ship // Handle collisions with the player ship
if((hitsize >= 0) && (norm < (hitsize + HIT_MYSHIP))) if((hitsize >= 0) && (norm < (hitsize + HIT_MYSHIP)))
{
if(prefs->cmd_indicator) if(prefs->cmd_indicator)
sound.g_player_damage(); sound.g_player_damage();
else if(myship.alive()) else if(myship.alive())
@ -355,6 +356,7 @@ inline void _enemy::move()
hit(game.damage); // Ship damages object hit(game.damage); // Ship damages object
myship.hit(damage); // Object damages ship myship.hit(damage); // Object damages ship
} }
}
// Handle collisions with player bolts (Player bolts kill themselves // Handle collisions with player bolts (Player bolts kill themselves
// when they hit something, so we don't need to hit them from here.) // when they hit something, so we don't need to hit them from here.)
@ -364,10 +366,12 @@ inline void _enemy::move()
int dmg = myship.hit_bolt(CS2PIXEL(x), CS2PIXEL(y), int dmg = myship.hit_bolt(CS2PIXEL(x), CS2PIXEL(y),
hitsize + HIT_BOLT, health); hitsize + HIT_BOLT, health);
if(dmg) if(dmg)
{
if(prefs->cmd_indicator) if(prefs->cmd_indicator)
sound.g_player_damage(); sound.g_player_damage();
else else
hit(dmg); // Bolt damages object hit(dmg); // Bolt damages object
}
} }
inline void _enemy::move_intro() inline void _enemy::move_intro()

View File

@ -241,6 +241,9 @@ void filemapper_t::addpath(const char *key, const char *path, int first)
try try
{ {
k = new fm_key_t; k = new fm_key_t;
if(strlen(key) > 8)
log_printf(WLOG, "filemapper_t::addpath():"
" key '%s' truncated to 8 chars.\n", key);
strncpy(k->key, key, 8); strncpy(k->key, key, 8);
k->key[8] = 0; k->key[8] = 0;
k->path = strdup(path); k->path = strdup(path);
@ -277,7 +280,7 @@ void filemapper_t::addpath(const char *key, const char *path, int first)
fm_key_t *filemapper_t::getkey(fm_key_t *key, const char *ref) fm_key_t *filemapper_t::getkey(fm_key_t *key, const char *ref)
{ {
int all = ref ? 0 : (ref[0] == '*'); int all = ref ? (ref[0] == '*') : 0;
if(key) if(key)
{ {
if(!ref) if(!ref)
@ -534,7 +537,6 @@ int filemapper_t::recurse_get(char *result, const char *ref, int kind,
{ {
// log_printf(DLOG, " [level %d]\n", level); // log_printf(DLOG, " [level %d]\n", level);
char buffer[FM_BUFFER_SIZE]; char buffer[FM_BUFFER_SIZE];
strncpy(buffer, ref, FM_BUFFER_SIZE);
// Check for the ">>" dereferencing token. // Check for the ">>" dereferencing token.
char *obj = (char *)strstr(ref, FM_DEREF_TOKEN); char *obj = (char *)strstr(ref, FM_DEREF_TOKEN);

View File

@ -31,6 +31,11 @@
#define WING_GUN_OFFSET 4 #define WING_GUN_OFFSET 4
// Unit-vector tables indexed by direction (1=N, 2=NE, ..., 8=NW). Index 0 unused.
// Odd directions are cardinals; even directions are diagonals.
static const int dir_dx[9] = { 0, 0, +1, +1, +1, 0, -1, -1, -1 };
static const int dir_dy[9] = { 0, -1, -1, 0, +1, +1, +1, 0, -1 };
_myship_state _myship::_state; _myship_state _myship::_state;
int _myship::di; int _myship::di;
int _myship::virtx; int _myship::virtx;
@ -89,16 +94,22 @@ void _myship::state(_myship_state s)
} }
void _myship::free_bolt(int i)
{
boltst[i] = 0;
if(bolt_objects[i])
{
gengine->free_obj(bolt_objects[i]);
bolt_objects[i] = NULL;
}
}
void _myship::off() void _myship::off()
{ {
state(dead); state(dead);
int i; for(int i = 0; i < MAX_BOLTS; i++)
for(i = 0; i < MAX_BOLTS; i++) free_bolt(i);
if(bolt_objects[i])
{
gengine->free_obj(bolt_objects[i]);
bolt_objects[i] = NULL;
}
} }
@ -119,17 +130,13 @@ int _myship::init()
apply_position(); apply_position();
int i; for(int i = 0; i < MAX_BOLTS; i++)
for(i = 0; i < MAX_BOLTS; i++)
{ {
boltx[i] = 0; boltx[i] = 0;
bolty[i] = 0; bolty[i] = 0;
boltdx[i] = 0; boltdx[i] = 0;
boltdy[i] = 0; boltdy[i] = 0;
boltst[i] = 0; free_bolt(i);
if(bolt_objects[i])
gengine->free_obj(bolt_objects[i]);
bolt_objects[i] = NULL;
} }
return 0; return 0;
} }
@ -194,37 +201,9 @@ int _myship::move()
vd = 0; vd = 0;
vo = 0; vo = 0;
} }
switch (di) int spd = (di & 1) ? vo : vd;
{ virtx += dir_dx[di] * spd;
case 1: virty += dir_dy[di] * spd;
virty -= vo;
break;
case 2:
virty -= vd;
virtx += vd;
break;
case 3:
virtx += vo;
break;
case 4:
virtx += vd;
virty += vd;
break;
case 5:
virty += vo;
break;
case 6:
virty += vd;
virtx -= vd;
break;
case 7:
virtx -= vo;
break;
case 8:
virtx -= vd;
virty -= vd;
break;
}
explo_time = 0; explo_time = 0;
} }
else if(_state == dead) else if(_state == dead)
@ -320,12 +299,7 @@ int _myship::move()
bolty[i] += boltdy[i]; bolty[i] += boltdy[i];
if((ABS(boltx[i] - x) >= (VIEWLIMIT >> 1) + 16) || if((ABS(boltx[i] - x) >= (VIEWLIMIT >> 1) + 16) ||
(ABS(bolty[i] - y) >= (VIEWLIMIT >> 1) + 16)) (ABS(bolty[i] - y) >= (VIEWLIMIT >> 1) + 16))
{ free_bolt(i);
boltst[i] = 0;
if(bolt_objects[i])
gengine->free_obj(bolt_objects[i]);
bolt_objects[i] = NULL;
}
else if(bolt_objects[i]) else if(bolt_objects[i])
cs_obj_image(bolt_objects[i], B_BOLT, cs_obj_image(bolt_objects[i], B_BOLT,
(bolt_objects[i]->anim.frame & 0xfffffffc) + (bolt_objects[i]->anim.frame & 0xfffffffc) +
@ -389,10 +363,7 @@ int _myship::hit_structure()
{ {
sound.g_bolt_hit(x1<<12, y1<<12); sound.g_bolt_hit(x1<<12, y1<<12);
enemies.make(&boltexpl, boltx[i], bolty[i]); enemies.make(&boltexpl, boltx[i], bolty[i]);
boltst[i] = 0; free_bolt(i);
if(bolt_objects[i])
gengine->free_obj(bolt_objects[i]);
bolt_objects[i] = NULL;
} }
} }
@ -424,12 +395,7 @@ int _myship::hit_bolt(int ex, int ey, int hitsize, int health)
if(ABS(ey - bolty[i]) >= hitsize) if(ABS(ey - bolty[i]) >= hitsize)
continue; continue;
if(!prefs->cmd_cheat) if(!prefs->cmd_cheat)
{ free_bolt(i);
boltst[i] = 0;
if(bolt_objects[i])
gengine->free_obj(bolt_objects[i]);
bolt_objects[i] = NULL;
}
enemies.make(&boltexpl, boltx[i], bolty[i]); enemies.make(&boltexpl, boltx[i], bolty[i]);
dmg += game.bolt_damage; dmg += game.bolt_damage;
if(dmg >= health) if(dmg >= health)
@ -487,72 +453,16 @@ void _myship::shot_single(int i, int dir, int offset)
boltst[i] = 1; boltst[i] = 1;
boltx[i] = x; boltx[i] = x;
bolty[i] = y; bolty[i] = y;
switch((dir + 1) % 8 + 1)
{ // Wing-gun position offset is perpendicular to the firing direction.
case 1: int pd = (dir + 1) % 8 + 1;
bolty[i] -= offset; int poff = (pd & 1) ? offset : doffset;
break; boltx[i] += dir_dx[pd] * poff;
case 2: bolty[i] += dir_dy[pd] * poff;
bolty[i] -= doffset;
boltx[i] += doffset; int v = (dir & 1) ? BEAMV1 : BEAMV2;
break; boltdx[i] = dir_dx[dir] * v;
case 3: boltdy[i] = dir_dy[dir] * v;
boltx[i] += offset;
break;
case 4:
boltx[i] += doffset;
bolty[i] += doffset;
break;
case 5:
bolty[i] += offset;
break;
case 6:
bolty[i] += doffset;
boltx[i] -= doffset;
break;
case 7:
boltx[i] -= offset;
break;
case 8:
boltx[i] -= doffset;
bolty[i] -= doffset;
break;
}
switch(dir)
{
case 1:
boltdx[i] = 0;
boltdy[i] = -BEAMV1;
break;
case 2:
boltdy[i] = -BEAMV2;
boltdx[i] = BEAMV2;
break;
case 3:
boltdx[i] = BEAMV1;
boltdy[i] = 0;
break;
case 4:
boltdx[i] = BEAMV2;
boltdy[i] = BEAMV2;
break;
case 5:
boltdx[i] = 0;
boltdy[i] = BEAMV1;
break;
case 6:
boltdy[i] = BEAMV2;
boltdx[i] = -BEAMV2;
break;
case 7:
boltdx[i] = -BEAMV1;
boltdy[i] = 0;
break;
case 8:
boltdx[i] = -BEAMV2;
boltdy[i] = -BEAMV2;
break;
}
if(!bolt_objects[i]) if(!bolt_objects[i])
bolt_objects[i] = gengine->get_obj(LAYER_PLAYER); bolt_objects[i] = gengine->get_obj(LAYER_PLAYER);
if(bolt_objects[i]) if(bolt_objects[i])

View File

@ -64,6 +64,7 @@ class _myship
static void shot_single(int i, int dir, int offset); static void shot_single(int i, int dir, int offset);
static void apply_position(); static void apply_position();
static void explode(int x, int y); static void explode(int x, int y);
static void free_bolt(int i);
public: public:
_myship(); _myship();
static inline int get_x() static inline int get_x()

View File

@ -219,7 +219,7 @@ void _screen::init_highscores()
{ {
hi_sc[i] = 0; hi_sc[i] = 0;
hi_st[i] = 0; hi_st[i] = 0;
strcpy(hi_nm[i], "---"); memcpy(hi_nm[i], "---", 4);
} }
} }