2026-03-08 16:55:49 +01:00
|
|
|
using FNAF_Server.Map;
|
|
|
|
|
using GlobalClassLib;
|
|
|
|
|
using PacketLib;
|
|
|
|
|
|
|
|
|
|
namespace FNAF_Server.Enemies;
|
|
|
|
|
|
|
|
|
|
public class NekoEnemy : Enemy {
|
|
|
|
|
private MovementOpportunity movementOpportunity;
|
|
|
|
|
private NekoPathfinder pathfinder;
|
|
|
|
|
|
|
|
|
|
public NekoEnemy(int difficulty) : base(difficulty) {
|
|
|
|
|
pathfinder = new NekoPathfinder(this, 1);
|
2026-03-17 20:14:29 +01:00
|
|
|
movementOpportunity = new MovementOpportunity(5000);
|
|
|
|
|
SetDifficulty(difficulty);
|
|
|
|
|
|
2026-03-08 16:55:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name{ get; } = "Neko";
|
2026-03-17 20:14:29 +01:00
|
|
|
public override int TypeId{ get; } = (int)EnemyType.NEKO;
|
2026-03-08 16:55:49 +01:00
|
|
|
public override bool BlocksTile{ get; set; } = true;
|
2026-03-17 20:14:29 +01:00
|
|
|
|
|
|
|
|
public bool Aggressive = false;
|
|
|
|
|
|
|
|
|
|
public ServerPlayer? Target{ get; set; }
|
2026-03-08 16:55:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void Spawn(MapTile location) {
|
|
|
|
|
base.Spawn(location);
|
|
|
|
|
// stopwatch.Start();
|
|
|
|
|
movementOpportunity.Start();
|
|
|
|
|
pathfinder.takenPath.Add(Location);
|
|
|
|
|
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Reset() {
|
|
|
|
|
pathfinder.takenPath.Clear();
|
|
|
|
|
MapTile[] resetLocations = new[]{MapManager.Get(7), MapManager.Get(12), MapManager.Get(17)}.Where(t => EnemyManager.GetByLocation(t).Length == 0).ToArray();
|
|
|
|
|
Location = resetLocations[new Random().Next(resetLocations.Length)];
|
2026-03-17 20:14:29 +01:00
|
|
|
Aggressive = false;
|
2026-03-08 16:55:49 +01:00
|
|
|
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 20:14:29 +01:00
|
|
|
public override void SetDifficulty(int difficulty) {
|
|
|
|
|
Difficulty = difficulty;
|
|
|
|
|
movementOpportunity.MovementChance =
|
|
|
|
|
((2 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (2 + Math.Pow(1.5f, 10));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 16:55:49 +01:00
|
|
|
public override void Update() {
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2026-03-17 20:14:29 +01:00
|
|
|
if (Location.Owner != null && Location.Lit){
|
|
|
|
|
Aggressive = true;
|
|
|
|
|
Server.SendUpdateToAll([GameEvent.NEKO_ANGERED(Location.Owner.state.pid, Id)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (movementOpportunity.CheckAndRoll() || (Aggressive && GameLogic.NSecondUpdate)){
|
|
|
|
|
if (Target == null){
|
|
|
|
|
if (Server.P1.state.power > Server.P2.state.power){
|
|
|
|
|
Target = Server.P2;
|
|
|
|
|
}
|
|
|
|
|
else if (Server.P1.state.power < Server.P2.state.power){
|
|
|
|
|
Target = Server.P1;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
|
2026-03-08 16:55:49 +01:00
|
|
|
switch (decision.type){
|
|
|
|
|
case Pathfinder.Decision.MoveType:
|
|
|
|
|
Location = decision.nextTile!;
|
|
|
|
|
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id)]);
|
|
|
|
|
Console.WriteLine($"Enemy {TypeId} ({Name}) moving to {Location.PositionAsString})");
|
|
|
|
|
break;
|
|
|
|
|
case Pathfinder.Decision.AttackType:
|
2026-03-09 20:05:21 +01:00
|
|
|
Attack(decision.attackTarget!);
|
|
|
|
|
break;
|
2026-03-08 16:55:49 +01:00
|
|
|
case Pathfinder.Decision.ResetType:
|
|
|
|
|
Reset();
|
|
|
|
|
break;
|
|
|
|
|
case Pathfinder.Decision.WaitType:
|
2026-03-17 20:14:29 +01:00
|
|
|
Aggressive = false;
|
2026-03-08 16:55:49 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NekoPathfinder : RoamingPathfinder {
|
|
|
|
|
private Random random = new();
|
|
|
|
|
|
|
|
|
|
private int tolerance;
|
|
|
|
|
public List<MapTile> takenPath = new();
|
|
|
|
|
|
|
|
|
|
public NekoPathfinder(Enemy enemy, int tolerance) : base(enemy) {
|
|
|
|
|
this.tolerance = tolerance;
|
2026-03-17 20:14:29 +01:00
|
|
|
AdditionalTileFilter = t => ((NekoEnemy)Enemy).Aggressive || t.Owner == null || !t.Lit;
|
2026-03-08 16:55:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Decision DecideNext(MapTile goal) {
|
|
|
|
|
Dictionary<MapTile, int> distances = Crawl(goal);
|
|
|
|
|
|
|
|
|
|
List<MapTile> closerTiles = GetNeighbours(Enemy.Location,
|
|
|
|
|
c => !c.Blocked || c.Type == ConnectorType.DOOR_OFFICE,
|
2026-03-17 20:14:29 +01:00
|
|
|
t =>
|
|
|
|
|
distances.ContainsKey(t) &&
|
|
|
|
|
distances[t] <= distances[Enemy.Location] + tolerance);
|
2026-03-08 16:55:49 +01:00
|
|
|
if (closerTiles.Contains(goal)){
|
|
|
|
|
if (Enemy.Location.GetConnector(goal)!.Blocked){
|
|
|
|
|
return Decision.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 20:14:29 +01:00
|
|
|
return Decision.Attack(((NekoEnemy)Enemy).Target);
|
2026-03-08 16:55:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (closerTiles.Count != 0 && closerTiles.All(t => takenPath.Contains(t))){
|
|
|
|
|
takenPath.Clear();
|
|
|
|
|
return DecideNext(goal);
|
|
|
|
|
}
|
|
|
|
|
closerTiles.RemoveAll(t => takenPath.Contains(t));
|
|
|
|
|
|
|
|
|
|
closerTiles.ForEach(t => distances[t] += Enemy.Location.GetConnector(t)!.Value);
|
|
|
|
|
double roll = random.NextDouble() * closerTiles.Sum(t => 1.0 / distances[t]);
|
|
|
|
|
foreach (var tile in closerTiles){
|
|
|
|
|
if (roll <= 1.0 / distances[tile]){
|
|
|
|
|
takenPath.Add(tile);
|
|
|
|
|
return Decision.Move(tile);
|
|
|
|
|
}
|
|
|
|
|
roll -= 1.0 / distances[tile];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Decision.Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|