Example #1
0
        public override void ReadData(Reader reader)
        {
            //Clear out any current jobs.
            while (normalJobs.Count > 0)
            {
                Cancel(normalJobs.First());
            }
            while (emergencyJobs.Count > 0)
            {
                Cancel(emergencyJobs.First());
            }

            //Stop JobQueries from doing stuff until after deserialization is done.
            JobQueries.Dispose();

            base.ReadData(reader);

            reader.Collection("normalJobs",
                              (MyData.Reader rd, ref Job outval, string name) =>
                              { outval = Job.Read(rd, name, TheMap); },
                              (i) => normalJobs);
            reader.Collection("emergencyJobs",
                              (MyData.Reader rd, ref Job outVal, string name) =>
                              { outVal = Job.Read(rd, name, TheMap); },
                              (i) => emergencyJobs);

            foreach (Job j in normalJobs.Concat(emergencyJobs))
            {
                InitJob(j);
            }
        }
Example #2
0
        /// <summary>
        /// Finds the most urgent job that the given PlayerChar can do,
        ///     removes it from this group, and returns it.
        /// Returns "null" if no applicable jobs exist.
        /// </summary>
        /// <param name="onlyEmergencies">If true, only emergency jobs will be looked at.</param>
        public Job TakeJob(Units.PlayerChar pChar, bool onlyEmergencies)
        {
            //First search emergency jobs, then (if applicable) non-emergencies.

            HashSet <Job> jobCollection = emergencyJobs;
            Job           toTake        = pChar.Career.ChooseApplicable(jobCollection);

            if (toTake == null && !onlyEmergencies)
            {
                jobCollection = normalJobs;
                toTake        = pChar.Career.ChooseApplicable(jobCollection);
            }

            if (toTake != null)
            {
                //Remove the job from this collection.
                DeInitJob(toTake);
                jobCollection.Remove(toTake);
                if (OnJobTaken != null)
                {
                    OnJobTaken(this, toTake, pChar);
                }
            }

            return(toTake);
        }
Example #3
0
        private void Callback_JobEmergencyChanged(Job j, bool oldVal, bool newVal)
        {
            HashSet <Job> oldSet = (oldVal ? emergencyJobs : normalJobs),
                          newSet = (newVal ? emergencyJobs : normalJobs);

            oldSet.Remove(j);
            newSet.Add(j);
        }
Example #4
0
 private void Callback_JobFinished(GameLogic.Units.Player_Char.Job job,
                                   bool wasSuccessful, string message)
 {
     if (message != null)
     {
         Announce(message);
     }
 }
Example #5
0
        public override void WriteData(Writer writer)
        {
            base.WriteData(writer);

            writer.Collection <Job, HashSet <Job> >(
                normalJobs, "normalJobs",
                (wr, val, name) => Job.Write(wr, val, name));
            writer.Collection <Job, HashSet <Job> >(
                emergencyJobs, "emergencyJobs",
                (wr, val, name) => Job.Write(wr, val, name));
        }
Example #6
0
        /// <summary>
        /// Adds the given job to this group.
        /// Returns whether it already existed in this collection.
        /// </summary>
        public bool AddJob(Job j)
        {
            HashSet <Job> toUse = (j.IsEmergency ? normalJobs : emergencyJobs);

            if (toUse.Add(j))
            {
                InitJob(j);

                if (OnNewJob != null)
                {
                    OnNewJob(this, j);
                }

                return(false);
            }
            else
            {
                return(true);
            }
        }
Example #7
0
        /// <summary>
        /// Removes the given job from this group, if it exists.
        /// Returns whether it existed.
        /// </summary>
        public bool Cancel(Job j)
        {
            HashSet <Job> toUse = (j.IsEmergency ? normalJobs : emergencyJobs);

            if (toUse.Remove(j))
            {
                DeInitJob(j);

                if (OnJobCanceled != null)
                {
                    OnJobCanceled(this, j);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #8
0
 private void DeInitJob(Job j)
 {
     j.IsEmergency.OnChanged -= Callback_JobEmergencyChanged;
 }
Example #9
0
 private void Callback_NewJob(GameLogic.Groups.PlayerGroup group,
                              GameLogic.Units.Player_Char.Job newJob)
 {
     newJob.OnJobFinished += Callback_JobFinished;
 }