Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
port Creature_AIInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Apr 14, 2024
1 parent e66042a commit 1491013
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 12 deletions.
16 changes: 8 additions & 8 deletions docs/progress.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions docs/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ typedef struct __unaligned FX_INFO {

typedef struct __unaligned AI_INFO {
int16_t zone_num;
int16_t enemy_zone;
int16_t enemy_zone_num;
int32_t distance;
int32_t ahead;
int32_t bite;
Expand Down Expand Up @@ -1740,7 +1740,7 @@ typedef enum GAME_OBJECT_ID {
# game/box.c
0040E1B0 0000002F + void __cdecl Creature_Initialise(int16_t item_num);
0040E1E0 00000047 + int32_t __cdecl Creature_Activate(int16_t item_num);
0040E230 00000242 - void __cdecl Creature_AIInfo(struct ITEM_INFO *item, struct AI_INFO *info);
0040E230 00000242 + void __cdecl Creature_AIInfo(struct ITEM_INFO *item, struct AI_INFO *info);
0040E490 000001F3 - int32_t __cdecl Box_SearchLOT(struct LOT_INFO *lot, int32_t expansion);
0040E690 0000006F - int32_t __cdecl Box_UpdateLOT(struct LOT_INFO *lot, int32_t expansion);
0040E700 00000095 - void __cdecl Box_TargetBox(struct LOT_INFO *lot, int16_t box_num);
Expand Down Expand Up @@ -3250,3 +3250,6 @@ typedef enum GAME_OBJECT_ID {
00463150 - GUID g_IID_IDirectDrawSurface3;
004640A0 + struct BITE_INFO g_CrowBite;
00464090 + struct BITE_INFO g_BirdBite;
005263C0 - int16_t *g_FlyZone[2];
00526398 - int16_t *g_GroundZone[2];
005263A0 - int16_t *g_GroundZone2[2];
88 changes: 88 additions & 0 deletions src/game/creature.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#include "game/creature.h"

#include "game/math.h"
#include "game/random.h"
#include "global/const.h"
#include "global/funcs.h"
#include "global/vars.h"
#include "util.h"

#define FRONT_ARC PHD_90
#define BLOCKED 0x4000
#define BLOCKED_SEARCH 0x8000

void __cdecl Creature_Initialise(const int16_t item_num)
{
Expand All @@ -29,3 +35,85 @@ int32_t __cdecl Creature_Activate(const int16_t item_num)
item->status = IS_ACTIVE;
return true;
}

void __cdecl Creature_AIInfo(
struct ITEM_INFO *const item, struct AI_INFO *const info)
{
CREATURE_INFO *creature = (CREATURE_INFO *)item->data;
if (creature == NULL) {
return;
}

switch (item->object_num) {
case O_BANDIT_1:
case O_BANDIT_2:
Creature_GetBaddieTarget(creature->item_num, false);
break;

case O_MONK_1:
case O_MONK_2:
Creature_GetBaddieTarget(creature->item_num, true);
break;

default:
creature->enemy = g_LaraItem;
break;
}

struct ITEM_INFO *enemy = creature->enemy;
if (enemy == NULL) {
enemy = g_LaraItem;
}

int16_t *zone;
if (creature->lot.fly)
zone = g_FlyZone[g_FlipStatus];
else if (creature->lot.step == STEP_L) {
zone = g_GroundZone[g_FlipStatus];
} else {
zone = g_GroundZone2[g_FlipStatus];
}

{
const ROOM_INFO *const r = &g_Rooms[item->room_num];
const int32_t z_floor = (item->pos.z - r->pos.z) >> WALL_SHIFT;
const int32_t x_floor = (item->pos.x - r->pos.x) >> WALL_SHIFT;
item->box_num = r->floor[z_floor + r->x_size * x_floor].box;
info->zone_num = zone[item->box_num];
}

{
const ROOM_INFO *const r = &g_Rooms[enemy->room_num];
const int32_t z_floor = (enemy->pos.z - r->pos.z) >> WALL_SHIFT;
const int32_t x_floor = (enemy->pos.x - r->pos.x) >> WALL_SHIFT;
enemy->box_num = r->floor[z_floor + r->x_size * x_floor].box;
info->enemy_zone_num = zone[enemy->box_num];
}

if (((g_Boxes[enemy->box_num].overlap_index & creature->lot.block_mask)
!= 0)
|| (creature->lot.node[item->box_num].search_num
== (creature->lot.search_num | BLOCKED_SEARCH))) {
info->enemy_zone_num |= BLOCKED;
}

const struct OBJECT_INFO *const object = &g_Objects[item->object_num];
const int32_t z = enemy->pos.z
- ((object->pivot_length * Math_Cos(item->rot.y)) >> W2V_SHIFT)
- item->pos.z;
const int32_t x = enemy->pos.x
- ((object->pivot_length * Math_Sin(item->rot.y)) >> W2V_SHIFT)
- item->pos.x;
PHD_ANGLE angle = Math_Atan(z, x);
if (creature->enemy != NULL) {
info->distance = SQUARE(x) + SQUARE(z);
} else {
info->distance = 0x7FFFFFFF;
}

info->angle = angle - item->rot.y;
info->enemy_facing = angle - enemy->rot.y + PHD_180;
info->ahead = info->angle > -FRONT_ARC && info->angle < FRONT_ARC;
info->bite = info->ahead && enemy->hit_points > 0
&& ABS(enemy->pos.y - item->pos.y) <= STEP_L;
}
3 changes: 3 additions & 0 deletions src/game/creature.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include "global/types.h"

#include <stdint.h>

void __cdecl Creature_Initialise(int16_t item_num);
int32_t __cdecl Creature_Activate(int16_t item_num);
void __cdecl Creature_AIInfo(struct ITEM_INFO *item, struct AI_INFO *info);
1 change: 0 additions & 1 deletion src/global/funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define Boat_Animation ((void __cdecl (*)(struct ITEM_INFO *boat, int32_t collide))0x0040D950)
#define Boat_Control ((void __cdecl (*)(int16_t item_num))0x0040DAC0)
#define Gondola_Control ((void __cdecl (*)(int16_t item_num))0x0040E0F0)
#define Creature_AIInfo ((void __cdecl (*)(struct ITEM_INFO *item, struct AI_INFO *info))0x0040E230)
#define Box_SearchLOT ((int32_t __cdecl (*)(struct LOT_INFO *lot, int32_t expansion))0x0040E490)
#define Box_UpdateLOT ((int32_t __cdecl (*)(struct LOT_INFO *lot, int32_t expansion))0x0040E690)
#define Box_TargetBox ((void __cdecl (*)(struct LOT_INFO *lot, int16_t box_num))0x0040E700)
Expand Down
2 changes: 1 addition & 1 deletion src/global/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ typedef struct FX_INFO {

typedef struct AI_INFO {
int16_t zone_num;
int16_t enemy_zone;
int16_t enemy_zone_num;
int32_t distance;
int32_t ahead;
int32_t bite;
Expand Down
3 changes: 3 additions & 0 deletions src/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,8 @@
#define g_CinePos (*(struct PHD_3DPOS*)0x00526300)
#define g_CineFrameIdx (*(int16_t*)0x00526314)
#define g_Camera (*(struct CAMERA_INFO*)0x00526320)
#define g_GroundZone (*(int16_t *(*)[2])0x00526398)
#define g_GroundZone2 (*(int16_t *(*)[2])0x005263A0)
#define g_FlyZone (*(int16_t *(*)[2])0x005263C0)
#define g_Boxes (*(struct BOX_INFO **)0x005263CC)
// clang-format on
1 change: 1 addition & 0 deletions src/inject_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ static void Inject_Creature(void)
{
INJECT(1, 0x0040E1B0, Creature_Initialise);
INJECT(1, 0x0040E1E0, Creature_Activate);
INJECT(1, 0x0040E230, Creature_AIInfo);
}

static void Inject_Objects(void)
Expand Down

0 comments on commit 1491013

Please sign in to comment.