Files
top-octane/src/sim/ai_driver.cpp
T

166 lines
6.6 KiB
C++
Raw Normal View History

2026-11-30 22:31:51 +02:00
#include "sim/ai_driver.hpp"
#include <algorithm>
#include <cmath>
namespace top_octane::sim {
CraftInput compute_ai_input(CraftState& ai, const assets::LevelData& level,
const std::vector<CraftState>& all_crafts, int difficulty) {
CraftInput out;
if (ai.recovery_state != RecoveryState::Active || level.main_waypoints.empty()) {
return out;
}
int n_wp = static_cast<int>(level.main_waypoints.size());
int wp_idx = ai.ai_target_wp % n_wp;
const auto& cur_wp = level.main_waypoints[wp_idx];
Vec2 to_wp(cur_wp.x - ai.curr_pose.pos.x, cur_wp.z - ai.curr_pose.pos.z);
if (to_wp.len() < 5.0f) {
ai.ai_target_wp = (wp_idx + 1) % n_wp;
wp_idx = ai.ai_target_wp;
}
// Decide whether to detour briefly over a nearby recharge pad when low on fuel or shield
Vec2 tgt_pos(level.main_waypoints[wp_idx].x, level.main_waypoints[wp_idx].z);
if ((ai.fuel < ai.max_fuel * 0.25f || ai.shield < ai.max_shield * 0.28f) && !level.pads.empty()) {
for (const auto& pad : level.pads) {
Vec2 p(pad.tile_x() + 0.5f, pad.tile_z() + 0.5f);
float d2 = (p - Vec2(ai.curr_pose.pos.x, ai.curr_pose.pos.z)).len_sq();
if (d2 < 14.0f * 14.0f) {
tgt_pos = p;
ai.ai_seeking_pit = true;
break;
}
}
} else {
ai.ai_seeking_pit = false;
}
// Spread AI racing lines gently so racers overtake instead of forming a single line
if (!ai.ai_seeking_pit) {
const auto& nxt = level.main_waypoints[(wp_idx + 1) % n_wp];
Vec2 seg = Vec2(nxt.x - tgt_pos.x, nxt.z - tgt_pos.y).normalized();
Vec2 right(-seg.y, seg.x);
float lane = (static_cast<float>((ai.id % 5) - 2)) * 0.55f;
Vec2 shifted = tgt_pos + right * lane;
if (!level.is_cell_solid_at_y(static_cast<int>(std::floor(shifted.x)),
static_cast<int>(std::floor(shifted.y)),
ai.curr_pose.pos.y)) {
tgt_pos = shifted;
}
}
// Forward column-obstacle lookahead & lateral bypass lane selection
Vec2 cur_pos(ai.curr_pose.pos.x, ai.curr_pose.pos.z);
Vec2 to_tgt = tgt_pos - cur_pos;
float tgt_dist = to_tgt.len();
Vec2 base_dir = (tgt_dist > 1e-3f)
? (to_tgt * (1.0f / tgt_dist))
: Vec2(std::sin(ai.curr_pose.yaw), std::cos(ai.curr_pose.yaw));
float hit_dist = -1.0f;
float max_probe = std::min(tgt_dist, 7.5f);
for (float d = 1.0f; d <= max_probe; d += 0.75f) {
Vec2 p = cur_pos + base_dir * d;
if (level.is_cell_solid_at_y(static_cast<int>(std::floor(p.x)),
static_cast<int>(std::floor(p.y)),
ai.curr_pose.pos.y)) {
hit_dist = d;
break;
}
}
if (hit_dist > 0.0f) {
Vec2 perp(-base_dir.y, base_dir.x);
const float probe_ds[3] = {hit_dist * 0.5f, hit_dist, hit_dist + 2.0f};
const float offs[5] = {1.5f, 2.5f, 3.5f, 4.5f, 5.5f};
bool found_bypass = false;
float best_side = 1.0f;
float best_off = 3.5f;
for (float off : offs) {
for (float side : {-1.0f, 1.0f}) {
bool clear = true;
for (float pd : probe_ds) {
Vec2 p = cur_pos + base_dir * pd + perp * (side * off);
if (p.x < 2.0f || p.x > assets::kMapWidth - 3.0f ||
p.y < 2.0f || p.y > assets::kMapHeight - 3.0f ||
level.is_cell_solid_at_y(static_cast<int>(std::floor(p.x)),
static_cast<int>(std::floor(p.y)),
ai.curr_pose.pos.y)) {
clear = false;
break;
}
}
if (clear) {
best_side = side;
best_off = off;
found_bypass = true;
break;
}
}
if (found_bypass) break;
}
if (found_bypass) {
tgt_pos = cur_pos + base_dir * std::max(hit_dist, 2.5f) + perp * (best_side * (best_off + 0.6f));
}
}
// Craft avoidance repulsion
Vec2 avoid(0.0f, 0.0f);
for (const auto& other : all_crafts) {
if (other.id == ai.id || other.recovery_state != RecoveryState::Active) continue;
Vec2 d(ai.curr_pose.pos.x - other.curr_pose.pos.x, ai.curr_pose.pos.z - other.curr_pose.pos.z);
float dist_sq = d.len_sq();
if (dist_sq > 0.01f && dist_sq < 2.25f) {
avoid = avoid + d.normalized() * (1.2f / std::sqrt(dist_sq));
}
}
Vec2 aim = (tgt_pos - cur_pos).normalized() + avoid * 0.45f;
float desired_yaw = std::atan2(aim.x, aim.y);
float err = wrap_angle(desired_yaw - ai.curr_pose.yaw);
out.steer = std::clamp(-err * 1.85f, -1.0f, 1.0f);
float diff_pace = (difficulty == 0) ? 0.78f : (difficulty == 2 ? 1.0f : 0.90f);
float spd = ai.speed_horizontal();
bool is_corner = (level.main_waypoints[wp_idx].subtype == 1);
if (hit_dist > 0.0f && hit_dist < 1.6f && spd < 1.2f && ai.race_time > 1.5f) {
out.throttle = 0.0f;
out.brake = 0.65f;
} else if (std::abs(err) > 0.65f && spd > 9.5f) {
out.throttle = 0.25f * diff_pace;
out.brake = 0.35f;
} else if (is_corner && spd > 13.0f * diff_pace) {
out.throttle = 0.50f * diff_pace;
} else {
out.throttle = diff_pace;
if (difficulty >= 1 && std::abs(err) < 0.18f && ai.fuel > ai.max_fuel * 0.45f &&
(ai.id + static_cast<int>(ai.race_time)) % 3 == 0) {
out.boost = true;
}
}
// Combat AI: shoot opponents in sights!
if (ai.ammo > 5.0f) {
Vec3 fwd = ai.forward_dir();
for (const auto& other : all_crafts) {
if (other.id == ai.id || other.recovery_state != RecoveryState::Active) continue;
Vec3 to_e = other.curr_pose.pos - ai.curr_pose.pos;
float dist = to_e.len();
if (dist < 1.2f || dist > 22.0f) continue;
float cos_ang = (to_e / dist).dot(fwd);
if (cos_ang > 0.93f && difficulty >= 0) {
out.fire_minigun = (difficulty == 2 || (static_cast<int>(ai.race_time * 10.0f) % 2 == 0));
}
}
if (ai.lock_progress >= 1.0f && difficulty >= 1 && ai.ammo >= 20.0f) {
out.fire_missile = true;
}
}
return out;
}
} // namespace top_octane::sim