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:
parent
4484b127c5
commit
9bfe63a166
27 changed files with 772 additions and 47 deletions
118
FNAF_Server/Enemies/LurkEnemy.cs
Normal file
118
FNAF_Server/Enemies/LurkEnemy.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue