Bugfixy, odstranění nepoužívaných tříd CameraSystem a ITargetingEnemy, přidání dedikované třídy ReturnToMenuElement, předělání pathfinding algoritmu z DFS na BFS, přesun správy obtížnosti do abstraktní třídy Enemy, přidání scriptů pro kompilaci na aplikaci nevyžadující dotnet

This commit is contained in:
Perry 2026-03-25 16:37:18 +01:00
parent ceac37920b
commit e5d746d597
24 changed files with 258 additions and 138 deletions

View file

@ -6,7 +6,7 @@ using PacketLib;
namespace ONDServer.Enemies;
public class DashEnemy : Enemy {
public DashEnemy(int difficulty) : base(difficulty) {
public DashEnemy(int difficulty) : base(difficulty, 5000) {
movementOpportunity = new(5000);
SetDifficulty(difficulty);
}
@ -15,7 +15,7 @@ public class DashEnemy : Enemy {
public override int TypeId{ get; } = (int)EnemyType.DASH;
public override bool BlocksTile{ get; set; } = false;
private MovementOpportunity movementOpportunity;
// private MovementOpportunity movementOpportunity;
private readonly (MapTile tile, Direction p1AttackDir, Direction p2AttackDir)[] positions =[
(MapManager.Get(7), Direction.EAST, Direction.WEST),
@ -34,10 +34,10 @@ 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 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);

View file

@ -6,9 +6,13 @@ namespace ONDServer.Enemies;
public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
public int Difficulty { get; protected set; }
protected MovementOpportunity movementOpportunity;
protected Enemy(int difficulty) {
Difficulty = difficulty;
protected Enemy(int difficulty, int movementInterval) {
movementOpportunity = new(movementInterval);
SetDifficulty(difficulty);
}
public abstract bool BlocksTile { get; set; }
@ -30,6 +34,9 @@ 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);
public virtual void SetDifficulty(int difficulty) {
Difficulty = difficulty;
movementOpportunity.MovementChance = ((5 + Math.Pow(1.5f, Difficulty)) * Math.Sign(Difficulty)) / (5 + Math.Pow(1.5f, 10));
}
}

View file

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

View file

@ -18,15 +18,13 @@ public class LurkEnemy : Enemy {
// 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 ServerPlayer? Target{ get; set; }
public LurkEnemy(int difficulty) : base(difficulty) {
public LurkEnemy(int difficulty) : base(difficulty, 5000) {
pathfinder = new LurkPathfinder(this, 1);
movementOpportunity = new MovementOpportunity(5000);
// movementOpportunity = new MovementOpportunity(5000);
Target = null;
SetDifficulty(difficulty);
// SetDifficulty(difficulty);
}
public override void Spawn(MapTile location) {
base.Spawn(location);
@ -37,20 +35,20 @@ public class LurkEnemy : Enemy {
}
public override void Reset() {
pathfinder.TakenPath.Clear();
pathfinder.TakenPath.Add(Location);
Target.state.power -= 30;
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();
pathfinder.TakenPath.Add(Location);
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 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();

View file

@ -11,23 +11,14 @@ public class MareEnemy : Enemy {
public override bool BlocksTile{ get; set; } = true;
private MarePathfinder pathfinder;
private MovementOpportunity movementOpportunity;
public ServerPlayer Target{ get; set; }
public MareEnemy(int difficulty) : base(difficulty) {
// private MovementOpportunity movementOpportunity;
public ServerPlayer? Target{ get; set; }
public MareEnemy(int difficulty) : base(difficulty, 6000) {
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);
// movementOpportunity = new MovementOpportunity(5000);
// SetDifficulty(difficulty);
}
public override void Reset() {
@ -38,19 +29,28 @@ public class MareEnemy : Enemy {
Location = decision.nextTile!;
}
Server.SendUpdateToAll([GameEvent.ENEMY_RESET(Id, Location.Id)]);;
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 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();
// stopwatch.Start()
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.Start();
pathfinder.TakenPath.Add(Location);
@ -64,6 +64,8 @@ public class MareEnemy : Enemy {
if (Location.Owner != null && Location.Lit){
Attack(Location.Owner);
}
if (Target == null) return;
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
switch (decision.type){

View file

@ -5,13 +5,13 @@ using PacketLib;
namespace ONDServer.Enemies;
public class NekoEnemy : Enemy {
private MovementOpportunity movementOpportunity;
// private MovementOpportunity movementOpportunity;
private RoamingPathfinder pathfinder;
public NekoEnemy(int difficulty) : base(difficulty) {
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);
// movementOpportunity = new MovementOpportunity(5000);
// SetDifficulty(difficulty);
}
@ -33,19 +33,20 @@ public class NekoEnemy : Enemy {
}
public override void Reset() {
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)];
pathfinder.TakenPath.Clear();
pathfinder.TakenPath.Add(Location);
Aggressive = false;
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 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();
@ -70,7 +71,8 @@ public class NekoEnemy : Enemy {
}
}
if (Target != null && (movementOpportunity.CheckAndRoll() || (Aggressive && GameLogic.NSecondUpdate))){
bool succeed = movementOpportunity.CheckAndRoll();
if (Target != null && (succeed || (Aggressive && GameLogic.NSecondUpdate))){
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
switch (decision.type){
case Pathfinder.Decision.MoveType:

View file

@ -12,38 +12,62 @@ public class RoamingPathfinder : Pathfinder {
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> Crawl(MapTile tile) {
Dictionary<MapTile, int> distances = new(){ [tile] = 0 };
CrawlStep(tile, distances);
protected Dictionary<MapTile, int> Search(MapTile startingTile) {
Dictionary<MapTile, int> distances = new(){ [startingTile] = 0 };
Queue<MapTile> tilesToSearch = new();
tilesToSearch.Enqueue(startingTile);
while (tilesToSearch.Count > 0){
MapTile currentTile = tilesToSearch.Dequeue();
List<MapTile> neighbours = GetNeighbours(currentTile,
c =>
AdditionalConnectorFilter(c) &&
(!c.Blocked || c.Type == ConnectorType.DOOR_OFFICE) &&
(!distances.ContainsKey(c.OtherTile(currentTile)) || distances[c.OtherTile(currentTile)] > distances[currentTile] + 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[currentTile] + currentTile.GetConnector(t)!.Value;
tilesToSearch.Enqueue(t);
});
}
return distances;
}
private void CrawlStep(MapTile tile, Dictionary<MapTile, int> distances) {
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, 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) {
Dictionary<MapTile, int> distances = Crawl(goal);
Dictionary<MapTile, int> distances = Search(goal);
List<MapTile> closerTiles = GetNeighbours(Enemy.Location,
c => AdditionalConnectorFilter(c) && !c.Blocked || c.Type == ConnectorType.DOOR_OFFICE,

View file

@ -5,11 +5,11 @@ using PacketLib;
namespace ONDServer.Enemies;
public class SpotEnemy : Enemy {
public SpotEnemy(int difficulty) : base(difficulty) {
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);
// movementOpportunity = new(6000);
// SetDifficulty(difficulty);
}
public override string Name{ get; } = "Spot";
@ -18,7 +18,7 @@ public class SpotEnemy : Enemy {
public bool Active{ get; set; } = false;
private MovementOpportunity movementOpportunity;
// private MovementOpportunity movementOpportunity;
private MapTile[] path;
private int pathId;
@ -26,15 +26,18 @@ public class SpotEnemy : Enemy {
private int p2WatchCounter = 0;
public override void Reset() {
if (Location.Owner != null){
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 SetDifficulty(int difficulty) {
// Difficulty = difficulty;
// movementOpportunity.MovementChance = (2 * Math.Sign(difficulty) + difficulty) / 12.0;
// }
public override void Update() {
if (GameLogic.NSecondUpdate){
@ -52,7 +55,7 @@ public class SpotEnemy : Enemy {
if (movementOpportunity.CheckAndRoll()){
if(!Active) {
Active = true;
movementOpportunity.Interval = 12_000;
movementOpportunity.Interval = 10_000;
movementOpportunity.GuaranteeSuccess(true);
Server.SendUpdateToAll([GameEvent.SPOT_SET_ACTIVE(Id, true)]);
}