/** * Creates a new MZCondition that requires this MZCondition to be satisfied and * requires another {@link MZSymbol} to be obtained as well. * * @param sym the Added symbol the player must have for the new MZCondition * to be satisfied * @return the new MZCondition */ public MZCondition And(MZSymbol sym) { MZCondition result = new MZCondition(this); result.Add(sym); return(result); }
/** * Places keys within the dungeon in such a way that the dungeon is * guaranteed to be solvable. * * @param levels the keyLevel -> room-set mapping to use * @ if it fails * @see KeyLevelRoomMapping */ protected void PlaceKeys(KeyLevelRoomMapping levels) { // Now place the keys. For every key-level but the last one, place a // key for the next level in it, preferring rooms with fewest links // (dead end rooms). for (int key = 0; key < levels.KeyCount() - 1; ++key) { List <MZRoom> rooms = levels.GetRooms(key); Shuffle(rooms); // Collections.sort is stable: it doesn't reorder "equal" elements, // which means the shuffling we just did is still useful. rooms.Sort(new MZRoomIntensityComparer()); // Alternatively, use the EDGE_COUNT_COMPARATOR to put keys at // 'dead end' rooms. MZSymbol keySym = new MZSymbol(key); bool placedKey = false; foreach (MZRoom room in rooms) { if (room.GetItem() == null && constraints.RoomCanFitItem(room.id, keySym)) { room.SetItem(keySym); placedKey = true; break; } } if (!placedKey) { // there were no rooms into which the key would fit throw new RetryException(); } } }
/** * Fill the dungeon's space with rooms and doors (some locked). * Keys are not inserted at this point. * * @param levels the keyLevel -> room-set mapping to update * @ if it fails * @see KeyLevelRoomMapping */ protected void PlaceRooms(KeyLevelRoomMapping levels, int roomsPerLock) { // keyLevel: the number of keys required to Get to the new room int keyLevel = 0; MZSymbol latestKey = null; // condition that must hold true for the player to reach the new room // (the set of keys they must have). MZCondition cond = new MZCondition(); // Loop to place rooms and link them while (dungeon.RoomCount() < constraints.GetMaxRooms()) { bool doLock = false; // Decide whether we need to place a new lock // (Don't place the last lock, since that's reserved for the boss) if (ShouldAddNewLock(keyLevel, levels.GetRooms(keyLevel).Count, roomsPerLock)) { latestKey = new MZSymbol(keyLevel++); cond = cond.And(latestKey); doLock = true; } // Find an existing room with a free edge: MZRoom parentRoom = null; if (!doLock && UnityEngine.Random.Range(0, 10) > 0) { parentRoom = ChooseRoomWithFreeEdge(levels.GetRooms(keyLevel), keyLevel); } if (parentRoom == null) { parentRoom = ChooseRoomWithFreeEdge(new List <MZRoom>(dungeon.GetRooms()), keyLevel); doLock = true; } if (parentRoom == null) { throw new OutOfRoomsException(); } // Decide which direction to put the new room in relative to the // parent int nextId = ChooseFreeEdge(parentRoom, keyLevel); List <Vector2Int> coords = constraints.GetCoords(nextId); MZRoom room = new MZRoom(nextId, coords, parentRoom, null, cond); // Add the room to the dungeon dungeon.Add(room); parentRoom.AddChild(room); dungeon.Link(parentRoom, room, doLock ? latestKey : null); levels.AddRoom(keyLevel, room); } }
public static bool Equals(MZSymbol a, MZSymbol b) { if (a == b) { return(true); } if (b == null) { return(a.Equals(b)); } return(b.Equals(a)); }
private void Add(MZSymbol sym) { if (sym.GetValue() == (int)MZSymbol.MZSymbolValue.SwitchOff) { switchState = SwitchState.Off; } else if (sym.GetValue() == (int)MZSymbol.MZSymbolValue.SwitchOn) { switchState = SwitchState.On; } else { keyLevel = Math.Max(keyLevel, sym.GetValue() + 1); } }
public MZEdge SetEdge(int targetRoomId, MZSymbol symbol) { MZEdge e = GetEdge(targetRoomId); if (e != null) { e.Symbol = symbol; } else { e = new MZEdge(targetRoomId, symbol); edges.Add(e); } return(e); }
public override string ToString() { String result = ""; if (keyLevel != 0) { result += new MZSymbol(keyLevel - 1).ToString(); } if (switchState != SwitchState.Either) { if (!result.Equals("")) { result += ","; } result += switchState.ToSymbol().ToString(); } return(result); }
/** * Creates a MZCondition that requires the player to have a particular * {@link MZSymbol}. * * @param e the symbol that the player must have for the MZCondition to be * satisfied */ public MZCondition(MZSymbol e) { if (e.GetValue() == (int)MZSymbol.MZSymbolValue.SwitchOff) { keyLevel = 0; switchState = SwitchState.Off; } else if (e.GetValue() == (int)MZSymbol.MZSymbolValue.SwitchOn) { keyLevel = 0; switchState = SwitchState.On; } else { keyLevel = e.GetValue() + 1; switchState = SwitchState.Either; } }
/** * Creates a MZRoom at the given coordinates, with the given parent, * containing a specific item, and having a certain pre-{@link MZCondition}. * <p> * The parent of a room is the parent node of this MZRoom in the initial * tree of the dungeon during * {@link generators.MZDungeonGenerator#Generate()}, and * before * {@link generators.MZDungeonGenerator#Graphify()}. * * @param coords the coordinates of the new room * @param parent the parent room or null if it is the root / entry room * @param item the symbol to place in the room or null if no item * @param precond the precondition of the room * @see MZCondition */ public MZRoom(int id, List <Vector2Int> coords, MZRoom parent, MZSymbol item, MZCondition precond) { this.id = id; this.coords = coords; this.item = item; this.edges = new List <MZEdge>(); this.precond = precond; this.intensity = 0.0; this.parent = parent; this.children = new List <MZRoom>(3); // all edges initially null int x = 0, y = 0; foreach (Vector2Int xy in coords) { x += xy.x; y += xy.y; } center = new Vector2Int(x / coords.Count, y / coords.Count); }
/** * Creates an MZEdge that requires a particular MZSymbol to be collected before * it may be used by the player to travel between the MZRooms. * * @param symbol the symbol that must be obtained */ public MZEdge(int targetRoomId, MZSymbol symbol) { this.targetRoomId = targetRoomId; this.Symbol = symbol; }
/** * Randomly links up some adjacent rooms to make the dungeon graph less of * a tree. * * @ if it fails */ protected void Graphify() { foreach (MZRoom room in dungeon.GetRooms()) { if (room.IsGoal() || room.IsBoss()) { continue; } foreach (KeyValuePair <Double, int> next in // Doesn't matter what the keyLevel is; later checks about // preconds ensure linkage doesn't trivialize the puzzle. constraints.GetAdjacentRooms(room.id, Int32.MaxValue)) { int nextId = next.Value; if (room.GetEdge(nextId) != null) { continue; } MZRoom nextRoom = dungeon.Get(nextId); if (nextRoom == null || nextRoom.IsGoal() || nextRoom.IsBoss()) { continue; } if (room.GetCoords()[0].x == -1 && room.GetCoords()[0].y == -3) { //Debug.Log(nextRoom.GetCoords()[0].x + " " + nextRoom.GetCoords()[0].y); Debug.Log(room.GetEdges().Count); } bool forwardImplies = room.GetPrecond().Implies(nextRoom.GetPrecond()), backwardImplies = nextRoom.GetPrecond().Implies(room.GetPrecond()); if (forwardImplies && backwardImplies) { // both rooms are at the same keyLevel. if (UnityEngine.Random.value >= constraints.EdgeGraphifyProbability(room.id, nextRoom.id)) { continue; } dungeon.Link(room, nextRoom); } else { MZSymbol difference = room.GetPrecond().SingleSymbolDifference( nextRoom.GetPrecond()); if (difference == null || (!difference.IsSwitchState() && UnityEngine.Random.value >= constraints.EdgeGraphifyProbability(room.id, nextRoom.id))) { continue; } dungeon.Link(room, nextRoom, difference); } } } }
/** * Makes some {@link MZEdge}s within the dungeon require the dungeon's switch * to be in a particular state, and places the switch in a room in the * dungeon. * * @ if it fails */ protected void PlaceSwitches() { // Possible TODO: have multiple switches on separate circuits // At the moment, we only have one switch per dungeon. if (constraints.GetMaxSwitches() <= 0) { return; } List <MZRoom> solution = GetSolutionPath(); for (int attempt = 0; attempt < 10; ++attempt) { List <MZRoom> rooms = new List <MZRoom>(dungeon.GetRooms()); Shuffle(rooms); Shuffle(solution); // Pick a base room from the solution path so that the player // will have to encounter a switch-lock to solve the dungeon. MZRoom baseRoom = null; foreach (MZRoom room in solution) { if (room.GetChildren().Count > 1 && room.GetParent() != null) { baseRoom = room; break; } } if (baseRoom == null) { throw new RetryException(); } MZCondition baseRoomCond = baseRoom.GetPrecond(); RemoveDescendantsFromList(rooms, baseRoom); MZSymbol switchSym = new MZSymbol((int)MZSymbol.MZSymbolValue.Switch); MZRoom switchRoom = null; foreach (MZRoom room in rooms) { if (room.GetItem() == null && baseRoomCond.Implies(room.GetPrecond()) && constraints.RoomCanFitItem(room.id, switchSym)) { switchRoom = room; break; } } if (switchRoom == null) { continue; } if (SwitchLockChildRooms(baseRoom, MZCondition.SwitchState.Either)) { switchRoom.SetItem(switchSym); return; } } throw new RetryException(); }
/** * Places the BOSS and GOAL rooms within the dungeon, in existing rooms. * These rooms are moved into the next keyLevel. * * @param levels the keyLevel -> room-set mapping to update * @ if it fails * @see KeyLevelRoomMapping */ protected void PlaceBossGoalRooms(KeyLevelRoomMapping levels) { List <MZRoom> possibleGoalRooms = new List <MZRoom>(dungeon.RoomCount()); MZSymbol goalSym = new MZSymbol((int)MZSymbol.MZSymbolValue.Goal), bossSym = new MZSymbol((int)MZSymbol.MZSymbolValue.Boss); foreach (MZRoom room in dungeon.GetRooms()) { if (room.GetChildren().Count > 0 || room.GetItem() != null) { continue; } MZRoom parent = room.GetParent(); if (parent == null) { continue; } if (IsGenerateGoal() && (parent.GetChildren().Count != 1 || !parent.GetPrecond().Implies(room.GetPrecond()))) { continue; } if (IsGenerateGoal()) { if (!constraints.RoomCanFitItem(room.id, goalSym) || !constraints.RoomCanFitItem(parent.id, bossSym)) { continue; } } else { if (!constraints.RoomCanFitItem(room.id, bossSym)) { continue; } } possibleGoalRooms.Add(room); } if (possibleGoalRooms.Count == 0) { throw new RetryException(); } MZRoom goalRoom = possibleGoalRooms[UnityEngine.Random.Range(0, possibleGoalRooms.Count)], bossRoom = goalRoom.GetParent(); if (!IsGenerateGoal()) { bossRoom = goalRoom; goalRoom = null; } if (goalRoom != null) { goalRoom.SetItem(goalSym); } bossRoom.SetItem(bossSym); int oldKeyLevel = bossRoom.GetPrecond().GetKeyLevel(), newKeyLevel = Math.Min(levels.KeyCount(), constraints.GetMaxKeys()); if (oldKeyLevel != newKeyLevel) { List <MZRoom> oklRooms = levels.GetRooms(oldKeyLevel); if (goalRoom != null) { oklRooms.Remove(goalRoom); } oklRooms.Remove(bossRoom); if (goalRoom != null) { levels.AddRoom(newKeyLevel, goalRoom); } levels.AddRoom(newKeyLevel, bossRoom); MZSymbol bossKey = new MZSymbol(newKeyLevel - 1); MZCondition precond = bossRoom.GetPrecond().And(bossKey); bossRoom.SetPrecond(precond); if (goalRoom != null) { goalRoom.SetPrecond(precond); } if (newKeyLevel == 0) { dungeon.Link(bossRoom.GetParent(), bossRoom); } else { dungeon.Link(bossRoom.GetParent(), bossRoom, bossKey); } if (goalRoom != null) { dungeon.Link(bossRoom, goalRoom); } } }
public void Link(MZRoom room1, MZRoom room2, MZSymbol cond) { LinkOneWay(room1, room2, cond); LinkOneWay(room2, room1, cond); }
public void LinkOneWay(MZRoom room1, MZRoom room2, MZSymbol cond) { room1.SetEdge(room2.id, cond); }
public bool RoomCanFitItem(int id, MZSymbol key) { return(true); }
/* * private int keyLevel; * * public AStarClient(int keyLevel) * { * this.keyLevel = keyLevel; * } * * public override List<int> GetNeighbors(int roomId) * { * List<int> ids = new List<int>(); * foreach (MZEdge edge in dungeon.Get(roomId).GetEdges()) * { * if (!edge.HasSymbol() || edge.GetSymbol().GetValue() < keyLevel) * { * ids.Add(edge.GetTargetRoomId()); * } * } * return ids; * } * public override Vector2Int GetVector2Int(int roomId) * { * return dungeon.Get(roomId).GetCenter(); * } * } */ /* * private List<int> AStar(int start, int goal, int keyLevel) * { * AStar<int> astar = new AStar<int>(); * return astar.Solve(); * } */ /** * Nonlinearity is measured as the number of rooms the player would have to * pass through multiple times to Get to the goal room (collecting keys and * unlocking doors along the way). * * Uses A* to find a path from the entry to the first key, from each key to * the next key and from the last key to the goal. * * @return The number of rooms passed through multiple times **/ public int MeasureNonlinearity() { List <MZRoom> keyRooms = new List <MZRoom>(constraints.GetMaxKeys()); for (int i = 0; i < constraints.GetMaxKeys(); ++i) { keyRooms.Add(null); } foreach (MZRoom room in dungeon.GetRooms()) { if (room.GetItem() == null) { continue; } MZSymbol item = room.GetItem(); if (item.GetValue() >= 0 && item.GetValue() < keyRooms.Count) { keyRooms.Insert(item.GetValue(), room); } } //for N >= 0: keyRooms[N] = location of key N MZRoom current = dungeon.FindStart(), goal = dungeon.FindGoal(); //clients may disable generation of the goal room if redundant, in which case the equivalent 'ending' room becomes the boss room if (goal == null) { goal = dungeon.FindBoss(); } Debug.Assert(current != null && goal != null); int nextKey = 0, nonlinearity = 0; List <int> visitedRooms = new List <int>(); while (current != goal) { MZRoom intermediateGoal; //Debug.Log("Current room ID: " + current.id); //Debug.Log("Max Keys:" + constraints.GetMaxKeys()); //Debug.Log("Next Key: " + nextKey); if (nextKey == constraints.GetMaxKeys()) { intermediateGoal = goal; } else { intermediateGoal = keyRooms[nextKey]; } //Debug.Log("Current goal ID: " + intermediateGoal.id); ///* //Debug.Log("Dungeon: " + dungeon.GetType()); //*/ //Debug.Log("A* running..."); List <int> steps = astar(current.id, intermediateGoal.id, nextKey, dungeon); //ちゃんとこれを作ってよ //Debug.Log("Reversing steps!"); steps.Reverse(); foreach (int id in steps) { Debug.Log("Visited Room: " + id); if (visitedRooms.Contains(id)) { ++nonlinearity; } } visitedRooms.AddRange(steps); nextKey++; current = dungeon.Get(steps[0]); MZRoom test = current; } return(nonlinearity); }
public void SetSymbol(MZSymbol symbol) { this.Symbol = symbol; }
/** * Determines whether this MZCondition implies that a particular * {@link MZSymbol} has been obtained. * * @param s the MZSymbol * @return whether the MZSymbol is implied by this MZCondition */ public bool Implies(MZSymbol s) { return(Implies(new MZCondition(s))); }
public MZRoom(int id, Vector2Int coords, MZRoom parent, MZSymbol item, MZCondition precond) : this(id, new List <Vector2Int> { coords }, parent, item, precond) { }
/** * @param item the item to place in the MZRoom */ public void SetItem(MZSymbol item) { this.item = item; }
public void CreateDungeon() { float roomRatio = 0.6875f; // 256x176 // Use CountConstraints to make a truly random map. /* * CountConstraints constraints = new CountConstraints((int)maxSpaces, (int)maxKeys, (int)maxSwitches); * Debug.Log("Constraints: " + maxSpaces + "," + maxKeys + "," + maxSwitches); */ // Use SpaceConstraints to make a map fitting to a shape. ///* MZSpaceMap spaceMap = new MZSpaceMap(); //random tile map //tileMap = tileMapObjects[Random.Range(0, tileMapObjects.Length)].GetComponentInChildren<Tilemap>(); tileMap = tileMapObjects[0].GetComponentInChildren <Tilemap>(); foreach (Vector3Int posWithZ in tileMap.cellBounds.allPositionsWithin.GetEnumerator()) { if (tileMap.HasTile(posWithZ)) { Vector2Int pos = new Vector2Int(posWithZ.x, posWithZ.y); spaceMap.Set(pos, true); } } SpaceConstraints constraints = new SpaceConstraints(spaceMap, (int)maxKeys, (int)maxSwitches); //*/ generator = new LinearDungeonGenerator(Random.Range(0, int.MaxValue), constraints, numberOfRooms, (int)maxGenerations); //generator = new DungeonGenerator(Random.Range(0, int.MaxValue), constraints, numberOfRooms); Debug.Log("Generate()"); generator.Generate(); dungeon = generator.GetMZDungeon(); foreach (MZRoom room in dungeon.GetRooms()) { MZSymbol item = room.GetItem(); GameObject toInstantiate = normalRoom; Color roomColor = new Color((float)room.GetIntensity(), 1.0f - (float)room.GetIntensity(), 0.5f - (float)room.GetIntensity() / 2); if (item != null) { switch (item.GetValue()) { case (int)MZSymbol.MZSymbolValue.Start: toInstantiate = entranceRoom; roomColor = Color.white; break; case (int)MZSymbol.MZSymbolValue.Boss: toInstantiate = bossRoom; break; case (int)MZSymbol.MZSymbolValue.Goal: toInstantiate = goalRoom; roomColor = Color.white; break; default: break; } if (item.GetValue() >= 0) { GameObject keyObjectInstance = Instantiate(key, new Vector3(room.GetCoords()[0].x, room.GetCoords()[0].y * roomRatio, 0), Quaternion.identity, transform); keyObjectInstance.GetComponent <SpriteRenderer>().color = keyColors[item.GetValue()]; keyObjectInstance.transform.localScale += new Vector3(2, 2, 2); instances.Add(keyObjectInstance); } else if (item.GetValue() == (int)MZSymbol.MZSymbolValue.Switch) { GameObject keyObjectInstance = Instantiate(roomSwitch, new Vector3(room.GetCoords()[0].x, room.GetCoords()[0].y * roomRatio, 0), Quaternion.identity, transform); keyObjectInstance.transform.localScale += new Vector3(2, 2, 2); instances.Add(keyObjectInstance); } } GameObject roomObject = Instantiate(toInstantiate, new Vector3(room.GetCoords()[0].x, room.GetCoords()[0].y * roomRatio, 0), Quaternion.identity, transform); roomObject.GetComponent <SpriteRenderer>().color = roomColor; instances.Add(roomObject); foreach (MZEdge edge in room.GetEdges()) { MZRoom targetRoom = dungeon.Get(edge.GetTargetRoomId()); Vector2Int edgeDir = targetRoom.GetCoords()[0] - room.GetCoords()[0]; toInstantiate = openDoor; GameObject keyObject = null; Color keyColor = Color.white; if (edge.GetSymbol() != null) { switch (edge.GetSymbol().GetValue()) { case (int)MZSymbol.MZSymbolValue.SwitchOn: toInstantiate = blockedDoor; keyObject = roomSwitchOn; break; case (int)MZSymbol.MZSymbolValue.SwitchOff: toInstantiate = blockedDoor; keyObject = roomSwitchOff; break; default: break; } if (edge.GetSymbol().GetValue() >= 0) { toInstantiate = lockedDoor; keyObject = key; keyColor = keyColors[edge.GetSymbol().GetValue()]; } } //this only works for identically sized rooms Vector3 pos = Vector3.zero; if (edgeDir == Vector2Int.right) { pos = new Vector3(room.GetCoords()[0].x + 0.5f, room.GetCoords()[0].y * roomRatio, 0); GameObject doorObject = Instantiate(toInstantiate, pos, Quaternion.identity, transform); instances.Add(doorObject); if (keyObject != null) { GameObject keyObjectInstance = Instantiate(keyObject, pos, Quaternion.identity, transform); instances.Add(keyObjectInstance); keyObjectInstance.GetComponent <SpriteRenderer>().color = keyColor; keyObjectInstance.transform.localScale += new Vector3(1, 1, 1); } } else if (edgeDir == Vector2Int.down) { pos = new Vector3(room.GetCoords()[0].x, room.GetCoords()[0].y * roomRatio - (roomRatio / 2), 0); Vector2Int relativePos = Vector2Int.zero + Vector2Int.up; float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg; Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward); //90 degrees GameObject doorObject = Instantiate(toInstantiate, pos, rotation, transform); instances.Add(doorObject); if (keyObject != null) { GameObject keyObjectInstance = Instantiate(keyObject, pos, Quaternion.identity, transform); instances.Add(keyObjectInstance); keyObjectInstance.GetComponent <SpriteRenderer>().color = keyColor; keyObjectInstance.transform.localScale += new Vector3(1, 1, 1); } } } } foreach (var room in dungeon.GetRooms()) { foreach (var edge in room.GetEdges()) { //wrap this up laterino } } }