/// <summary> /// Open a user dialog which allows th user navigate through the NPCs Inventory and take Items /// </summary> public void loot() { if (!this.inventory.ContainsAnyItems()) { CIO.Print(this.name + "'s inventory is empty"); return; } GenericOption takeAllOption = new GenericOption("take all"); Optionhandler oh = new Optionhandler(this.name + "'s inventory ", true); Option selectedOption = null; //the user stays inside the inventory until all items are gon or he picks exit while (selectedOption != Optionhandler.Exit && this.inventory.ContainsAnyItems()) { List <Item> allitems = this.inventory.GetAllItems(); oh.ClearOptions(); oh.AddOptions(Optionhandler.ItemToOption(allitems)); oh.AddOption(takeAllOption); selectedOption = oh.selectOption(); if (selectedOption == takeAllOption) { foreach (Item i in allitems) { Inventory.transferItem(this.inventory, Player.getInstance().inventory, i); } } else { Inventory.transferItem(this.inventory, Player.getInstance().inventory, (Item)selectedOption); } } }
/// Inserts a single rectangle into the bin. The packer might rotate the rectangle, in which case the returned /// struct will have the width and height values swapped. /// @param merge If true, performs free Rectangle Merge procedure after packing the new rectangle. This procedure /// tries to defragment the list of disjoint free rectangles to improve packing performance, but also takes up /// some extra time. /// @param rectChoice The free rectangle choice heuristic rule to use. /// @param splitMethod The free rectangle split heuristic rule to use. public override Rect Insert(RectSize rectSize, GenericOption option) { var opt = option as Option; int width = rectSize.Width; int height = rectSize.Height; // Find where to put the new rectangle. int freeNodeIndex = 0; Rect newRect = FindPositionForNewNode(width, height, opt.FreeRectChoice, ref freeNodeIndex); // Abort if we didn't have enough space in the bin. if (newRect.Height == 0) { return(newRect); } // Remove the space that was just consumed by the new rectangle. SplitFreeRectByHeuristic(freeRectangles[freeNodeIndex], newRect, opt.GuillotineSplit); freeRectangles.RemoveAt(freeNodeIndex); // Perform a Rectangle Merge step if desired. if (opt.Merge) { MergeFreeList(); } // Remember the new used rectangle. UsedRectangles.Add(newRect); // Check that we're really producing correct packings here. disjointRects.Add(newRect); return(newRect); }
public override Boolean OnEnter() { base.OnEnter(); AddRoom(typeof(Forest_start)); Optionhandler threat = new Optionhandler("A Bandit appears between the trees. He points a saber at you"); GenericOption opt; opt = new GenericOption("flee"); opt.AddExecutionAction(() => tryflee()); threat.AddOption(opt); opt = new GenericOption("Attack"); opt.AddExecutionAction(() => attack()); threat.AddOption(opt); threat.selectOption(); if (!bandit.isAlive()) { Optionhandler oh = new Optionhandler("next move?", true); opt = new GenericOption("Loot"); oh.AddOption(opt); opt.AddExecutionAction(() => this.bandit.loot()); oh.selectOption().Select(); } return(true); }
public FightAction getPlayerWeapon() { while (true) { Optionhandler ohCategory = new Optionhandler("Choose your next Action"); ohCategory.AddOption(new GenericOption("Weapons")); ohCategory.AddOption(new GenericOption("Potion")); ohCategory.AddOption(new GenericOption("Spell"));//todo: check if items are in the option beforehand GenericOption selectedCategory = (GenericOption)ohCategory.selectOption(); List <Option> optionsInTheCategory; if (selectedCategory.name == "Spell") { optionsInTheCategory = Player.getInstance().getAllSpells().Cast <Option>().ToList(); } else { optionsInTheCategory = Player.getInstance().inventory.GetAllItems(selectedCategory.name).Cast <Option>().ToList(); } Optionhandler ohItem = new Optionhandler("choose your " + selectedCategory.name, true); ohItem.AddOptions(optionsInTheCategory); Option selected = ohItem.selectOption(); if (selected != Optionhandler.Exit) { return((FightAction)selected); } } }
public override Rect Insert(RectSize rectSize, GenericOption option) { Rect rect = new Rect(); // There are three cases: // 1. short edge <= long edge <= shelf height. Then store the long edge vertically. // 2. short edge <= shelf height <= long edge. Then store the short edge vertically. // 3. shelf height <= short edge <= long edge. Then store the short edge vertically. // If the long edge of the new rectangle fits vertically onto the current shelf, // flip it. If the short edge is larger than the current shelf height, store // the short edge vertically. int width = rectSize.Width; int height = rectSize.Height; if (((width > height && width < shelfHeight) || (width < height && height > shelfHeight))) { Swap(ref width, ref height); } if (currentX + width > BinWidth) { currentX = 0; currentY += shelfHeight; shelfHeight = 0; // When starting a new shelf, store the new long edge of the new rectangle horizontally // to minimize the new shelf height. if (width < height) { Swap(ref width, ref height); } } // If the rectangle doesn't fit in this orientation, try flipping. if (width > BinWidth || currentY + height > BinHeight) { Swap(ref width, ref height); } // If flipping didn't help, return failure. if (width > BinWidth || currentY + height > BinHeight) { return(rect); } rect.Width = width; rect.Height = height; rect.X = currentX; rect.Y = currentY; currentX += width; shelfHeight = Math.Max(shelfHeight, height); IncrementUsedArea(width * height); return(rect); }
private static void Demo_SaaImpliedVolitity() { var target = new GenericOption("go1", pv1: 100, pv2: 120, maturity: 1, sigma: 0.2); var n = 100; var dummys = new GenericOption[n]; for (var i = 0; i < n; i++) { dummys[i] = new GenericOption(string.Empty, target.Pv1, target.Pv2, target.Maturity, sigma: 0); } var xNames = new[] { nameof(GenericOption.Sigma) }; var range = new[] { (0D, 1D) };
/// <summary> /// Open the Inventory to navigate within it. /// <para> /// getsCommand is a function which takes an Item and returns wether the Item should receive a/many Command/s or not /// </para> /// <para>onItemSelection is an Action which is executed with the selected Item</para> /// <para>isAvailable is a FUnc which determines wether the item is available or not. It is appended to each Option</para> /// <para>UnavailableMessage is a Func which retruns the string to be printed when the Optionw as selected & unavailable</para> /// <para>if allowItemActions = false, no Item Actions are allowed</para> /// </summary> public void Open(Func <Item, bool> getsCommand, Action <Item> onItemSelection, Func <Item, bool> isAvailable, Func <Item, string> UnavailableMessage, bool allowItemActions) { while (true) { GenericOption g = PickCategory(); if (g == Optionhandler.Exit) { return; } NavigateItems(g.name, getsCommand, onItemSelection, isAvailable, UnavailableMessage, allowItemActions);//hier kann nur eine Exit-option rauskommen } }
private void friend() { Optionhandler d; d = new Optionhandler("Creature is barking: Hey hooman. Just need someone to talk. Nice to meet you. Byebye."); d.AddOption(new GenericOption("Petting the Dog-Ghost")); GenericOption opt = new GenericOption("Flee"); d.AddOption(opt); opt.AddExecutionAction(() => flee()); d.selectOption(); }
private static void Demo_ImpliedVolitity() { var go1 = new GenericOption("go1", pv1: 100, pv2: 120, maturity: 1, sigma: 0.2); var go2 = new GenericOption("go2", pv1: go1.Pv1, pv2: go1.Pv2, maturity: go1.Maturity, price: go1.Price); Console.WriteLine(go1); Console.WriteLine(go2); go1.Sigma = 0.3.Wrap(); go1.Pv2 = 100.Wrap(); Console.WriteLine(go1); Console.WriteLine(go2); Console.WriteLine(); }
/// <summary> /// Open the Inventory to pick a category. /// </summary> protected GenericOption PickCategory() { printHeader(); Optionhandler OH = new Optionhandler("pick a category", true); OH.setName("Inventory.Category"); foreach (string i in this.GetCategories()) { GenericOption GO = new GenericOption(i); OH.AddOption(GO); } GenericOption selected = (GenericOption)OH.selectOption(false); return(selected); }
/// <summary> /// Opens a Dialog for the user to trade with the merchant. The user can choose between selling and buying /// </summary> public void trade() { Option selectedOption = null; Optionhandler oh = new Optionhandler("trade with " + this.name, true); GenericOption opt = new GenericOption("Buy"); opt.AddExecutionAction(this.OpenInventoryForTrade); oh.AddOption(opt); opt = new GenericOption("Sell"); opt.AddExecutionAction(() => Player.getInstance().OpenInventoryForTrade(this)); oh.AddOption(opt); while (selectedOption != Optionhandler.Exit) { selectedOption = oh.selectOption(); } }
// Use our factory pattern to create objects from our OptionData.cs file private void CreateOptions() { OptionFactory optionFactory = new OptionFactory(); for (int i = 0; i < OptionData.GetOptions().Length; ++i) { string name = OptionData.GetOptions()[i]; // Make sure we don't already have an option with the same name if (optionDictionary.ContainsKey(name)) { throw new System.Exception($"There is already a {name} option!\nOptions.cs::CreateOptions()"); } GenericOption op = optionFactory.GetOption(in name); AddOption(in op); } }
/// Inserts a single rectangle into the bin, possibly rotated. public override Rect Insert(RectSize rect, GenericOption option) { Rect newNode = new Rect(); // Unused in this function. We don't need to know the score after finding the position. int score1 = int.MaxValue; int score2 = int.MaxValue; int width = rect.Width, height = rect.Height; var optionMaxRectsBinPath = option as Option; switch (optionMaxRectsBinPath.Method) { case FreeRectChoiceHeuristic.RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2); break; case FreeRectChoiceHeuristic.RectBottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2); break; case FreeRectChoiceHeuristic.RectContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, ref score1); break; case FreeRectChoiceHeuristic.RectBestLongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1); break; case FreeRectChoiceHeuristic.RectBestAreaFit: newNode = FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2); break; } if (newNode.Height == 0) { return(newNode); } int numRectanglesToProcess = freeRectangles.Count; for (int i = 0; i < numRectanglesToProcess; ++i) { if (SplitFreeNode(freeRectangles[i], ref newNode)) { freeRectangles.RemoveAt(i); --i; --numRectanglesToProcess; } } PruneFreeList(); UsedRectangles.Add(newNode); return(newNode); }
// Load our saved option values from file private void LoadOptions() { string fileLine; // Check Option file exists if (File.Exists(OPTIONFILEPATH)) { using (StreamReader sr = new StreamReader(OPTIONFILEPATH)) { // Parse each line in the file while ((fileLine = sr.ReadLine()) != null) { // Should be format of [OptionName]=[OptionValue] string[] values = fileLine.Split('='); if (values.Length != 2) { Debug.Log($"Improper format for option detected!"); continue; } // Make sure [OptionName] matches one of our listed options in our data if (!OptionData.IsOption(in values[0])) { Debug.Log($"{values[0]} is not a listed option. If it should be, make sure it is added to OptionData.cs."); continue; } // Get our option from data GenericOption genOp = TryGetOption(in values[0]); // Make sure [OptionValue] is a valid selection for that option if (genOp.IsValidChoice(in values[1])) { // Now we can set the value Debug.Log($"{values[1]} is a valid choice for {genOp.GetName()} option"); genOp.SetValue(in values[1]); } else { Debug.Log("invalid choice"); } }
private static void Main(string[] args) { Console.Title = "Quantitative Finance Pricing Console"; //Demo_DynamicOptionsPricing(); //Demo_ImpliedVolitity(); //Demo_SaaImpliedVolitity(); //Demo_EquationSetSaaSolver(); Demo_HW(); //Demo_HW_SaaSolver(); var t = new GenericOption(string.Empty, pv1: 100, pv2: 120, maturity: 1, sigma: 0); Func <double> getter = t.RemoteGetter(nameof(t.Price)); Action <double> setter = t.RemoteSetter("Sigma"); Func <double, double> link = t.RemoteLink(xName: "Sigma", yName: "Price"); Console.WriteLine(); Console.Read(); }
private void levelUp() { this.level++; CIO.Print("Reached level " + level); this.health += (int)Math.Floor(health * 0.02); GenericOption oStrength = new GenericOption("Strength"); GenericOption oIntelligence = new GenericOption("Intelligence"); Optionhandler oh = new Optionhandler("Choose an Attribute to upgrade "); oh.AddOption(oStrength); oh.AddOption(oIntelligence); Option result = (Option)oh.selectOption(); if (result == oStrength) { this.IncreaseStrength(1); } else if (result == oIntelligence) { this.IncreaseIntelligence(1); } }
/// Inserts a single rectangle into the bin, possibly rotated. public override Rect Insert(RectSize rectSize, GenericOption option) { int width = rectSize.Width; int height = rectSize.Height; // First try to pack this rectangle into the waste map, if it fits. Rect node = wasteMap.Insert(rectSize, new GuillotineBinPack.Option() { Merge = true, FreeRectChoice = GuillotineBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit, GuillotineSplit = GuillotineBinPack.GuillotineSplitHeuristic.SplitMaximizeArea }); Debug.Assert(disjointRects.Disjoint(node)); if (node.Height != 0) { Rect newNode = new Rect(); newNode.X = node.X; newNode.Y = node.Y; newNode.Width = node.Width; newNode.Height = node.Height; IncrementUsedArea(width * height); Debug.Assert(disjointRects.Disjoint(newNode)); disjointRects.Add(newNode); return(newNode); } Option opt = option as Option; switch (opt.Method) { case LevelChoiceHeuristic.LevelBottomLeft: return(InsertBottomLeft(width, height)); case LevelChoiceHeuristic.LevelMinWasteFit: return(InsertMinWaste(width, height)); default: Debug.Assert(false); return(node); } }
public override bool OnEnter() { base.OnEnter(); //CIO.PrintStory("The cave is as dark as night and an unsettling static lies in the air. Your Lucifer creates but a dim light that bareley reaches your feet." ); Optionhandler d = new Optionhandler("The cave is it dark and scary, to have some you grab your lucifer. You hear a creepy noise just several meters infront of you. A white, dusty creation apperas in front of you, moving her mouth without sound Seconds later echoing from the walls 'Greetings traveler' "); d.AddOption(new GenericOption("Attack")); GenericOption opt = new GenericOption("Hello spooky creature? How can I help you?"); d.AddOption(opt); opt.AddExecutionAction(() => friend()); d.selectOption(); if (thrownToNewRoom) { return(false); } AddRoom(typeof(Forest_start)); return(true); }
/// Inserts a single rectangle into the bin. The packer might rotate the rectangle, in which case the returned /// struct will have the width and height values swapped. /// @param method The heuristic rule to use for choosing a shelf if multiple ones are possible. public override Rect Insert(RectSize rectSize, GenericOption option) { Rect newNode = new Rect(); // First try to pack this rectangle into the waste map, if it fits. if (UseWasteMap) { newNode = wasteMap.Insert(rectSize, new GuillotineBinPack.Option() { Merge = true, FreeRectChoice = GuillotineBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit, GuillotineSplit = GuillotineBinPack.GuillotineSplitHeuristic.SplitMaximizeArea }); if (newNode.Height != 0) { // Track the space we just used. IncrementUsedArea(rectSize.Area); return(newNode); } } int width = rectSize.Width, height = rectSize.Height; Option shelfBinPackOption = option as Option; switch (shelfBinPackOption.Method) { case ShelfChoiceHeuristic.ShelfNextFit: if (FitsOnShelf(shelves.Last(), width, height, true)) { AddToShelf(shelves.Last(), width, height, ref newNode); return(newNode); } break; case ShelfChoiceHeuristic.ShelfFirstFit: for (int i = 0; i < shelves.Count; ++i) { if (FitsOnShelf(shelves[i], width, height, i == shelves.Count - 1)) { AddToShelf(shelves[i], width, height, ref newNode); return(newNode); } } break; case ShelfChoiceHeuristic.ShelfBestAreaFit: { // Best Area Fit rule: Choose the shelf with smallest remaining shelf area. Shelf bestShelf = null; int bestShelfSurfaceArea = int.MaxValue; for (int i = 0; i < shelves.Count; ++i) { // Pre-rotate the rect onto the shelf here already so that the area fit computation // is done correctly. RotateToShelf(shelves[i], ref width, ref height); if (FitsOnShelf(shelves[i], width, height, i == shelves.Count - 1)) { int surfaceArea = (BinWidth - shelves[i].currentX) * shelves[i].height; if (surfaceArea < bestShelfSurfaceArea) { bestShelf = shelves[i]; bestShelfSurfaceArea = surfaceArea; } } } if (null != bestShelf) { AddToShelf(bestShelf, rectSize.Width, rectSize.Height, ref newNode); return(newNode); } } break; case ShelfChoiceHeuristic.ShelfWorstAreaFit: { // Worst Area Fit rule: Choose the shelf with smallest remaining shelf area. Shelf bestShelf = null; int bestShelfSurfaceArea = -1; for (int i = 0; i < shelves.Count; ++i) { // Pre-rotate the rect onto the shelf here already so that the area fit computation // is done correctly. RotateToShelf(shelves[i], ref width, ref height); if (FitsOnShelf(shelves[i], width, height, i == shelves.Count - 1)) { int surfaceArea = (BinWidth - shelves[i].currentX) * shelves[i].height; if (surfaceArea > bestShelfSurfaceArea) { bestShelf = shelves[i]; bestShelfSurfaceArea = surfaceArea; } } } if (null != bestShelf) { AddToShelf(bestShelf, width, height, ref newNode); return(newNode); } } break; case ShelfChoiceHeuristic.ShelfBestHeightFit: { // Best Height Fit rule: Choose the shelf with best-matching height. Shelf bestShelf = null; int bestShelfHeightDifference = int.MaxValue; for (int i = 0; i < shelves.Count; ++i) { // Pre-rotate the rect onto the shelf here already so that the height fit computation // is done correctly. RotateToShelf(shelves[i], ref width, ref height); if (FitsOnShelf(shelves[i], width, height, i == shelves.Count - 1)) { int heightDifference = Math.Max(shelves[i].height - height, 0); Debug.Assert(heightDifference >= 0); if (heightDifference < bestShelfHeightDifference) { bestShelf = shelves[i]; bestShelfHeightDifference = heightDifference; } } } if (null != bestShelf) { AddToShelf(bestShelf, width, height, ref newNode); return(newNode); } } break; case ShelfChoiceHeuristic.ShelfBestWidthFit: { // Best Width Fit rule: Choose the shelf with smallest remaining shelf width. Shelf bestShelf = null; int bestShelfWidthDifference = int.MaxValue; for (int i = 0; i < shelves.Count; ++i) { // Pre-rotate the rect onto the shelf here already so that the height fit computation // is done correctly. RotateToShelf(shelves[i], ref width, ref height); if (FitsOnShelf(shelves[i], width, height, i == shelves.Count - 1)) { int widthDifference = BinWidth - shelves[i].currentX - width; Debug.Assert(widthDifference >= 0); if (widthDifference < bestShelfWidthDifference) { bestShelf = shelves[i]; bestShelfWidthDifference = widthDifference; } } } if (null != bestShelf) { AddToShelf(bestShelf, width, height, ref newNode); return(newNode); } } break; case ShelfChoiceHeuristic.ShelfWorstWidthFit: { // Worst Width Fit rule: Choose the shelf with smallest remaining shelf width. Shelf bestShelf = null; int bestShelfWidthDifference = -1; for (int i = 0; i < shelves.Count; ++i) { // Pre-rotate the rect onto the shelf here already so that the height fit computation // is done correctly. RotateToShelf(shelves[i], ref width, ref height); if (FitsOnShelf(shelves[i], width, height, i == shelves.Count - 1)) { int widthDifference = BinWidth - shelves[i].currentX - width; Debug.Assert(widthDifference >= 0); if (widthDifference > bestShelfWidthDifference) { bestShelf = shelves[i]; bestShelfWidthDifference = widthDifference; } } } if (null != bestShelf) { AddToShelf(bestShelf, width, height, ref newNode); return(newNode); } } break; } // The rectangle did not fit on any of the shelves. Open a new shelf. // Flip the rectangle so that the long side is horizontal. if (width < height && height <= BinWidth) { Swap(ref width, ref height); } if (CanStartNewShelf(height)) { if (UseWasteMap) { MoveShelfToWasteMap(shelves.LastOrDefault()); } StartNewShelf(height); Debug.Assert(FitsOnShelf(shelves.LastOrDefault(), width, height, true)); AddToShelf(shelves.LastOrDefault(), width, height, ref newNode); return(newNode); } /* * ///\todo This is problematic: If we couldn't start a new shelf - should we give up * /// and move all the remaining space of the bin for the waste map to track, * /// or should we just wait if the next rectangle would fit better? For now, * /// don't add the leftover space to the waste map. * else if (useWasteMap) * { * assert(binHeight - shelves.back().startY >= shelves.back().height); * shelves.back().height = binHeight - shelves.back().startY; * if (shelves.back().height > 0) * MoveShelfToWasteMap(shelves.back()); * * // Try to pack the rectangle again to the waste map. * GuillotineBinPack::Node node = wasteMap.Insert(width, height, true, 1, 3); * if (node.height != 0) * { * newNode.x = node.x; * newNode.y = node.y; * newNode.width = node.width; * newNode.height = node.height; * return newNode; * } * } */ // The rectangle didn't fit. return(newNode); }
public abstract Rect Insert(RectSize rectSize, GenericOption option);