protected override bool spawnFilteredInputs(string outputName) { DBItem rewardDBItem = Database.Instance.getItem(outputName); if (!rewardDBItem.propertyExists("madeby")) { if (_verbose) Debug.Log("Failed to generate combine puzzle: Reward could not be the result of combination"); return false; } List<KeyValuePair<string, string>> workingCombinations = rewardDBItem.getProperty("madeby") as List<KeyValuePair<string, string>>; List<KeyValuePair<string, string>> filteredCombinations = filterWorkingCombinations(workingCombinations); if (filteredCombinations.Count == 0) { if (_verbose) Debug.Log("Failed to generate combine puzzle: No working combinations fit constraints"); return false; } // Randomly shuffle our working combinations BuildingBlock.shuffle(filteredCombinations); // and try each combination foreach (KeyValuePair<string, string> itemPair in filteredCombinations) { // Unpack the "Tuples" string itemName1 = itemPair.Key; string itemName2 = itemPair.Value; PuzzleOutput possibleInput1 = _input1.generatePuzzle(itemName1); PuzzleOutput possibleInput2 = _input2.generatePuzzle(itemName2); if (possibleInput1 == null || possibleInput2 == null) { if (_verbose) Debug.Log("Failed to generate combine puzzle using " + itemName1 + " and " + itemName2); Debug.Log(string.Format("input1: {0}, input2: {1}", possibleInput1, possibleInput2)); _input1.despawnItems(); _input2.despawnItems(); continue; } else if (!BuildingBlock.areCarryable(new List<string>() { itemName1, itemName2}, new List<BuildingBlock>() { _input1, _input2 })) { if (_verbose) Debug.Log("Failed to generate combine puzzle using " + itemName1 + " and " + itemName2 + " because neither item is carryable to the other item."); _input1.despawnItems(); _input2.despawnItems(); continue; } // Success! else { if (_verbose) Debug.Log("Successfully generated combine puzzle for " + outputName + " with " + itemName1 + " and " + itemName2); // Add all the items spawned by our inputs _itemsToSpawn.AddRange(possibleInput1.Items); _itemsToSpawn.AddRange(possibleInput2.Items); _relationshipsToSpawn.AddRange(possibleInput1.Relationships); _relationshipsToSpawn.AddRange(possibleInput2.Relationships); // Generate the essential combine relationship CombineRelationship combine = new CombineRelationship(itemName1, _input1.outputSpawnIndex(), itemName2, _input2.outputSpawnIndex(), _spawnedOutput as PuzzleItem); _relationshipsToSpawn.Add(combine); return true; } } if (_verbose) Debug.Log("Failed to generate combine puzzle: no working combinations worked!"); return false; }
public void accept(CombineRelationship rel) { // Add the combine relationship to the map if (!_relationshipMap.ContainsKey(rel.ingredient1Name)) _relationshipMap[rel.ingredient1Name] = new Dictionary<string, IRelationship>(); _relationshipMap[rel.ingredient1Name][rel.ingredient2Name] = rel; if (!_relationshipMap.ContainsKey(rel.ingredient2Name)) _relationshipMap[rel.ingredient2Name] = new Dictionary<string, IRelationship>(); _relationshipMap[rel.ingredient2Name][rel.ingredient1Name] = rel; // Make sure our reward item is included in the list of item names _itemNames.Add(rel.resultItem.Name); }
public void accept(CombineRelationship rel) { // Create a new spawn item for the new item SpawnedPuzzleItem itemToSpawn = spawnItem(rel.resultItem); // Now, need to figure out where to spawn the new item. bool spawnInInventory = false, spawnAtLocation = false, spawnInItem1Container = false, spawnInItem2Container = false; Vector2 locationToSpawn = Vector2.zero; SpawnedPuzzleItem item1Container = _item1.parentItem; SpawnedPuzzleItem item2Container = _item2.parentItem; bool destroyItem1 = !_item1.propertyExists("destroyoncombine") || (bool)_item1.getProperty("destroyoncombine"); bool destroyItem2 = !_item2.propertyExists("destroyoncombine") || (bool)_item2.getProperty("destroyoncombine"); // If neither ingredient is destoyed, just spawn in the room if (!destroyItem1 && !destroyItem2) { spawnAtLocation = false; spawnInInventory = false; spawnInItem1Container = false; spawnInItem2Container = false; } else if (itemToSpawn.carryable && ((destroyItem1 && _item1.inInventory) || (destroyItem2 && _item2.inInventory))) spawnInInventory = true; // If the item is not carryable, have to spawn in room else { spawnInInventory = false; // Check if we can spawn in item1's container if (destroyItem1 && _item1.insideItem && itemToSpawn.propertyExists("fills") && (itemToSpawn.getProperty("fills") as List<string>).Contains(item1Container.itemName)) { spawnInItem1Container = true; } else if (destroyItem2 && _item2.insideItem && itemToSpawn.propertyExists("fills") && (itemToSpawn.getProperty("fills") as List<string>).Contains(item2Container.itemName)) { spawnInItem2Container = true; } else if (destroyItem1 && !_item1.inInventory) { spawnAtLocation = true; locationToSpawn = _item1.gridPos; } else if (destroyItem2 && !_item2.inInventory) { spawnAtLocation = true; locationToSpawn = _item2.gridPos; } } // Destroy items that need to be destroyed. if (destroyItem1) { if (_item1.inInventory) _item1.currentSlot.removeItem(); if (_item1.insideItem) _item1.removeFromOtherItem(); _item1.die(); } if (destroyItem2) { if (_item2.inInventory) _item2.currentSlot.removeItem(); if (_item2.insideItem) _item2.removeFromOtherItem(); _item2.die(); } // Now finally spawn the item. if (spawnInInventory) { itemToSpawn.init(); itemToSpawn.addToInventory(); } else if (spawnInItem1Container) { itemToSpawn.init(); itemToSpawn.addToOtherItem(item1Container); } else if (spawnInItem2Container) { itemToSpawn.addToOtherItem(item2Container); } else if (spawnAtLocation) { _currentRoom.addPiece(itemToSpawn, locationToSpawn); } else { _currentRoom.addPiece(itemToSpawn); } // Play a sound PlayState.instance.playAudio(PlayState.instance.pickupClip); }
protected void addAuxiliaryRelationship(string name1, string name2) { IRelationship relToAdd = null; DBItem dbitem1 = Database.Instance.getItem(name1); DBItem dbitem2 = Database.Instance.getItem(name2); // Figure out if one of the items can be inserted in the other if (dbitem1.propertyExists("container") && (bool)dbitem1.getProperty("container") && dbitem1.propertyExists("filledby") && (dbitem1.getProperty("filledby") as List<string>).Contains(name2)) { relToAdd = new InsertionRelationship(name2, 0, name1, 0); } if (relToAdd == null) { if (dbitem2.propertyExists("container") && (bool)dbitem2.getProperty("container") && dbitem2.propertyExists("filledby") && (dbitem2.getProperty("filledby") as List<string>).Contains(name1)) { relToAdd = new InsertionRelationship(name1, 0, name2, 0); } } // Next, try combine relationships if (relToAdd == null) { if (dbitem1.propertyExists("makes")) { List<KeyValuePair<string, string>> makes = new List<KeyValuePair<string, string>>((List<KeyValuePair<string, string>>)dbitem1.getProperty("makes")); Globals.shuffle(makes); foreach (KeyValuePair<string, string> recipe in makes) { if (recipe.Key == name2) { if (!Database.Instance.itemExists(recipe.Value)) continue; DBItem dbResult = Database.Instance.getItem(recipe.Value); if (dbResult.Spawned) continue; PuzzleItem resultItem = dbResult.spawnItem(); relToAdd = new CombineRelationship(name1, 0, name2, 0, resultItem); break; } } } } if (relToAdd == null) { if (dbitem2.propertyExists("makes")) { List<KeyValuePair<string, string>> makes = new List<KeyValuePair<string, string>>((List<KeyValuePair<string, string>>)dbitem2.getProperty("makes")); Globals.shuffle(makes); foreach (KeyValuePair<string, string> recipe in makes) { if (recipe.Key == name1) { if (!Database.Instance.itemExists(recipe.Value)) continue; DBItem dbResult = Database.Instance.getItem(recipe.Value); if (dbResult.Spawned) continue; PuzzleItem resultItem = dbResult.spawnItem(); relToAdd = new CombineRelationship(name1, 0, name2, 0, resultItem); break; } } } } // Finally try property change relationships if (relToAdd == null) { if (dbitem1.propertyExists("changes")) { List<KeyValuePair<string, string>> changes = new List<KeyValuePair<string, string>>(); Dictionary<string, List<string>> changesDict = (Dictionary<string, List<string>>)dbitem1.getProperty("changes"); foreach (string changeProp in changesDict.Keys) { foreach (string changeVal in changesDict[changeProp]) { changes.Add(new KeyValuePair<string, string>(changeProp, changeVal)); } } Globals.shuffle(changes); foreach (KeyValuePair<string, string> change in changes) { // Check if the property is mutable and allows our value if (!dbitem2.propertyExists("mutables") || !((List<string>)dbitem2.getProperty("mutables")).Contains(change.Key)) continue; if (!dbitem2.propertyExists(change.Key) || !((List<string>)dbitem2.getProperty(change.Key)).Contains(change.Value)) continue; relToAdd = new PropertyChangeRelationship(name2, 0, name1, 0, change.Key, change.Value); break; } } } if (relToAdd == null) { if (dbitem2.propertyExists("changes")) { List<KeyValuePair<string, string>> changes = new List<KeyValuePair<string, string>>(); Dictionary<string, List<string>> changesDict = (Dictionary<string, List<string>>)dbitem2.getProperty("changes"); foreach (string changeProp in changesDict.Keys) { foreach (string changeVal in changesDict[changeProp]) { changes.Add(new KeyValuePair<string, string>(changeProp, changeVal)); } } Globals.shuffle(changes); foreach (KeyValuePair<string, string> change in changes) { // Check if the property is mutable and allows our value if (!dbitem1.propertyExists("mutables") || !((List<string>)dbitem1.getProperty("mutables")).Contains(change.Key)) continue; if (!dbitem1.propertyExists(change.Key) || !((List<string>)dbitem1.getProperty(change.Key)).Contains(change.Value)) continue; relToAdd = new PropertyChangeRelationship(name1, 0, name2, 0, change.Key, change.Value); break; } } } if (relToAdd != null) { if (!_relationshipMap.ContainsKey(name1)) _relationshipMap[name1] = new Dictionary<string, IRelationship>(); _relationshipMap[name1][name2] = relToAdd; if (!_relationshipMap.ContainsKey(name2)) _relationshipMap[name2] = new Dictionary<string, IRelationship>(); _relationshipMap[name2][name1] = relToAdd; } }