movement: generalize; use vectors #16

Merged
NotAShelf merged 6 commits from SquirrelModeler/rogged:generalize-movement into main 2026-04-09 14:11:47 +00:00
Contributor

Generalized movement, so that all entities move the same.

Generalized movement, so that all entities move the same.
NotAShelf changed title from refactor(movement): generalize movement and use vectors to movement: generalize; use vectors 2026-04-09 12:46:31 +00:00
NotAShelf requested changes 2026-04-09 13:02:10 +00:00
Dismissed
NotAShelf left a comment
Owner

Two minor comments. The former is a nitpick but I'd still like for it to be gotten out of our way since it's a trivial change

Two minor comments. The former is a nitpick but I'd still like for it to be gotten out of our way since it's a trivial change
src/main.c Outdated
@ -444,0 +448,4 @@
if (result == MOVE_RESULT_MOVED) {
player_on_move(&gs->player);
action = 1;
} else if (result == MOVE_RESULT_BLOCKED_ENEMY) {
Owner

You should probably guard against potential null target here before attacking.

Because if try_move_entity returns MOVE_RESULT_BLOCKED_ENEMY but player_find_enemy_at fails to find the enemy (be it due to differing lookup logic, or if the enemy was killed by effects earlier in the turn), target would be NULL, and thus lead to an ub in player_attack. Something like:

   } else if (result == MOVE_RESULT_BLOCKED_ENEMY) {
     target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y);
-    player_attack(&gs->player, target);
-    action = 1;
+    if (target != NULL) {
+      player_attack(&gs->player, target);
+      action = 1;
+    }
   }
You should probably guard against potential null target here before attacking. Because if `try_move_entity` returns `MOVE_RESULT_BLOCKED_ENEMY` but `player_find_enemy_at` fails to find the enemy (be it due to differing lookup logic, or if the enemy was killed by effects earlier in the turn), target would be `NULL`, and thus lead to an ub in `player_attack`. Something like: ```diff } else if (result == MOVE_RESULT_BLOCKED_ENEMY) { target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y); - player_attack(&gs->player, target); - action = 1; + if (target != NULL) { + player_attack(&gs->player, target); + action = 1; + } } ```
Author
Contributor

If we move the action = 1 into the null guarded if statement, then enemy AI breaks. We can do:

if (target != NULL) {
  player_attack(&gs->player, target);
}
action = 1;

Which fixes that bug.

Ignore this. I added an incorrect guard.

~~If we move the action = 1 into the null guarded if statement, then enemy AI breaks. We can do:~~ ``` if (target != NULL) { player_attack(&gs->player, target); } action = 1; ``` ~~Which fixes that bug.~~ Ignore this. I added an incorrect guard.
SquirrelModeler marked this conversation as resolved
src/player.h Outdated
@ -8,3 +8,2 @@
// Move player to (x+dx, y+dy); returns 1 if moved, 0 if blocked
int player_move(Player *p, int dx, int dy, Map *map);
// Apply status effect, healing ect
Owner

Typo, should be "healing, etc."

Typo, should be "healing, etc."
SquirrelModeler marked this conversation as resolved
SquirrelModeler changed title from movement: generalize; use vectors to WIP: movement: generalize; use vectors 2026-04-09 13:25:55 +00:00
Author
Contributor

Currently there is a bug with healing. Putting this into WIP until I've fixed it.

Currently there is a bug with healing. Putting this into WIP until I've fixed it.
Author
Contributor

I am stupid. I added an incorrect guard. Fixing it now.

I am stupid. I added an incorrect guard. Fixing it now.
I am stupid. I added the guard on the wrong check. Ffs...
SquirrelModeler changed title from WIP: movement: generalize; use vectors to movement: generalize; use vectors 2026-04-09 13:30:10 +00:00
NotAShelf approved these changes 2026-04-09 14:11:34 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
NotAShelf/rogged!16
No description provided.