private void Update()
        {
            if (workerEnabled)
            {
                if (scenarioController == null)
                {
                    foreach (ProtoScenarioModule psm in HighLogic.CurrentGame.scenarios)
                    {
                        if (psm != null)
                        {
                            if (psm.moduleName == "ScenarioDiscoverableObjects")
                            {
                                if (psm.moduleRef != null)
                                {
                                    scenarioController = (ScenarioDiscoverableObjects)psm.moduleRef;
                                    scenarioController.spawnInterval = float.MaxValue;
                                }
                            }
                        }
                    }
                }

                if (scenarioController != null)
                {
                    if ((Client.realtimeSinceStartup - lastAsteroidCheck) > ASTEROID_CHECK_INTERVAL)
                    {
                        lastAsteroidCheck = Client.realtimeSinceStartup;
                        //Try to acquire the asteroid-spawning lock if nobody else has it.
                        if (!lockSystem.LockExists("asteroid-spawning"))
                        {
                            lockSystem.AcquireLock("asteroid-spawning", false);
                        }

                        //We have the spawn lock, lets do stuff.
                        if (lockSystem.LockIsOurs("asteroid-spawning"))
                        {
                            if ((HighLogic.CurrentGame.flightState.protoVessels != null) && (FlightGlobals.fetch.vessels != null))
                            {
                                if ((HighLogic.CurrentGame.flightState.protoVessels.Count == 0) || (FlightGlobals.fetch.vessels.Count > 0))
                                {
                                    int beforeSpawn      = GetAsteroidCount();
                                    int asteroidsToSpawn = maxNumberOfUntrackedAsteroids - beforeSpawn;
                                    for (int asteroidsSpawned = 0; asteroidsSpawned < asteroidsToSpawn; asteroidsSpawned++)
                                    {
                                        DarkLog.Debug("Spawning asteroid, have " + (beforeSpawn + asteroidsSpawned) + ", need " + maxNumberOfUntrackedAsteroids);
                                        scenarioController.SpawnAsteroid();
                                    }
                                }
                            }
                        }

                        //Check for changes to tracking
                        foreach (Vessel asteroid in GetCurrentAsteroids())
                        {
                            if (asteroid.state != Vessel.State.DEAD)
                            {
                                if (!serverAsteroidTrackStatus.ContainsKey(asteroid.id.ToString()))
                                {
                                    serverAsteroidTrackStatus.Add(asteroid.id.ToString(), asteroid.DiscoveryInfo.trackingStatus.Value);
                                }
                                else
                                {
                                    if (asteroid.DiscoveryInfo.trackingStatus.Value != serverAsteroidTrackStatus[asteroid.id.ToString()])
                                    {
                                        ProtoVessel pv = asteroid.BackupVessel();
                                        DarkLog.Debug("Sending changed asteroid, new state: " + asteroid.DiscoveryInfo.trackingStatus.Value + "!");
                                        serverAsteroidTrackStatus[asteroid.id.ToString()] = asteroid.DiscoveryInfo.trackingStatus.Value;
                                        networkWorker.SendVesselProtoMessage(pv, false, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void Update()
        {
            if (!workerEnabled)
            {
                return;
            }
            InitializeScenario();
            if (scenario == null)
            {
                return;
            }

            if (lastAsteroidCheck + ASTEROID_CHECK_INTERVAL > Client.realtimeSinceStartup)
            {
                return;
            }
            lastAsteroidCheck = Client.realtimeSinceStartup;

            //Try to acquire the asteroid-spawning lock if nobody else has it.
            if (!lockSystem.LockExists("asteroid-spawning"))
            {
                lockSystem.AcquireLock("asteroid-spawning", false);
            }

            //We have the spawn lock, lets do stuff.
            if (lockSystem.LockIsOurs("asteroid-spawning"))
            {
                if (HighLogic.LoadedSceneIsFlight && !FlightGlobals.ready)
                {
                    return;
                }
                if (HighLogic.CurrentGame.flightState.protoVessels == null)
                {
                    return;
                }
                if (FlightGlobals.fetch.vessels == null)
                {
                    return;
                }
                if (HighLogic.CurrentGame.flightState.protoVessels.Count == 0)
                {
                    return;
                }
                if (FlightGlobals.fetch.vessels.Count == 0)
                {
                    return;
                }
                int beforeSpawn = GetAsteroidCount();
                if (beforeSpawn < maxNumberOfUntrackedAsteroids)
                {
                    ProtoVessel asty = SpawnAsteroid();
                    DarkLog.Debug("Spawned asteroid " + asty.vesselName + ", have " + (beforeSpawn) + ", need " + maxNumberOfUntrackedAsteroids);
                }
            }

            //Check for changes to tracking
            lock (serverAsteroids)
            {
                foreach (Vessel asteroid in GetCurrentAsteroids())
                {
                    if (asteroid.state != Vessel.State.DEAD)
                    {
                        if (!serverAsteroidTrackStatus.ContainsKey(asteroid.id))
                        {
                            serverAsteroidTrackStatus.Add(asteroid.id, asteroid.DiscoveryInfo.trackingStatus.Value);
                        }
                        else
                        {
                            if (asteroid.DiscoveryInfo.trackingStatus.Value != serverAsteroidTrackStatus[asteroid.id])
                            {
                                ProtoVessel pv = asteroid.BackupVessel();
                                DarkLog.Debug("Sending changed asteroid, new state: " + asteroid.DiscoveryInfo.trackingStatus.Value + "!");
                                serverAsteroidTrackStatus[asteroid.id] = asteroid.DiscoveryInfo.trackingStatus.Value;
                                networkWorker.SendVesselProtoMessage(pv, false, false);
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        private void Update()
        {
            if (!workerEnabled)
            {
                return;
            }
            if (Client.realtimeSinceStartup - lastAsteroidCheck < ASTEROID_CHECK_INTERVAL)
            {
                return;
            }
            else
            {
                lastAsteroidCheck = Client.realtimeSinceStartup;
            }
            if (!initialized)
            {
                InitializeScenario();
            }

            //Try to acquire the asteroid-spawning lock if nobody else has it.
            if (!lockSystem.LockExists("asteroid-spawning"))
            {
                lockSystem.AcquireLock("asteroid-spawning", false);
            }

            //We have the spawn lock, lets do stuff.
            if (lockSystem.LockIsOurs("asteroid-spawning"))
            {
                if ((HighLogic.CurrentGame.flightState.protoVessels != null) && (FlightGlobals.fetch.vessels != null))
                {
                    if ((HighLogic.CurrentGame.flightState.protoVessels.Count == 0) || (FlightGlobals.fetch.vessels.Count > 0))
                    {
                        int beforeSpawn      = GetAsteroidCount();
                        int asteroidsToSpawn = maxNumberOfUntrackedAsteroids - beforeSpawn;
                        if (asteroidsToSpawn > 0)
                        {
                            foreach (Vessel asty in SpawnAsteroids(1)) // spawn 1 every ASTEROID_CHECK_INTERVAL seconds
                            {
                                DarkLog.Debug("Spawned asteroid " + asty.name + ", have " + (beforeSpawn) + ", need " + maxNumberOfUntrackedAsteroids);
                            }
                        }
                    }
                }
            }

            //Check for changes to tracking
            foreach (Vessel asteroid in GetCurrentAsteroids())
            {
                if (asteroid.state != Vessel.State.DEAD)
                {
                    if (!serverAsteroidTrackStatus.ContainsKey(asteroid.id.ToString()))
                    {
                        serverAsteroidTrackStatus.Add(asteroid.id.ToString(), asteroid.DiscoveryInfo.trackingStatus.Value);
                    }
                    else
                    {
                        if (asteroid.DiscoveryInfo.trackingStatus.Value != serverAsteroidTrackStatus[asteroid.id.ToString()])
                        {
                            ProtoVessel pv = asteroid.BackupVessel();
                            DarkLog.Debug("Sending changed asteroid, new state: " + asteroid.DiscoveryInfo.trackingStatus.Value + "!");
                            serverAsteroidTrackStatus[asteroid.id.ToString()] = asteroid.DiscoveryInfo.trackingStatus.Value;
                            networkWorker.SendVesselProtoMessage(pv, false, false);
                        }
                    }
                }
            }
        }