Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1] aiGoalFollowWayPts::Update() #137

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions code/midtown/mmai/aiGoalFollowWayPts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ define_dummy_symbol(mmai_aiGoalFollowWayPts);

#include "aiData.h"
#include "aiMap.h"
#include "aiPath.h"
#include "aiVehicleOpponent.h"

b32 aiGoalFollowWayPts::Context()
{
Expand Down Expand Up @@ -55,3 +57,127 @@ b32 aiGoalFollowWayPts::Context()
return false;
}
}

i32 aiGoalFollowWayPts::GetWayPtId(i16 index)
{
if (index < 0 || index > NumWayPts)
{
Warningf("Check Point Index: %d, is outside of the array bounds.", index);
Warningf("Requested by: Opp %d.", Vehicle->OppId);
return WayPtIds[NumWayPts];
}

return WayPtIds[index];
}

void aiGoalFollowWayPts::Update()
{
++UpdateCount;

if ((Car->Sim.ICS.Constraints & (ICS_CONSTRAIN_TX | ICS_CONSTRAIN_TZ)) != 0)
{
if (Car->Sim.FrontLeft.OnGround)
{
Car->Sim.Steering = 0.0f;
Car->Sim.Brakes = 0.0f;
Car->Sim.Engine.Throttle = 1.0f;
}

return;
}

Stuck.Update();

if (Car->Sim.Stuck.State == CAR_STUCK)
{
PlanRoute();
*BackingUp = true;
Car->Sim.ICS.LinearMomentum = {0.0f, 0.0f, 0.0f};
Car->Sim.ICS.AngularMomentum = {0.0f, 0.0f, 0.0f};
UpdateCount = 0;
}
else if (Stuck.State == CAR_STUCK)
{
Car->Sim.Steering = 0.5f;
Car->Sim.Brakes = 0.0f;
Car->Sim.Engine.Throttle = 1.0f;
Car->Sim.Stuck.State = CAR_NOT_STUCK;
}
else
{
i16 road_segment_id;

if (WayPtIdx <= 1 || WayPtIdx >= NumWayPts - 1)
{
road_segment_id = -1;
}
else
{
i16 prev_wp_index = WayPtIdx - 1;

i32 prev_wp_id = GetWayPtId(prev_wp_index);
i32 cur_wp_id = GetWayPtId(WayPtIdx);

road_segment_id = DetRdSegBetweenInts(AIMAP.Intersection(prev_wp_id), AIMAP.Intersection(cur_wp_id))->Id;
}

LastMapCompType = road_segment_id;

i16 out_index = 0;
f32 dist_to_side = 1.0f;

CurMapCompIdx = static_cast<i16>(
AIMAP.DetermineOppMapComponent(Car->Sim.ICS.Matrix, Rail, &CurMapCompType, &CurRdVertIdx, &Rail->RoadDist,
&DistToSide, &out_index, &TargetPtOffset, Car->Sim.Speed, LastMapCompType, road_segment_id));

PlanRoute();

if (Rail->NextLink)
{
Rail->NextLink->StopDestinationSources(1);
}

if (Vehicle->IsSemi || !DetectCollision(reinterpret_cast<i32*>(&dist_to_side)))
{
aiVehicleOpponent* opp = DetectOpponentCollision();

if (opp)
{
AvoidOpponentCollision(opp);
}
else
{
SolveTargetPoint();
}
}
else
{
AvoidCollision(reinterpret_cast<i32>(&dist_to_side));
}

Vector3 target_dir = TargetPt - Car->Sim.ICS.Matrix.m3;

f32 angle = std::atan2(target_dir ^ Car->Sim.ICS.Matrix.m0, target_dir ^ -Car->Sim.ICS.Matrix.m2);

Steering = std::clamp(angle, -1.0f, 1.0f);

if (Car->Model.Flags & INST_FLAG_COLLIDED_PLAYER)
{
*Car->Sim.Realism = 1.0f;
}
else
{
*Car->Sim.Realism = 0.0f;

if (angle < 0.1f && angle > -0.1f)
{
Car->Sim.ICS.AngularMomentum *= 0.1f;
}
}

Car->Sim.Steering = Steering;
Car->Sim.Brakes = Brakes;
Car->Sim.Engine.Throttle = Throttle;
LastMapCompType = CurMapCompType;
}
}
7 changes: 5 additions & 2 deletions code/midtown/mmai/aiGoalFollowWayPts.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class aiGoalFollowWayPts final : public aiGoal
ARTS_IMPORT void Reset() override;

// ?Update@aiGoalFollowWayPts@@UAEXXZ
ARTS_IMPORT void Update() override;
ARTS_EXPORT void Update() override;

// ?DeclareFields@aiGoalFollowWayPts@@SAXXZ
ARTS_IMPORT static void DeclareFields();
Expand All @@ -106,7 +106,7 @@ class aiGoalFollowWayPts final : public aiGoal
ARTS_IMPORT void AddToBlockedRange(f32 arg1, f32 arg2, f32 arg3);

// ?AvoidCollision@aiGoalFollowWayPts@@AAEXH@Z
ARTS_IMPORT void AvoidCollision(i32 arg1);
ARTS_IMPORT void AvoidCollision(i32 dist_to_side);

// ?AvoidOpponentCollision@aiGoalFollowWayPts@@AAEXPAVaiVehicleOpponent@@@Z
ARTS_IMPORT void AvoidOpponentCollision(aiVehicleOpponent* arg1);
Expand Down Expand Up @@ -135,6 +135,9 @@ class aiGoalFollowWayPts final : public aiGoal
// ?SolveTargetPoint@aiGoalFollowWayPts@@AAEXXZ
ARTS_IMPORT void SolveTargetPoint();

i32 GetWayPtId(i16 index);

public:
aiVehicleOpponent* Vehicle;
aiRailSet* Rail;
mmCar* Car;
Expand Down
14 changes: 13 additions & 1 deletion code/midtown/mmai/aiStuck.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/

#include "arts7/node.h"
#include "vector7/vector3.h"

class mmCarSim;

Expand Down Expand Up @@ -69,7 +70,18 @@ class aiStuck final : public asNode
// ?Update@aiStuck@@UAEXXZ
ARTS_IMPORT void Update() override;

u8 gap20[0x38];
i32 State;
i32 Impacted;
i32 field_28;
Vector3 field_2C;
i32 TimeThresh;
i32 PosThresh;
i32 MoveThresh;
i32 PosThreshSqr;
i32 MoveThreshSqr;
i32 RotAmount;
i32* ICS;
mmCarSim* CarSim;
};

check_size(aiStuck, 0x58);
6 changes: 5 additions & 1 deletion code/midtown/mmai/aiVehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
0x61BC40 | const aiVehicle::`vftable' | ??_7aiVehicle@@6B@
*/

#include "data7/list.h"

class aiVehicle
{
public:
Expand All @@ -51,7 +53,9 @@ class aiVehicle
// ?Init@aiVehicle@@QAEXH@Z
ARTS_IMPORT void Init(i32 arg1);

u8 gap4[0x10];
List Goals;
i16 OppId;
i16 UpdateGroup;
};

check_size(aiVehicle, 0x14);
4 changes: 4 additions & 0 deletions code/midtown/mmcar/stuck.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
class mmCarSim;
class asInertialCS;

#define CAR_NOT_STUCK 0
#define CAR_MAYBE_STUCK 1
#define CAR_STUCK 2

class mmStuck final : public asNode
{
public:
Expand Down
2 changes: 1 addition & 1 deletion code/midtown/mmcity/inst.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class mmInstance : public Base
#define INST_FLAG_TERRAIN 0x800
#define INST_FLAG_1000 0x1000 // mmBangerInstance::Draw - Increment lod
#define INST_FLAG_2000 0x2000 // Important shadows
#define INST_FLAG_4000 0x4000 // Collided with Player?
#define INST_FLAG_COLLIDED_PLAYER 0x4000

// INST_FLAG_*
u16 Flags {INST_FLAG_ACTIVE};
Expand Down