Projekt přejmenován. Neko nastaven na výchozí pozici
This commit is contained in:
parent
1a27dd6fab
commit
ceac37920b
104 changed files with 873 additions and 208 deletions
70
ONDServer/CommandProcessor.cs
Normal file
70
ONDServer/CommandProcessor.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer;
|
||||
|
||||
public class CommandProcessor {
|
||||
public static void Evaluate(PlayerCommand[] commands, int pid) {
|
||||
ServerPlayer currentPlayer = Server.Players[pid];
|
||||
|
||||
foreach (var playerCommand in commands){
|
||||
switch (playerCommand.ID){
|
||||
case 0:
|
||||
Console.WriteLine($"C: Player {pid} switched to camera {playerCommand.Args[0]}");
|
||||
currentPlayer.state.camera = playerCommand.Args[0];
|
||||
Server.SendUpdateToAll([GameEvent.SWITCH_CAM(pid, playerCommand.Args[0])]);
|
||||
break;
|
||||
case 1:
|
||||
bool monitorState = playerCommand.Args[0] == 1;
|
||||
currentPlayer.state.monitorUp = monitorState;
|
||||
Console.WriteLine($"C: Player {pid} toggled camera {(monitorState ? "on" : "off")}");
|
||||
Server.SendUpdateToAll([GameEvent.TOGGLE_MONITOR(pid, monitorState)]);
|
||||
break;
|
||||
case 2:
|
||||
bool doorState = playerCommand.Args[1] == 1;
|
||||
currentPlayer.state.doorStates[playerCommand.Args[0]] = doorState;
|
||||
TileConnector? officeDoor = MapManager.Get(currentPlayer.state.officeTileId).GetConnector(currentPlayer.state.neighbouringTiles[playerCommand.Args[0]]);
|
||||
officeDoor.Blocked = doorState;
|
||||
|
||||
if (doorState){
|
||||
GameLogic.PowerConsumers[officeDoor] = (GameLogic.OfficeDoorUsage, pid);
|
||||
}
|
||||
else{
|
||||
GameLogic.PowerConsumers.Remove(officeDoor);
|
||||
}
|
||||
|
||||
Console.WriteLine($"C: Player {pid} {(doorState ? "closed" : "opened")} door {playerCommand.Args[0]}");
|
||||
Server.SendUpdateToAll([GameEvent.TOGGLE_DOOR_OFFICE(pid,playerCommand.Args[0] ,doorState)]);
|
||||
break;
|
||||
case 3:
|
||||
TileConnector? door = MapManager.Get(playerCommand.Args[0]).GetConnector(playerCommand.Args[1]);
|
||||
if(door == null) return;
|
||||
|
||||
door.Blocked = playerCommand.Args[2] == 1;
|
||||
if (door.Blocked){
|
||||
GameLogic.PowerConsumers[door] = (GameLogic.RemoteDoorUsage, pid);
|
||||
}
|
||||
else{
|
||||
GameLogic.PowerConsumers.Remove(door);
|
||||
}
|
||||
|
||||
Console.WriteLine($"C: Player {pid} {(door.Blocked ? "closed" : "opened")} door {(playerCommand.Args[0], playerCommand.Args[1])}");
|
||||
Server.SendUpdateToAll([GameEvent.TOGGLE_DOOR_REMOTE(pid, (playerCommand.Args[0], playerCommand.Args[1]), door.Blocked)]);
|
||||
break;
|
||||
case 4:
|
||||
bool lit = playerCommand.Args[1] == 1;
|
||||
MapTile lightTile = MapManager.Get(playerCommand.Args[0]);
|
||||
lightTile.Lit = lit;
|
||||
if (lit){
|
||||
GameLogic.PowerConsumers[lightTile] = (GameLogic.LightUsage, pid);
|
||||
}
|
||||
else{
|
||||
GameLogic.PowerConsumers.Remove(lightTile);
|
||||
}
|
||||
|
||||
Server.SendUpdateToAll([GameEvent.TOGGLE_LIGHT(pid, playerCommand.Args[0], lit)]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
70
ONDServer/Enemies/DashEnemy.cs
Normal file
70
ONDServer/Enemies/DashEnemy.cs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
using System.Net.Mime;
|
||||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
ONDServer/Enemies/Enemy.cs
Normal file
35
ONDServer/Enemies/Enemy.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public abstract class Enemy : GlobalEnemy<MapTile, TileConnector> {
|
||||
public int Difficulty { get; protected set; }
|
||||
|
||||
protected Enemy(int difficulty) {
|
||||
Difficulty = difficulty;
|
||||
}
|
||||
|
||||
public abstract bool BlocksTile { get; set; }
|
||||
public bool Spawned { get; set; }
|
||||
|
||||
public virtual void SpawnSilent(MapTile location) {
|
||||
Console.WriteLine($"!!! Silent spawn not implemented for enemy {TypeId} ({Name}), reverting to regular spawn");
|
||||
Spawn(location);
|
||||
}
|
||||
|
||||
public override void Spawn(MapTile location) {
|
||||
base.Spawn(location);
|
||||
Spawned = true;
|
||||
}
|
||||
|
||||
public abstract void Reset();
|
||||
|
||||
public virtual void Attack(ServerPlayer player) {
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_ATTACK(Id, player.state.pid)]);
|
||||
GameLogic.DeclareWinner(Server.OtherPlayer(player));
|
||||
}
|
||||
|
||||
public abstract void SetDifficulty(int difficulty);
|
||||
}
|
||||
32
ONDServer/Enemies/EnemyManager.cs
Normal file
32
ONDServer/Enemies/EnemyManager.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using ONDServer.Map;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public class EnemyManager {
|
||||
private static Dictionary<int, Enemy> enemies = new();
|
||||
|
||||
public static void Update() {
|
||||
foreach (var pair in enemies){
|
||||
if (pair.Value.Spawned) pair.Value.Update();
|
||||
}
|
||||
}
|
||||
|
||||
public static Enemy AddEnemy(Enemy enemy) {
|
||||
enemies.Add(enemy.Id, enemy);
|
||||
return enemy;
|
||||
}
|
||||
|
||||
public static Enemy[] GetByLocation(MapTile tile) {
|
||||
List<Enemy> output = new();
|
||||
foreach (var e in enemies.Values){
|
||||
if (e.Location == tile){
|
||||
output.Add(e);
|
||||
}
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
public static Enemy Get(int id) => enemies[id];
|
||||
public static Enemy[] GetAll() => enemies.Values.ToArray();
|
||||
}
|
||||
5
ONDServer/Enemies/ITargetingEnemy.cs
Normal file
5
ONDServer/Enemies/ITargetingEnemy.cs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
namespace ONDServer.Enemies;
|
||||
|
||||
public interface ITargetingEnemy {
|
||||
ServerPlayer Target { get; set; }
|
||||
}
|
||||
131
ONDServer/Enemies/LurkEnemy.cs
Normal file
131
ONDServer/Enemies/LurkEnemy.cs
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
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 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 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.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);
|
||||
|
||||
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 = ((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){
|
||||
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:
|
||||
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})");
|
||||
break;
|
||||
case Pathfinder.Decision.AttackType:
|
||||
Attack(decision.attackTarget!);
|
||||
break;
|
||||
case Pathfinder.Decision.ResetType:
|
||||
Reset();
|
||||
break;
|
||||
case Pathfinder.Decision.WaitType:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
130
ONDServer/Enemies/MareEnemy.cs
Normal file
130
ONDServer/Enemies/MareEnemy.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer.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 =
|
||||
((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();
|
||||
|
||||
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();
|
||||
|
||||
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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
62
ONDServer/Enemies/MovementOpportunity.cs
Normal file
62
ONDServer/Enemies/MovementOpportunity.cs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public class MovementOpportunity {
|
||||
public int Interval{ get; set; }
|
||||
// public double ChanceDenominator;
|
||||
public double MovementChance{
|
||||
get;
|
||||
set{
|
||||
field = value;
|
||||
GuaranteeSuccess(value >= 1);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Running => stopwatch.IsRunning;
|
||||
|
||||
private Stopwatch stopwatch = new();
|
||||
private long stopwatchOffset = 0;
|
||||
|
||||
private Random random = new();
|
||||
|
||||
public MovementOpportunity(int intervalMs, double movementChance) {
|
||||
Interval = intervalMs;
|
||||
MovementChance = movementChance;
|
||||
}
|
||||
|
||||
public MovementOpportunity(int intervalMs) {
|
||||
Interval = intervalMs;
|
||||
MovementChance = 1;
|
||||
}
|
||||
|
||||
public void Start() {
|
||||
stopwatch.Start();
|
||||
}
|
||||
public void Stop() {
|
||||
stopwatch.Stop();
|
||||
}
|
||||
|
||||
public bool Check() {
|
||||
if (stopwatch.ElapsedMilliseconds - stopwatchOffset >= Interval){
|
||||
int overshoot = (int)(stopwatch.ElapsedMilliseconds - stopwatchOffset - Interval);
|
||||
stopwatchOffset = stopwatch.ElapsedMilliseconds - overshoot;
|
||||
// stopwatch.Restart();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Roll() => roll();
|
||||
private Func<bool> roll = () => true;
|
||||
|
||||
public bool CheckAndRoll() {
|
||||
if (Check()) return Roll();
|
||||
return false;
|
||||
}
|
||||
|
||||
public void GuaranteeSuccess(bool value) {
|
||||
roll = value ? () => true : () => random.NextDouble() <= MovementChance;
|
||||
}
|
||||
|
||||
}
|
||||
140
ONDServer/Enemies/NekoEnemy.cs
Normal file
140
ONDServer/Enemies/NekoEnemy.cs
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public class NekoEnemy : Enemy {
|
||||
private MovementOpportunity movementOpportunity;
|
||||
private RoamingPathfinder pathfinder;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
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)]);
|
||||
}
|
||||
|
||||
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)];
|
||||
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 Update() {
|
||||
base.Update();
|
||||
|
||||
if (Location.Owner != null && Location.Lit && !Aggressive){
|
||||
Aggressive = true;
|
||||
Server.SendUpdateToAll([GameEvent.NEKO_ANGERED(Location.Owner.state.pid, Id)]);
|
||||
}
|
||||
|
||||
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{
|
||||
if (GameLogic.PhaseCounter > 1){
|
||||
Target = Server.Players[new Random().Next(2)];
|
||||
SetDifficulty(8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Target != null && (movementOpportunity.CheckAndRoll() || (Aggressive && GameLogic.NSecondUpdate))){
|
||||
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})");
|
||||
break;
|
||||
case Pathfinder.Decision.AttackType:
|
||||
Attack(decision.attackTarget!);
|
||||
break;
|
||||
case Pathfinder.Decision.ResetType:
|
||||
Reset();
|
||||
break;
|
||||
case Pathfinder.Decision.WaitType:
|
||||
Aggressive = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
36
ONDServer/Enemies/Pathfinder.cs
Normal file
36
ONDServer/Enemies/Pathfinder.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using ONDServer.Map;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public abstract class Pathfinder {
|
||||
public Enemy Enemy;
|
||||
public Pathfinder(Enemy enemy) {
|
||||
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();
|
||||
}
|
||||
|
||||
public class Decision {
|
||||
public int type;
|
||||
|
||||
public MapTile? nextTile;
|
||||
public ServerPlayer? attackTarget;
|
||||
|
||||
private Decision() { }
|
||||
|
||||
public static Decision Move(MapTile tile) => new() { type = MoveType, nextTile = tile };
|
||||
public static Decision Attack(ServerPlayer player) => new(){ type = AttackType, attackTarget = player };
|
||||
public static Decision Reset() => new(){ type = ResetType };
|
||||
public static Decision Wait() => new(){ type = WaitType };
|
||||
|
||||
public const int MoveType = 0;
|
||||
public const int AttackType = 1;
|
||||
public const int ResetType = 2;
|
||||
public const int WaitType = 3;
|
||||
|
||||
}
|
||||
}
|
||||
82
ONDServer/Enemies/RoamingPathfinder.cs
Normal file
82
ONDServer/Enemies/RoamingPathfinder.cs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
public class RoamingPathfinder : Pathfinder {
|
||||
|
||||
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;
|
||||
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);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
97
ONDServer/Enemies/SpotEnemy.cs
Normal file
97
ONDServer/Enemies/SpotEnemy.cs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
using GlobalClassLib;
|
||||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer.Enemies;
|
||||
|
||||
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);
|
||||
SetDifficulty(difficulty);
|
||||
}
|
||||
|
||||
public override string Name{ get; } = "Spot";
|
||||
public override int TypeId{ get; } = (int)EnemyType.SPOT;
|
||||
public override bool BlocksTile{ get; set; } = false;
|
||||
|
||||
public bool Active{ get; set; } = false;
|
||||
|
||||
private MovementOpportunity movementOpportunity;
|
||||
private MapTile[] path;
|
||||
private int pathId;
|
||||
|
||||
private int p1WatchCounter = 0;
|
||||
private int p2WatchCounter = 0;
|
||||
|
||||
public override void Reset() {
|
||||
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 (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++;
|
||||
|
||||
Console.WriteLine($"P1: {p1WatchCounter} | P2: {p2WatchCounter}");
|
||||
}
|
||||
}
|
||||
|
||||
if (movementOpportunity.CheckAndRoll()){
|
||||
if(!Active) {
|
||||
Active = true;
|
||||
movementOpportunity.Interval = 12_000;
|
||||
movementOpportunity.GuaranteeSuccess(true);
|
||||
Server.SendUpdateToAll([GameEvent.SPOT_SET_ACTIVE(Id, true)]);
|
||||
}
|
||||
else{
|
||||
movementOpportunity.Interval = 6000;
|
||||
movementOpportunity.GuaranteeSuccess(false);
|
||||
movementOpportunity.Stop();
|
||||
|
||||
if (p1WatchCounter > p2WatchCounter){
|
||||
pathId++;
|
||||
if (Location.GetConnector(path[pathId])!.Blocked){
|
||||
Reset();
|
||||
}
|
||||
else if (pathId == path.Length - 1){
|
||||
Attack(Server.P2);
|
||||
}
|
||||
}
|
||||
else if (p2WatchCounter > p1WatchCounter){
|
||||
pathId--;
|
||||
if (Location.GetConnector(path[pathId])!.Blocked){
|
||||
Reset();
|
||||
}
|
||||
else if (pathId == 0){
|
||||
Attack(Server.P1);
|
||||
}
|
||||
}
|
||||
|
||||
Location = path[pathId];
|
||||
Active = false;
|
||||
p1WatchCounter = 0;
|
||||
p2WatchCounter = 0;
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_MOVEMENT(Id, Location.Id), GameEvent.SPOT_SET_ACTIVE(Id, false)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Spawn(MapTile location) {
|
||||
base.Spawn(location);
|
||||
Server.SendUpdateToAll([GameEvent.ENEMY_SPAWN(TypeId, Id, Difficulty, Location.Id)]);
|
||||
|
||||
}
|
||||
}
|
||||
207
ONDServer/GameLogic.cs
Normal file
207
ONDServer/GameLogic.cs
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
using System.Diagnostics;
|
||||
using GlobalClassLib;
|
||||
using ONDServer.Enemies;
|
||||
using ONDServer.Map;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer;
|
||||
|
||||
public class GameLogic {
|
||||
|
||||
public const int START_CAMERA = 12;
|
||||
|
||||
private static MovementOpportunity secondCycleTimer = new(1000);
|
||||
private static MovementOpportunity gamePhaseTimer = new(60_000);
|
||||
public static bool NSecondUpdate{ get; private set; } = false;
|
||||
|
||||
public static MapTile P1Office;
|
||||
public static MapTile P2Office;
|
||||
public static MapTile[] Offices =>[P1Office, P2Office];
|
||||
|
||||
public static int OfficeDoorUsage{ get; set; } = 10;
|
||||
public static int RemoteDoorUsage{ get; set; } = 3;
|
||||
public static int LightUsage{ get; set; } = 2;
|
||||
public static int PhaseCounter{ get; private set; } = 1;
|
||||
|
||||
private static List<List<Enemy>> spawnOrder;
|
||||
|
||||
private static int[] defaultSpawnPoints = [2, 22];
|
||||
|
||||
private static Random random = new();
|
||||
|
||||
private static bool unlimitedPower = false; // Debug
|
||||
|
||||
// public const int POWER_MAX = 1000;
|
||||
// public static int P1Power{ get; set; } = Power.MAX_POWER_VALUE;
|
||||
// public static int P2Power{ get; set; } = Power.MAX_POWER_VALUE;
|
||||
// public static int[] PowerValues =>[P1Power, P2Power];
|
||||
|
||||
public static Dictionary<object, (int usage, int pid)> PowerConsumers{ get; set; } = new();
|
||||
|
||||
public static void Init() {
|
||||
// Create map
|
||||
MapManager.InitMap();
|
||||
P1Office = MapManager.Get(10);
|
||||
P2Office = MapManager.Get(14);
|
||||
//
|
||||
// foreach (var player in Server.Players.Values){
|
||||
// player.state.power = Power.MAX_POWER_VALUE;
|
||||
// }
|
||||
|
||||
//Send map to client
|
||||
TileConnector[] connectors = MapManager.GetAllConnectors();
|
||||
int[] connectorsConverted = new int[connectors.Length * 4];
|
||||
for (int i = 0; i < connectors.Length; i++){
|
||||
connectorsConverted[i * 4] = connectors[i].Tiles.tile1.Id;
|
||||
connectorsConverted[i * 4 + 1] = connectors[i].Tiles.tile2.Id;
|
||||
connectorsConverted[i * 4 + 2] = (int)connectors[i].Type;
|
||||
connectorsConverted[i * 4 + 3] = connectors[i].Owner == null ? -1 : connectors[i].Owner.state.pid;
|
||||
}
|
||||
|
||||
List<int> p1Tiles = new();
|
||||
List<int> p2Tiles = new();
|
||||
List<int> neutralTiles = new();
|
||||
foreach (var tile in MapManager.GetAllTiles()){
|
||||
if(tile.Owner == null) neutralTiles.Add(tile.Id);
|
||||
else if(tile.Owner == Server.P1) p1Tiles.Add(tile.Id);
|
||||
else if(tile.Owner == Server.P2) p2Tiles.Add(tile.Id);
|
||||
}
|
||||
|
||||
Server.SendPacket(new MapInitPacket{Connectors = connectorsConverted, UpsideDown = false, YourTiles = p1Tiles.ToArray(), OpponentTiles = p2Tiles.ToArray(), LitTiles = neutralTiles.ToArray()}, Server.P1.peer);
|
||||
Server.SendPacket(new MapInitPacket{Connectors = connectorsConverted, UpsideDown = true, YourTiles = p2Tiles.ToArray(), OpponentTiles = p1Tiles.ToArray(), LitTiles = neutralTiles.ToArray()}, Server.P2.peer);
|
||||
|
||||
spawnOrder = [[new SpotEnemy(3), new MareEnemy(3)], [new DashEnemy(6)], [new LurkEnemy(3), new NekoEnemy(3)]];
|
||||
|
||||
Thread.Sleep(3000);
|
||||
secondCycleTimer.Start();
|
||||
gamePhaseTimer.Start();
|
||||
Server.SendUpdateToAll([GameEvent.GAME_START()]);
|
||||
|
||||
// EnemyManager.AddEnemy(new SpotEnemy(10)).Spawn(MapManager.Get(12));
|
||||
// EnemyManager.AddEnemy(new LurkEnemy(10)).Spawn(MapManager.Get(12));
|
||||
// EnemyManager.AddEnemy(new NekoEnemy(10)).Spawn(MapManager.Get(2));
|
||||
// EnemyManager.AddEnemy(new DashEnemy(10)).Spawn(MapManager.Get(12));
|
||||
// EnemyManager.AddEnemy(new MareEnemy(10)).Spawn(MapManager.Get(22));
|
||||
|
||||
EnemyManager.AddEnemy(new LurkEnemy(4)).Spawn(MapManager.Get(2));
|
||||
EnemyManager.AddEnemy(new NekoEnemy(4)).Spawn(MapManager.Get(22));
|
||||
|
||||
}
|
||||
public static void Update() {
|
||||
if(secondCycleTimer.Check()) NSecondUpdate = true;
|
||||
|
||||
if (NSecondUpdate){
|
||||
(int p1, int p2) powerUsage = CalculatePowerUsage();
|
||||
Server.P1.state.power -= powerUsage.p1;
|
||||
Server.P2.state.power -= powerUsage.p2;
|
||||
|
||||
foreach (var player in Server.Players.Values){
|
||||
if (player.state.power < 0){
|
||||
player.state.poweredOut = true;
|
||||
player.state.power = 0;
|
||||
PowerOut(player);
|
||||
}
|
||||
// Server.SendUpdateToAll([GameEvent.POWER_OUT(player.state.pid)]);
|
||||
}
|
||||
|
||||
if (unlimitedPower){ // Debug
|
||||
Server.P1.state.power = Power.MAX_POWER_VALUE;
|
||||
Server.P2.state.power = Power.MAX_POWER_VALUE;
|
||||
}
|
||||
|
||||
Server.SendUpdateToAll([GameEvent.POWER_TICK(Server.P1.state.pid, Server.P1.state.power), GameEvent.POWER_TICK(Server.P2.state.pid, Server.P2.state.power)]);
|
||||
|
||||
if (gamePhaseTimer.Check()){
|
||||
NextPhase();
|
||||
}
|
||||
}
|
||||
|
||||
EnemyManager.Update();
|
||||
|
||||
NSecondUpdate = false;
|
||||
}
|
||||
|
||||
public static void DeclareWinner(ServerPlayer player) {
|
||||
if (Server.Players.Count == 2){
|
||||
Server.SendUpdateToAll([GameEvent.PLAYER_WIN(player.state.pid)]);
|
||||
Console.WriteLine("Player " + player.state.pid + " won!");
|
||||
}
|
||||
Thread.Sleep(1000);
|
||||
Server.Stop();
|
||||
|
||||
if (!Server.AutoStop){
|
||||
Program.Restart();
|
||||
}
|
||||
}
|
||||
|
||||
private static (int p1Usage, int p2Usage) CalculatePowerUsage() {
|
||||
int p1Usage = 0;
|
||||
int p2Usage = 0;
|
||||
|
||||
foreach (var consumer in PowerConsumers){
|
||||
if (consumer.Value.pid == Server.P1.state.pid){
|
||||
p1Usage += consumer.Value.usage;
|
||||
}
|
||||
else if (consumer.Value.pid == Server.P2.state.pid){
|
||||
p2Usage += consumer.Value.usage;
|
||||
}
|
||||
}
|
||||
|
||||
return (p1Usage, p2Usage);
|
||||
}
|
||||
|
||||
private static void PowerOut(ServerPlayer player) {
|
||||
foreach (var door in MapManager.GetDoors(player)){
|
||||
door.Blocked = false;
|
||||
}
|
||||
foreach (var tile in MapManager.GetAllTiles().Where(t => t.Owner == player)){
|
||||
tile.Lit = false;
|
||||
}
|
||||
PowerConsumers.Where(c => c.Value.pid == player.state.pid).ToList().ForEach(c => PowerConsumers.Remove(c.Key));
|
||||
Server.SendUpdateToAll([GameEvent.POWER_OUT(player.state.pid)]);
|
||||
}
|
||||
|
||||
private static void NextPhase() {
|
||||
PhaseCounter++;
|
||||
Server.SendUpdateToAll([GameEvent.NEXT_PHASE()]);
|
||||
|
||||
int roll = spawnOrder.Count > 0 ? random.Next(3) : random.Next(1,3);
|
||||
switch (roll){
|
||||
case 0:
|
||||
int spawnRoll = random.Next(spawnOrder[0].Count);
|
||||
SpawnNext(spawnOrder[0][spawnRoll]);
|
||||
spawnOrder[0].RemoveAt(spawnRoll);
|
||||
if (spawnOrder[0].Count == 0){
|
||||
spawnOrder.RemoveAt(0);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
Enemy[] allEnemies = EnemyManager.GetAll();
|
||||
for (int i = 0; i < 2; i++){
|
||||
Enemy enemy = allEnemies[random.Next(allEnemies.Length)];
|
||||
enemy.SetDifficulty(enemy.Difficulty + 2);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
Enemy[] enemies = EnemyManager.GetAll();
|
||||
for (int i = 0; i < enemies.Length; i++){
|
||||
enemies[i].SetDifficulty(enemies[i].Difficulty + 1);
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void SpawnNext(Enemy enemy) {
|
||||
if ((EnemyType)enemy.TypeId == EnemyType.NEKO || (EnemyType)enemy.TypeId == EnemyType.LURK || (EnemyType)enemy.TypeId == EnemyType.MARE){
|
||||
MapTile[] spawnLocations = defaultSpawnPoints.Select(i => MapManager.Get(i)).Where(t => EnemyManager.GetByLocation(t).All(e => !e.BlocksTile)).ToArray();
|
||||
if (spawnLocations.Length == 0){
|
||||
return;
|
||||
}
|
||||
enemy.Spawn(spawnLocations[random.Next(spawnLocations.Length)]);
|
||||
}
|
||||
else if ((EnemyType)enemy.TypeId == EnemyType.SPOT || (EnemyType)enemy.TypeId == EnemyType.DASH){
|
||||
enemy.Spawn(MapManager.Get(12));
|
||||
}
|
||||
}
|
||||
}
|
||||
104
ONDServer/Map/MapManager.cs
Normal file
104
ONDServer/Map/MapManager.cs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
using GlobalClassLib;
|
||||
|
||||
namespace ONDServer.Map;
|
||||
|
||||
public static class MapManager {
|
||||
private static MapTile[,] map = new MapTile[5, 5];
|
||||
private static List<TileConnector> doors = new();
|
||||
private static List<TileConnector> doorsP1 = new();
|
||||
private static List<TileConnector> doorsP2 = new();
|
||||
|
||||
public static MapTile Get((int x, int y) coords) => map[coords.x, coords.y];
|
||||
public static MapTile Get(int tileId) => Get(IdToCoords(tileId));
|
||||
|
||||
|
||||
private static Dictionary<(int x1, int y1), (int x2, int y2, int value, ConnectorType type)[]> halfConnectors = new(){
|
||||
[(0, 0)] =[(1, 0, 1, ConnectorType.HALL), (0, 1, 1, ConnectorType.DOOR_REMOTE)],
|
||||
[(1, 0)] =[(2, 0, 1, ConnectorType.DOOR_OFFICE), (1, 1, 1, ConnectorType.DOOR_REMOTE)],
|
||||
[(3, 0)] =[(2, 0, 1, ConnectorType.DOOR_OFFICE), (4, 0, 1, ConnectorType.HALL), (3, 1, 1, ConnectorType.DOOR_REMOTE)],
|
||||
[(4, 0)] =[(4, 1, 1, ConnectorType.DOOR_REMOTE)],
|
||||
[(0, 1)] =[(1, 1, 1, ConnectorType.HALL)],
|
||||
[(1, 1)] =[(1, 2, 1, ConnectorType.DOOR_REMOTE)],
|
||||
[(2, 1)] =[(2, 2, 2, ConnectorType.HALL), (2, 0, 1, ConnectorType.DOOR_OFFICE)],
|
||||
[(3, 1)] =[(3, 2, 1, ConnectorType.DOOR_REMOTE), (4, 1, 1, ConnectorType.HALL)]
|
||||
};
|
||||
|
||||
private static Dictionary<(int x1, int y1), (int x2, int y2, int value)[]> mainHallwayConnectors = new(){
|
||||
[(0,2)] = [(1,2,1)],
|
||||
[(1,2)] = [(2,2,1)],
|
||||
[(2,2)] = [(3,2,1)],
|
||||
[(3,2)] = [(4,2,1)]
|
||||
};
|
||||
|
||||
private static (int x, int y)[] ventTiles =[(0, 1), (4, 1), (2, 2), (0, 3), (4, 3)];
|
||||
|
||||
public static void InitMap() {
|
||||
for (int i = 0; i < 5; i++){
|
||||
for (int j = 0; j < 2; j++){
|
||||
map[i, j] = new MapTile(CoordsToId(i, j)){Owner = Server.P1};
|
||||
}
|
||||
map[i, 2] = new MapTile(CoordsToId(i, 2)){Lit = true};
|
||||
for (int j = 3; j < 5; j++){
|
||||
map[i, j] = new MapTile(CoordsToId(i, j)){Owner = Server.P2};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var con in mainHallwayConnectors){
|
||||
map[con.Key.x1, con.Key.y1].AddConnectors(con.Value.Select(c => new TileConnector(map[c.x2, c.y2], ConnectorType.HALL, c.value)).ToArray());
|
||||
}
|
||||
|
||||
foreach (var con in halfConnectors){
|
||||
map[con.Key.x1, con.Key.y1].AddConnectors(con.Value.Select(c => new TileConnector(map[c.x2, c.y2], c.type, c.value){Owner = Server.P1}).ToArray());
|
||||
}
|
||||
foreach (var con in halfConnectors){
|
||||
map[4 - con.Key.x1, 4 - con.Key.y1].AddConnectors(con.Value.Select(c => new TileConnector(map[4 - c.x2, 4 - c.y2], c.type, c.value){Owner = Server.P2}).ToArray());
|
||||
}
|
||||
|
||||
doors = GetAllConnectors().Where(c => c.Type == ConnectorType.DOOR_OFFICE || c.Type == ConnectorType.DOOR_REMOTE).ToList();
|
||||
doorsP1 = doors.Where(c => c.Owner == Server.P1).ToList();
|
||||
doorsP2 = doors.Where(c => c.Owner == Server.P2).ToList();
|
||||
|
||||
foreach (var tile1Coords in ventTiles){
|
||||
MapTile tile1 = map[tile1Coords.x, tile1Coords.y];
|
||||
foreach (var tile2Coords in ventTiles){
|
||||
MapTile tile2 = map[tile2Coords.x, tile2Coords.y];
|
||||
if(tile1.GetConnector(tile2) != null || tile1 == tile2) continue;
|
||||
tile1.AddConnector(new TileConnector(tile2, ConnectorType.VENT, 2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TileConnector[] GetAllConnectors() {
|
||||
List<TileConnector> connectors = new();
|
||||
|
||||
for (int i = 0; i < 5; i++){
|
||||
for (int j = 0; j < 5; j++){
|
||||
MapTile tile = map[i, j];
|
||||
Array.ForEach(tile.GetAllConnectors(), c => {
|
||||
if(tile.Id < c.OtherTile(tile).Id)
|
||||
connectors.Add(c);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return connectors.ToArray();
|
||||
}
|
||||
|
||||
public static MapTile[] GetAllTiles() {
|
||||
List<MapTile> tiles = new();
|
||||
|
||||
foreach (var tile in map){
|
||||
tiles.Add(tile);
|
||||
}
|
||||
|
||||
return tiles.ToArray();
|
||||
}
|
||||
|
||||
public const int ID_X_OFFSET = 5; // map grid height
|
||||
public static int CoordsToId(int x, int y) => x * ID_X_OFFSET + y;
|
||||
public static (int, int) IdToCoords(int id) => (id / ID_X_OFFSET, id % ID_X_OFFSET);
|
||||
|
||||
public static TileConnector[] GetDoors() => doors.ToArray();
|
||||
public static TileConnector[] GetDoors(ServerPlayer player) => player == Server.P1 ? doorsP1.ToArray() : doorsP2.ToArray();
|
||||
}
|
||||
43
ONDServer/Map/MapTile.cs
Normal file
43
ONDServer/Map/MapTile.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using System.Net.Security;
|
||||
using GlobalClassLib;
|
||||
|
||||
namespace ONDServer.Map;
|
||||
|
||||
public class MapTile : GlobalMapTile<TileConnector, MapTile> {
|
||||
public ServerPlayer? Owner{ get; set; }
|
||||
|
||||
public MapTile(int id) : base(id, MapManager.IdToCoords(id)) {
|
||||
}
|
||||
|
||||
// public int Id { get; private set; }
|
||||
// public ServerPlayer Owner { get; private set; }
|
||||
// public bool Lit { get; set; } = false;
|
||||
//
|
||||
// private List<TileConnector> connectors = new();
|
||||
//
|
||||
// public MapTile(int id, ServerPlayer owner) {
|
||||
// Id = id;
|
||||
// Owner = owner;
|
||||
// }
|
||||
// public void AddConnector(MapTile tile, TileConnector.ConnectorType type, int value) {
|
||||
// connectors.Add(new TileConnector(this, tile, type, value));
|
||||
// tile.connectors.Add(new TileConnector(tile, this, type, value));
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public void AddConnectors((MapTile tile, TileConnector.ConnectorType type, int value)[] connectors) =>
|
||||
// Array.ForEach(connectors, c => AddConnector(c.tile, c.type, c.value));
|
||||
//
|
||||
// public override string ToString() => $"{PositionAsString} -> {string.Join(", ", connectors.Select(c => c.Tiles.Item2.PositionAsString))}";
|
||||
// public string PositionAsString => $"[{Id}]"; // for debug purposes
|
||||
//
|
||||
// public TileConnector? GetConnector(int id) {
|
||||
// foreach (var con in connectors){
|
||||
// if (con.Tiles.Item2.Id == id) return con;
|
||||
// }
|
||||
//
|
||||
// return null;
|
||||
// }
|
||||
|
||||
}
|
||||
45
ONDServer/Map/TileConnector.cs
Normal file
45
ONDServer/Map/TileConnector.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using GlobalClassLib;
|
||||
|
||||
namespace ONDServer.Map;
|
||||
|
||||
public class TileConnector : GlobalTileConnector<MapTile,TileConnector> {
|
||||
public int Value{ get; set; }
|
||||
public ServerPlayer? Owner{ get; set; }
|
||||
public TileConnector(MapTile tile1, MapTile tile2, ConnectorType type, int value) : base(tile1, tile2, type) {
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public TileConnector(MapTile tile2, ConnectorType type, int value) : base(tile2, type) {
|
||||
Value = value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// private readonly MapTile _tile1;
|
||||
// private readonly MapTile _tile2;
|
||||
//
|
||||
// public TileConnector(MapTile tile1, MapTile tile2, TileConnector.ConnectorType type, int value) {
|
||||
// _tile1 = tile1;
|
||||
// _tile2 = tile2;
|
||||
// Type = type;
|
||||
// Value = value;
|
||||
// }
|
||||
//
|
||||
// public (MapTile, MapTile) Tiles => (tile1: _tile1, tile2: _tile2);
|
||||
//
|
||||
// public ConnectorType Type { get; set; }
|
||||
// public bool Blocked { get; set; } = false;
|
||||
// public int Value{ get; set; }
|
||||
//
|
||||
// public MapTile OtherTile(MapTile tile) => Tiles.Item1 == tile ? Tiles.Item2 : Tiles.Item1;
|
||||
//
|
||||
// public override string ToString() => $"Con ({Tiles.Item1.PositionAsString} -> {Tiles.Item2.PositionAsString})";
|
||||
// public enum ConnectorType {
|
||||
// HALL,
|
||||
// DOOR_REMOTE,
|
||||
// DOOR_OFFICE,
|
||||
// VENT
|
||||
// }
|
||||
|
||||
public override TileConnector Clone() => new(Tiles.tile1, Tiles.tile2, Type, Value){Owner = Owner};
|
||||
}
|
||||
28
ONDServer/ONDServer.csproj
Normal file
28
ONDServer/ONDServer.csproj
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>14</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<OutputPath>../bin/Debug/Server</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<OutputPath>../bin/Server</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="LiteNetLib" Version="1.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GlobalClassLib\GlobalClassLib.csproj" />
|
||||
<ProjectReference Include="..\PacketLib\PacketLib.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
18
ONDServer/Program.cs
Normal file
18
ONDServer/Program.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System.Diagnostics;
|
||||
|
||||
namespace ONDServer;
|
||||
|
||||
public class Program {
|
||||
public static void Main(string[] args) {
|
||||
if (args.Contains("--persistent")){
|
||||
Server.AutoStop = false;
|
||||
}
|
||||
Server.Start(9012);
|
||||
}
|
||||
|
||||
public static void Restart() {
|
||||
Console.WriteLine("Game concluded -> restarting server");
|
||||
Process.Start(new ProcessStartInfo{ FileName = Environment.ProcessPath, UseShellExecute = false });
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
204
ONDServer/Server.cs
Normal file
204
ONDServer/Server.cs
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using ONDServer.Map;
|
||||
using GlobalClassLib;
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer;
|
||||
|
||||
public class Server {
|
||||
public static ServerPlayer P1;
|
||||
public static ServerPlayer P2;
|
||||
public static readonly Dictionary<int, ServerPlayer> Players = new();
|
||||
public static ServerPlayer OtherPlayer(ServerPlayer player) => player.state.pid == P1.state.pid ? P2 : P1;
|
||||
|
||||
public static bool AutoStop{ get; set; } = true;
|
||||
|
||||
private static EventBasedNetListener listener;
|
||||
private static NetManager server;
|
||||
|
||||
private const int TargetTickRate = 30;
|
||||
private const double TargetDeltaTime = 1.0 / TargetTickRate;
|
||||
private const long TargetTickTimeMs = 1000 / TargetTickRate;
|
||||
|
||||
private static NetDataWriter writer;
|
||||
private static NetPacketProcessor processor;
|
||||
|
||||
private static bool isRunning;
|
||||
|
||||
public static void Start(int port) {
|
||||
writer = new NetDataWriter();
|
||||
processor = new NetPacketProcessor();
|
||||
|
||||
NestedTypeManager.AutoRegister(processor);
|
||||
|
||||
processor.SubscribeReusable<JoinPacket, NetPeer>(OnJoinReceived);
|
||||
processor.SubscribeReusable<PlayerCommandPacket, NetPeer>(OnCommandReceived);
|
||||
|
||||
listener = new EventBasedNetListener(); // ← Initialize the listener!
|
||||
|
||||
|
||||
server = new NetManager(listener){
|
||||
AutoRecycle = true
|
||||
};
|
||||
|
||||
Console.WriteLine($"Starting server on {port}");
|
||||
|
||||
listener.ConnectionRequestEvent += request => {
|
||||
Console.WriteLine($"Connection Request from {request.RemoteEndPoint}");
|
||||
if (Players.Count >= 2){
|
||||
Console.WriteLine($"{request.RemoteEndPoint} denied, server full");
|
||||
return;
|
||||
}
|
||||
request.Accept();
|
||||
};
|
||||
|
||||
listener.NetworkReceiveEvent += (peer, reader, channel, method) => {
|
||||
OnNetworkReceive(peer, reader, method);
|
||||
};
|
||||
|
||||
listener.PeerDisconnectedEvent += (peer, info) => {
|
||||
OnPeerDisconnected(peer, info);
|
||||
};
|
||||
|
||||
server.Start(port);
|
||||
|
||||
Run();
|
||||
}
|
||||
|
||||
public static void SendPacket<T>(T packet, NetPeer peer, DeliveryMethod deliveryMethod = DeliveryMethod.ReliableOrdered) where T : class, new() {
|
||||
if (peer != null) {
|
||||
writer.Reset();
|
||||
processor.Write(writer, packet);
|
||||
peer.Send(writer, deliveryMethod);
|
||||
}
|
||||
}
|
||||
public static void SendPacket<T>(T packet, int pid, DeliveryMethod deliveryMethod = DeliveryMethod.ReliableOrdered) where T : class, new() {
|
||||
SendPacket(packet, Players[pid].peer, deliveryMethod);
|
||||
}
|
||||
public static void SendPacketToAll<T>(T packet, DeliveryMethod deliveryMethod = DeliveryMethod.ReliableOrdered) where T : class, new() {
|
||||
foreach (var player in Players.Values){
|
||||
SendPacket(packet, player.peer, deliveryMethod);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SendUpdate(GameEvent[] gevents, int pid) {
|
||||
// SendPacket(new UpdatePlayerPacket{eventQueue = new EventQueue{Events = events}}, pid);
|
||||
SendPacket(new UpdatePlayerPacket{events = gevents}, pid);
|
||||
|
||||
}
|
||||
|
||||
public static void SendUpdateToAll(GameEvent[] gevents) {
|
||||
// SendPacketToAll(new UpdatePlayerPacket{eventQueue = new EventQueue{Events = events}});
|
||||
SendPacketToAll(new UpdatePlayerPacket{events = gevents});
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void OnJoinReceived(JoinPacket packet, NetPeer peer) {
|
||||
Console.WriteLine($"Received join from {packet.username} (pid: {(uint)peer.Id})");
|
||||
|
||||
ServerPlayer newPlayer = (Players[peer.Id] = new ServerPlayer {
|
||||
peer = peer,
|
||||
state = new PlayerState {
|
||||
pid = peer.Id,
|
||||
camera = GameLogic.START_CAMERA,
|
||||
doorStates = [false, false, false],
|
||||
power = Power.MAX_POWER_VALUE,
|
||||
poweredOut = false
|
||||
},
|
||||
username = packet.username
|
||||
});
|
||||
|
||||
|
||||
if (Players.Count == 1){
|
||||
newPlayer.state.officeTileId = 10;
|
||||
newPlayer.state.neighbouringTiles = [5, 11, 15];
|
||||
SendPacket(new JoinAcceptPacket { state = newPlayer.state }, peer);
|
||||
P1 = newPlayer;
|
||||
}
|
||||
else{
|
||||
newPlayer.state.officeTileId = 14;
|
||||
newPlayer.state.neighbouringTiles = [19, 13, 9];
|
||||
|
||||
SendPacket(new JoinAcceptPacket { state = newPlayer.state }, peer);
|
||||
P2 = newPlayer;
|
||||
|
||||
SendPacket(new OpponentInitPacket{state = newPlayer.state, username = newPlayer.username}, P1.peer);
|
||||
SendPacket(new OpponentInitPacket{state = P1.state, username = P1.username}, P2.peer);
|
||||
GameLogic.Init();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) {
|
||||
processor.ReadAllPackets(reader, peer);
|
||||
}
|
||||
|
||||
public static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) {
|
||||
GameLogic.DeclareWinner(OtherPlayer(Players.Values.First(p => Equals(p.peer, peer))));
|
||||
|
||||
if (peer.Tag != null) {
|
||||
Players.Remove(peer.Id);
|
||||
}
|
||||
}
|
||||
|
||||
public static void OnCommandReceived(PlayerCommandPacket packet, NetPeer peer) {
|
||||
CommandProcessor.Evaluate(packet.commands, peer.Id);
|
||||
}
|
||||
|
||||
public static void Update() {
|
||||
server.PollEvents();
|
||||
// Console.WriteLine("update");
|
||||
GameLogic.Update();
|
||||
}
|
||||
|
||||
public static void Run(CancellationToken cancellationToken = default)
|
||||
{
|
||||
isRunning = true;
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
long previousTicks = 0;
|
||||
|
||||
while (isRunning && !cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
long currentTicks = stopwatch.ElapsedMilliseconds;
|
||||
long elapsed = currentTicks - previousTicks;
|
||||
|
||||
if (elapsed >= TargetTickTimeMs)
|
||||
{
|
||||
Update();
|
||||
|
||||
previousTicks = currentTicks;
|
||||
if (elapsed > TargetTickTimeMs * 2)
|
||||
{
|
||||
Console.WriteLine($"Warning: Tick took {elapsed}ms (target: {TargetTickTimeMs}ms)");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
int sleepTime = (int)(TargetTickTimeMs - elapsed - 2);
|
||||
if (sleepTime > 0)
|
||||
{
|
||||
Thread.Sleep(sleepTime);
|
||||
}
|
||||
|
||||
while (stopwatch.ElapsedMilliseconds - previousTicks < TargetTickTimeMs){
|
||||
Thread.SpinWait(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Stop()
|
||||
{
|
||||
isRunning = false;
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
ONDServer/ServerPlayer.cs
Normal file
10
ONDServer/ServerPlayer.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using LiteNetLib;
|
||||
using PacketLib;
|
||||
|
||||
namespace ONDServer;
|
||||
|
||||
public class ServerPlayer {
|
||||
public NetPeer peer;
|
||||
public PlayerState state;
|
||||
public string username;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue