public DungeonFloor(DungeonGenerator dungeonGenerator, DungeonFloorData floorData, bool isLastFloor, DungeonFloor prevFloor) { _pos = new Position(0, 0); _startPos = new Position(0, 0); _startDirection = Direction.Down; this.Width = 1; this.Height = 1; this.MazeGenerator = new MazeGenerator(); this.Sections = new List<DungeonFloorSection>(); _dungeonGenerator = dungeonGenerator; _branchProbability = floorData.Branch; _coverageFactor = floorData.Coverage; this.IsLastFloor = isLastFloor; _prevFloor = prevFloor; this.HasBossRoom = floorData.HasBoss; this.Statue = floorData.Statue; this.CalculateSize(floorData); this.InitRoomtraits(); this.GenerateMaze(floorData); this.GenerateRooms(floorData); this.InitSections(floorData); }
public DungeonFloor(DungeonGenerator dungeonGenerator, DungeonFloorData floorData, bool isLastFloor, DungeonFloor prevFloor) { _pos = new Position(0, 0); _startPos = new Position(0, 0); _startDirection = Direction.Down; this.Width = 1; this.Height = 1; this.MazeGenerator = new MazeGenerator(); this.Sections = new List <DungeonFloorSection>(); _dungeonGenerator = dungeonGenerator; _branchProbability = floorData.Branch; _coverageFactor = floorData.Coverage; this.IsLastFloor = isLastFloor; _prevFloor = prevFloor; this.HasBossRoom = floorData.HasBoss; this.Statue = floorData.Statue; this.CalculateSize(floorData); this.InitRoomtraits(); this.GenerateMaze(floorData); this.GenerateRooms(floorData); this.InitSections(floorData); }
private string ChooseRandomEnemy(DungeonFloorData floorData) { DungeonData dungeonData = Game.instance.currentDungeonData; DungeonEnemyData enemyData = floorData.enemyData; // About 20% of enemies are from previous levels when possible bool useBackfill = Random.Range(0, 100) < 20; if (useBackfill && dungeonData.backfillEnemies != null && dungeonData.backfillEnemies.Count > 0) { return(dungeonData.backfillEnemies.Sample().name); } int randomNum = Random.Range(0, 100); if (randomNum < 50) { return(enemyData.commonEnemy.name); } else if (randomNum < 75) { return(enemyData.uncommonEnemy.name); } else if (randomNum < 95) { return(enemyData.rareEnemy.name); } else { return(enemyData.scaryEnemy.name); } }
public Puzzle NewPuzzle(Dungeon dungeon, DungeonFloorData floorData, DungeonPuzzleData puzzleData, PuzzleScript puzzleScript) { var puzzle = new Puzzle(dungeon, this, floorData, puzzleData, puzzleScript); this.Puzzles.Add(puzzle); return(puzzle); }
private void CalculateSize(DungeonFloorData floorData) { var width = floorData.Width; var height = floorData.Height; if (floorData.Width < 6) { width = 6; } else if (floorData.Width > 18) { width = 18; } if (floorData.Height < 6) { height = 6; } else if (floorData.Height > 18) { height = 18; } var rndNum = _dungeonGenerator.RngMaze.GetUInt32(); this.Width = (int)(width - rndNum % (int)(width / 5.0)); rndNum = _dungeonGenerator.RngMaze.GetUInt32(); this.Height = (int)(height - rndNum % (int)(height / 5.0)); }
private void PlaceEnemies() { List <Vector2Int> walkablePositions = mCollisionMap.EmptyPositions(); // Remove any walkable positions too close to the avatar walkablePositions.RemoveAll((pos) => VectorHelper.OrthogonalDistance(pos, mAvatarStartPosition) < 10); DungeonFloorData data = CurrentDungeonFloorData(); int numEnemies = Random.Range(data.enemyData.minEnemies, data.enemyData.maxEnemies); numEnemies = WantedQuirk.ApplyQuirkIfPresent(numEnemies); for (int i = 0; i < numEnemies; ++i) { if (walkablePositions.Count == 0) { return; } Vector2Int pos2 = walkablePositions[Random.Range(0, walkablePositions.Count)]; walkablePositions.Remove(pos2); PlaceEnemy(data, pos2); } }
private RandomDungeonGenerationData DungeonGenerationData() { DungeonFloorData floorData = CurrentDungeonFloorData(); RandomDungeonGenerationData data = floorData.generationData; data.randomSeed = Random.Range(int.MinValue, int.MaxValue); return(data); }
protected override IEnumerator ActivationCoroutine() { yield return(base.ActivationCoroutine()); if (WasAccepted()) { NumberPopupGenerator.instance.GeneratePopup(gameObject, "Strength Increased", NumberPopupReason.Good); CharacterStatModifier modifier = Game.instance.playerStats.gameObject.AddComponent <CharacterStatModifier>(); modifier.SetRelativeModification(CharacterStatType.Strength, 2); CollisionMap collisionMap = GameObject.FindObjectOfType <CollisionMap>(); // Spawn a bunch of enemies and give the player more strength. List <Vector2Int> walkablePositions = new List <Vector2Int>(); Vector2Int playerPosition = MapCoordinateHelper.WorldToMapCoords(Game.instance.avatar.transform.position); for (int xOffset = -4; xOffset <= 4; ++xOffset) { for (int yOffset = -4; yOffset <= 4; ++yOffset) { if (Mathf.Abs(xOffset) < 2 || Mathf.Abs(yOffset) < 2) { continue; } int x = playerPosition.x + xOffset; int y = playerPosition.y + yOffset; if (collisionMap.SpaceMarking(x, y) == 0) { walkablePositions.Add(new Vector2Int(x, y)); } } } DungeonFloorData data = CurrentDungeonFloorData(); int numEnemies = 5; for (int i = 0; i < numEnemies; ++i) { if (walkablePositions.Count == 0) { continue; } string enemy = data.enemyData.rareEnemy.name; GameObject newEnemy = GameObject.Instantiate(PrefabManager.instance.PrefabByName(enemy)); Vector2Int pos2 = walkablePositions[Random.Range(0, walkablePositions.Count)]; walkablePositions.Remove(pos2); Vector3 pos = MapCoordinateHelper.MapToWorldCoords(pos2); newEnemy.transform.position = pos; collisionMap.MarkSpace(pos2.x, pos2.y, newEnemy.GetComponent <SimpleMovement>().uniqueCollisionIdentity); } } yield break; }
private void PlaceEnemy(DungeonFloorData data, Vector2Int pos2) { string enemy = ChooseEnemy(data); GameObject newEnemy = GameObject.Instantiate(PrefabManager.instance.PrefabByName(enemy), transform); Vector3 pos = MapCoordinateHelper.MapToWorldCoords(pos2); newEnemy.transform.position = pos; mCollisionMap.MarkSpace(pos2.x, pos2.y, newEnemy.GetComponent <SimpleMovement>().uniqueCollisionIdentity); }
private string ChooseEnemy(DungeonFloorData floorData) { if (IsPresetRoom()) { return(ChoosePresetEnemy(floorData)); } else { return(ChooseRandomEnemy(floorData)); } }
private void GenerateRooms(DungeonFloorData floorData) { if (this.HasBossRoom) { var endPos = this.MazeGenerator.EndPos; var preEndRoom = this.GetRoom(endPos.GetBiasedPosition(Direction.Down)); preEndRoom.RoomType = RoomType.Room; preEndRoom.SetDoorType(Direction.Up, (int)DungeonBlockType.BossDoor); } }
/// <summary> /// Split this floor into sections. /// </summary> /// <param name="floorData"></param> private void InitSections(DungeonFloorData floorData) { var criticalPathLength = this.MazeGenerator.CriticalPath.Count - 1; var criticalPathLeft = criticalPathLength; var sectionCount = floorData.Sections.Count; if (sectionCount == 0) return; var sectionStart = 0; var puzzleCountSum = 0f; var weightsList = CalculateWeights(); //Log.Debug("Floor weightsList: " + string.Join(",", weightsList)); var pathWeight = weightsList.Sum(); floorData.Sections.ForEach(x => puzzleCountSum += x.Max); for (var i = 0; i < sectionCount; ++i) { List<MazeMove> sectionPath; var haveBossDoor = false; var sectionLength = (int)Math.Round(floorData.Sections[i].Max / puzzleCountSum * pathWeight); var sectionEnd = sectionStart; var currentWeight = 0; for (; sectionEnd < weightsList.Length; ++sectionEnd) { currentWeight += weightsList[sectionEnd]; if (currentWeight >= sectionLength) break; } if (currentWeight > sectionLength) { if (currentWeight - weightsList[sectionEnd] >= (int)Math.Round(floorData.Sections[i].Max / puzzleCountSum * criticalPathLeft)) { currentWeight -= weightsList[sectionEnd]; --sectionEnd; } } pathWeight -= currentWeight; criticalPathLeft -= sectionEnd - sectionStart + 1; puzzleCountSum -= floorData.Sections[i].Max; // if last section if (i == sectionCount - 1) { sectionPath = this.MazeGenerator.CriticalPath.GetRange(sectionStart + 1, criticalPathLength - sectionStart); haveBossDoor = this.HasBossRoom; } else sectionPath = this.MazeGenerator.CriticalPath.GetRange(sectionStart + 1, sectionEnd - sectionStart + 1); this.Sections.Add(new DungeonFloorSection(this.GetRoom(sectionPath[0].PosFrom), sectionPath, haveBossDoor, this._dungeonGenerator.RngPuzzles)); var weightsListSegment = (i == sectionCount - 1 ? new ArraySegment<int>(weightsList, sectionStart, criticalPathLength - sectionStart) : new ArraySegment<int>(weightsList, sectionStart, sectionEnd - sectionStart + 1)); //Log.Debug("section weightsList: " + string.Join(",", weightsListSegment)); //Log.Debug(string.Format("section {0}: max puzzles: {1}, wanted length: {2}, length: {3}", i, floorData.Sections[i].Max, sectionLength, weightsListSegment.Sum())); sectionStart = sectionEnd + 1; } }
private string ChoosePresetEnemy(DungeonFloorData floorData) { ++mPresetEnemyCounter; if (mPresetEnemyCounter == 1) { return(floorData.enemyData.commonEnemy.name); } else if (mPresetEnemyCounter == 2) { return(floorData.enemyData.uncommonEnemy.name); } else { return(floorData.enemyData.rareEnemy.name); } }
private void GenerateMaze(DungeonFloorData floorDesc) { var critPathMin = Math.Max(1, floorDesc.CritPathMin); var critPathMax = Math.Max(1, floorDesc.CritPathMax); if (critPathMin > critPathMax) { var temp = critPathMax; critPathMax = critPathMin; critPathMin = temp; } this.CreateCriticalPath(critPathMin, critPathMax); this.CreateSubPath(_coverageFactor, _branchProbability); this.SetRoomTypes(); }
/// <summary> /// Creates new puzzle. /// </summary> /// <param name="dungeon"></param> /// <param name="section"></param> /// <param name="floorData"></param> /// <param name="puzzleData"></param> /// <param name="puzzleScript"></param> public Puzzle(Dungeon dungeon, DungeonFloorSection section, DungeonFloorData floorData, DungeonPuzzleData puzzleData, PuzzleScript puzzleScript) { _variables = new Dictionary<string, Object>(); _monsterGroups = new Dictionary<string, MonsterGroup>(); _monsterGroupData = new Dictionary<string, DungeonMonsterGroupData>(); this.Props = new Dictionary<string, Prop>(); this.Keys = new Dictionary<string, Item>(); _section = section; this.Name = puzzleScript.Name; this.Data = puzzleData; this.Dungeon = dungeon; this.Script = puzzleScript; this.FloorData = floorData; for (int i = 1; i <= puzzleData.Groups.Count; ++i) _monsterGroupData["Mob" + i] = puzzleData.Groups[i - 1].Copy(); }
/// <summary> /// Creates new puzzle. /// </summary> /// <param name="dungeon"></param> /// <param name="section"></param> /// <param name="floorData"></param> /// <param name="puzzleData"></param> /// <param name="puzzleScript"></param> public Puzzle(Dungeon dungeon, DungeonFloorSection section, DungeonFloorData floorData, DungeonPuzzleData puzzleData, PuzzleScript puzzleScript) { _variables = new Dictionary <string, Object>(); _monsterGroups = new Dictionary <string, MonsterGroup>(); _monsterGroupData = new Dictionary <string, DungeonMonsterGroupData>(); this.Props = new Dictionary <string, Prop>(); this.Keys = new Dictionary <string, Item>(); _section = section; this.Name = puzzleScript.Name; this.Data = puzzleData; this.Dungeon = dungeon; this.Script = puzzleScript; this.FloorData = floorData; for (int i = 1; i <= puzzleData.Groups.Count; ++i) { _monsterGroupData["Mob" + i] = puzzleData.Groups[i - 1].Copy(); } }
void SaveData() { heroData.heroWrapper.HeroData.HeroInfo.CurrentDungeonFloor = playerCurrentFloor; heroData.heroWrapper.HeroData.HeroInfo.CurrentXPosition = playerDungeonPosition.x; heroData.heroWrapper.HeroData.HeroInfo.CurrentYPosition = playerDungeonPosition.y; Dictionary <int, DungeonFloorData> dungeonData = new Dictionary <int, DungeonFloorData>(); foreach (var floor in BSP_MapDictionary) { DungeonFloorData floorData = new DungeonFloorData(); floorData.map = floor.Value.GetMap; floorData.upStairsPos = floor.Value.GetMapUpStairs; floorData.downStairsPos = floor.Value.GetMapDownStairs; floorData.cacheList = floor.Value.GetCacheList; dungeonData.Add(floor.Key, floorData); } SaveSystem.FullSave(heroData.heroWrapper.HeroData.HeroInfo, dungeonData); }
private void CalculateSize(DungeonFloorData floorData) { var width = floorData.Width; var height = floorData.Height; if (floorData.Width < 6) width = 6; else if (floorData.Width > 18) width = 18; if (floorData.Height < 6) height = 6; else if (floorData.Height > 18) height = 18; var rndNum = _dungeonGenerator.RngMaze.GetUInt32(); this.Width = (int)(width - rndNum % (int)(width / 5.0)); rndNum = _dungeonGenerator.RngMaze.GetUInt32(); this.Height = (int)(height - rndNum % (int)(height / 5.0)); }
/// <summary> /// Split this floor into sections. /// </summary> /// <param name="floorData"></param> private void InitSections(DungeonFloorData floorData) { var criticalPathLength = this.MazeGenerator.CriticalPath.Count - 1; var criticalPathLeft = criticalPathLength; var sectionCount = floorData.Sections.Count; if (sectionCount == 0) { return; } var sectionStart = 0; var puzzleCountSum = 0f; var weightsList = CalculateWeights(); //Log.Debug("Floor weightsList: " + string.Join(",", weightsList)); var pathWeight = weightsList.Sum(); floorData.Sections.ForEach(x => puzzleCountSum += x.Max); for (var i = 0; i < sectionCount; ++i) { List <MazeMove> sectionPath; var haveBossDoor = false; var sectionLength = (int)Math.Round(floorData.Sections[i].Max / puzzleCountSum * pathWeight); var sectionEnd = sectionStart; var currentWeight = 0; for (; sectionEnd < weightsList.Length; ++sectionEnd) { currentWeight += weightsList[sectionEnd]; if (currentWeight >= sectionLength) { break; } } if (currentWeight > sectionLength) { if (currentWeight - weightsList[sectionEnd] >= (int)Math.Round(floorData.Sections[i].Max / puzzleCountSum * criticalPathLeft)) { currentWeight -= weightsList[sectionEnd]; --sectionEnd; } } pathWeight -= currentWeight; criticalPathLeft -= sectionEnd - sectionStart + 1; puzzleCountSum -= floorData.Sections[i].Max; // if last section if (i == sectionCount - 1) { sectionPath = this.MazeGenerator.CriticalPath.GetRange(sectionStart + 1, criticalPathLength - sectionStart); haveBossDoor = this.HasBossRoom; } else { sectionPath = this.MazeGenerator.CriticalPath.GetRange(sectionStart + 1, sectionEnd - sectionStart + 1); } this.Sections.Add(new DungeonFloorSection(this.GetRoom(sectionPath[0].PosFrom), sectionPath, haveBossDoor, this._dungeonGenerator.RngPuzzles)); var weightsListSegment = (i == sectionCount - 1 ? new ArraySegment <int>(weightsList, sectionStart, criticalPathLength - sectionStart) : new ArraySegment <int>(weightsList, sectionStart, sectionEnd - sectionStart + 1)); //Log.Debug("section weightsList: " + string.Join(",", weightsListSegment)); //Log.Debug(string.Format("section {0}: max puzzles: {1}, wanted length: {2}, length: {3}", i, floorData.Sections[i].Max, sectionLength, weightsListSegment.Sum())); sectionStart = sectionEnd + 1; } }
public Puzzle NewPuzzle(Dungeon dungeon, DungeonFloorData floorData, DungeonPuzzleData puzzleData, PuzzleScript puzzleScript) { var puzzle = new Puzzle(dungeon, this, floorData, puzzleData, puzzleScript); this.Puzzles.Add(puzzle); return puzzle; }