internal void AddItemBunch(ItemBunch itemBunch) { if (Items.ContainsKey(itemBunch.Type)) { var existedBunch = Items[itemBunch.Type]; var requiredCapacity = (itemBunch + existedBunch).RequiredCapacity - existedBunch.RequiredCapacity; if (requiredCapacity + _usedCapacity > Type.StorageCapacity) { throw new Exception("Storage is full"); } _usedCapacity += requiredCapacity; existedBunch.Count = itemBunch.Count; } else { if (_usedCapacity + itemBunch.RequiredCapacity > Type.StorageCapacity) { throw new Exception("Storage is full"); } _usedCapacity += itemBunch.RequiredCapacity; Items.Add(itemBunch.Type, itemBunch); } EventHandler.OnItemBunchAdded(new ItemBunchDto(itemBunch)); }
internal void RemoveItemBunch(ItemBunch itemBunch) { if (itemBunch.Count > 0) { if (!Contains(itemBunch)) { throw new Exception("Construction doesn't contain item bunch"); } var capacityForBunch = Items[itemBunch.Type].RequiredCapacity; Items[itemBunch.Type] -= itemBunch; _usedCapacity += Items[itemBunch.Type].RequiredCapacity - capacityForBunch; EventHandler.OnItemBunchRemoved(new ItemBunchDto(itemBunch)); } }
internal void Interpret(Construction construction) { var magazine = new Stack <(int Number, string Str)>(); var k = 0; var rpn = construction.Type.Logic[construction.State]; while (k < rpn.Count) { (int, string)value = (rpn[k].Number, rpn[k].Str); switch (rpn[k].Type) { case Elem.ElemType.Arg: magazine.Push(value); break; case Elem.ElemType.TryCraft: var target = new ItemBunch(new ItemBunchDto { Count = magazine.Pop().Number, TypeId = magazine.Pop().Number }, _types); var requiredItemCount = magazine.Pop().Number; var requiredItems = new List <ItemBunch>(requiredItemCount); for (int i = 0; i < requiredItemCount; i++) { requiredItems.Add(new ItemBunch(new ItemBunchDto { Count = magazine.Pop().Number, TypeId = magazine.Pop().Number }, _types)); } var craftResult = construction.TryCraft(target, requiredItems); if (craftResult) { magazine.Push((1, null)); } else { magazine.Push((0, null)); } break; } k++; } }
internal bool TryCraft(ItemBunch target, List <ItemBunch> requiredItems) { var releasedCapacity = 0; foreach (var requiredItemBunch in requiredItems) { if (!Contains(requiredItemBunch)) { return(false); } var itemType = requiredItemBunch.Type; var existedBunch = Items[itemType]; if (target.Type == itemType) { target.Count -= requiredItemBunch.Count; requiredItemBunch.Count = 0; } else { releasedCapacity += existedBunch.RequiredCapacity - (existedBunch - requiredItemBunch).RequiredCapacity; } } var capacityDiff = Items.ContainsKey(target.Type) ? (target + Items[target.Type]).RequiredCapacity - Items[target.Type].RequiredCapacity : target.RequiredCapacity; if (_usedCapacity - releasedCapacity + capacityDiff > Type.StorageCapacity) { return(false); } foreach (var requiredItemBunch in requiredItems) { RemoveItemBunch(requiredItemBunch); } AddItemBunch(target); return(true); }
internal bool Contains(ItemBunch itemBunch) { return(Items.ContainsKey(itemBunch.Type) && Items[itemBunch.Type].Count >= itemBunch.Count); }