Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using maddox.game;
using maddox.game.world;
using maddox.GP;
public class Mission : AMission
{
#region InGameActors
internal class InGameActors
{
internal class Actor
{
internal bool destroyed = false;
internal AiActor actor { get; set; }
internal DateTime CreationTime { get; set; }
internal int MissionNumber { get; set; }
public Actor(AiActor actor, int missionNumber)
{
this.actor = actor;
this.CreationTime = DateTime.Now;
this.MissionNumber = missionNumber;
this.destroyed = false;
}
}
internal List<Actor> ActorsInGame = new List<Actor>();
private bool IsActorDestroyable(AiActor actor)
{
bool ActorDestroyable = true;
//Check if actor is empty (no Player)
if (actor is AiCart)
{
if ((actor as AiCart).ExistCabin(0))
for (int i = 0; i < (actor as AiCart).Places(); i++)
{
if ((actor as AiCart).Player(i) != null)
{
ActorDestroyable = false;
break;
}
}
}
return ActorDestroyable;
}
private void destroyActor(AiActor actor)
{
if (actor != null)
if (actor is AiCart)
(actor as AiCart).Destroy();
}
public void Add(AiActor actor, int missionNumber)
{
if (actor != null && actor is AiCart)
ActorsInGame.Add(new Actor(actor, missionNumber));
}
public void Removeable(AiActor actor)
{
if (ActorsInGame.Exists(item => item.actor == actor))
ActorsInGame[ActorsInGame.FindIndex(item => item.actor == actor)].destroyed = true;
}
public List<AiActor> GetActors()
{
List<AiActor> tmpActors = new List<AiActor>();
ActorsInGame.ForEach(item => { tmpActors.Add(item.actor); });
return tmpActors;
}
public void DestroyActor(AiActor actor)
{
if (ActorsInGame.Exists(item => item.actor == actor))
{
int i = ActorsInGame.FindIndex(item => item.actor == actor);
if (IsActorDestroyable(ActorsInGame[i].actor))
{
destroyActor(ActorsInGame[i].actor);
}
}
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(int missionNumber)
{
if (missionNumber == -1)
{
ActorsInGame.ForEach(item => { if (IsActorDestroyable(item.actor)) destroyActor(item.actor); });
}
else if (ActorsInGame.Exists(item => item.MissionNumber == missionNumber))
ActorsInGame.ForEach(item =>
{
if (item.MissionNumber == missionNumber)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(int missionNumber, int army)
{
if (ActorsInGame.Exists(item => item.MissionNumber == missionNumber && item.actor.Army() == army))
ActorsInGame.ForEach(item =>
{
if (item.MissionNumber == missionNumber && item.actor.Army() == army)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(Point3d Position, double Radius)
{
if (ActorsInGame.Exists(item => item.actor.Pos().distance(ref Position) < Radius))
ActorsInGame.ForEach(item =>
{
if (item.actor.Pos().distance(ref Position) < Radius)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(int missionNumber, Point3d Position, double Radius)
{
if (ActorsInGame.Exists(item => (item.MissionNumber == missionNumber && item.actor.Pos().distance(ref Position) < Radius)))
ActorsInGame.ForEach(item =>
{
if (item.MissionNumber == missionNumber && item.actor.Pos().distance(ref Position) < Radius)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(int missionNumber, params AiGroundActorType[] types)
{
foreach (AiGroundActorType abat in types)
{
if (ActorsInGame.Exists(item => item.MissionNumber == missionNumber && (item.actor as AiGroundActor).Type() == abat))
ActorsInGame.ForEach(item =>
{
if (item.MissionNumber == missionNumber && (item.actor as AiGroundActor).Type() == abat)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
}
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(TimeSpan DestroyAfterTime)
{
DateTime tmpTime = DateTime.Now.Subtract(DestroyAfterTime);
if (ActorsInGame.Exists(item => item.CreationTime <= tmpTime))
ActorsInGame.ForEach(item =>
{
if (item.CreationTime <= tmpTime)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
public void DestroyActors(params AiGroundActorType[] types)
{
foreach (AiGroundActorType abat in types)
{
if (ActorsInGame.Exists(item => (item.actor is AiGroundActor) && (item.actor as AiGroundActor).Type() == abat))
ActorsInGame.ForEach(item =>
{
if ((item.actor is AiGroundActor) && (item.actor as AiGroundActor).Type() == abat)
if (IsActorDestroyable(item.actor))
destroyActor(item.actor);
});
}
ActorsInGame.RemoveAll(item => item.destroyed == true);
}
}
#endregion
#region Messages
private void sendChatMessage(string msg, params object[] args)
{
GamePlay.gpLogServer(null, msg, args);
}
private void sendChatMessage(Player player, string msg, params object[] args)
{
if (player != null)
GamePlay.gpLogServer(new Player[] { player }, msg, args);
}
private void sendChatMessage(int army, string msg, params object[] args)
{
List<Player> Consignees = new List<Player>();
if (GamePlay.gpPlayer() != null)
Consignees.Add(GamePlay.gpPlayer());
if (GamePlay.gpRemotePlayers() != null)
Consignees.AddRange(GamePlay.gpRemotePlayers());
if (army == -1)
GamePlay.gpLogServer(null, msg, args);
else if (Consignees.Exists(item => item.Army() == army))
GamePlay.gpLogServer(Consignees.FindAll(item => item.Army() == army).ToArray(), msg, args);
}
private void sendScreenMessage(string msg, params object[] args)
{
GamePlay.gpHUDLogCenter(null, msg, args);
}
private void sendScreenMessage(Player player, string msg, params object[] args)
{
if (player != null)
GamePlay.gpHUDLogCenter(new Player[] { player }, msg, args);
}
private void sendScreenMessage(int army, string msg, params object[] args)
{
List<Player> Consignees = new List<Player>();
if (GamePlay.gpPlayer() != null)
Consignees.Add(GamePlay.gpPlayer());
if (GamePlay.gpRemotePlayers() != null)
Consignees.AddRange(GamePlay.gpRemotePlayers());
if (army == -1)
GamePlay.gpHUDLogCenter(null, msg, args);
else if (Consignees.Exists(item => item.Army() == army))
GamePlay.gpHUDLogCenter(Consignees.FindAll(item => item.Army() == army).ToArray(), msg, args);
}
#endregion
Stopwatch MissionTimer = new Stopwatch();
internal InGameActors ActorList = new InGameActors();
public override void OnBattleStarted()
{
base.OnBattleStarted();
MissionNumberListener = -1;
//GamePlay.gpPostMissionLoad("missions/Tests/DestroyTestSub1.mis");
MissionTimer.Start();
}
public override void OnActorCreated(int missionNumber, string shortName, AiActor actor)
{
base.OnActorCreated(missionNumber, shortName, actor);
ActorList.Add(actor, missionNumber);
}
public override void OnActorDestroyed(int missionNumber, string shortName, AiActor actor)
{
base.OnActorDestroyed(missionNumber, shortName, actor);
ActorList.Removeable(actor);
}
TimeSpan DestroyAfter = new TimeSpan(0, 1, 0); // Destroyafter set to one minute
public override void OnTickGame()
{
base.OnTickGame();
if (MissionTimer.Elapsed.TotalSeconds >= 10)
{
sendChatMessage("MissionNr: {0}", GamePlay.gpNextMissionNumber());
//Point3d test = new Point3d(40128.09, 20605.83, 0);
foreach (AiActor ac in ActorList.GetActors())
sendChatMessage("Actor: {0}", ac.Name());
//InGameGroundActors.DestroyActors(AiGroundActorType.Tank, AiGroundActorType.AAGun);
ActorList.DestroyActors(-1);
//InGameGroundActors.DestroyActors(1, AiGroundActorType.Artillery);
//InGameGroundActors.DestroyActors(GamePlay.gpPlayer().Place().Pos(), 300.0);
//if (GamePlay.gpNextMissionNumber() - 1 >0)
// InGameGroundActors.DestroyActors(GamePlay.gpNextMissionNumber() - 1);
//GamePlay.gpPostMissionLoad("missions/Training/DestroyTestSub1.mis");
sendChatMessage("-----------------");
foreach (AiActor ac in ActorList.GetActors())
sendChatMessage("Actor: {0}", ac.Name());
MissionTimer.Reset();
}
}
}