První 3 monstra z plánovaných pěti. Kompletní pathfinding i zrcadlení do clienta. Útoky implementované nejsou. Lurk a Neko jsou hardcoded aby útočili na P1.

This commit is contained in:
Perry 2026-03-08 16:55:49 +01:00
parent 4484b127c5
commit 9bfe63a166
27 changed files with 772 additions and 47 deletions

View file

@ -0,0 +1,26 @@
using FNAF_Server.Map;
using GlobalClassLib;
namespace FNAF_Server.Enemies;
public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
protected Enemy(int difficulty) : base(difficulty) {
}
public abstract bool BlocksTile { get; set; }
public bool Spawned { get; set; }
public virtual void SpawnSilent(MapTile location) {
Console.WriteLine($"!!! Silent spawn not implemented for enemy {TypeId} ({Name}), reverting to regular spawn");
Spawn(location);
}
public override void Spawn(MapTile location) {
base.Spawn(location);
Spawned = true;
// EnemyManager.AddEnemy(this);
}
public abstract void Reset();
public abstract void Attack(ServerPlayer player);
}

View file

@ -0,0 +1,29 @@
using FNAF_Server.Map;
namespace FNAF_Server.Enemies;
public class EnemyManager {
private static Dictionary<int, Enemy> enemies = new();
public static void Update() {
foreach (var pair in enemies){
if (pair.Value.Spawned) pair.Value.Update();
}
}
public static Enemy AddEnemy(Enemy enemy) {
enemies.Add(enemy.Id, enemy);
return enemy;
}
public static Enemy[] GetByLocation(MapTile tile) {
List<Enemy> output = new();
foreach (var e in enemies.Values){
if (e.Location == tile){
output.Add(e);
}
}
return output.ToArray();
}
}

View file

@ -0,0 +1,118 @@
using System.Diagnostics;
using System.Reflection;
using FNAF_Server.Map;
using GlobalClassLib;
using PacketLib;
namespace FNAF_Server.Enemies;
public class LurkEnemy : Enemy {
public override string Name{ get; } = "Lurk";
public override int TypeId{ get; } = (int)EnemyType.LURK;
public override bool BlocksTile{ get; set; } = true;
private LurkPathfinder pathfinder;
// private int movementRollInterval = 5000;
// private static readonly double CHANCE_DENOMINATOR = 2 + Math.Pow(1.5f, 10); // d10 Lurk will have a 100% chance to move
// private double movementChance => (2 + Math.Pow(1.5f, Difficulty)) / CHANCE_DENOMINATOR; // chance scales exponentially using a constant multiplier
private MovementOpportunity movementOpportunity;
public LurkEnemy(int difficulty) : base(difficulty) {
pathfinder = new LurkPathfinder(this, 1);
}
public override void Spawn(MapTile location) {
base.Spawn(location);
// stopwatch.Start();
movementOpportunity = new MovementOpportunity(5000, ((2 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (2 + Math.Pow(1.5f, 10)));
movementOpportunity.Start();
pathfinder.takenPath.Add(Location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
}
public override void Reset() {
pathfinder.takenPath.Clear();
pathfinder.takenPath.Add(Location);
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)];
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
}
public override void Attack(ServerPlayer player) {
throw new NotImplementedException();
}
public override void Update() {
base.Update();
if (movementOpportunity.CheckAndRoll()){
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(10));
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:
throw new NotImplementedException();
case Pathfinder.Decision.ResetType:
Reset();
break;
case Pathfinder.Decision.WaitType:
break;
}
}
}
private class LurkPathfinder : RoamingPathfinder {
// public override List<MapTile> FindPath(MapTile start, MapTile end) {
// throw new NotImplementedException();
// }
private Random random = new();
private int tolerance;
public List<MapTile> takenPath = new();
public LurkPathfinder(Enemy enemy, int tolerance) : base(enemy) {
this.tolerance = tolerance;
}
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,
t => distances.ContainsKey(t) && distances[t] <= distances[Enemy.Location] + tolerance);
if (closerTiles.Contains(goal)){
if (Enemy.Location.GetConnector(goal)!.Blocked){
return Decision.Reset();
}
return Decision.Attack(Server.P1); // TODO: un-hardcode this
}
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();
}
}
}

View file

@ -0,0 +1,60 @@
using System.Diagnostics;
namespace FNAF_Server.Enemies;
public class MovementOpportunity {
public int Interval{ get; set; }
// public double ChanceDenominator;
public double MovementChance{ get; set; }
public bool Running => stopwatch.IsRunning;
public bool ConstantChance{ get; private set; }
private Stopwatch stopwatch = new();
private int stopwatchOffset = 0;
private Random random = new();
public MovementOpportunity(int intervalMs, double movementChance) {
Interval = intervalMs;
MovementChance = movementChance;
GuaranteeSuccess(false);
ConstantChance = true; // unused
}
public MovementOpportunity(int intervalMs) {
Interval = intervalMs;
GuaranteeSuccess(true);
MovementChance = 1;
}
public void Start() {
stopwatch.Start();
}
public void Stop() {
stopwatch.Stop();
}
public bool Check() {
if (stopwatch.ElapsedMilliseconds + stopwatchOffset >= Interval){
stopwatchOffset = (int)(stopwatch.ElapsedMilliseconds - Interval);
stopwatch.Restart();
return true;
}
return false;
}
public bool Roll() => roll();
private Func<bool> roll = () => true;
public bool CheckAndRoll() {
if (Check()) return Roll();
return false;
}
public void GuaranteeSuccess(bool value) {
roll = value ? () => true : () => random.NextDouble() <= MovementChance;
}
}

View file

@ -0,0 +1,106 @@
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);
}
public override string Name{ get; } = "Neko";
public override int TypeId{ get; } = 1;
public override bool BlocksTile{ get; set; } = true;
public override void Spawn(MapTile location) {
base.Spawn(location);
// stopwatch.Start();
movementOpportunity = new MovementOpportunity(5000, ((2 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (2 + Math.Pow(1.5f, 10)));
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)];
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
}
public override void Attack(ServerPlayer player) {
throw new NotImplementedException();
}
public override void Update() {
base.Update();
if (movementOpportunity.CheckAndRoll()){
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(10));
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:
throw new NotImplementedException();
case Pathfinder.Decision.ResetType:
Reset();
break;
case Pathfinder.Decision.WaitType:
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;
}
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,
t => distances.ContainsKey(t) && distances[t] <= distances[Enemy.Location] + tolerance);
if (closerTiles.Contains(goal)){
if (Enemy.Location.GetConnector(goal)!.Blocked){
return Decision.Reset();
}
return Decision.Attack(Server.P1); // TODO: un-hardcode this
}
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();
}
}
}

View file

@ -0,0 +1,36 @@
using FNAF_Server.Map;
namespace FNAF_Server.Enemies;
public abstract class Pathfinder {
public Enemy Enemy;
public Pathfinder(Enemy enemy) {
Enemy = enemy;
}
public abstract Decision DecideNext(MapTile goal);
// protected abstract Dictionary<MapTile, int> Crawl(MapTile tile); // fill Distances with all reachable tiles
protected virtual List<MapTile> GetNeighbours(MapTile tile, Predicate<TileConnector> connectorFilter, Predicate<MapTile> tileFilter) {
return tile.GetAllConnectors().Where(c => connectorFilter(c)).Select(c => c.OtherTile(tile)).Where(t => tileFilter(t)).ToList();
}
public class Decision {
public int type;
public MapTile? nextTile;
public ServerPlayer? attackTarget;
private Decision() { }
public static Decision Move(MapTile tile) => new() { type = MoveType, nextTile = tile };
public static Decision Attack(ServerPlayer player) => new(){ type = AttackType, attackTarget = player };
public static Decision Reset() => new(){ type = ResetType };
public static Decision Wait() => new(){ type = WaitType };
public const int MoveType = 0;
public const int AttackType = 1;
public const int ResetType = 2;
public const int WaitType = 3;
}
}

View file

@ -0,0 +1,29 @@
using FNAF_Server.Map;
using GlobalClassLib;
namespace FNAF_Server.Enemies;
public abstract class RoamingPathfinder : Pathfinder{
protected RoamingPathfinder(Enemy enemy) : base(enemy) {
}
protected Dictionary<MapTile, int> Crawl(MapTile tile) {
Dictionary<MapTile, int> distances = new(){ [tile] = 0 };
CrawlStep(tile, distances);
return distances;
}
private void CrawlStep(MapTile tile, Dictionary<MapTile, int> distances) {
List<MapTile> neighbours = GetNeighbours(tile,
c => (!c.Blocked || c.Type == ConnectorType.DOOR_OFFICE) && tile != Enemy.Location,
t =>
(!distances.ContainsKey(t) || distances[t] > distances[tile]) &&
(!Enemy.BlocksTile || EnemyManager.GetByLocation(t).All(e => !e.BlocksTile || e == Enemy)));
neighbours.ForEach(t => distances[t] = distances[tile] + tile.GetConnector(t)!.Value);
if (neighbours.Count != 0){
neighbours.ForEach(t => CrawlStep(t, distances));
}
}
}

View file

@ -0,0 +1,95 @@
using FNAF_Server.Map;
using GlobalClassLib;
using PacketLib;
namespace FNAF_Server.Enemies;
public class SpotEnemy : Enemy {
public SpotEnemy(int difficulty) : base(difficulty) {
path = [MapManager.Get(10), MapManager.Get(11), MapManager.Get(12), MapManager.Get(13), MapManager.Get(14)];
pathId = 2;
movementOpportunity = new(6000, (2 * Math.Sign(difficulty) + difficulty) / 12.0);
}
public override string Name{ get; } = "Spot";
public override int TypeId{ get; } = (int)EnemyType.SPOT;
public override bool BlocksTile{ get; set; } = false;
public bool Active{ get; set; } = false;
private MovementOpportunity movementOpportunity;
private MapTile[] path;
private int pathId;
private int p1WatchCounter = 0;
private int p2WatchCounter = 0;
public override void Reset() {
pathId = 2;
Location = path[pathId];
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
}
public override void Attack(ServerPlayer player) {
throw new NotImplementedException();
}
public override void Update() {
if (GameLogic.NSecondUpdate){
if(!movementOpportunity.Running)
movementOpportunity.Start();
if (Active){
if (Server.P1.state.monitorUp && Server.P1.state.camera == Location.Id) p1WatchCounter++;
if (Server.P2.state.monitorUp && Server.P2.state.camera == Location.Id) p2WatchCounter++;
Console.WriteLine($"P1: {p1WatchCounter} | P2: {p2WatchCounter}");
}
}
if (movementOpportunity.CheckAndRoll()){
if(!Active) {
Active = true;
movementOpportunity.Interval = 12_000;
movementOpportunity.GuaranteeSuccess(true);
Server.SendUpdateToAll([GameEvent.SPOT_SET_ACTIVE(Id, true)]);
}
else{
movementOpportunity.Interval = 6000;
movementOpportunity.GuaranteeSuccess(false);
movementOpportunity.Stop();
if (p1WatchCounter > p2WatchCounter){
pathId++;
if (Location.GetConnector(path[pathId])!.Blocked){
Reset();
}
else if (pathId == path.Length - 1){
Attack(Server.P1);
}
}
else if (p2WatchCounter > p1WatchCounter){
pathId--;
if (Location.GetConnector(path[pathId])!.Blocked){
Reset();
}
else if (pathId == 0){
Attack(Server.P2);
}
}
Location = path[pathId];
Active = false;
p1WatchCounter = 0;
p2WatchCounter = 0;
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id), GameEvent.SPOT_SET_ACTIVE(Id, false)]);
}
}
}
public override void Spawn(MapTile location) {
base.Spawn(location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
}
}