Beispiel #1
0
        public void UpdateState(int tick, bool verbose)
        {
            // check that we have a valid enumerator
            // also check that the time of the hero's next state update matches the current tick
            //	otherwise, there's nothing for us to do here until the simulation advances further
            if (MyHero.IsEnumeratorValid() == false || MyHero.GetCurrentStep().Tick > tick)
            {
                return;
            }


            // ok, so checking for the case where an item is just shuffled to a different slot in
            //  the inventory is a bit messy
            // for each time step keep track of all the items modified
            //  and keep a running total of increments and decrements
            //	we'll use that at the end to determine what has actually been purchased (if anything)
            //	at this time step
            // this should be able to handle the case where an item (or 2) are just swapped
            //  from one slot to another
            // i.e., we'll decrement its count, but also increment it
            //	so there should be no net change once we're finished processing the time step
            // also the case where an inventory update was issued, but only the item's charges changed
            //	 - again no net change

            List <int> diffIds    = new List <int>();
            List <int> diffCounts = new List <int>();
            int        index      = -1; // the index of an item of interest and its count in the above lists

            List <Item> purchasedItems = new List <Item>();

            // check each state change in the time step
            foreach (StateChange diff in MyHero.GetCurrentStep().Diffs)
            {
                // for now, we are only interested in state changes that involve inventory updates and purchases

                if (diff.Type == UpdateType.ItemPurchase)
                {
                    purchasedItems.Add(((ItemPurchase)diff).NewItem);
                }
                else if (diff.Type == UpdateType.InventoryUpdate)
                {
                    Item item    = ((InventoryUpdate)diff).Contents;
                    Item oldItem = MyHero.GetSlot(((InventoryUpdate)diff).Slot);                        // get the current contents of the slot
                    int  slot    = ((InventoryUpdate)diff).Slot;

                    // for now, we only care about items that can be purchased and are not consumable
                    // we also need to track the case where and inventory slot's contents are set to empty

                    // check for the case where the slot is emptied
                    if (item.IsEmpty())
                    {
                        // check whether we care about the item being removed from this slot
                        if (oldItem != null && oldItem.IsEmpty() == false &&
                            oldItem.IsConsumable() == false && oldItem.IsPurchasable() == true)
                        {
                            // check whether we are already tracking oldItem's id for this time step
                            index = diffIds.IndexOf((int)(oldItem.Id));
                            if (index != -1)                    // we are already tracking this item
                            {
                                diffCounts[index]--;            // remove one instance of the item from the count
                            }
                            else                                // we need to add this item to the list of tracked items
                            {
                                diffIds.Add((int)(oldItem.Id)); // add the id to the list of tracked ids
                                diffCounts.Add(-1);             // init the count to record the removal of one instance
                            }
                        }
                    }
                    // check for the case where an item's charges have changed
                    // but no items are added or removed
                    // right now, we don't really do anything in this case
                    else if (oldItem != null && oldItem.Id == item.Id)
                    {
                        // do nothing for now
                    }
                    // check for the case where a new item is being added
                    // possibly replacing an old item (e.g., the old item is consumed in an updgrade)
                    else
                    {
                        // if there was already an item in the slot, and we care about it, decrement its count
                        if (oldItem != null && oldItem.IsEmpty() == false && oldItem.IsConsumable() == false &&
                            oldItem.IsPurchasable() == true)
                        {
                            // check whether we are already tracking the old item's id for this time step
                            index = diffIds.IndexOf((int)(oldItem.Id));
                            if (index != -1)                    // we are already tracking this item
                            {
                                diffCounts[index]--;            // remove one instance of the item from the count
                            }
                            else                                // we need to add this item to the list of tracked items
                            {
                                diffIds.Add((int)(oldItem.Id)); // add the id to the list of tracked ids
                                diffCounts.Add(-1);             // init the count to record the removal of one instance
                            }
                        }

                        // check whether we care about the new item, and if so, increment its count
                        if (item != null && item.IsEmpty() == false && item.IsConsumable() == false &&
                            item.IsPurchasable() == true)
                        {
                            // check whether we are already tracking the item's id for this tiem step
                            index = diffIds.IndexOf((int)(item.Id));
                            if (index != -1)                    // we are already tracking this item
                            {
                                diffCounts[index]++;            // add one instance of the item to the count
                            }
                            else                                // we need to add this item to the list of tracked items
                            {
                                diffIds.Add((int)(item.Id));    // add the id to the list of tracked ids
                                diffCounts.Add(1);              // init the count to record the addition of one instance
                            }
                        }
                    }
                }
            }
            // update the agent's inventory totals based on the lists of items that have changed counts this time step
            bool itemsChanged = false;

            for (int i = 0; i < diffIds.Count; i++)
            {
                // if the count is 0, then there was no net change
                //  - just moved from one slot to another, or the item's charges changed
                if (diffCounts[i] != 0)                         // there was a net change + or -
                {
                    Items[diffIds[i]] += diffCounts[i];

                    itemsChanged = true;
                }
            }
            if (verbose && itemsChanged == true)
            {
                int id = 0;
                Console.Write(MyHero.GetCurrentStep().Tick + ": ");
                if (purchasedItems.Count > 0)
                {
                    Console.Write("(");
                    foreach (Item item in purchasedItems)
                    {
                        if (item.IsUpgrage())
                        {
                            Console.Write("+" + item.Id.ToString().ToUpper());
                        }
                        else
                        {
                            Console.Write("+" + item.Id.ToString());
                        }
                    }
                    Console.Write(") : ");
                }
                foreach (int count in Items)
                {
                    if (count != 0)
                    {
                        if (Item.IsUpgrade((ItemId)(id)) == true)
                        {
                            Console.Write(((ItemId)id).ToString().ToUpper() + "=" + count + " ");
                        }
                        else
                        {
                            Console.Write(((ItemId)id).ToString() + "=" + count + " ");
                        }
                    }
                    id++;
                }

                Console.WriteLine("");
            }

            // update the hero to the current time step
            // Note: this will also increment the Hero's enumerator if need be
            MyHero.DoTimeStep(MyHero.GetCurrentStep().Tick);
        }