#1
|
|||
|
|||
Tracking down NullReferenceException?
I have a NullReferenceException error in my mission script that produces the error in the server log below. I need help tracking down the offending piece of code please. Mission script is also below.
SERVER LOG ================================================= [01:06:46] System.NullReferenceException: Object reference not set to an instance of an object. [01:06:46] [01:06:46] Server stack trace: [01:06:46] at Mission.DoDamage() [01:06:46] at Mission.OnAircraftTookOff(Int32 missionNumber, String shortName, AiAircraft aircraft) [01:06:46] at maddox.game.ABattle.OnAircraftTookOff(Int32 missionNumber, String shortName, AiAircraft aircraft) [01:06:46] at maddox.game.ABattle.OnEventGame(GameEventId eventId, Object eventArg0, Object eventArg1, Int32 eventArgInt) [01:06:46] at maddox.game.world.Strategy.OnEventGame(GameEventId eventId, Object eventArg0, Object eventArg1, Int32 eventArgInt) [01:06:46] at System.Runtime.Remoting.Messaging.StackBuilderSink ._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs) [01:06:46] at System.Runtime.Remoting.Messaging.StackBuilderSink .SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext) [01:06:46] [01:06:46] Exception rethrown at [0]: [01:06:46] at System.Runtime.Remoting.Proxies.RealProxy.HandleRe turnMessage(IMessage reqMsg, IMessage retMsg) [01:06:46] at System.Runtime.Remoting.Proxies.RealProxy.PrivateI nvoke(MessageData& msgData, Int32 type) [01:06:46] at maddox.game.IBattle.OnEventGame(GameEventId eventId, Object eventArg0, Object eventArg1, Int32 eventArgInt) [01:06:46] at maddox.game.GameDef.eventGame(GameEventId eventId, Object eventArg0, Object eventArg1, Int32 eventArgInt) [01:06:46] at 2pcDPOMCAn8BIb8ysUA.NFCE7DMiT9FpQagZIAw.eventGame( GameEventId , Object , Object , Int32 ) [01:06:46] at aZ8NnuxUKValf3nKWOT.ePqQP5xS2NPR5XHeyL6.LvaJnnsnxj f(GameEventId , Object , Object , Int32 ) [01:06:46] ================================================= Code:
using System; using maddox.game; using maddox.game.world; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Security.Cryptography; public class Mission : AMission { string BattleStart; List<string> connections = new List<string>(); //List<string> BlueBomberMissions = new List<string> {"Dorniers.mis", "Stukas.mis", "Heinkels.mis" }; //List<string> BlueFighterMissions = new List<string>("Bf109E.mis", "G50.mis"); //List<string> BlueShipMissions = new List<string> { "Ships1.mis" }; //List<string> RedBomberMissions = new List<string>("Spit_Mk1.mis", "Hurricane(Rotol).mis"); //List<string> RedFighterMissions = new List<string>("Blenheim_Mk1"); //paths to missions string BlueMissionPath = "missions/Multi/Dogfight/aus1/blue/"; string RedMissionPath = "missions/Multi/Dogfight/aus1/red/"; // Web statistics page bool HasStats = true; // show stats on a web page? string StatsServerURL = @"aus.no-ip.org/aus/nations@war/COD/"; // URL & directory for stats page string ServerName = "AUS_Nations@War Cliffs of Dover Server"; // name of the game server (appears on stats page) // initialise some variables AiAircraft PlayerPlane; bool BlueShips = false; bool RedShips = false; bool RedTrains = false; // allowed to change only in constructor private readonly Random m_random; // Constructor (do not call manually) public Mission() { // initialize a new instance of the Random class var provider = new RNGCryptoServiceProvider(); var buffer = new byte[4]; provider.GetBytes(buffer); m_random = new Random(BitConverter.ToInt32(buffer, 0)); } public override void OnBattleStarted() { base.OnBattleStarted(); // time the mission for messages // MissionTimer.Start(); BattleStart = DateTime.Now.ToString("f"); connections.Clear(); } public override void OnPlayerConnected(maddox.game.Player player) { connections.Add(player.Name()); WriteTable1(); //} } public override void OnBattleInit() { base.OnBattleInit(); //listen to events from all missions. MissionNumberListener = -1; // initial mission loads LoadBlueSubMission(); LoadRedSubMission(); // create a log file WriteTable1(); } public override void OnMissionLoaded(int missionNumber) { // GamePlay.gpHUDLogCenter("Mission starts"); } public override void OnBattleStoped() { WriteTable1(); //WriteToLogFile("Battle stopped"); } public override void OnPlayerDisconnected(maddox.game.Player player, string diagnostic) { //WriteToLogFile(player.Name() + " disconnected (" + diagnostic + ")"); //then wait 15 seconds Timeout(15.0, () => { WriteTable1(); }); } public void WriteToLogFile(string info) { StreamWriter sw = new StreamWriter(StatsServerURL + "index.html", true); sw.WriteLine(DateTime.Now.ToString() + " " + info + "<br>"); sw.Close(); } public void WriteTable1() { if (HasStats == true) { //StreamWriter sw = new StreamWriter(StatsServerURL + "index.html", false); StreamWriter sw = new StreamWriter(@"F:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\aus\nations@war\COD\index.html", false); sw.WriteLine("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>"); sw.WriteLine("<html>"); sw.WriteLine("<head>"); sw.WriteLine("<meta"); sw.WriteLine("content='text/html; charset=ISO-8859-1'"); sw.WriteLine("http-equiv='content-type'>"); sw.WriteLine("<title></title>"); sw.WriteLine("</head>"); sw.WriteLine("<body>"); sw.WriteLine("<img src='il2banner.jpg' alt=''>"); sw.WriteLine("<h2>"); sw.WriteLine(ServerName); sw.WriteLine("</h2>"); sw.WriteLine("<br>"); sw.WriteLine("<b>Battle started on:</b> " + BattleStart); sw.WriteLine("<br>"); sw.WriteLine("<br><br>"); sw.WriteLine("<table style='text-align: left; width: 571px border='1' cellpadding='2' cellspacing='2'>"); sw.WriteLine("<tbody>"); sw.WriteLine("<tr>"); sw.WriteLine("<td"); sw.WriteLine("style='width: 240px; font-weight: bold; text-align: center;'>"); sw.WriteLine("<span' style='color: rgb(255, 0, 0);'>RED</span></td>"); sw.WriteLine("<td style='width: 62px; font-weight: bold; text-align: center;'></td>"); sw.WriteLine("<td style='width: 241px; font-weight: bold; text-align: center;'><span style='color: rgb(0, 0, 153);'>BLUE</span></td>"); sw.WriteLine("</tr>"); sw.WriteLine("<tr>"); sw.WriteLine("<td style='width: 240px; text-align: center;'>"); sw.WriteLine(NumberOfPlayers(1)); sw.WriteLine("</td>"); sw.WriteLine("<td style='width: 62px; text-align: center;'></td>"); sw.WriteLine("<td style='width: 241px; text-align: center;'>"); sw.WriteLine(NumberOfPlayers(2)); sw.WriteLine("</td>"); sw.WriteLine("</tr>"); sw.WriteLine("<tr>"); sw.WriteLine("<td style='width: 240px;'></td>"); sw.WriteLine("<td style='width: 62px;'></td>"); sw.WriteLine("<td style='width: 241px;'></td>"); sw.WriteLine("</tr>"); sw.WriteLine("</tbody>"); // write the players list table sw.WriteLine("<table style='text-align: left; width: 574px;' border='1' cellpadding='0' cellspacing='0'>"); sw.WriteLine("<tbody>"); sw.WriteLine("<tr>"); sw.WriteLine("<td style='text-align: center; font-weight: bold; width: 243px; background-color: rgb(204, 204, 204);'>PLAYER</td>"); sw.WriteLine("<td style='text-align: center; font-weight: bold; width: 242px; background-color: rgb(204, 204, 204);'>AIRCRAFT</td>"); sw.WriteLine("<td style='text-align: center; font-weight: bold; width: 61px; background-color: rgb(204, 204, 204);'>PING</td>"); sw.WriteLine("</tr>"); //Stats[] Statistics = maddox.game.IPlayer.GetBattleStat(); //Stats[] Statistics = maddox.game.IPlayerStat(); //GamePlay.gpHUDLogCenter(Statistics.GetUpperBound); Player[] Players = GamePlay.gpRemotePlayers(); //if ((Players.Length <=0) || (Players.Length == null)) //{ // return; //} int i = 0; string txtColor = ""; string bgColor = ""; foreach (Player p in Players) { i++; sw.WriteLine("<tr>"); // get the table row background color if (i % 2 == 0) { // it's an even number row bgColor = "rgb(0, 0, 0)"; } else { // it's an odd number row bgColor = "rgb(240, 240, 240)"; } // get the text forecolor if (p.Army() == 1) // allied army { txtColor = "color: rgb(255, 0, 0)"; } else if (p.Army() == 2) // axis army { txtColor = "color: rgb(0, 0, 153)"; } else { // player has no army txtColor = "color: rgb(102, 102, 102)"; } // write a line for each player online sw.WriteLine("<td style='width: 243px;'><span style='" + txtColor + ";'>"); sw.WriteLine(" " + p.Name()); sw.WriteLine("</span></td>"); sw.WriteLine("<td style='width: 242px; background-color: " + bgColor + " text-align: left;'>"); //sw.WriteLine(p.AiAircraft.Type()); // PlayerPlane //sw.WriteLine(" " + (AiAircraft)GamePlay.gpPlayer().Place()); //sw.WriteLine(" Players plane goes here"); //(p.plane); //sw.Writeline(" {0}", new object[] { (actor as AiAircraft).Type() }); sw.WriteLine("</td>"); sw.WriteLine("<td style='width: 61px; background-color: " + bgColor + " text-align: center;'>"); sw.WriteLine(" " + p.Ping()); sw.WriteLine("</td>"); sw.WriteLine("</tr>"); } // end the table & html text sw.WriteLine("</tbody>"); sw.WriteLine("</table>"); sw.WriteLine("<br>"); //write connections summary sw.WriteLine("<p>"); sw.WriteLine("Connections since battle started"); sw.WriteLine("<p>"); i = 0; foreach (string connection in connections) // Loop through List with foreach { i++; sw.WriteLine(connection + " "); if (i % 6 == 0) // it's time for a line break { i = 0; sw.WriteLine("<br>"); } } sw.WriteLine("</body>"); sw.WriteLine("</html>"); sw.Close(); } } public override void OnTrigger(int missionNumber, string shortName, bool active) { //call base classes OnTrigger method. base.OnTrigger(missionNumber, shortName, active); //if the trigger that was called is the trigger that we're looking for if (shortName.Equals("BlueOnRadar") && active) { //GamePlay.gpHUDLogCenter("Radar reports large enemy formation in E2"); } //if the trigger that was called is the trigger that we're looking for if (shortName.Equals("TrainNorthOfDymchurch") && active) { sendScreenMessageTo(2, "Train sighted at Dymchurch (E4) heading north along English coast", null); } } public override void OnPlaceEnter(Player player, AiActor actor, int placeIndex) { int army = player.Army(); string forces = "Allied"; if (army == 2) forces = "Axis"; sendScreenMessageTo(-1, player.Name() + " joins the " + forces + " airforce", null); WriteTable1(); } public override void OnPlaceLeave(Player player, AiActor actor, int placeIndex) { base.OnPlaceLeave(player, actor, placeIndex); Timeout(1, () => { damageAiControlledPlane(actor); } ); WriteTable1(); } public override void OnAircraftKilled(int missionNumber, string shortName, maddox.game.world.AiAircraft aircraft) { //base.OnAircraftKilled(int missionNumber, string shortName, maddox.game.world.AiAircraft aircraft) // GamePlay.gpHUDLogCenter("shortName=" + shortName + " callsign=" + aircraft.CallSign() + " callsignnumber=" + aircraft.CallSignNumber() + " Type=" + aircraft.Type() + // " TypedName=" + aircraft.TypedName() + " hullnumber=" + aircraft.HullNumber() + " regiment=" + aircraft.Regiment() + " killed."); } public override void OnAircraftCrashLanded(int missionNumber, string shortName, AiAircraft aircraft) { base.OnAircraftCrashLanded(missionNumber, shortName, aircraft); //GamePlay.gpHUDLogCenter("shortName=" + shortName + " callsign=" + aircraft.CallSign() + " callsignnumber=" + aircraft.CallSignNumber() + " Type=" + aircraft.Type() + // " TypedName=" + aircraft.TypedName() + " hullnumber=" + aircraft.HullNumber() + " regiment=" + aircraft.Regiment() + " crash landed."); Timeout(300, () => { destroyPlane(aircraft); } ); } public override void OnAircraftLanded(int missionNumber, string shortName, AiAircraft aircraft) { base.OnAircraftLanded(missionNumber, shortName, aircraft); //GamePlay.gpHUDLogCenter("shortName=" + shortName + " callsign=" + aircraft.CallSign() + " callsignnumber=" + aircraft.CallSignNumber() + " Type=" + aircraft.Type() + // " TypedName=" + aircraft.TypedName() + " hullnumber=" + aircraft.HullNumber() + " regiment=" + aircraft.Regiment() + " landed OK."); Timeout(300, () => { destroyPlane(aircraft); } ); } private void destroyAiControlledPlane(AiAircraft aircraft) { if (isAiControlledPlane(aircraft)) { destroyPlane(aircraft); } } private void damageAiControlledPlane(AiActor actor) { if (actor == null || !(actor is AiAircraft)) { return; } AiAircraft aircraft = (actor as AiAircraft); if (!isAiControlledPlane(aircraft)) { return; } if (aircraft == null) { return; } aircraft.hitNamed(part.NamedDamageTypes.ControlsElevatorDisabled); aircraft.hitNamed(part.NamedDamageTypes.ControlsAileronsDisabled); aircraft.hitNamed(part.NamedDamageTypes.ControlsRudderDisabled); aircraft.hitNamed(part.NamedDamageTypes.FuelPumpFailure); int iNumOfEngines = (aircraft.Group() as AiAirGroup).aircraftEnginesNum(); for (int i = 0; i < iNumOfEngines; i++) { aircraft.hitNamed((part.NamedDamageTypes)Enum.Parse(typeof(part.NamedDamageTypes), "Eng" + i.ToString() + "TotalFailure")); } /***Timeout (240, () => {explodeFuelTank (aircraft);} ); * ***/ Timeout(300, () => { destroyPlane(aircraft); } ); } // destroys aircraft abandoned by a player. private bool isAiControlledPlane(AiAircraft aircraft) { if (aircraft == null) { return false; } Player[] players = GamePlay.gpRemotePlayers(); foreach (Player p in players) { if (p != null && (p.Place() is AiAircraft) && (p.Place() as AiAircraft) == aircraft) { return false; } } return true; } private void destroyPlane(AiAircraft aircraft) { if (aircraft != null) { aircraft.Destroy(); } } public override void OnTickGame() { base.OnTickGame(); // if (ShipTimer_b.Elapsed.Minutes >= 60) // { // shipTimer_b.Stop(); // shipTimer_b.Reset(); // // destroy ships // if ((GamePlay.gpGroundGroups(1).Length > 0) && (GamePlay.gpGroundGroups(1) != null)) // { // foreach (AiGroundGroup group in GamePlay.gpGroundGroups(1)) // { // if (group.Type() == AiGroundGroupType.Ship) // { // destroyGroundGroup(group); // } // } // } // } // check for new day //string tm = gpTime().ToSting(); //GamePlay.gpHUDLogCenter("" + tm); // ======= User parameters (values in seconds, change to suit your needs) ========== int LoadFirstMin = 901; // load sub-mission for first time after at least 15 minutes int LoadFirstMax = 1801; // load sub-mission for first time before 30 minutes int LoadAfterMin = 900; // thereafter load sub-mission at least every 15 min int LoadAfterMax = 1800; // thereafter load sub-mission no longer than after 30 min // ----- check sub-mission timing parameters for errors ------- if (LoadFirstMax < LoadFirstMin) { LoadFirstMax = LoadFirstMin; } if (LoadAfterMax < LoadAfterMin) { LoadAfterMax = LoadAfterMin; } // ------------------------------------------------------------- // ------ Convert sub-mission timing parameters to game ticks ----- // LoadFirst = number of ticks before first loading sub-mission // LoadAfter = number of ticks thereafter before loading sub-mission again int LoadFirst = m_random.Next(LoadFirstMin * 30, LoadFirstMax * 30); int LoadAfter = m_random.Next(LoadAfterMin * 30, LoadAfterMax * 30); if (LoadAfter >= LoadFirst) { LoadAfter = LoadFirst - 1; // second parameter must always be less than first parameter } // ------------------------------------------------------------- // -------- Load the sub-mission ------------------------------- if (Time.tickCounter() % LoadFirst == LoadAfter) { LoadBlueSubMission(); LoadRedSubMission(); } } public void LoadBlueSubMission() { bool BlueBombers = false; //GamePlay.gpPostMissionLoad(BlueMissionPath + BlueBomberMissions[(m_random.Next(0, BlueBomberMissions.Count)]); switch (m_random.Next(1, 5 + 1)) { case 1: // single Dornier staffel GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Dorniers.mis"); sendScreenMessageTo(2, "Dornier Staffel activated", null); //GamePlay.gpHUDLogCenter("Dornier Staffel activated"); BlueBombers = true; break; case 2: // single stuka staffel GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Stukas.mis"); sendScreenMessageTo(2, "Stuka Staffel activated", null); //GamePlay.gpHUDLogCenter("Stuka Staffel activated"); BlueBombers = true; break; case 3: // single Heinkel staffel GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Heinkels.mis"); sendScreenMessageTo(2, "Heinkel Staffel activated", null); //GamePlay.gpHUDLogCenter("Heinkel Staffel activated"); BlueBombers = true; break; case 4: // two stuka staffels GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Stukas.mis"); //GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Stukas.mis"); sendScreenMessageTo(2, "Stuka Staffels activated", null); //GamePlay.gpHUDLogCenter("Stuka Staffels activated"); BlueBombers = true; break; case 5: // ship convoy with 2 x 109E escorts if (BlueShips == true) break; GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Ships1.mis"); sendScreenMessageTo(1, "German ships sighted in I2 heading north-east along French coast", null); sendScreenMessageTo(2, "Kriegsmarine ships in I2 heading north-east requesting Luftwaffe air cover", null); //GamePlay.gpHUDLogCenter("German ships sighted in F2 heading north-east along French coast"); BlueBombers = false; BlueShips = true; Timeout(3600.0, () => // wait 1 hour { destroyShips(2); // destroy all blue ships BlueShips = false; }); break; default: GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Stukas.mis"); sendScreenMessageTo(2, "Stuka Staffel activated", null); //GamePlay.gpHUDLogCenter("Stuka Staffel activated"); break; } // blue fighter escorts? if (BlueBombers == true) { if (m_random.Next(1, 100 + 1) < 51) // 50% chance of blue fighter escorts { if (m_random.Next(1, 100 + 1) < 67) // 66% chance of Me109 escort { GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Bf109E.mis"); if (m_random.Next(1, 100 + 1) < 34) // 33% chance of double escort fighters { GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Bf109E.mis"); } sendScreenMessageTo(2, "Me109 Staffel activated", null); //GamePlay.gpHUDLogCenter("Me109 Staffel activated"); } else { // 33% chance of G50 escort GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/G50.mis"); if (m_random.Next(1, 100 + 1) < 34) // 33% chance of double escort fighters { GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/G50.mis"); } sendScreenMessageTo(2, "G50 Gruppe activated", null); //GamePlay.gpHUDLogCenter("G50 Gruppe activated"); } } } // blue free huntering fighters if (m_random.Next(1, 100 + 1) < 34) // 33% chance of free hunting blue fighters { GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/blue/Bf109E2_freehunt1.mis"); sendScreenMessageTo(2, "Me109 Staffel activated", null); } } public void LoadRedSubMission() { switch (m_random.Next(1, 6 + 1)) { case 1: // single blenheim squadron GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/Blenheim_Mk1.mis"); sendScreenMessageTo(1, "Blenheims requesting escort: Meet F5 in 5 min alt 3,900 ft (1200m)", null); //GamePlay.gpHUDLogCenter("Blenheim Squadron activated"); break; case 2: // single spitfire squadron GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/Spit_Mk1.mis"); sendScreenMessageTo(1, "Spitfire Squadron activated", null); //GamePlay.gpHUDLogCenter("Spitfire Squadron activated"); break; case 3: // single hurricane squadron GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/Hurricane(Rotol).mis"); sendScreenMessageTo(1, "Hurricane Squadron activated", null); //GamePlay.gpHUDLogCenter("Hurricane Squadron activated"); break; case 4: // single Defiant squadron GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/Defiant.mis"); sendScreenMessageTo(1, "Defiant Squadron activated", null); break; case 5: // allied ship convoy if (RedShips == true) break; GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/ships1.mis"); sendScreenMessageTo(2, "Allied ships sighted in C3 heading north-east along English coast", null); sendScreenMessageTo(1, "Allied ships in C3 heading north-east requesting RAF air cover", null); //GamePlay.gpHUDLogCenter("German ships sighted in F2 heading north-east along French coast"); RedShips = true; Timeout(3600.0, () => // wait 1 hour { destroyShips(1); // destroy all red ships RedShips = false; }); break; case 6: // Allied train leaving New Romney for Folkstone if (RedTrains == true) break; GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/train1.mis"); sendScreenMessageTo(2, "Train sighted leaving New Romney (E3) heading north along English coast", null); Timeout(2700.0, () => // wait 3/4 hour { destroyTrains(1); // destroy all red trains RedTrains = false; }); break; default: GamePlay.gpPostMissionLoad("missions/Multi/Dogfight/aus1/red/Hurricane(Rotol).mis"); sendScreenMessageTo(1, "Hurricane Squadron activated", null); //GamePlay.gpHUDLogCenter("Hurricane Squadron activated"); break; } } private int NumberOfPlayers(int Army) { // gets the number of players for the specified army (1=red; 2=blue) int i = 0; foreach (Player p in GamePlay.gpRemotePlayers()) { if (p.Army() == Army) i++; } return i; } internal ISectionFile RandomiseMission() { //time of day bool RandomiseTime = true; int minTime = 6; // 6am int maxTime = 8; // 8am // WeatherIndex bool RandomiseWeatherIndex = true; int minWeatherIndex = 0; // clear int maxWeatherIndex = 2; // medium clouds // cloud height bool RandomiseClouds = true; int minClouds = 500; // 500m int maxClouds = 1500; // 1500m // BreezeActivity bool RandomiseBreezeActivity = true; int minBreezeActivity = 0; // zero breeze int maxBreezeActivity = 10; // max breeze // ThermalActivity bool RandomiseThermalActivity = true; int minThermalActivity = 0; // zero thermals int maxThermalActivity = 10; // max thermals ISectionFile secfile = GamePlay.gpCreateSectionFile(); // time of day if (RandomiseTime == true) { string value = (m_random.Next(minTime * 100, (maxTime) * 100) / 100f).ToString(); secfile.add("MAIN", "TIME", value); } // WeatherIndex if (RandomiseWeatherIndex == true) { string value = m_random.Next(minWeatherIndex, maxWeatherIndex + 1).ToString(); secfile.add("MAIN", "WeatherIndex", value); } // clouds if (RandomiseClouds == true) { string value = m_random.Next(minClouds, maxClouds + 1).ToString(); secfile.add("MAIN", "CloudsHeight", value); } // minBreezeActivity if (RandomiseBreezeActivity == true) { string value = m_random.Next(minBreezeActivity, maxBreezeActivity + 1).ToString(); secfile.add("MAIN", "BreezeActivity", value); } // ThermalActivity if (RandomiseThermalActivity == true) { string value = m_random.Next(minThermalActivity, maxThermalActivity + 1).ToString(); secfile.add("MAIN", "ThermalActivity", value); } // This post loads the section file secfile into the current battle. // It's also possible to save the section file secfile first by secfile.save("filename") and then // call GamePlay.gpPostMissionLoad("filename"); if you want to save the randomised file // on your hard disk. GamePlay.gpPostMissionLoad(secfile); return secfile; } public override void OnAircraftTookOff(int missionNumber, string shortName, maddox.game.world.AiAircraft aircraft) { if (m_random.Next(1, 200 + 1) != 67) // 0.5% chance of takeoff failure { DoDamage(); } } private void DoDamage() { // Script that triggered an accidental damage to the player plane // Author: FG28_Kodiak Modified by: Salmo // Ref: http://forum.1cpublishing.eu/showthread.php?t=22184 PlayerPlane = (AiAircraft)GamePlay.gpPlayer().Place(); switch (m_random.Next(1, 7 + 1)) { // =================================== // ---- Engine(s) failures ---- // =================================== case 1: PlayerPlane.hitNamed(part.NamedDamageTypes.Eng0TotalFailure); //sendScreenMessageToPilot(PlayerPlane, "Engine Failure", null); //GamePlay.gpHUDLogCenter("Engine Failure"); break; case 2: PlayerPlane.hitNamed(part.NamedDamageTypes.Eng0OilSecondariesFire); //sendScreenMessageToPilot(aircraft, "Oil is on fire"); GamePlay.gpHUDLogCenter("Oil is on fire"); break; // =================================== // ---- Hydrolicss & cables failures ---- // =================================== case 3: if (m_random.Next(1, 2) == 1) { PlayerPlane.hitNamed(part.NamedDamageTypes.UndercarriageUpLockFailureL); //sendScreenMessageToPilot(aircraft, "Undercarriage failure"); GamePlay.gpHUDLogCenter("Undercarriage failure"); } else { PlayerPlane.hitNamed(part.NamedDamageTypes.UndercarriageUpLockFailureR); //sendScreenMessageToPilot(aircraft, "Undercarriage failure"); GamePlay.gpHUDLogCenter("Undercarriage failure"); } break; case 4: PlayerPlane.hitNamed(part.NamedDamageTypes.Eng0ThrottleControlBroken); //sendScreenMessageToPilot(aircraft, "Broken throttle cable"); GamePlay.gpHUDLogCenter("Broken throttle cable"); break; // =================================== // ---- Flight surfaces failures ---- // =================================== case 5: PlayerPlane.hitNamed(part.NamedDamageTypes.ControlsElevatorDisabled); //sendScreenMessageToPilot(aircraft, "Elevator jammed"); GamePlay.gpHUDLogCenter("Elevator jammed"); break; case 6: PlayerPlane.hitNamed(part.NamedDamageTypes.ControlsAileronsDisabled); //sendScreenMessageToPilot(aircraft, "Ailerons jammed"); GamePlay.gpHUDLogCenter("Ailerons jammed"); break; case 7: PlayerPlane.hitNamed(part.NamedDamageTypes.ControlsRudderDisabled); //sendScreenMessageToPilot(aircraft, "Rudder jammed"); GamePlay.gpHUDLogCenter("Rudder jammed"); break; default: PlayerPlane.hitNamed(part.NamedDamageTypes.Eng0TotalFailure); //sendScreenMessageToPilot(aircraft, "Engine Failure"); GamePlay.gpHUDLogCenter("Engine Failure"); break; } } private void sendScreenMessageTo(int army, string msg, object[] parms) { List<Player> Players = new List<Player>(); // on Dedi the server or for singleplayertesting if (GamePlay.gpPlayer() != null) { if (GamePlay.gpPlayer().Army() == army || army == -1) Players.Add(GamePlay.gpPlayer()); } if (GamePlay.gpRemotePlayers() != null || GamePlay.gpRemotePlayers().Length > 0) { foreach (Player p in GamePlay.gpRemotePlayers()) { if (p.Army() == army || army == -1) Players.Add(p); } } if (Players != null && Players.Count > 0) GamePlay.gpHUDLogCenter(Players.ToArray(), msg, parms); } private void sendChatMessageTo(int army, string msg, object[] parms) { List<Player> Players = new List<Player>(); // on Dedi the server: if (GamePlay.gpPlayer() != null) { if (GamePlay.gpPlayer().Army() == army || army == -1) Players.Add(GamePlay.gpPlayer()); } //rest of the crowd if (GamePlay.gpRemotePlayers() != null || GamePlay.gpRemotePlayers().Length > 0) { foreach (Player p in GamePlay.gpRemotePlayers()) { if (p.Army() == army || army == -1) Players.Add(p); } } if (Players != null && Players.Count > 0) GamePlay.gpLogServer(Players.ToArray(), msg, parms); } private void sendScreenMessageToPilot(AiAircraft aircraft, string msg, object[] parms) { GamePlay.gpHUDLogCenter(new Player[] { aircraft.Player(0) }, msg, null); } private void sendScreenMessageToAllCrew(AiAircraft aircraft, string msg, object[] parms) { for (int i = 0; i < aircraft.Places(); i++) if (aircraft.Player(i) != null) GamePlay.gpHUDLogCenter(new Player[] { aircraft.Player(i) }, msg, null); } private void destroyShips(int army) { if (GamePlay.gpGroundGroups(1).Length == 0 || GamePlay.gpGroundGroups(army) == null || (army != 1 && army != 2)) return; foreach (AiGroundGroup group in GamePlay.gpGroundGroups(army)) { if (group.GroupType() == AiGroundGroupType.Ship) { destroyGroundGroup(group); } } } private void destroyTrains(int army) { if (GamePlay.gpGroundGroups(1).Length == 0 || GamePlay.gpGroundGroups(army) == null || (army != 1 && army != 2)) return; foreach (AiGroundGroup group in GamePlay.gpGroundGroups(army)) { if (group.GroupType() == AiGroundGroupType.Train) { destroyGroundGroup(group); } } } private void destroyGroundGroup(AiGroundGroup group) { if (group != null) { foreach (AiActor actor in group.GetItems()) { (actor as AiGroundActor).Destroy(); } } } }
__________________
When one engine fails on a two engine bomber, you will always have enough power left to get to the scene of the crash. Get the latest COD Team Fusion patch info HERE |
#2
|
|||
|
|||
Just a guess: gpPlayer() returns null if you are hosting it on a dedicated server and gpPlayer().Place() returns null if the player is not within an vehicle.
Code:
private void DoDamage() { // Script that triggered an accidental damage to the player plane // Author: FG28_Kodiak Modified by: Salmo // Ref: http://forum.1cpublishing.eu/showthread.php?t=22184 if(GamePlay.gpPlayer() != null && GamePlay.gpPlayer().Place() != null && GamePlay.gpPlayer().Place() is AiAircraft) { PlayerPlane = (AiAircraft)GamePlay.gpPlayer().Place(); ... } } |
#3
|
|||
|
|||
Thanks Banks, I thought the same, so I've played with removing the Dodamage() call etc but it did not fix the error. So while your code suggestion is more elegant (& I've implimented it), it doesnt fix the NullReferenceException error
__________________
When one engine fails on a two engine bomber, you will always have enough power left to get to the scene of the crash. Get the latest COD Team Fusion patch info HERE |
#4
|
|||
|
|||
Hi Salmo,
I've been seeing this error through the dedi box since the game was released (scripts or not). There's still a huge disconnect between running missions through the in-game GUI and running them through the -server. Nice stuff you've done with scripting btw! |
#5
|
|||
|
|||
@salmo:
Modified: Code:
public override void OnAircraftTookOff(int missionNumber, string shortName, AiAircraft aircraft) { if (m_random.Next(1, 200 + 1) != 67) // 0.5% chance of takeoff failure { DoDamage(aircraft); } } private void DoDamage(AiAircraft aircraft) { // Script that triggered an accidental damage to the player plane // Author: FG28_Kodiak Modified by: Salmo // Ref: http://forum.1cpublishing.eu/showthread.php?t=22184 if (aircraft.Player(0) == null) return; // If no human pilot leave switch (m_random.Next(1, 7 + 1)) { // =================================== // ---- Engine(s) failures ---- // =================================== case 1: aircraft.hitNamed(part.NamedDamageTypes.Eng0TotalFailure); //sendScreenMessageToPilot(PlayerPlane, "Engine Failure", null); //GamePlay.gpHUDLogCenter("Engine Failure"); break; case 2: aircraft.hitNamed(part.NamedDamageTypes.Eng0OilSecondariesFire); //sendScreenMessageToPilot(aircraft, "Oil is on fire"); GamePlay.gpHUDLogCenter("Oil is on fire"); break; // =================================== // ---- Hydrolicss & cables failures ---- // =================================== case 3: if (m_random.Next(1, 2) == 1) { aircraft.hitNamed(part.NamedDamageTypes.UndercarriageUpLockFailureL); //sendScreenMessageToPilot(aircraft, "Undercarriage failure"); GamePlay.gpHUDLogCenter("Undercarriage failure"); } else { aircraft.hitNamed(part.NamedDamageTypes.UndercarriageUpLockFailureR); //sendScreenMessageToPilot(aircraft, "Undercarriage failure"); GamePlay.gpHUDLogCenter("Undercarriage failure"); } break; case 4: aircraft.hitNamed(part.NamedDamageTypes.Eng0ThrottleControlBroken); //sendScreenMessageToPilot(aircraft, "Broken throttle cable"); GamePlay.gpHUDLogCenter("Broken throttle cable"); break; // =================================== // ---- Flight surfaces failures ---- // =================================== case 5: aircraft.hitNamed(part.NamedDamageTypes.ControlsElevatorDisabled); //sendScreenMessageToPilot(aircraft, "Elevator jammed"); GamePlay.gpHUDLogCenter("Elevator jammed"); break; case 6: aircraft.hitNamed(part.NamedDamageTypes.ControlsAileronsDisabled); //sendScreenMessageToPilot(aircraft, "Ailerons jammed"); GamePlay.gpHUDLogCenter("Ailerons jammed"); break; case 7: aircraft.hitNamed(part.NamedDamageTypes.ControlsRudderDisabled); //sendScreenMessageToPilot(aircraft, "Rudder jammed"); GamePlay.gpHUDLogCenter("Rudder jammed"); break; default: aircraft.hitNamed(part.NamedDamageTypes.Eng0TotalFailure); //sendScreenMessageToPilot(aircraft, "Engine Failure"); GamePlay.gpHUDLogCenter("Engine Failure"); break; } } |
#6
|
|||
|
|||
Cool. Simple modification, to add a random delay before failure:
Code:
public override void OnAircraftTookOff(int missionNumber, string shortName, AiAircraft aircraft) { if (m_random.Next(1, 200 + 1) != 67) // 0.5% chance of takeoff failure { Timeout(m_random.Next(1, 60), ()=> DoDamage(aircraft)); } } Last edited by Octocat; 12-16-2011 at 06:21 AM. |
|
|