Všechna monstra, dynamický targeting. Bugfixy u monster.
This commit is contained in:
parent
55fd052072
commit
4fdff0a0cc
18 changed files with 345 additions and 43 deletions
70
FNAF_Server/Enemies/DashEnemy.cs
Normal file
70
FNAF_Server/Enemies/DashEnemy.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using System.Net.Mime;
|
||||
using FNAF_Server.Map;
|
||||
using GlobalClassLib;
|
||||
using PacketLib;
|
||||
|
||||
namespace FNAF_Server.Enemies;
|
||||
|
||||
public class DashEnemy : Enemy {
|
||||
public DashEnemy(int difficulty) : base(difficulty) {
|
||||
movementOpportunity = new(5000);
|
||||
SetDifficulty(difficulty);
|
||||
}
|
||||
|
||||
public override string Name{ get; } = "Dash";
|
||||
public override int TypeId{ get; } = (int)EnemyType.DASH;
|
||||
public override bool BlocksTile{ get; set; } = false;
|
||||
|
||||
private MovementOpportunity movementOpportunity;
|
||||
|
||||
private readonly (MapTile tile, Direction p1AttackDir, Direction p2AttackDir)[] positions =[
|
||||
(MapManager.Get(7), Direction.EAST, Direction.WEST),
|
||||
(MapManager.Get(12), Direction.NORTH, Direction.NORTH),
|
||||
(MapManager.Get(17), Direction.WEST, Direction.EAST)
|
||||
];
|
||||
|
||||
private int currentPosIndex = 0;
|
||||
private Random random = new();
|
||||
|
||||
public override void Reset() {
|
||||
int roll = random.Next(0, positions.Length - 1);
|
||||
if (roll >= currentPosIndex) roll++;
|
||||
currentPosIndex = roll;
|
||||
Location = positions[roll].tile;
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
|
||||
}
|
||||
|
||||
public override void SetDifficulty(int difficulty) {
|
||||
Difficulty = difficulty;
|
||||
movementOpportunity.MovementChance = (2 * Math.Sign(difficulty) + difficulty) / 12.0;
|
||||
}
|
||||
|
||||
public override void Spawn(MapTile location) {
|
||||
currentPosIndex = positions.ToList().FindIndex(p => p.tile == location);
|
||||
if (currentPosIndex == -1){
|
||||
Console.WriteLine("Dash failed to spawn on " + location.Id);
|
||||
return;
|
||||
}
|
||||
base.Spawn(location);
|
||||
|
||||
movementOpportunity.Start();
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
|
||||
}
|
||||
|
||||
public override void Update() {
|
||||
base.Update();
|
||||
|
||||
if (movementOpportunity.CheckAndRoll()){
|
||||
bool attackP1 = !Server.P1.state.doorStates[(int)positions[currentPosIndex].p1AttackDir];
|
||||
bool attackP2 = !Server.P2.state.doorStates[(int)positions[currentPosIndex].p2AttackDir];
|
||||
|
||||
if (attackP1 != attackP2){
|
||||
if(attackP1) Attack(Server.P1);
|
||||
else if(attackP2) Attack(Server.P2);
|
||||
return;
|
||||
}
|
||||
|
||||
Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,10 @@ using PacketLib;
|
|||
namespace FNAF_Server.Enemies;
|
||||
|
||||
public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
|
||||
protected Enemy(int difficulty) : base(difficulty) {
|
||||
public int Difficulty { get; protected set; }
|
||||
|
||||
protected Enemy(int difficulty) {
|
||||
Difficulty = difficulty;
|
||||
}
|
||||
|
||||
public abstract bool BlocksTile { get; set; }
|
||||
|
|
@ -27,4 +30,6 @@ public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
|
|||
Server.SendUpdateToAll([GameEvent.ENEMY_ATTACK(Id, player.state.pid)]);
|
||||
GameLogic.DeclareWinner(Server.OtherPlayer(player));
|
||||
}
|
||||
|
||||
public abstract void SetDifficulty(int difficulty);
|
||||
}
|
||||
|
|
@ -19,14 +19,18 @@ public class LurkEnemy : Enemy {
|
|||
// private double movementChance => (2 + Math.Pow(1.5f, Difficulty)) / CHANCE_DENOMINATOR; // chance scales exponentially using a constant multiplier
|
||||
|
||||
private MovementOpportunity movementOpportunity;
|
||||
|
||||
public ServerPlayer? Target{ get; set; }
|
||||
|
||||
public LurkEnemy(int difficulty) : base(difficulty) {
|
||||
pathfinder = new LurkPathfinder(this, 1);
|
||||
movementOpportunity = new MovementOpportunity(5000);
|
||||
Target = null;
|
||||
SetDifficulty(difficulty);
|
||||
}
|
||||
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)]);
|
||||
|
|
@ -35,17 +39,34 @@ public class LurkEnemy : Enemy {
|
|||
public override void Reset() {
|
||||
pathfinder.takenPath.Clear();
|
||||
pathfinder.takenPath.Add(Location);
|
||||
|
||||
Target.state.power -= 30;
|
||||
|
||||
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 SetDifficulty(int difficulty) {
|
||||
Difficulty = difficulty;
|
||||
movementOpportunity.MovementChance = ((2 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (2 + Math.Pow(1.5f, 10));
|
||||
}
|
||||
|
||||
public override void Update() {
|
||||
base.Update();
|
||||
|
||||
if (movementOpportunity.CheckAndRoll()){
|
||||
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(10));
|
||||
if (Server.P1.state.power > Server.P2.state.power){
|
||||
Target = Server.P1;
|
||||
}
|
||||
else if (Server.P1.state.power < Server.P2.state.power){
|
||||
Target = Server.P2;
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
|
||||
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
|
||||
switch (decision.type){
|
||||
case Pathfinder.Decision.MoveType:
|
||||
Location = decision.nextTile!;
|
||||
|
|
@ -65,10 +86,6 @@ public class LurkEnemy : Enemy {
|
|||
}
|
||||
|
||||
private class LurkPathfinder : RoamingPathfinder {
|
||||
// public override List<MapTile> FindPath(MapTile start, MapTile end) {
|
||||
// throw new NotImplementedException();
|
||||
// }
|
||||
|
||||
private Random random = new();
|
||||
|
||||
private int tolerance;
|
||||
|
|
@ -88,8 +105,8 @@ public class LurkEnemy : Enemy {
|
|||
if (Enemy.Location.GetConnector(goal)!.Blocked){
|
||||
return Decision.Reset();
|
||||
}
|
||||
|
||||
return Decision.Attack(Server.P1); // TODO: un-hardcode this
|
||||
|
||||
return Decision.Attack(((LurkEnemy)Enemy).Target);
|
||||
}
|
||||
|
||||
if (closerTiles.Count != 0 && closerTiles.All(t => takenPath.Contains(t))){
|
||||
|
|
|
|||
132
FNAF_Server/Enemies/MareEnemy.cs
Normal file
132
FNAF_Server/Enemies/MareEnemy.cs
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
using FNAF_Server.Map;
|
||||
using GlobalClassLib;
|
||||
using PacketLib;
|
||||
|
||||
namespace FNAF_Server.Enemies;
|
||||
|
||||
public class MareEnemy : Enemy {
|
||||
|
||||
public override string Name{ get; } = "Mare";
|
||||
public override int TypeId{ get; } = (int)EnemyType.MARE;
|
||||
public override bool BlocksTile{ get; set; } = true;
|
||||
|
||||
private MarePathfinder pathfinder;
|
||||
private MovementOpportunity movementOpportunity;
|
||||
|
||||
public ServerPlayer Target{ get; set; }
|
||||
public MareEnemy(int difficulty) : base(difficulty) {
|
||||
pathfinder = new MarePathfinder(this, 1);
|
||||
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{
|
||||
Target = new Random().Next(2) == 0 ? Server.P1 : Server.P2;
|
||||
}
|
||||
|
||||
movementOpportunity = new MovementOpportunity(5000);
|
||||
SetDifficulty(difficulty);
|
||||
}
|
||||
|
||||
public override void Reset() {
|
||||
pathfinder.takenPath.Clear();
|
||||
Target = Server.OtherPlayer(Target);
|
||||
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
|
||||
if (decision.type == Pathfinder.Decision.MoveType){
|
||||
Location = decision.nextTile!;
|
||||
}
|
||||
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
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 Update() {
|
||||
base.Update();
|
||||
|
||||
if (movementOpportunity.CheckAndRoll()){
|
||||
if (Location.Owner != null && Location.Lit){
|
||||
Attack(Location.Owner);
|
||||
}
|
||||
|
||||
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
|
||||
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:
|
||||
Attack(decision.attackTarget!);
|
||||
break;
|
||||
case Pathfinder.Decision.ResetType:
|
||||
Reset();
|
||||
break;
|
||||
case Pathfinder.Decision.WaitType:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private class MarePathfinder : RoamingPathfinder {
|
||||
private Random random = new();
|
||||
|
||||
private int tolerance;
|
||||
public List<MapTile> takenPath = new();
|
||||
|
||||
public MarePathfinder(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(((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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,11 +5,15 @@ namespace FNAF_Server.Enemies;
|
|||
public class MovementOpportunity {
|
||||
public int Interval{ get; set; }
|
||||
// public double ChanceDenominator;
|
||||
public double MovementChance{ get; set; }
|
||||
public double MovementChance{
|
||||
get;
|
||||
set{
|
||||
field = value;
|
||||
GuaranteeSuccess(value >= 1);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Running => stopwatch.IsRunning;
|
||||
|
||||
public bool ConstantChance{ get; private set; }
|
||||
|
||||
private Stopwatch stopwatch = new();
|
||||
private long stopwatchOffset = 0;
|
||||
|
|
@ -19,13 +23,10 @@ public class MovementOpportunity {
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,17 +10,23 @@ public class NekoEnemy : Enemy {
|
|||
|
||||
public NekoEnemy(int difficulty) : base(difficulty) {
|
||||
pathfinder = new NekoPathfinder(this, 1);
|
||||
movementOpportunity = new MovementOpportunity(5000);
|
||||
SetDifficulty(difficulty);
|
||||
|
||||
}
|
||||
|
||||
public override string Name{ get; } = "Neko";
|
||||
public override int TypeId{ get; } = 1;
|
||||
public override int TypeId{ get; } = (int)EnemyType.NEKO;
|
||||
public override bool BlocksTile{ get; set; } = true;
|
||||
|
||||
public bool Aggressive = false;
|
||||
|
||||
public ServerPlayer? Target{ get; set; }
|
||||
|
||||
|
||||
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)]);
|
||||
|
|
@ -30,14 +36,38 @@ public class NekoEnemy : Enemy {
|
|||
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;
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
public override void Update() {
|
||||
base.Update();
|
||||
|
||||
if (movementOpportunity.CheckAndRoll()){
|
||||
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(10));
|
||||
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));
|
||||
switch (decision.type){
|
||||
case Pathfinder.Decision.MoveType:
|
||||
Location = decision.nextTile!;
|
||||
|
|
@ -51,6 +81,7 @@ public class NekoEnemy : Enemy {
|
|||
Reset();
|
||||
break;
|
||||
case Pathfinder.Decision.WaitType:
|
||||
Aggressive = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -64,6 +95,7 @@ public class NekoEnemy : Enemy {
|
|||
|
||||
public NekoPathfinder(Enemy enemy, int tolerance) : base(enemy) {
|
||||
this.tolerance = tolerance;
|
||||
AdditionalTileFilter = t => ((NekoEnemy)Enemy).Aggressive || t.Owner == null || !t.Lit;
|
||||
}
|
||||
|
||||
public override Decision DecideNext(MapTile goal) {
|
||||
|
|
@ -71,13 +103,15 @@ public class NekoEnemy : Enemy {
|
|||
|
||||
List<MapTile> closerTiles = GetNeighbours(Enemy.Location,
|
||||
c => !c.Blocked || c.Type == ConnectorType.DOOR_OFFICE,
|
||||
t => distances.ContainsKey(t) && distances[t] <= distances[Enemy.Location] + tolerance);
|
||||
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
|
||||
return Decision.Attack(((NekoEnemy)Enemy).Target);
|
||||
}
|
||||
|
||||
if (closerTiles.Count != 0 && closerTiles.All(t => takenPath.Contains(t))){
|
||||
|
|
|
|||
|
|
@ -3,7 +3,12 @@ using GlobalClassLib;
|
|||
|
||||
namespace FNAF_Server.Enemies;
|
||||
|
||||
public abstract class RoamingPathfinder : Pathfinder{
|
||||
public abstract class RoamingPathfinder : Pathfinder {
|
||||
|
||||
protected Predicate<MapTile> AdditionalTileFilter = _ => true;
|
||||
protected Predicate<TileConnector> AdditionalConnectorFilter = _ => true;
|
||||
|
||||
// private Func<TileConnector, MapTile, bool> defaultConnectorFilter;
|
||||
protected RoamingPathfinder(Enemy enemy) : base(enemy) {
|
||||
}
|
||||
|
||||
|
|
@ -15,8 +20,12 @@ public abstract class RoamingPathfinder : Pathfinder{
|
|||
|
||||
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,
|
||||
c =>
|
||||
AdditionalConnectorFilter(c) &&
|
||||
(!c.Blocked || c.Type == ConnectorType.DOOR_OFFICE) &&
|
||||
tile != Enemy.Location,
|
||||
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));
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ 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);
|
||||
movementOpportunity = new(6000);
|
||||
SetDifficulty(difficulty);
|
||||
}
|
||||
|
||||
public override string Name{ get; } = "Spot";
|
||||
|
|
@ -30,6 +31,11 @@ public class SpotEnemy : Enemy {
|
|||
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
|
||||
}
|
||||
|
||||
public override void SetDifficulty(int difficulty) {
|
||||
Difficulty = difficulty;
|
||||
movementOpportunity.MovementChance = (2 * Math.Sign(difficulty) + difficulty) / 12.0;
|
||||
}
|
||||
|
||||
public override void Update() {
|
||||
if (GameLogic.NSecondUpdate){
|
||||
if(!movementOpportunity.Running)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue