From be85e999f0fb35d200f5c44a0fd72e6f2de77c4d Mon Sep 17 00:00:00 2001 From: Ville Lindholm Date: Thu, 28 May 2026 20:56:11 +0300 Subject: [PATCH] Fix latent bugs and collapse repetition in myship/filemap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- enemies.h | 4 ++ filemap.cpp | 6 +- myship.cpp | 162 ++++++++++++---------------------------------------- myship.h | 1 + screen.cpp | 2 +- 5 files changed, 46 insertions(+), 129 deletions(-) diff --git a/enemies.h b/enemies.h index 2971f61..92bc01e 100644 --- a/enemies.h +++ b/enemies.h @@ -348,6 +348,7 @@ inline void _enemy::move() // Handle collisions with the player ship if((hitsize >= 0) && (norm < (hitsize + HIT_MYSHIP))) + { if(prefs->cmd_indicator) sound.g_player_damage(); else if(myship.alive()) @@ -355,6 +356,7 @@ inline void _enemy::move() hit(game.damage); // Ship damages object myship.hit(damage); // Object damages ship } + } // Handle collisions with player bolts (Player bolts kill themselves // 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), hitsize + HIT_BOLT, health); if(dmg) + { if(prefs->cmd_indicator) sound.g_player_damage(); else hit(dmg); // Bolt damages object + } } inline void _enemy::move_intro() diff --git a/filemap.cpp b/filemap.cpp index 5a865c9..22cbfbd 100644 --- a/filemap.cpp +++ b/filemap.cpp @@ -241,6 +241,9 @@ void filemapper_t::addpath(const char *key, const char *path, int first) try { 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); k->key[8] = 0; 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) { - int all = ref ? 0 : (ref[0] == '*'); + int all = ref ? (ref[0] == '*') : 0; if(key) { 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); char buffer[FM_BUFFER_SIZE]; - strncpy(buffer, ref, FM_BUFFER_SIZE); // Check for the ">>" dereferencing token. char *obj = (char *)strstr(ref, FM_DEREF_TOKEN); diff --git a/myship.cpp b/myship.cpp index 954e9ae..e3dc0a2 100644 --- a/myship.cpp +++ b/myship.cpp @@ -31,6 +31,11 @@ #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; int _myship::di; 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() { state(dead); - int i; - for(i = 0; i < MAX_BOLTS; i++) - if(bolt_objects[i]) - { - gengine->free_obj(bolt_objects[i]); - bolt_objects[i] = NULL; - } + for(int i = 0; i < MAX_BOLTS; i++) + free_bolt(i); } @@ -119,17 +130,13 @@ int _myship::init() apply_position(); - int i; - for(i = 0; i < MAX_BOLTS; i++) + for(int i = 0; i < MAX_BOLTS; i++) { boltx[i] = 0; bolty[i] = 0; boltdx[i] = 0; boltdy[i] = 0; - boltst[i] = 0; - if(bolt_objects[i]) - gengine->free_obj(bolt_objects[i]); - bolt_objects[i] = NULL; + free_bolt(i); } return 0; } @@ -194,37 +201,9 @@ int _myship::move() vd = 0; vo = 0; } - switch (di) - { - case 1: - 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; - } + int spd = (di & 1) ? vo : vd; + virtx += dir_dx[di] * spd; + virty += dir_dy[di] * spd; explo_time = 0; } else if(_state == dead) @@ -320,12 +299,7 @@ int _myship::move() bolty[i] += boltdy[i]; if((ABS(boltx[i] - x) >= (VIEWLIMIT >> 1) + 16) || (ABS(bolty[i] - y) >= (VIEWLIMIT >> 1) + 16)) - { - boltst[i] = 0; - if(bolt_objects[i]) - gengine->free_obj(bolt_objects[i]); - bolt_objects[i] = NULL; - } + free_bolt(i); else if(bolt_objects[i]) cs_obj_image(bolt_objects[i], B_BOLT, (bolt_objects[i]->anim.frame & 0xfffffffc) + @@ -389,10 +363,7 @@ int _myship::hit_structure() { sound.g_bolt_hit(x1<<12, y1<<12); enemies.make(&boltexpl, boltx[i], bolty[i]); - boltst[i] = 0; - if(bolt_objects[i]) - gengine->free_obj(bolt_objects[i]); - bolt_objects[i] = NULL; + free_bolt(i); } } @@ -424,12 +395,7 @@ int _myship::hit_bolt(int ex, int ey, int hitsize, int health) if(ABS(ey - bolty[i]) >= hitsize) continue; if(!prefs->cmd_cheat) - { - boltst[i] = 0; - if(bolt_objects[i]) - gengine->free_obj(bolt_objects[i]); - bolt_objects[i] = NULL; - } + free_bolt(i); enemies.make(&boltexpl, boltx[i], bolty[i]); dmg += game.bolt_damage; if(dmg >= health) @@ -487,72 +453,16 @@ void _myship::shot_single(int i, int dir, int offset) boltst[i] = 1; boltx[i] = x; bolty[i] = y; - switch((dir + 1) % 8 + 1) - { - case 1: - bolty[i] -= offset; - break; - case 2: - bolty[i] -= doffset; - boltx[i] += doffset; - break; - case 3: - 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; - } + + // Wing-gun position offset is perpendicular to the firing direction. + int pd = (dir + 1) % 8 + 1; + int poff = (pd & 1) ? offset : doffset; + boltx[i] += dir_dx[pd] * poff; + bolty[i] += dir_dy[pd] * poff; + + int v = (dir & 1) ? BEAMV1 : BEAMV2; + boltdx[i] = dir_dx[dir] * v; + boltdy[i] = dir_dy[dir] * v; if(!bolt_objects[i]) bolt_objects[i] = gengine->get_obj(LAYER_PLAYER); if(bolt_objects[i]) diff --git a/myship.h b/myship.h index 18e953e..52f0a8f 100644 --- a/myship.h +++ b/myship.h @@ -64,6 +64,7 @@ class _myship static void shot_single(int i, int dir, int offset); static void apply_position(); static void explode(int x, int y); + static void free_bolt(int i); public: _myship(); static inline int get_x() diff --git a/screen.cpp b/screen.cpp index e16243a..f2c17f3 100644 --- a/screen.cpp +++ b/screen.cpp @@ -219,7 +219,7 @@ void _screen::init_highscores() { hi_sc[i] = 0; hi_st[i] = 0; - strcpy(hi_nm[i], "---"); + memcpy(hi_nm[i], "---", 4); } }