Zvuky, změny v pathfindingu, přidány ventilace
This commit is contained in:
parent
4fdff0a0cc
commit
c5adebb2db
36 changed files with 527 additions and 143 deletions
5
FNAF_Server/Enemies/ITargetingEnemy.cs
Normal file
5
FNAF_Server/Enemies/ITargetingEnemy.cs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
namespace FNAF_Server.Enemies;
|
||||
|
||||
public interface ITargetingEnemy {
|
||||
ServerPlayer Target { get; set; }
|
||||
}
|
||||
|
|
@ -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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue