Oprava spawnování monster, optimalizace v CommandProcessor a EventProcessor. Přesunutí některých tříd do vlastních namespaců, pročištění kódu, úpravy formátování, odstranění nepoužívaných souborů a zakomentovaného kódu

This commit is contained in:
Perry 2026-03-28 09:59:31 +01:00
parent e5d746d597
commit 243f071a43
62 changed files with 873 additions and 1217 deletions

View file

@ -1,21 +1,19 @@
using System.Net.Mime;
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
using PacketLib;
namespace ONDServer.Enemies;
public class DashEnemy : Enemy {
public DashEnemy(int difficulty) : base(difficulty, 5000) {
movementOpportunity = new(5000);
MovementOpportunity = new(5000);
SetDifficulty(difficulty);
}
public override string Name{ get; } = "Dash";
public override int TypeId{ get; } = (int)EnemyType.DASH;
public override EnemyType Type{ get; } = 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),
@ -34,11 +32,6 @@ public class DashEnemy : 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 Spawn(MapTile location) {
currentPosIndex = positions.ToList().FindIndex(p => p.tile == location);
if (currentPosIndex == -1){
@ -47,16 +40,16 @@ public class DashEnemy : Enemy {
}
base.Spawn(location);
movementOpportunity.Start();
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
MovementOpportunity.Start();
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(Type, 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 (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);

View file

@ -1,25 +1,25 @@
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
using PacketLib;
namespace ONDServer.Enemies;
public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
public int Difficulty { get; protected set; }
protected MovementOpportunity movementOpportunity;
protected Enemy(int difficulty, int movementInterval) {
movementOpportunity = new(movementInterval);
SetDifficulty(difficulty);
}
public abstract bool BlocksTile { get; set; }
public bool Spawned { get; set; }
protected OpportunityTimer MovementOpportunity;
protected Enemy(int difficulty, int movementInterval) {
MovementOpportunity = new(movementInterval);
SetDifficulty(difficulty);
}
// unused
public virtual void SpawnSilent(MapTile location) {
Console.WriteLine($"!!! Silent spawn not implemented for enemy {TypeId} ({Name}), reverting to regular spawn");
Console.WriteLine($"!!! Silent spawn not implemented for enemy {Type} ({Name}), reverting to regular spawn");
Spawn(location);
}
@ -31,12 +31,13 @@ public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
public abstract void Reset();
public virtual void Attack(ServerPlayer player) {
Server.SendUpdateToAll([GameEvent.ENEMY_ATTACK(Id, player.state.pid)]);
Server.SendUpdateToAll([GameEvent.ENEMY_ATTACK(Id, player.State.Pid)]);
GameLogic.DeclareWinner(Server.OtherPlayer(player));
}
public virtual void SetDifficulty(int difficulty) {
if (difficulty > 10) return;
Difficulty = difficulty;
movementOpportunity.MovementChance = ((5 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (5 + Math.Pow(1.5f, 10));
MovementOpportunity.MovementChance = ((5 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (5 + Math.Pow(1.5f, 10));
}
}

View file

@ -2,7 +2,7 @@ using ONDServer.Map;
namespace ONDServer.Enemies;
public class EnemyManager {
public static class EnemyManager {
private static Dictionary<int, Enemy> enemies = new();
public static void Update() {

View file

@ -2,41 +2,33 @@ using System.Diagnostics;
using System.Reflection;
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
using PacketLib;
namespace ONDServer.Enemies;
public class LurkEnemy : Enemy {
public override string Name{ get; } = "Lurk";
public override int TypeId{ get; } = (int)EnemyType.LURK;
public override EnemyType Type{ get; } = 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
public ServerPlayer? Target{ get; set; }
private readonly RoamingPathfinder pathfinder;
public LurkEnemy(int difficulty) : base(difficulty, 5000) {
pathfinder = new LurkPathfinder(this, 1);
// movementOpportunity = new MovementOpportunity(5000);
Target = null;
// SetDifficulty(difficulty);
pathfinder = new RoamingPathfinder(this, 1);
Target = null;
}
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)]);
MovementOpportunity.Start();
pathfinder.TakenPath.Add(Location!);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(Type, Id, Difficulty, Location!.Id)]);
}
public override void Reset() {
Target.state.power -= 80;
if (Target != null) Target.State.Power -= 80;
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)];
pathfinder.TakenPath.Clear();
@ -45,26 +37,23 @@ public class LurkEnemy : Enemy {
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
}
// public override void SetDifficulty(int difficulty) {
// Difficulty = difficulty;
// movementOpportunity.MovementChance = ((5 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (5 + Math.Pow(1.5f, 10));
// }
public override void Update() {
base.Update();
if (movementOpportunity.CheckAndRoll()){
if (Server.P1.state.power > Server.P2.state.power){
if(Location == null) return;
if (MovementOpportunity.CheckAndRoll()){
if (Server.P1.State.Power > Server.P2.State.Power){
Target = Server.P1;
}
else if (Server.P1.state.power < Server.P2.state.power){
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));
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){
@ -72,7 +61,7 @@ public class LurkEnemy : Enemy {
}
Location = decision.nextTile!;
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id)]);
Console.WriteLine($"Enemy {TypeId} ({Name}) moving to {Location.PositionAsString})");
Console.WriteLine($"Enemy {Type} ({Name}) moving to {Location.IdAsString})");
break;
case Pathfinder.Decision.AttackType:
Attack(decision.attackTarget!);
@ -85,45 +74,4 @@ public class LurkEnemy : Enemy {
}
}
}
private class LurkPathfinder : RoamingPathfinder {
// private Random random = new();
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();
// }
}
}

View file

@ -1,5 +1,6 @@
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
using PacketLib;
namespace ONDServer.Enemies;
@ -7,72 +8,60 @@ namespace ONDServer.Enemies;
public class MareEnemy : Enemy {
public override string Name{ get; } = "Mare";
public override int TypeId{ get; } = (int)EnemyType.MARE;
public override EnemyType Type{ get; } = EnemyType.MARE;
public override bool BlocksTile{ get; set; } = true;
private MarePathfinder pathfinder;
// private MovementOpportunity movementOpportunity;
private readonly RoamingPathfinder pathfinder;
public ServerPlayer? Target{ get; set; }
public MareEnemy(int difficulty) : base(difficulty, 6000) {
pathfinder = new MarePathfinder(this, 1);
// movementOpportunity = new MovementOpportunity(5000);
// SetDifficulty(difficulty);
pathfinder = new RoamingPathfinder(this, 1){ AdditionalConnectorFilter = c => c.Type != ConnectorType.VENT };
}
public override void Reset() {
pathfinder.TakenPath.Clear();
Target = Server.OtherPlayer(Target);
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
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 =
// ((5 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (5 + Math.Pow(1.5f, 10));
// }
public override void Spawn(MapTile location) {
base.Spawn(location);
// stopwatch.Start()
if (Server.P1.state.power > Server.P2.state.power){
if (Server.P1.State.Power > Server.P2.State.Power){
Target = Server.P2;
}
else if(Server.P1.state.power < Server.P2.state.power){
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.Start();
MovementOpportunity.Start();
pathfinder.TakenPath.Add(Location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(Type, Id, Difficulty, Location.Id)]);
}
public override void Update() {
base.Update();
if (movementOpportunity.CheckAndRoll()){
if (MovementOpportunity.CheckAndRoll()){
if (Location.Owner != null && Location.Lit){
Attack(Location.Owner);
}
if (Target == null) return;
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
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})");
Console.WriteLine($"Enemy {Type} ({Name}) moving to {Location.IdAsString})");
break;
case Pathfinder.Decision.AttackType:
Attack(decision.attackTarget!);
@ -85,48 +74,4 @@ public class MareEnemy : Enemy {
}
}
}
private class MarePathfinder : RoamingPathfinder {
// 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();
// }
}
}

View file

@ -1,35 +1,29 @@
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
using PacketLib;
namespace ONDServer.Enemies;
public class NekoEnemy : Enemy {
// private MovementOpportunity movementOpportunity;
private RoamingPathfinder pathfinder;
public override string Name{ get; } = "Neko";
public override EnemyType Type{ get; } = EnemyType.NEKO;
public override bool BlocksTile{ get; set; } = true;
public bool Aggressive = false;
public ServerPlayer? Target{ get; set; }
private readonly RoamingPathfinder pathfinder;
public NekoEnemy(int difficulty) : base(difficulty, 4000) {
pathfinder = new RoamingPathfinder(this, 1){AdditionalTileFilter = t => Aggressive || t.Owner == null || !t.Lit};
// movementOpportunity = new MovementOpportunity(5000);
// SetDifficulty(difficulty);
}
public override string Name{ get; } = "Neko";
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.Start();
pathfinder.TakenPath.Add(Location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
MovementOpportunity.Start();
pathfinder.TakenPath.Add(Location!);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(Type, Id, Difficulty, Location!.Id)]);
}
public override void Reset() {
@ -38,29 +32,25 @@ public class NekoEnemy : Enemy {
pathfinder.TakenPath.Clear();
pathfinder.TakenPath.Add(Location);
Aggressive = false;
Target = Server.OtherPlayer(Target);
Target = Server.OtherPlayer(Target!);
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);
}
// public override void SetDifficulty(int difficulty) {
// Difficulty = difficulty;
// movementOpportunity.MovementChance =
// ((5 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (5 + Math.Pow(1.5f, 10));
// }
public override void Update() {
base.Update();
if(Location == null) return;
if (Location.Owner != null && Location.Lit && !Aggressive){
Aggressive = true;
Server.SendUpdateToAll([GameEvent.NEKO_ANGERED(Location.Owner.state.pid, Id)]);
Server.SendUpdateToAll([GameEvent.NEKO_ANGERED(Location.Owner.State.Pid, Id)]);
}
if (Target == null){
if (Server.P1.state.power > Server.P2.state.power){
if (Server.P1.State.Power > Server.P2.State.Power){
Target = Server.P2;
}
else if (Server.P1.state.power < Server.P2.state.power){
else if (Server.P1.State.Power < Server.P2.State.Power){
Target = Server.P1;
}
else{
@ -71,9 +61,9 @@ public class NekoEnemy : Enemy {
}
}
bool succeed = movementOpportunity.CheckAndRoll();
bool succeed = MovementOpportunity.CheckAndRoll();
if (Target != null && (succeed || (Aggressive && GameLogic.NSecondUpdate))){
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
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){
@ -81,7 +71,7 @@ public class NekoEnemy : Enemy {
}
Location = decision.nextTile!;
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id)]);
Console.WriteLine($"Enemy {TypeId} ({Name}) moving to {Location.PositionAsString})");
Console.WriteLine($"Enemy {Type} ({Name}) moving to {Location.IdAsString})");
break;
case Pathfinder.Decision.AttackType:
Attack(decision.attackTarget!);
@ -95,48 +85,4 @@ public class NekoEnemy : Enemy {
}
}
}
private class NekoPathfinder : RoamingPathfinder {
// private Random random = new();
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();
// }
}
}

View file

@ -2,9 +2,8 @@ using System.Diagnostics;
namespace ONDServer.Enemies;
public class MovementOpportunity {
public class OpportunityTimer {
public int Interval{ get; set; }
// public double ChanceDenominator;
public double MovementChance{
get;
set{
@ -20,12 +19,12 @@ public class MovementOpportunity {
private Random random = new();
public MovementOpportunity(int intervalMs, double movementChance) {
public OpportunityTimer(int intervalMs, double movementChance) {
Interval = intervalMs;
MovementChance = movementChance;
}
public MovementOpportunity(int intervalMs) {
public OpportunityTimer(int intervalMs) {
Interval = intervalMs;
MovementChance = 1;
}
@ -41,7 +40,6 @@ public class MovementOpportunity {
if (stopwatch.ElapsedMilliseconds - stopwatchOffset >= Interval){
int overshoot = (int)(stopwatch.ElapsedMilliseconds - stopwatchOffset - Interval);
stopwatchOffset = stopwatch.ElapsedMilliseconds - overshoot;
// stopwatch.Restart();
return true;
}
return false;

View file

@ -1,4 +1,5 @@
using ONDServer.Map;
using ONDServer.Net;
namespace ONDServer.Enemies;
@ -8,7 +9,6 @@ public abstract class Pathfinder {
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();

View file

@ -1,25 +1,19 @@
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
namespace ONDServer.Enemies;
public class RoamingPathfinder : Pathfinder {
public class RoamingPathfinder(Enemy enemy, int tolerance) : Pathfinder(enemy) {
public Predicate<MapTile> AdditionalTileFilter{ get; set; } = _ => true;
public Predicate<TileConnector> AdditionalConnectorFilter{ get; set; } = _ => true;
protected int tolerance;
protected int Tolerance = tolerance;
public List<MapTile> TakenPath { get; } = new();
private Random random = new();
// private Queue<MapTile> tilesToCrawl = new();
// private Dictionary<MapTile, int> distances = new();
// private Func<TileConnector, MapTile, bool> defaultConnectorFilter;
public RoamingPathfinder(Enemy enemy, int tolerance) : base(enemy) {
this.tolerance = tolerance;
}
protected Dictionary<MapTile, int> Search(MapTile startingTile) {
Dictionary<MapTile, int> distances = new(){ [startingTile] = 0 };
Queue<MapTile> tilesToSearch = new();
@ -36,7 +30,7 @@ public class RoamingPathfinder : Pathfinder {
t =>
AdditionalTileFilter(t) &&
(!Enemy.BlocksTile || EnemyManager.GetByLocation(t).All(e => !e.BlocksTile || e == Enemy)) &&
Server.Players.All(p => p.Value.state.officeTileId != t.Id));
Server.Players.All(p => p.Value.State.OfficeTileId != t.Id));
neighbours.ForEach(t => {
distances[t] = distances[currentTile] + currentTile.GetConnector(t)!.Value;
@ -47,37 +41,20 @@ public class RoamingPathfinder : Pathfinder {
return distances;
}
// private void CrawlStep(MapTile tile) {
// List<MapTile> neighbours = GetNeighbours(tile,
// c =>
// AdditionalConnectorFilter(c) &&
// (!c.Blocked || c.Type == ConnectorType.DOOR_OFFICE) &&
// tile != Enemy.Location &&
// (!distances.ContainsKey(c.OtherTile(tile)) || distances[c.OtherTile(tile)] > distances[tile] + c.Value),
// t =>
// AdditionalTileFilter(t) &&
// (!Enemy.BlocksTile || EnemyManager.GetByLocation(t).All(e => !e.BlocksTile || e == Enemy)) &&
// Server.Players.All(p => p.Value.state.officeTileId != t.Id));
//
// neighbours.ForEach(t => distances[t] = distances[tile] + tile.GetConnector(t)!.Value);
//
// if (neighbours.Count != 0){
// neighbours.ForEach(t => CrawlStep(t));
// }
// }
public override Decision DecideNext(MapTile goal) {
if (Enemy.Location == null) return Decision.Wait();
Dictionary<MapTile, int> distances = Search(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);
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);
IEnumerable<ServerPlayer> players = Server.Players.Values.Where(p => p.State.OfficeTileId == goal.Id);
if (players.Count() == 1){
return Decision.Attack(players.First());
}

View file

@ -1,5 +1,6 @@
using GlobalClassLib;
using ONDServer.Map;
using ONDServer.Net;
using PacketLib;
namespace ONDServer.Enemies;
@ -8,17 +9,14 @@ public class SpotEnemy : Enemy {
public SpotEnemy(int difficulty) : base(difficulty, 6000) {
path = [MapManager.Get(10), MapManager.Get(11), MapManager.Get(12), MapManager.Get(13), MapManager.Get(14)];
pathId = 2;
// movementOpportunity = new(6000);
// SetDifficulty(difficulty);
}
public override string Name{ get; } = "Spot";
public override int TypeId{ get; } = (int)EnemyType.SPOT;
public override EnemyType Type{ get; } = EnemyType.SPOT;
public override bool BlocksTile{ get; set; } = false;
public bool Active{ get; set; } = false;
// private MovementOpportunity movementOpportunity;
private MapTile[] path;
private int pathId;
@ -27,42 +25,37 @@ public class SpotEnemy : Enemy {
public override void Reset() {
if (Location.Owner != null){
Location.Owner.state.power -= 200;
Location.Owner.State.Power -= 200;
}
pathId = 2;
Location = path[pathId];
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)
movementOpportunity.Start();
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++;
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 (MovementOpportunity.CheckAndRoll()){
if(!Active) {
Active = true;
movementOpportunity.Interval = 10_000;
movementOpportunity.GuaranteeSuccess(true);
MovementOpportunity.Interval = 10_000;
MovementOpportunity.GuaranteeSuccess(true);
Server.SendUpdateToAll([GameEvent.SPOT_SET_ACTIVE(Id, true)]);
}
else{
movementOpportunity.Interval = 6000;
movementOpportunity.GuaranteeSuccess(false);
movementOpportunity.Stop();
MovementOpportunity.Interval = 6000;
MovementOpportunity.GuaranteeSuccess(false);
MovementOpportunity.Stop();
if (p1WatchCounter > p2WatchCounter){
pathId++;
@ -94,7 +87,7 @@ public class SpotEnemy : Enemy {
public override void Spawn(MapTile location) {
base.Spawn(location);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(Type, Id, Difficulty, Location.Id)]);
}
}