Skip to content

Commit

Permalink
add thing ai struct
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed May 23, 2024
1 parent bfa23e9 commit e230734
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 160 deletions.
4 changes: 4 additions & 0 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ void command_fini(void)
TRACE_NO_INDENT();
if (command_inited) {
command_inited = false;
for (auto iter : commands_map) {
auto command = iter.second;
delete command;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void Game::state_change(uint8_t new_state, const std::string &why)
//
// Why oh why change state
//
CON("INF: Game state change: %s -> %s, reason %s", gama_state_to_string(old_state).c_str(),
LOG("INF: Game state change: %s -> %s, reason %s", gama_state_to_string(old_state).c_str(),
gama_state_to_string(new_state).c_str(), why.c_str());
TRACE_AND_INDENT();

Expand Down
2 changes: 1 addition & 1 deletion src/game_load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ void game_load_last_config(const char *appdata)
{
TRACE_NO_INDENT();

CON("INI: Load config");
LOG("INI: Load config");

game = new Game(std::string(appdata));
auto config_error = game->load_config();
Expand Down
10 changes: 5 additions & 5 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,19 +309,19 @@ void level_map_set(Levelp l, int z, const char *in)

if (need_floor) {
auto tp_add = tp_floor;
auto t = level_thing_init(l, tp_add, x, y, z);
auto t = thing_init(l, tp_add, x, y, z);
if (t) {
level_thing_push(l, t);
thing_push(l, t);
}
}

if (! tp) {
continue;
}

auto t = level_thing_init(l, tp, x, y, z);
auto t = thing_init(l, tp, x, y, z);
if (t) {
level_thing_push(l, t);
thing_push(l, t);
}
}
}
Expand All @@ -341,7 +341,7 @@ bool level_is_same_type(Levelp l, int x, int y, int z, Tpp tp)

for (auto slot = 0; slot < MAP_SLOTS; slot++) {
Tpp it_tp;
Thingp it = level_thing_and_tp_get(l, x, y, z, slot, &it_tp);
Thingp it = thing_and_tp_get(l, x, y, z, slot, &it_tp);
if (! it) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/level_anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void level_anim(Levelp l)
//
// What level is the player on?
//
auto player = level_thing_player(l);
auto player = thing_player(l);
if (! player) {
return;
}
Expand All @@ -37,7 +37,7 @@ void level_anim(Levelp l)
for (auto y = l->miny; y < l->maxy; y++) {
for (auto x = l->minx; x < l->maxx; x++) {
Tpp tp;
Thingp t = level_thing_and_tp_get(l, x, y, z, slot, &tp);
Thingp t = thing_and_tp_get(l, x, y, z, slot, &tp);
if (! t) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/level_cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ static std::vector< point > level_cursor_path_draw_line(Levelp l, point start, p
{
static std::vector< point > empty;

auto player = level_thing_player(l);
auto player = thing_player(l);
if (! player) {
return empty;
}
Expand Down Expand Up @@ -245,7 +245,7 @@ void level_cursor_update(Levelp l)
//
memset(l->cursor, 0, sizeof(l->cursor));

auto player = level_thing_player(l);
auto player = thing_player(l);
if (! player) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/level_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static void level_display_cursor(Levelp l, int x, int y)
static void level_display_slot(Levelp l, int x, int y, int z, int slot, int depth, bool deco)
{
Tpp tp;
auto t = level_thing_and_tp_get(l, x, y, z, slot, &tp);
auto t = thing_and_tp_get(l, x, y, z, slot, &tp);
if (! tp) {
return;
}
Expand All @@ -187,7 +187,7 @@ void level_display(Levelp l)
//
// What level is the player on?
//
auto player = level_thing_player(l);
auto player = thing_player(l);
if (! player) {
return;
}
Expand Down
38 changes: 19 additions & 19 deletions src/level_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "my_level.hpp"
#include "my_tp.hpp"

Thingp level_thing_player(Levelp l)
Thingp thing_player(Levelp l)
{
TRACE_NO_INDENT();

Expand All @@ -19,10 +19,10 @@ Thingp level_thing_player(Levelp l)
return nullptr;
}

return level_thing_find(l, l->player);
return thing_find(l, l->player);
}

void level_thing_player_move_delta(Levelp l, int dx, int dy, int dz)
void thing_player_move_delta(Levelp l, int dx, int dy, int dz)
{
TRACE_NO_INDENT();

Expand All @@ -33,24 +33,24 @@ void level_thing_player_move_delta(Levelp l, int dx, int dy, int dz)
return;
}

auto t = level_thing_player(l);
auto t = thing_player(l);
if (! t) {
return;
}

if (level_thing_can_move_to(l, t, t->x + dx, t->y + dy, t->z + dz)) {
level_thing_move(l, t, t->x + dx, t->y + dy, t->z + dz);
if (thing_can_move_to(l, t, t->x + dx, t->y + dy, t->z + dz)) {
thing_move(l, t, t->x + dx, t->y + dy, t->z + dz);

level_tick_begin_requested(l, "player moved");
}

level_thing_player_move_reset(l);
thing_player_move_reset(l);
}

//
// All keys have been released, forget any accumulation of events
//
void level_thing_player_move_reset(Levelp l)
void thing_player_move_reset(Levelp l)
{
l->requested_move_up = false;
l->requested_move_left = false;
Expand All @@ -61,7 +61,7 @@ void level_thing_player_move_reset(Levelp l)
//
// Allow moves to accumulate so we can do diagonal moves.
//
void level_thing_player_move_accum(Levelp l, bool up, bool down, bool left, bool right)
void thing_player_move_accum(Levelp l, bool up, bool down, bool left, bool right)
{
if (up) {
l->requested_move_up = up;
Expand All @@ -83,9 +83,9 @@ void level_thing_player_move_accum(Levelp l, bool up, bool down, bool left, bool
//
// Attempt to move
//
bool level_thing_player_move_request(Levelp l, bool up, bool down, bool left, bool right)
bool thing_player_move_request(Levelp l, bool up, bool down, bool left, bool right)
{
level_thing_player_move_accum(l, up, down, left, right);
thing_player_move_accum(l, up, down, left, right);

//
// If a move is in progress, do nothing
Expand All @@ -99,24 +99,24 @@ bool level_thing_player_move_request(Levelp l, bool up, bool down, bool left, bo

if (l->requested_move_up) {
if (l->requested_move_keft) {
level_thing_player_move_delta(l, -1, -1, 0);
thing_player_move_delta(l, -1, -1, 0);
} else if (l->requested_move_right) {
level_thing_player_move_delta(l, 1, -1, 0);
thing_player_move_delta(l, 1, -1, 0);
} else {
level_thing_player_move_delta(l, 0, -1, 0);
thing_player_move_delta(l, 0, -1, 0);
}
} else if (l->requested_move_left) {
if (l->requested_move_keft) {
level_thing_player_move_delta(l, -1, 1, 0);
thing_player_move_delta(l, -1, 1, 0);
} else if (l->requested_move_right) {
level_thing_player_move_delta(l, 1, 1, 0);
thing_player_move_delta(l, 1, 1, 0);
} else {
level_thing_player_move_delta(l, 0, 1, 0);
thing_player_move_delta(l, 0, 1, 0);
}
} else if (l->requested_move_keft) {
level_thing_player_move_delta(l, -1, 0, 0);
thing_player_move_delta(l, -1, 0, 0);
} else if (l->requested_move_right) {
level_thing_player_move_delta(l, 1, 0, 0);
thing_player_move_delta(l, 1, 0, 0);
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/level_scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void level_scroll_to_player(Levelp l)
{
TRACE_NO_INDENT();

auto player = level_thing_player(l);
auto player = thing_player(l);
if (! player) {
return;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ void level_scroll_warp_to_player(Levelp l)
{
TRACE_NO_INDENT();

auto t = level_thing_player(l);
auto t = thing_player(l);
if (! t) {
return;
}
Expand Down
45 changes: 23 additions & 22 deletions src/level_thing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@
#include <sstream>
#include <string.h>

Tpp tp(Thingp t)
{
if (t->tp_id) {
return tp_find(t->tp_id);
}

return nullptr;
}

Thingp level_thing_get(Levelp l, int x, int y, int z, int slot)
Thingp thing_get(Levelp l, int x, int y, int z, int slot)
{
TRACE_NO_INDENT();

Expand All @@ -33,15 +24,15 @@ Thingp level_thing_get(Levelp l, int x, int y, int z, int slot)
return nullptr;
}

auto t = level_thing_find(l, id);
auto t = thing_find(l, id);
if (! t) {
return nullptr;
}

return t;
}

Thingp level_thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out)
Thingp thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out)
{
TRACE_NO_INDENT();

Expand All @@ -55,7 +46,7 @@ Thingp level_thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out)
return nullptr;
}

auto t = level_thing_find(l, id);
auto t = thing_find(l, id);
if (! t) {
return nullptr;
}
Expand All @@ -67,8 +58,10 @@ Thingp level_thing_and_tp_get(Levelp l, int x, int y, int z, int slot, Tpp *out)
return t;
}

Thingp level_thing_find_optional(Level *l, ThingId id)
Thingp thing_find_optional(Level *l, ThingId id)
{
TRACE_NO_INDENT();

if (! id) {
return nullptr;
}
Expand All @@ -85,8 +78,10 @@ Thingp level_thing_find_optional(Level *l, ThingId id)
return nullptr;
}

Thingp level_thing_find(Levelp l, ThingId id)
Thingp thing_find(Levelp l, ThingId id)
{
TRACE_NO_INDENT();

auto thing_id = id;
auto index = THING_COMMON_ID_GET(thing_id);

Expand All @@ -104,7 +99,7 @@ Thingp level_thing_find(Levelp l, ThingId id)
return t;
}

Thingp level_thing_new(Levelp l, Tpp tp, int x, int y, int z)
Thingp thing_new(Levelp l, Tpp tp, int x, int y, int z)
{
TRACE_NO_INDENT();

Expand All @@ -128,27 +123,33 @@ Thingp level_thing_new(Levelp l, Tpp tp, int x, int y, int z)
t->id = thing_id;
t->tp_id = tp_id_get(tp);

if (thing_is_monst(t) || thing_is_player(t)) {
thing_ai_new(l, t);
}

return t;
}

DIE("out of things");
}

void level_thing_free(Levelp l, Thingp t)
void thing_free(Levelp l, Thingp t)
{
TRACE_NO_INDENT();

auto o = level_thing_find(l, t->id);
auto o = thing_find(l, t->id);
if (t != o) {
DIE("Thing mismatch found for id, %" PRIX32 "", t->id);
}

thing_ai_free(l, t);

memset(t, 0, sizeof(*t));
}

ThingAip level_thing_ai_new(Levelp l, Thingp t)
ThingAip thing_ai_new(Levelp l, Thingp t)
{
TRACE_AND_INDENT();
TRACE_NO_INDENT();

static ThingAiId last_index;

Expand All @@ -173,9 +174,9 @@ ThingAip level_thing_ai_new(Levelp l, Thingp t)
return 0;
}

void level_thing_ai_free(Levelp l, Thingp t)
void thing_ai_free(Levelp l, Thingp t)
{
TRACE_AND_INDENT();
TRACE_NO_INDENT();

auto ai_id = t->ai_id;
if (! ai_id) {
Expand Down
4 changes: 2 additions & 2 deletions src/level_tick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void level_tick_body(Levelp l, float dt)
{
TRACE_NO_INDENT();

auto p = level_thing_player(l);
auto p = thing_player(l);
int player_speed = p ? p->speed : 100;

FOR_ALL_THINGS(l, t)
Expand All @@ -89,7 +89,7 @@ void level_tick_body(Levelp l, float dt)
t->thing_dt = 1.0;
}

level_thing_interpolate(l, t, t->thing_dt);
thing_interpolate(l, t, t->thing_dt);

if (t->thing_dt >= 1.0) {
// thing tick
Expand Down
2 changes: 1 addition & 1 deletion src/level_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void level_assign_tiles(Levelp l, int z)
for (auto y = 0; y < MAP_HEIGHT; y++) {
for (auto x = 0; x < MAP_WIDTH; x++) {
Tpp tp;
auto t = level_thing_and_tp_get(l, x, y, z, slot, &tp);
auto t = thing_and_tp_get(l, x, y, z, slot, &tp);
if (! t) {
continue;
}
Expand Down
Loading

0 comments on commit e230734

Please sign in to comment.