Remove some code noise and improved the chinook cargo check

This commit is contained in:
ScottNZ
2012-06-30 23:20:04 +12:00
parent 781cbc00d3
commit b9be918b6c

View File

@@ -1,276 +1,271 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS) * Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
* see COPYING. * see COPYING.
*/ */
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Mods.RA.Activities; using OpenRA.Mods.RA.Activities;
using OpenRA.Mods.RA.Air; using OpenRA.Mods.RA.Air;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA.Missions namespace OpenRA.Mods.RA.Missions
{ {
public class Allies01ScriptInfo : TraitInfo<Allies01Script>, Requires<SpawnMapActorsInfo> { } class Allies01ScriptInfo : TraitInfo<Allies01Script>, Requires<SpawnMapActorsInfo> { }
public class Allies01Script : IWorldLoaded, ITick class Allies01Script : IWorldLoaded, ITick
{ {
private static readonly string[] objectives = static readonly string[] objectives =
{ {
"Find Einstein.", "Find Einstein.",
"Wait for the helicopter and extract Einstein." "Wait for the helicopter and extract Einstein."
}; };
private int currentObjective; int currentObjective;
private Player allies; Player allies;
private Player soviets; Player soviets;
private ISound music; ISound music;
private Actor insertionLZ; Actor insertionLZ;
private Actor extractionLZ; Actor extractionLZ;
private Actor lab; Actor lab;
private Actor insertionLZEntryPoint; Actor insertionLZEntryPoint;
private Actor extractionLZEntryPoint; Actor extractionLZEntryPoint;
private Actor chinookExitPoint; Actor chinookExitPoint;
private Actor shipSpawnPoint; Actor shipSpawnPoint;
private Actor shipMovePoint; Actor shipMovePoint;
private Actor einstein; Actor einstein;
private Actor einsteinChinook; Actor einsteinChinook;
private Actor tanya; Actor tanya;
private Actor attackEntryPoint1; Actor attackEntryPoint1;
private Actor attackEntryPoint2; Actor attackEntryPoint2;
private static readonly string[] taunts = { "laugh1.aud", "lefty1.aud", "cmon1.aud", "gotit1.aud" }; static readonly string[] taunts = { "laugh1.aud", "lefty1.aud", "cmon1.aud", "gotit1.aud" };
private static readonly string[] ships = { "ca", "ca", "ca", "ca" }; static readonly string[] ships = { "ca", "ca", "ca", "ca" };
private static readonly string[] attackWave = { "e1", "e1", "e1", "e1", "e2", "e2", "e2", "e2", "dog" }; static readonly string[] attackWave = { "e1", "e1", "e1", "e1", "e2", "e2", "e2", "e2", "dog" };
private static readonly string[] lastAttackWaveAddition = { "3tnk", "e1", "e1", "e1", "e1", "e2", "e2", "e2", "e2" }; static readonly string[] lastAttackWaveAddition = { "3tnk", "e1", "e1", "e1", "e1", "e2", "e2", "e2", "e2" };
private int currentAttackWaveFrameNumber; int currentAttackWaveFrameNumber;
private int currentAttackWave; int currentAttackWave;
private const int einsteinChinookArrivesAtAttackWave = 5; const int einsteinChinookArrivesAtAttackWave = 5;
private const int labRange = 5; const int labRange = 5;
private const string einsteinName = "einstein"; const string einsteinName = "einstein";
private const string tanyaName = "e7"; const string tanyaName = "e7";
private const string chinookName = "tran"; const string chinookName = "tran";
private const string signalFlareName = "flare"; const string signalFlareName = "flare";
private void NextObjective() void DisplayObjective()
{ {
currentObjective++; Game.AddChatLine(Color.LimeGreen, "Objective", objectives[currentObjective]);
} Sound.Play("bleep6.aud", 5);
}
private void DisplayObjective()
{ void MissionFailed(Actor self, string text)
Game.AddChatLine(Color.LimeGreen, "Objective", objectives[currentObjective]); {
Sound.Play("bleep6.aud", 5); if (allies.WinState != WinState.Undefined)
} {
return;
private void MissionFailed(Actor self, string text) }
{ allies.WinState = WinState.Lost;
if (allies.WinState != WinState.Undefined) Game.AddChatLine(Color.Red, "Mission failed", text);
{ self.World.LocalShroud.Disabled = true;
return; Sound.Play("misnlst1.aud", 5);
} }
allies.WinState = WinState.Lost;
Game.AddChatLine(Color.Red, "Mission failed", text); void MissionAccomplished(Actor self, string text)
self.World.LocalShroud.Disabled = true; {
Sound.Play("misnlst1.aud", 5); if (allies.WinState != WinState.Undefined)
} {
return;
private void MissionAccomplished(Actor self, string text) }
{ allies.WinState = WinState.Won;
if (allies.WinState != WinState.Undefined) Game.AddChatLine(Color.Blue, "Mission accomplished", text);
{ self.World.LocalShroud.Disabled = true;
return; Sound.Play("misnwon1.aud", 5);
} }
allies.WinState = WinState.Won;
Game.AddChatLine(Color.Blue, "Mission accomplished", text); public void Tick(Actor self)
self.World.LocalShroud.Disabled = true; {
Sound.Play("misnwon1.aud", 5); if (allies.WinState != WinState.Undefined)
} {
return;
public void Tick(Actor self) }
{ // display current objective every so often
if (allies.WinState != WinState.Undefined) if (self.World.FrameNumber % 1500 == 1)
{ {
return; DisplayObjective();
} }
// display current objective every so often // taunt every so often
if (self.World.FrameNumber % 1500 == 1) if (self.World.FrameNumber % 1000 == 0)
{ {
DisplayObjective(); Sound.Play(taunts[self.World.SharedRandom.Next(taunts.Length)]);
} }
// taunt every so often // take Tanya to the LZ
if (self.World.FrameNumber % 1000 == 0) if (self.World.FrameNumber == 1)
{ {
Sound.Play(taunts[self.World.SharedRandom.Next(taunts.Length)]); FlyTanyaToInsertionLZ(self);
} }
// take Tanya to the LZ // objectives
if (self.World.FrameNumber == 1) if (currentObjective == 0)
{ {
FlyTanyaToInsertionLZ(self); if (AlliesControlLab(self))
} {
// objectives SpawnSignalFlare(self);
if (currentObjective == 0) Sound.Play("flaren1.aud", 5);
{ SpawnEinsteinAtLab(self); // spawn Einstein once the area is clear
if (AlliesControlLab(self)) Sound.Play("einok1.aud"); // "Incredible!" - Einstein
{ SendShips(self);
SpawnSignalFlare(self); currentObjective++;
Sound.Play("flaren1.aud", 5); DisplayObjective();
SpawnEinsteinAtLab(self); // spawn Einstein once the area is clear currentAttackWaveFrameNumber = self.World.FrameNumber;
Sound.Play("einok1.aud"); // "Incredible!" - Einstein }
SendShips(self); if (lab.Destroyed)
NextObjective(); {
DisplayObjective(); MissionFailed(self, "Einstein was killed.");
currentAttackWaveFrameNumber = self.World.FrameNumber; }
} }
if (lab.Destroyed) else if (currentObjective == 1)
{ {
MissionFailed(self, "Einstein was killed."); if (self.World.FrameNumber >= currentAttackWaveFrameNumber + 600)
} {
} Sound.Play("enmyapp1.aud", 5);
else if (currentObjective == 1) SendAttackWave(self, attackWave);
{ currentAttackWave++;
if (self.World.FrameNumber >= currentAttackWaveFrameNumber + 600) currentAttackWaveFrameNumber = self.World.FrameNumber;
{ if (currentAttackWave >= einsteinChinookArrivesAtAttackWave)
Sound.Play("enmyapp1.aud", 5); {
SendAttackWave(self, attackWave); SendAttackWave(self, lastAttackWaveAddition);
currentAttackWave++; }
currentAttackWaveFrameNumber = self.World.FrameNumber; if (currentAttackWave == einsteinChinookArrivesAtAttackWave)
if (currentAttackWave >= einsteinChinookArrivesAtAttackWave) {
{ FlyEinsteinFromExtractionLZ(self);
SendAttackWave(self, lastAttackWaveAddition); }
} }
if (currentAttackWave == einsteinChinookArrivesAtAttackWave) if (einsteinChinook != null && !self.World.Map.IsInMap(einsteinChinook.Location) && einsteinChinook.Trait<Cargo>().Passengers.Contains(einstein))
{ {
FlyEinsteinFromExtractionLZ(self); MissionAccomplished(self, "Einstein was rescued.");
} }
} if (einstein.Destroyed)
if (einsteinChinook != null && !self.World.Map.IsInMap(einsteinChinook.Location) && !einstein.IsInWorld) {
{ MissionFailed(self, "Einstein was killed.");
MissionAccomplished(self, "Einstein was rescued."); }
} }
if (einstein.Destroyed) if (tanya.Destroyed)
{ {
MissionFailed(self, "Einstein was killed."); MissionFailed(self, "Tanya was killed.");
} }
} }
if (tanya.Destroyed)
{ void SpawnSignalFlare(Actor self)
MissionFailed(self, "Tanya was killed."); {
} self.World.CreateActor(signalFlareName, new TypeDictionary { new OwnerInit(allies), new LocationInit(extractionLZ.Location) });
} }
private void SpawnSignalFlare(Actor self) void SendAttackWave(Actor self, IEnumerable<string> wave)
{ {
self.World.CreateActor(signalFlareName, new TypeDictionary { new OwnerInit(allies), new LocationInit(extractionLZ.Location) }); foreach (var unit in wave)
} {
var spawnActor = self.World.SharedRandom.Next(2) == 0 ? attackEntryPoint1 : attackEntryPoint2;
private void SendAttackWave(Actor self, IEnumerable<string> wave) var actor = self.World.CreateActor(unit, new TypeDictionary { new OwnerInit(soviets), new LocationInit(spawnActor.Location) });
{ actor.QueueActivity(new AttackMove.AttackMoveActivity(actor, new Attack(Target.FromActor(einstein), 3))); // better way of doing this?
foreach (var unit in wave) }
{ }
var spawnActor = self.World.SharedRandom.Next(2) == 0 ? attackEntryPoint1 : attackEntryPoint2;
var actor = self.World.CreateActor(unit, new TypeDictionary { new OwnerInit(soviets), new LocationInit(spawnActor.Location) }); IEnumerable<Actor> UnitsNearActor(Actor self, Actor actor, int range)
actor.QueueActivity(new AttackMove.AttackMoveActivity(actor, new Attack(Target.FromActor(einstein), 3))); // better way of doing this? {
} return self.World.FindUnitsInCircle(actor.CenterLocation, Game.CellSize * range)
} .Where(a => a.IsInWorld && a != self.World.WorldActor && !a.Destroyed && a.HasTrait<IMove>() && !a.Owner.NonCombatant);
}
private IEnumerable<Actor> UnitsNearActor(Actor self, Actor actor, int range)
{ bool AlliesControlLab(Actor self)
return self.World.FindUnitsInCircle(actor.CenterLocation, Game.CellSize * range) {
.Where(a => a.IsInWorld && a != self.World.WorldActor && !a.Destroyed && a.HasTrait<IMove>() && !a.Owner.NonCombatant); var units = UnitsNearActor(self, lab, labRange);
} return units.Any() && units.All(a => a.Owner == allies);
}
private bool AlliesControlLab(Actor self)
{ void SpawnEinsteinAtLab(Actor self)
var units = UnitsNearActor(self, lab, labRange); {
return units.Any() && units.All(a => a.Owner == allies); einstein = self.World.CreateActor(einsteinName, new TypeDictionary { new OwnerInit(allies), new LocationInit(lab.Location) });
} einstein.QueueActivity(new Move.Move(lab.Location - new CVec(0, 2)));
}
private void SpawnEinsteinAtLab(Actor self)
{ void SendShips(Actor self)
einstein = self.World.CreateActor(einsteinName, new TypeDictionary { new OwnerInit(allies), new LocationInit(lab.Location) }); {
einstein.QueueActivity(new Move.Move(lab.Location - new CVec(0, 2))); for (int i = 0; i < ships.Length; i++)
} {
var actor = self.World.CreateActor(ships[i],
private void SendShips(Actor self) new TypeDictionary { new OwnerInit(allies), new LocationInit(shipSpawnPoint.Location + new CVec(i * 2, 0)) });
{ actor.QueueActivity(new Move.Move(shipMovePoint.Location + new CVec(i * 4, 0)));
for (int i = 0; i < ships.Length; i++) }
{ }
var actor = self.World.CreateActor(ships[i],
new TypeDictionary { new OwnerInit(allies), new LocationInit(shipSpawnPoint.Location + new CVec(i * 2, 0)) }); void FlyEinsteinFromExtractionLZ(Actor self)
actor.QueueActivity(new Move.Move(shipMovePoint.Location + new CVec(i * 4, 0))); {
} einsteinChinook = self.World.CreateActor(chinookName, new TypeDictionary { new OwnerInit(allies), new LocationInit(extractionLZEntryPoint.Location) });
} einsteinChinook.QueueActivity(new HeliFly(extractionLZ.CenterLocation));
einsteinChinook.QueueActivity(new Turn(0));
private void FlyEinsteinFromExtractionLZ(Actor self) einsteinChinook.QueueActivity(new HeliLand(true));
{ einsteinChinook.QueueActivity(new WaitFor(() => einsteinChinook.Trait<Cargo>().Passengers.Contains(einstein)));
einsteinChinook = self.World.CreateActor(chinookName, new TypeDictionary { new OwnerInit(allies), new LocationInit(extractionLZEntryPoint.Location) }); einsteinChinook.QueueActivity(new Wait(150));
einsteinChinook.QueueActivity(new HeliFly(extractionLZ.CenterLocation)); einsteinChinook.QueueActivity(new HeliFly(chinookExitPoint.CenterLocation));
einsteinChinook.QueueActivity(new Turn(0)); einsteinChinook.QueueActivity(new RemoveSelf());
einsteinChinook.QueueActivity(new HeliLand(true)); }
einsteinChinook.QueueActivity(new WaitFor(() => einsteinChinook.Trait<Cargo>().Passengers.Contains(einstein)));
einsteinChinook.QueueActivity(new Wait(150)); void FlyTanyaToInsertionLZ(Actor self)
einsteinChinook.QueueActivity(new HeliFly(chinookExitPoint.CenterLocation)); {
einsteinChinook.QueueActivity(new RemoveSelf()); tanya = self.World.CreateActor(false, tanyaName, new TypeDictionary { new OwnerInit(allies) });
} var chinook = self.World.CreateActor(chinookName, new TypeDictionary { new OwnerInit(allies), new LocationInit(insertionLZEntryPoint.Location) });
chinook.Trait<Cargo>().Load(chinook, tanya);
private void FlyTanyaToInsertionLZ(Actor self) // use CenterLocation for HeliFly, Location for Move
{ chinook.QueueActivity(new HeliFly(insertionLZ.CenterLocation));
tanya = self.World.CreateActor(false, tanyaName, new TypeDictionary { new OwnerInit(allies) }); chinook.QueueActivity(new Turn(0));
var chinook = self.World.CreateActor(chinookName, new TypeDictionary { new OwnerInit(allies), new LocationInit(insertionLZEntryPoint.Location) }); chinook.QueueActivity(new HeliLand(true));
chinook.Trait<Cargo>().Load(chinook, tanya); chinook.QueueActivity(new UnloadCargo());
// use CenterLocation for HeliFly, Location for Move chinook.QueueActivity(new CallFunc(() => Sound.Play("laugh1.aud")));
chinook.QueueActivity(new HeliFly(insertionLZ.CenterLocation)); chinook.QueueActivity(new Wait(150));
chinook.QueueActivity(new Turn(0)); chinook.QueueActivity(new HeliFly(chinookExitPoint.CenterLocation));
chinook.QueueActivity(new HeliLand(true)); chinook.QueueActivity(new RemoveSelf());
chinook.QueueActivity(new UnloadCargo()); }
chinook.QueueActivity(new CallFunc(() => Sound.Play("laugh1.aud")));
chinook.QueueActivity(new Wait(150)); public void WorldLoaded(World w)
chinook.QueueActivity(new HeliFly(chinookExitPoint.CenterLocation)); {
chinook.QueueActivity(new RemoveSelf()); allies = w.Players.Single(p => p.InternalName == "Allies");
} soviets = w.Players.Single(p => p.InternalName == "Soviets");
var actors = w.WorldActor.Trait<SpawnMapActors>().Actors;
public void WorldLoaded(World w) insertionLZ = actors["InsertionLZ"];
{ extractionLZ = actors["ExtractionLZ"];
allies = w.Players.Single(p => p.InternalName == "Allies"); lab = actors["Lab"];
soviets = w.Players.Single(p => p.InternalName == "Soviets"); insertionLZEntryPoint = actors["InsertionLZEntryPoint"];
var actors = w.WorldActor.Trait<SpawnMapActors>().Actors; chinookExitPoint = actors["ChinookExitPoint"];
insertionLZ = actors["InsertionLZ"]; extractionLZEntryPoint = actors["ExtractionLZEntryPoint"];
extractionLZ = actors["ExtractionLZ"]; shipSpawnPoint = actors["ShipSpawnPoint"];
lab = actors["Lab"]; shipMovePoint = actors["ShipMovePoint"];
insertionLZEntryPoint = actors["InsertionLZEntryPoint"]; attackEntryPoint1 = actors["SovietAttackEntryPoint1"];
chinookExitPoint = actors["ChinookExitPoint"]; attackEntryPoint2 = actors["SovietAttackEntryPoint2"];
extractionLZEntryPoint = actors["ExtractionLZEntryPoint"]; Game.MoveViewport(insertionLZ.Location.ToFloat2());
shipSpawnPoint = actors["ShipSpawnPoint"]; music = Sound.Play("hell226m.aud"); // Hell March
shipMovePoint = actors["ShipMovePoint"]; Game.ConnectionStateChanged += StopMusic;
attackEntryPoint1 = actors["SovietAttackEntryPoint1"]; }
attackEntryPoint2 = actors["SovietAttackEntryPoint2"];
Game.MoveViewport(insertionLZ.Location.ToFloat2()); void StopMusic(OrderManager orderManager)
music = Sound.Play("hell226m.aud"); // Hell March {
Game.ConnectionStateChanged += StopMusic; if (!orderManager.GameStarted)
} {
Sound.StopSound(music);
private void StopMusic(OrderManager orderManager) Game.ConnectionStateChanged -= StopMusic;
{ }
if (!orderManager.GameStarted) }
{ }
Sound.StopSound(music); }
Game.ConnectionStateChanged -= StopMusic;
}
}
}
}