Exemplo n.º 1
0
 public TaskNode(Task task)
 {
     this.data = task;
     done = false;
     children = new List<TaskNode>();
     parents = new List<TaskNode>();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Validates a given task, forcing any open preconditions.  In theory,
        /// nothing should ever happen within this method - but just in case.
        /// </summary>
        /// <param name="task">The task to validate.</param>
        private void EmergencyRepair(Task task)
        {
            //Saving myself some typing.
            WorldScript ws = Globals.Instance.WorldScript;

            //Teleporting around the world is always valid.
            if (task.Type == "goto")
                return;

            //Make sure the actor and actee (if not null) are in the location.
            if (task.Actor.Locale != task.Locale && (task.Actor != ws.PartyCharacter || this.RepairPlayer)) {
                Debug.LogWarning (task.Actor.name + " not at locale " + task.Locale.name + " to execute " + task.Type + ".  Repairing.");
                ws.Teleport (task.Actor, task.Locale);
            }
            if (task.Actee != null && task.Actee.Locale != task.Locale && (task.Actee != ws.PartyCharacter || this.RepairPlayer)) {
                Debug.LogWarning (task.Actee.name + " not at locale " + task.Locale.name + " to execute " + task.Type + ".  Repairing.");
                ws.Teleport (task.Actee, task.Locale);
            }

            //Nothing more to check here.
            if (task.Type == "enter-combat")
                return;

            //If the item is supposed to be picked up, make sure it's on the ground at location.
            if (task.Type == "pickup") {
                ItemScript iScript = task.Item.Owner.GetComponent<ItemScript> ();
                if (iScript == null) {
                    Debug.LogWarning (task.Item.Name + " not on ground to execute " + task.Type + ".  Repairing.");
                    ws.DropItem (task.Item);
                    iScript = task.Item.Owner.GetComponent<ItemScript> ();
                }
                if (iScript.Locale != task.Locale) {
                    Debug.LogWarning (task.Item.Name + " not at locale " + task.Locale.name + " to execute " + task.Type + ".  Repairing.");
                    ws.Teleport (iScript, task.Locale);
                }
            }
            //If the item is supposed to be taken, make sure the actee has it.
            if ((task.Type == "collect" || task.Type == "steal" || task.Type == "loot") &&
                !task.Actee.Inventory.Contains (task.Item)) {
                Debug.LogWarning (task.Actee.name + " does not own " + task.Item.Name + " to execute " + task.Type + ".  Repairing.");
                //Figure out if it needs to be picked up or traded.
                if (task.Item.Owner.GetComponent<ItemScript> () != null)
                    ws.PickupItem (task.Actee, task.Item);
                else {
                    task.Item.Owner.GetComponent<CharacterScript> ().Inventory.Remove (task.Item);
                    task.Actee.Inventory.Add (task.Item);
                }
            }
            //If the item is supposed to be given or used, make sure the actor has it.
            if ((task.Type == "deliver" || task.Type == "kill-by-item" || task.Type == "revive-by-item") &&
                !task.Actor.Inventory.Contains (task.Item)) {
                if (task.Item == null) {
                    Debug.LogError ("There is no item specified for action #" + task.ID + ": " + task.Type);
                }
                Debug.LogWarning (task.Actor.name + " does not own " + task.Item.Name + " to execute " + task.Type + ".  Repairing.");
                //Figure out if it needs to be picked up or traded.
                if (task.Item.Owner.GetComponent<ItemScript> () != null)
                    ws.PickupItem (task.Actor, task.Item);
                else {
                    task.Item.Owner.GetComponent<CharacterScript> ().Inventory.Remove (task.Item);
                    task.Actor.Inventory.Add (task.Item);
                }
            }

            //....I think that's everything....
        }
Exemplo n.º 3
0
        private void buildTaskGraph(List<StoryEvent> events, List<Link> links)
        {
            Dictionary<int, TaskNode> dictionary = new Dictionary<int, TaskNode> ();
            foreach (StoryEvent e in events) {
                int id = e.ID;
                Task t = new Task (e);
                Debug.Log ("built task " + id+","+t.IsEnding+","+t.Mood+","+t.Type);
                TaskNode node = new TaskNode (t);
                dictionary.Add (id, node);
            }

            foreach (Link l in links) {
                TaskNode parentNode = dictionary [l.Source];
                TaskNode childNode = dictionary [l.Target];
                if (!parentNode.children.Contains (childNode))
                    parentNode.children.Add (childNode);
                if (!childNode.parents.Contains (parentNode))
                    childNode.parents.Add (parentNode);
            }

            List<TaskNode> roots = new List<TaskNode> ();
            foreach (KeyValuePair<int, TaskNode> pair in dictionary) {
                if (pair.Value.parents.Count == 0)
                    roots.Add (pair.Value);
            }
            // TODO: throw an exception when there are more than one root
            this.scriptRoot = roots.ElementAt (0);

            this.endings = new List<TaskNode> ();
            foreach (KeyValuePair<int, TaskNode> pair in dictionary) {
                if (pair.Value.children.Count == 0)
                    endings.Add (pair.Value);
            }
        }