Zvuky, změny v pathfindingu, přidány ventilace

This commit is contained in:
Perry 2026-03-19 20:10:45 +01:00
parent 4fdff0a0cc
commit c5adebb2db
36 changed files with 527 additions and 143 deletions

View file

@ -0,0 +1,5 @@
namespace FNAF_Server.Enemies;
public interface ITargetingEnemy {
ServerPlayer Target { get; set; }
}

View file

@ -32,13 +32,13 @@ public class LurkEnemy : Enemy {
base.Spawn(location);
// stopwatch.Start();
movementOpportunity.Start();
pathfinder.takenPath.Add(Location);
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);
pathfinder.TakenPath.Clear();
pathfinder.TakenPath.Add(Location);
Target.state.power -= 30;
@ -69,6 +69,9 @@ public class LurkEnemy : Enemy {
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
switch (decision.type){
case Pathfinder.Decision.MoveType:
if (Location.GetConnector(decision.nextTile!)!.Type == ConnectorType.VENT){
Server.SendUpdateToAll([GameEvent.VENT_USED()]);
}
Location = decision.nextTile!;
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id)]);
Console.WriteLine($"Enemy {TypeId} ({Name}) moving to {Location.PositionAsString})");
@ -86,46 +89,43 @@ public class LurkEnemy : Enemy {
}
private class LurkPathfinder : RoamingPathfinder {
private Random random = new();
// private Random random = new();
private int tolerance;
public List<MapTile> takenPath = new();
public LurkPathfinder(Enemy enemy, int tolerance) : base(enemy) {
public LurkPathfinder(Enemy enemy, int tolerance) : base(enemy, tolerance) {
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(((LurkEnemy)Enemy).Target);
}
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();
}
// 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(((LurkEnemy)Enemy).Target);
// }
//
// 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

@ -31,7 +31,7 @@ public class MareEnemy : Enemy {
}
public override void Reset() {
pathfinder.takenPath.Clear();
pathfinder.TakenPath.Clear();
Target = Server.OtherPlayer(Target);
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
if (decision.type == Pathfinder.Decision.MoveType){
@ -53,7 +53,7 @@ public class MareEnemy : Enemy {
// stopwatch.Start();
movementOpportunity.Start();
pathfinder.takenPath.Add(Location);
pathfinder.TakenPath.Add(Location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
}
@ -87,46 +87,44 @@ public class MareEnemy : Enemy {
private class MarePathfinder : RoamingPathfinder {
private Random random = new();
private int tolerance;
public List<MapTile> takenPath = new();
public MarePathfinder(Enemy enemy, int tolerance) : base(enemy) {
// private Random random = new();
public MarePathfinder(Enemy enemy, int tolerance) : base(enemy, tolerance) {
this.tolerance = tolerance;
AdditionalConnectorFilter = c => c.Type != ConnectorType.VENT;
}
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(((MareEnemy)Enemy).Target);
}
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();
}
// 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(((MareEnemy)Enemy).Target);
// }
//
// 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

@ -6,10 +6,10 @@ namespace FNAF_Server.Enemies;
public class NekoEnemy : Enemy {
private MovementOpportunity movementOpportunity;
private NekoPathfinder pathfinder;
private RoamingPathfinder pathfinder;
public NekoEnemy(int difficulty) : base(difficulty) {
pathfinder = new NekoPathfinder(this, 1);
public NekoEnemy(int difficulty) : base(difficulty) {
pathfinder = new RoamingPathfinder(this, 1){AdditionalTileFilter = t => Aggressive || t.Owner == null || !t.Lit};
movementOpportunity = new MovementOpportunity(5000);
SetDifficulty(difficulty);
@ -28,15 +28,16 @@ public class NekoEnemy : Enemy {
base.Spawn(location);
// stopwatch.Start();
movementOpportunity.Start();
pathfinder.takenPath.Add(Location);
pathfinder.TakenPath.Add(Location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
}
public override void Reset() {
pathfinder.takenPath.Clear();
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)];
Aggressive = false;
Target = Server.OtherPlayer(Target);
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
}
@ -49,7 +50,7 @@ public class NekoEnemy : Enemy {
public override void Update() {
base.Update();
if (Location.Owner != null && Location.Lit){
if (Location.Owner != null && Location.Lit && !Aggressive){
Aggressive = true;
Server.SendUpdateToAll([GameEvent.NEKO_ANGERED(Location.Owner.state.pid, Id)]);
}
@ -70,6 +71,9 @@ public class NekoEnemy : Enemy {
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
switch (decision.type){
case Pathfinder.Decision.MoveType:
if (Location.GetConnector(decision.nextTile!)!.Type == ConnectorType.VENT){
Server.SendUpdateToAll([GameEvent.VENT_USED()]);
}
Location = decision.nextTile!;
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id)]);
Console.WriteLine($"Enemy {TypeId} ({Name}) moving to {Location.PositionAsString})");
@ -88,49 +92,46 @@ public class NekoEnemy : Enemy {
}
private class NekoPathfinder : RoamingPathfinder {
private Random random = new();
// private Random random = new();
private int tolerance;
public List<MapTile> takenPath = new();
public NekoPathfinder(Enemy enemy, int tolerance) : base(enemy) {
public NekoPathfinder(Enemy enemy, int tolerance) : base(enemy, tolerance) {
this.tolerance = tolerance;
AdditionalTileFilter = t => ((NekoEnemy)Enemy).Aggressive || t.Owner == null || !t.Lit;
}
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(((NekoEnemy)Enemy).Target);
}
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();
}
// 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(((NekoEnemy)Enemy).Target);
// }
//
// 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

@ -3,13 +3,18 @@ using GlobalClassLib;
namespace FNAF_Server.Enemies;
public abstract class RoamingPathfinder : Pathfinder {
public class RoamingPathfinder : Pathfinder {
protected Predicate<MapTile> AdditionalTileFilter = _ => true;
protected Predicate<TileConnector> AdditionalConnectorFilter = _ => true;
public Predicate<MapTile> AdditionalTileFilter{ get; set; } = _ => true;
public Predicate<TileConnector> AdditionalConnectorFilter{ get; set; } = _ => true;
protected int tolerance;
public List<MapTile> TakenPath { get; } = new();
private Random random = new();
// private Func<TileConnector, MapTile, bool> defaultConnectorFilter;
protected RoamingPathfinder(Enemy enemy) : base(enemy) {
public RoamingPathfinder(Enemy enemy, int tolerance) : base(enemy) {
this.tolerance = tolerance;
}
protected Dictionary<MapTile, int> Crawl(MapTile tile) {
@ -23,10 +28,10 @@ public abstract class RoamingPathfinder : Pathfinder {
c =>
AdditionalConnectorFilter(c) &&
(!c.Blocked || c.Type == ConnectorType.DOOR_OFFICE) &&
tile != Enemy.Location,
tile != Enemy.Location &&
(!distances.ContainsKey(c.OtherTile(tile)) || distances[c.OtherTile(tile)] > distances[tile] + c.Value),
t =>
AdditionalTileFilter(t) &&
(!distances.ContainsKey(t) || distances[t] > distances[tile]) &&
(!Enemy.BlocksTile || EnemyManager.GetByLocation(t).All(e => !e.BlocksTile || e == Enemy)) &&
Server.Players.All(p => p.Value.state.officeTileId != t.Id));
@ -36,4 +41,42 @@ public abstract class RoamingPathfinder : Pathfinder {
neighbours.ForEach(t => CrawlStep(t, distances));
}
}
public override Decision DecideNext(MapTile goal) {
Dictionary<MapTile, int> distances = Crawl(goal);
List<MapTile> closerTiles = GetNeighbours(Enemy.Location,
c => AdditionalConnectorFilter(c) && !c.Blocked || c.Type == ConnectorType.DOOR_OFFICE,
t => AdditionalTileFilter(t) && distances.ContainsKey(t) && distances[t] + t.GetConnector(Enemy.Location).Value <= distances[Enemy.Location] + tolerance);
if (closerTiles.Contains(goal)){
if (Enemy.Location.GetConnector(goal)!.Blocked){
return Decision.Reset();
}
IEnumerable<ServerPlayer> players = Server.Players.Values.Where(p => p.state.officeTileId == goal.Id);
if (players.Count() == 1){
return Decision.Attack(players.First());
}
return Decision.Move(goal);
}
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){
double value = 1.0 / distances[tile];
if (roll <= value){
TakenPath.Add(tile);
return Decision.Move(tile);
}
roll -= value;
}
return Decision.Wait();
}
}

View file

@ -20,6 +20,8 @@ public class GameLogic {
public static int OfficeDoorUsage{ get; set; } = 10;
public static int RemoteDoorUsage{ get; set; } = 3;
public static int LightUsage{ get; set; } = 2;
private static bool unlimitedPower = false; // Debug
// public const int POWER_MAX = 1000;
// public static int P1Power{ get; set; } = Power.MAX_POWER_VALUE;
@ -64,11 +66,11 @@ public class GameLogic {
secondCycleTimer.Start();
Server.SendUpdateToAll([GameEvent.GAME_START()]);
EnemyManager.AddEnemy(new SpotEnemy(10)).Spawn(MapManager.Get(12));
// EnemyManager.AddEnemy(new SpotEnemy(10)).Spawn(MapManager.Get(12));
// EnemyManager.AddEnemy(new LurkEnemy(10)).Spawn(MapManager.Get(12));
// EnemyManager.AddEnemy(new NekoEnemy(10)).Spawn(MapManager.Get(2));
EnemyManager.AddEnemy(new NekoEnemy(10)).Spawn(MapManager.Get(2));
// EnemyManager.AddEnemy(new DashEnemy(10)).Spawn(MapManager.Get(12));
// EnemyManager.AddEnemy(new MareEnemy(0)).Spawn(MapManager.Get(22));
// EnemyManager.AddEnemy(new MareEnemy(10)).Spawn(MapManager.Get(22));
}
public static void Update() {
if(secondCycleTimer.Check()) NSecondUpdate = true;
@ -86,7 +88,12 @@ public class GameLogic {
}
// Server.SendUpdateToAll([GameEvent.POWER_OUT(player.state.pid)]);
}
if (unlimitedPower){ // Debug
Server.P1.state.power = Power.MAX_POWER_VALUE;
Server.P2.state.power = Power.MAX_POWER_VALUE;
}
Server.SendUpdateToAll([GameEvent.POWER_TICK(Server.P1.state.pid, Server.P1.state.power), GameEvent.POWER_TICK(Server.P2.state.pid, Server.P2.state.power)]);
}

View file

@ -19,7 +19,7 @@ public static class MapManager {
[(4, 0)] =[(4, 1, 1, ConnectorType.DOOR_REMOTE)],
[(0, 1)] =[(1, 1, 1, ConnectorType.HALL)],
[(1, 1)] =[(1, 2, 1, ConnectorType.DOOR_REMOTE)],
[(2, 1)] =[(2, 2, 1, ConnectorType.HALL), (2, 0, 1, ConnectorType.DOOR_OFFICE)],
[(2, 1)] =[(2, 2, 2, ConnectorType.HALL), (2, 0, 1, ConnectorType.DOOR_OFFICE)],
[(3, 1)] =[(3, 2, 1, ConnectorType.DOOR_REMOTE), (4, 1, 1, ConnectorType.HALL)]
};
@ -29,6 +29,8 @@ public static class MapManager {
[(2,2)] = [(3,2,1)],
[(3,2)] = [(4,2,1)]
};
private static (int x, int y)[] ventTiles =[(0, 1), (4, 1), (2, 2), (0, 3), (4, 3)];
public static void InitMap() {
for (int i = 0; i < 5; i++){
@ -56,6 +58,15 @@ public static class MapManager {
doors = GetAllConnectors().Where(c => c.Type == ConnectorType.DOOR_OFFICE || c.Type == ConnectorType.DOOR_REMOTE).ToList();
doorsP1 = doors.Where(c => c.Owner == Server.P1).ToList();
doorsP2 = doors.Where(c => c.Owner == Server.P2).ToList();
foreach (var tile1Coords in ventTiles){
MapTile tile1 = map[tile1Coords.x, tile1Coords.y];
foreach (var tile2Coords in ventTiles){
MapTile tile2 = map[tile2Coords.x, tile2Coords.y];
if(tile1.GetConnector(tile2) != null || tile1 == tile2) continue;
tile1.AddConnector(new TileConnector(tile2, ConnectorType.VENT, 2));
}
}
}
public static TileConnector[] GetAllConnectors() {