public static string GetForestSpriteNameForTile(int x, int y, string[,] nsewMap, int[,] map, PseudoRandom prnd) { string spriteName = ""; if (map [y, x] == 1) { if (nsewMap [y, x].Contains("_")) { string num = prnd.Next (1,2).ToString(); spriteName = "Forest_TreeTopSide" + nsewMap [y, x] + "_" + num; } else { string num = prnd.Next (1,4).ToString(); spriteName = "Forest_TreeTop" + "_" + num; } } else { if(y+1 < map.GetLength (0) && map [y+1, x] == 1) { string num = prnd.Next (1,6).ToString(); spriteName = "Forest_TreeTrunk_" + num; } else { string num = prnd.Next (1,3).ToString(); spriteName = "Forest_Floor_" + num; } } return spriteName; }
//================================================================// // CreateNSEWMap //================================================================// // Creates a maze in the form of a 2D string array that is populated // with directions e.g. "NSEW". If a direction is in the string then // there is as wall on that side of the point. For example if the // point (3,4) is "SE" then there is a wall on the south and east // side of the point (3,4). //================================================================// public string[,] CreateNSEWMap(int width, int length, int divRate, int[] startPoint) { int maximumX = width; int maximumY = length; bool mirrorEdges = false; PseudoRandom rnd = new PseudoRandom(seedX, seedY, seedZ); string[,] cellMap = CreateMapShape(maximumX, maximumY, rnd); int[,] cellMapNo = new int[maximumY, maximumX]; List<Point> cellList = new List<Point>(); int[] start = startPoint; cellList.Add(new Point(start[0], start[1])); cellMap[cellList[0].Y, cellList[0].X] += "X"; cellMapNo[cellList[0].Y, cellList[0].X] = 0; while(cellList.Count != 0) { int cellNo = cellList.Count-1;; if(rnd.Next(0,99) < divRate) { cellNo = cellList.Count-1; } else { cellNo = rnd.Next(0, cellList.Count-1); } Point cell = cellList[cellNo]; List<int> list = CreateRandomOrderDirectionList(rnd); bool visitedAllNeighours = true; for(int c = 0; c < 4; c++) { int dir = list[c]; int newX = NewXFromDir(dir, cell.X, cell.Y,maximumX, cellMap, mirrorEdges); int newY = NewYFromDir(dir, cell.Y, cell.X,maximumY, cellMap, mirrorEdges); if(cellMap[newY,newX] == "NSEW") { visitedAllNeighours = false; char carve = CharToIntConversion(dir); cellMap[cell.Y, cell.X] = cellMap[cell.Y, cell.X].Replace(carve, ' '); cellMap[newY,newX] = cellMap[newY,newX].Replace(GetOppositeDirection(carve), ' '); cellMapNo[newY, newX] = cellMapNo[cell.Y, cell.X] + 1; cellList.Add(new Point(newX,newY)); break; } } if(visitedAllNeighours) { cellList.RemoveAt(cellNo); } } cellMap = FindEndPoint(cellMapNo, cellMap, maximumX, maximumY); return cellMap; }
public unsafe void TestMinimumIntensityProjection() { const int pixels = 512 * 512; const int subsamples = 11; var rng = new PseudoRandom(0x2DB8498F); var slabData = new ushort[pixels * subsamples]; for (var n = 0; n < slabData.Length; ++n) { slabData[n] = (ushort)rng.Next(ushort.MinValue, ushort.MaxValue); } var expectedResults = new ushort[pixels]; for (var p = 0; p < pixels; ++p) { expectedResults[p] = Enumerable.Range(0, subsamples).Select(s => slabData[s * pixels + p]).Min(); } var actualResults = new ushort[pixels]; var projectedData = new byte[pixels * sizeof(ushort)]; fixed(ushort *pSlabData = slabData) { SlabProjection.AggregateSlabMinimumIntensity((IntPtr)pSlabData, projectedData, subsamples, pixels, 2, false); Buffer.BlockCopy(projectedData, 0, actualResults, 0, projectedData.Length); } Assert.AreEqual(expectedResults, actualResults); }
/// <summary> /// Immediately generates one or more particles, particle is created in the /// concrete emitter class by the method GenerateParticle() /// </summary> /// <param name="count">How many particles to generate at once</param> /// <returns>List of particles generated, these will be added to the ParticleSys</returns> public void Generate(int count) { Debug.Assert(textures.Count > 0, "No textures have been added to the emitter"); Debug.Assert(hostEffect != null, "This emitter is not added to any ParticleEffect"); ParticlePool pool = hostEffect.ParticleSystem.Pool; for (int i = 0; i < count; i++) { if (Flags.HasFlag(EmitterModes.UseBudgetOnly)) { budget--; if (budget < 0) { break; } } Particle p = null; if (pool != null) { p = pool.Get(); p.Texture = textures[random.Next(textures.Count)]; } else { p = new Particle(textures[random.Next(textures.Count)]); } Debug.Assert(p != null, "Particle was not instantiated or fetched from cache!"); // Apply all generators to this particle foreach (PropertyGenerator g in generators) { g.Apply(p); } // Call the concrete emitter for last modifications GenerateParticle(p); particles.Add(p); } }
static void ModulateQPSK() { // Manual adjust values int sampleRate = 8000; int symbolCount = 1000; float symbolRate = 250; // Symbols per second int carrierHz = 1000; string outputPath = Path.Combine(LocalPath, "qpsk_output.8k.3ch.pcm32f"); // Fixed values float symbolPeriod = 1f / symbolRate; int symbolLengthSamples = sampleRate / (int)symbolRate; Vco vco = new Vco(sampleRate, carrierHz); PseudoRandom random = new PseudoRandom(23); using (Stream output = File.Create(outputPath)) { for (int i = 0; i < symbolCount; i++) { // Generate 2 data bits int bits = (int)random.Next(0, 4); // Generate symbol samples for (int p = 0; p < symbolLengthSamples; p++) { // Generate symbol sample float sampleI = (float)vco.Cos() * ((bits >> 1) * 2 - 1); float sampleQ = (float)vco.Sin() * ((bits & 1) * 2 - 1); vco.Next(); // Reduce amplitude so the sum equals 1.0 sampleI *= 0.707f; sampleQ *= 0.707f; // Combine I and Q samples float sampleOutput = sampleI + sampleQ; // Output sample to file output.Write(BitConverter.GetBytes(sampleOutput), 0, 4); output.Write(BitConverter.GetBytes(((bits >> 1) * 2f - 1)), 0, 4); output.Write(BitConverter.GetBytes(((bits & 1) * 2f - 1)), 0, 4); } } } }
public static int[,] CreateAgentBasedMap(int _mapSize, int seedX, int seedY, int seedZ) { int mapSize = _mapSize; PseudoRandom rnd = new PseudoRandom(seedX, seedY, seedZ); int[,] map = InitialiseMap(mapSize); int[] startPoint = ChooseRandomPoint( (int)Math.Round(mapSize/3f), (int)Math.Round(2f*mapSize/3f), rnd); AgentBased agent = new AgentBased(0.05, 0.05, startPoint, rnd.Next(0,4)); while(agent.CanContinue(map, rnd) && (!CheckFilled(map, 0.5f)) ) { map = agent.PlaceRoom(map, rnd); map = agent.MoveInDirection(map, rnd); } return map; }
protected void GenerateFoesForRegion(int regionID) { var tRand = new PseudoRandom((uint)core.RandomInt(1024)); var myAngleFromCenter = Geometry.FixAngle(Math.Atan2(gs.currentPlayer.posY, gs.currentPlayer.posX)); //Sector angles should be smaller for farther-out regions //This formula results in generating 100% of region 0, 67% of region 1, 50% of region 2, 40% of region 3, 6.25% of region 30... var sectorCount = regionID + 2; var sectorAngle = Math.PI * 2 / sectorCount; var regionBaseAngle = regionID * 0.1; //Needed so that all the spawn points to the east aren't lined up //Round myAngleFromCenter by sectorAngle (always a whole number of sectorAngles per circle) var sectorID = (int)((myAngleFromCenter + regionBaseAngle) / sectorAngle) % sectorCount; if (regionID == gs.regionID) { gs.sectorID = sectorID; } for (int sector = sectorID - 1; sector <= sectorID + 1; sector++) { var idx = (sector + sectorCount) % sectorCount; if (!gs.regionSpawnRecord[regionID][idx]) { gs.regionSpawnRecord[regionID][idx] = true; var ringInner = gs.ringThickness * regionID; //Distance from the innermost part of this ring to the origin var angle = idx * sectorAngle - regionBaseAngle; var spawnCount = regionID + 5; //Number of chances to spawn an enemy in this sector var subsectorAngle = sectorAngle / spawnCount; //Angles at which to potentially spawn enemies //Randomly do or don't generate random enemies at each subsectorAngle within (minArc, maxArc) for (int spawnIdx = 0; spawnIdx < spawnCount; spawnIdx++) { tRand.Next(); //Locate a random distance from the origin at this angle, but within the region var subPos = tRand.RandomDouble() * gs.ringThickness; var x = Math.Cos(angle) * (ringInner + subPos); var y = Math.Sin(angle) * (ringInner + subPos); gs.generateEnemy(tRand, regionID, x, y, onEnemyFireBasic, onEnemyDamagedBasic, onEnemyDeathBasic); angle += subsectorAngle; } } } }
public void TestVolumeS16() { const short expectedMinimum = -100; const short expectedMaximum = 200; var rng = new PseudoRandom(0x48E4B0B4); var data = new short[1000]; for (var n = 1; n < data.Length - 1; ++n) { data[n] = (short)rng.Next(expectedMinimum + 1, expectedMaximum - 1); } data[0] = expectedMaximum; data[999] = expectedMinimum; var volume = new S16Volume(data, new Size3D(5, 10, 20), new Vector3D(2, 1, 0.5f), new Vector3D(5, 0, -5), new Matrix3D(new float[, ] { { 0, 1, 0 }, { 0, 0, 1 }, { 1, 0, 0 } }), new DicomAttributeCollection(), 123); Assert.AreSame(data, volume.Array, "Array"); Assert.AreSame(data, volume.ArrayData, "ArrayData"); Assert.AreEqual(new Size3D(5, 10, 20), volume.ArrayDimensions, "ArrayDimensions"); Assert.AreEqual(1000, volume.ArrayLength, "ArrayLength"); Assert.AreEqual(16, volume.BitsPerVoxel, "BitsPerVoxel"); Assert.AreEqual(true, volume.Signed, "Signed"); Assert.AreEqual(123, volume.PaddingValue, "PaddingValue"); Assert.AreEqual(new Vector3D(10, 10, 10), volume.VolumeSize, "VolumeSize"); Assert.AreEqual(new Rectangle3D(0, 0, 0, 10, 10, 10), volume.VolumeBounds, "VolumeBounds"); Assert.AreEqual(new Vector3D(5, 5, 5), volume.VolumeCenter, "VolumeCenter"); Assert.AreEqual(new Vector3D(10, 5, 0), volume.VolumeCenterPatient, "VolumeCenterPatient"); Assert.AreEqual(new Vector3D(0, 1, 0), volume.VolumeOrientationPatientX, "VolumeOrientationPatientX"); Assert.AreEqual(new Vector3D(0, 0, 1), volume.VolumeOrientationPatientY, "VolumeOrientationPatientY"); Assert.AreEqual(new Vector3D(1, 0, 0), volume.VolumeOrientationPatientZ, "VolumeOrientationPatientZ"); Assert.AreEqual(new Vector3D(5, 0, -5), volume.VolumePositionPatient, "VolumePositionPatient"); Assert.AreEqual(expectedMinimum, volume.MinimumVolumeValue, "MinimumVolumeValue"); Assert.AreEqual(expectedMaximum, volume.MaximumVolumeValue, "MaximumVolumeValue"); }
int[,] PlaceRoom(int[,] map, PseudoRandom rnd) { if(rnd.Next(0,99) < roomChance*100) { bool canPlace = CanPlaceRoom(map, rnd); Console.Write("Place Room: " + canPlace + "\n"); if(canPlace) { int[] firstStep = MoveDirection(currentPosition[0], currentPosition[1], currentDirection); int[] roomCenter = MoveDirection(firstStep[0], firstStep[1], currentDirection); int[] newPosition = MoveDirection(roomCenter[0], roomCenter[1], currentDirection); for(int y = roomCenter[1]-1; y <= roomCenter[1]+1; y++) { for(int x = roomCenter[0]-1; x <= roomCenter[0]+1; x++) { map = ChangeToRoom(x, y, map); } } currentPosition[0] = newPosition[0]; currentPosition[1] = newPosition[1]; for(int i = 0; i < 4; i++) { int[] newStart = MoveDirection(roomCenter[0], roomCenter[1], i); positions.Add(newStart[0]); positions.Add(newStart[1]); } roomChance = 0.05; } else { roomChance += roomRate; } } roomChance += roomRate; return map; }
static void ModulateOffsetQPSK() { // Manual adjust values int sampleRate = 8000; int symbolCount = 1000; float symbolRate = 250; // Symbols per second int carrierHz = 1000; string outputPath = Path.Combine(LocalPath, "oqpsk_output.8k.3ch.pcm32f"); // Fixed values float symbolPeriod = 1f / symbolRate; int symbolLengthSamples = sampleRate / (int)symbolRate; Vco vco = new Vco(sampleRate, carrierHz); PseudoRandom random = new PseudoRandom(23); BiQuadraticFilter iFilter = new BiQuadraticFilter(BiQuadraticFilter.Type.LOWPASS, symbolRate, sampleRate, 0.707); BiQuadraticFilter qFilter = new BiQuadraticFilter(BiQuadraticFilter.Type.LOWPASS, symbolRate, sampleRate, 0.707); using (Stream output = File.Create(outputPath)) { int bits = 0; int nextBits = 0; for (int i = 0; i < symbolCount * 2; i++) { bool even = i % 2 == 0; // On each half-symbol period, shift in one data bit for transmission // while keeping the previous other arm's bit transmitting if (even) { nextBits = (int)random.Next(0, 4); bits = (bits & 0x01) | (nextBits & 0x02); } else { bits = (bits & 0x02) | (nextBits & 0x01); } // Generate symbol samples for (int p = 0; p < symbolLengthSamples / 2; p++) { // Generate symbol sample // Also filter symbols with a low pass filter for basic pulse shaping float sampleI = (float)vco.Cos() * (float)iFilter.filter((bits >> 1) * 2 - 1); float sampleQ = (float)vco.Sin() * (float)qFilter.filter((bits & 1) * 2 - 1); vco.Next(); // Reduce amplitude so the sum equals 1.0 sampleI *= 0.707f; sampleQ *= 0.707f; // Combine I and Q samples float sampleOutput = sampleI + sampleQ; // Output sample to file output.Write(BitConverter.GetBytes(sampleOutput), 0, sizeof(float)); output.Write(BitConverter.GetBytes(((bits >> 1) * 2f - 1)), 0, sizeof(float)); output.Write(BitConverter.GetBytes(((bits & 1) * 2f - 1)), 0, sizeof(float)); } } } }
public int[] FindRandomPoint(PseudoRandom rnd) { int[] coords = new int[2]; coords[0] = rnd.Next(0,width); coords[1] = rnd.Next(0,height); bool valid = false; while (!valid) { if (IsOpen (coords [0], coords [1])) { valid = true; } else { coords[0] = rnd.Next(0,width); coords[1] = rnd.Next(0,height); } } return coords; }
int NewDirection(int dir, PseudoRandom rnd) { int newDir = dir; if(rnd.Next(0,2) == 0) { newDir = (dir + 1)%4; } else { newDir = (dir - 1)%4; } if(newDir < 0) { newDir = 4 + newDir; } return newDir; }
static int[] ChooseRandomPoint(int lowerLimit, int upperLimit, PseudoRandom rnd) { int[] coords = new int[2]; coords[0] = rnd.Next(lowerLimit, upperLimit); coords[1] = rnd.Next(lowerLimit, upperLimit); return coords; }
static int[,] CreateLeftSide(int[,] newMap, MapInfo mapInfo) { PseudoRandom leftPRnd = new PseudoRandom (mapInfo.seedX-1, mapInfo.seedY, mapInfo.seedZ); int[] leftDirection = new int[2]{1,0}; int numOfEntries = 0; for (int i = 0; i < mapInfo.maxNumOfEntries; i++) { int startPoint = leftPRnd.Next(1, newMap.GetLength(0)-1-mapInfo.maxEntrySize); int entrySize = leftPRnd.Next(2, mapInfo.maxEntrySize+1); int[] startCoord = new int[2]{0, startPoint}; int[] connect = PathGenerator.FindEndPoint (startCoord, entrySize, leftDirection, newMap); //Debug.Log ("Connection: " + i + " (" + connect[0] + "," + connect[1] + ")"); newMap = PathGenerator.CreateHorizontalPath(startCoord, connect, entrySize, newMap); /*for (int y = startPoint; y <= (startPoint + entrySize); y++) { for (int x = 0; x <= connect [0]; x++) { newMap [y, x] = 0; } }*/ } return newMap; }
bool CanContinue(int[,] map, PseudoRandom rnd) { bool returnable = false; int[] newPoint = MoveDirection(currentPosition[0], currentPosition[1], currentDirection ); if(IsValidRoute(newPoint, map)) { returnable = true; } else { int startDir = rnd.Next(0,4); for(int i = 0; i < 4; i++) { newPoint = MoveDirection(currentPosition[0], currentPosition[1], (startDir+i)%4 ); if(IsValidRoute(newPoint, map)) { returnable = true; Console.Write("CC Direction Change From: " + currentDirection + "\n"); currentDirection = NewDirection(currentDirection, rnd); Console.Write("CC Direction Change To: " + currentDirection + "\n"); directionChance = 0.05; break; } } } if(returnable == false && positions.Count > 0) { currentPosition[0] = positions[0]; currentPosition[1] = positions[1]; positions.RemoveAt(1); positions.RemoveAt(0); returnable = true; } return returnable; }
public override float Generate() { return(prng.Next()); }
public int[,] InitialiseMap(double r, int mapSize, PseudoRandom rnd) { int[,] map = new int[mapSize, mapSize]; for(int y = 0; y < mapSize; y++) { for(int x = 0; x < mapSize; x++) { double number = Math.Round(rnd.Next(), 3); if(number < r) { map[y,x] = stateRock; } else { map[y,x] = stateEmpty; } } } return map; }
public int PickRandomY(PseudoRandom rnd) { return rnd.Next(Y, Y+height); }
public void DrawGame() { g.BeforeFrame(); g.TranslateTransform((float)-gs.currentPlayer.posX + gameWidth / 2, (float)-gs.currentPlayer.posY + gameHeight / 2); var oldTransform = g.GetMatrix(); //Draw a starry background using a predictable pseudorandom number sequence var starSeed = new PseudoRandom(backgroundSeed); for (double x = gs.currentPlayer.posX - (gameWidth / 2); x < gs.currentPlayer.posX + (gameWidth / 2) + 256; x += 256) { int squareX = (int)Math.Floor(x / 256); for (double y = gs.currentPlayer.posY - (gameHeight / 2); y < gs.currentPlayer.posY + (gameHeight / 2) + 256; y += 256) { int squareY = (int)Math.Floor(y / 256); starSeed.lastValue = (uint)(((long)squareX * 13 + squareY * 58) & uint.MaxValue); int numberOfStars = Math.Min(8 + ((int)(starSeed.Next() & 0xF00) >> 8), 25); //10 to 25 stars for (int i = 0; i < numberOfStars; i++) { var xc = (float)squareX * 256 + (starSeed.Next() & 255); var yc = (float)squareY * 256 + (starSeed.Next() & 255); g.DrawLine(Pens.White, xc, yc, xc, yc + 1); } } } #if DEBUG DrawRegionSector(gs.regionID); g.ResetMatrix(); g.DrawString(String.Format("Sector Area: {0:0.00}", gs.regionSectorArea), new Font(MenuFont.FontFamily, 12), Brushes.Azure, 10, gameHeight - 40); #endif //Draw projectiles foreach (var projectile in gs.playerProjectiles.Union(gs.enemyProjectiles)) { g.SetMatrix(oldTransform); g.TranslateTransform((float)projectile.posX, (float)projectile.posY); g.DrawLines(projectile.uGraphics.color, projectile.uGraphics.points.ToArray()); #if DEBUG foreach (var circle in projectile.collider.dCircles) { g.DrawEllipse(Pens.Aqua, (float)(-circle.Radius + circle.X), (float)(-circle.Radius + circle.Y), (float)circle.Radius * 2, (float)circle.Radius * 2); } #endif } //Draw ships (and in debug mode, draw their colliders) foreach (var ship in gs.playerShips.Union(gs.enemyShips).Where(p => p.isAlive)) { g.SetMatrix(oldTransform); g.TranslateTransform((float)ship.posX, (float)ship.posY); g.RotateTransform((float)(ship.facing / Math.PI * 180)); if (ship.lastDamagedFrame <= frameCounter - 8) { g.DrawLines(ship.uGraphics.color, ship.uGraphics.points.ToArray()); } else //Invert ship color when recently damaged { var tempColor = ship.uGraphics.color.Color.ToArgb(); var tempPen = new Pen(Color.FromArgb(tempColor ^ 0x00FFFFFF)); g.DrawLines(tempPen, ship.uGraphics.points.ToArray()); } #if DEBUG foreach (var circle in ship.collider.dCircles) { g.DrawEllipse(Pens.Aqua, (float)(-circle.Radius + circle.X), (float)(-circle.Radius + circle.Y), (float)circle.Radius * 2, (float)circle.Radius * 2); } #endif g.ResetMatrix(); } if (input.GetState(input.shiftKey) == InputState.Held) { g.DrawLine(new Pen(Color.FromArgb(255, 0, 39, 45)), (float)(gameWidth / 2 - gs.currentPlayer.posX), (float)(gameHeight / 2 - gs.currentPlayer.posY), gameWidth / 2, gameHeight / 2); } g.AfterFrame(); }
public static void CreateForestMeshFromMap(Layout layout, Grid grid, int seedX, int seedY, int seedZ) { Texture2D tex = CreateForestTextureFromMap (layout.layout, grid, seedX, seedY, seedZ); Rect rec = new Rect (0, 0, grid.width*16, grid.height*16); //Hardcoded Paths GameObject blankObj = Resources.Load ("Prefabs/BlankSprite") as GameObject; GameObject blockObj = Resources.Load ("Prefabs/BlankInter") as GameObject; Vector3 vec = new Vector3 ( ((float)(grid.width))/2, ((float)(grid.height))/2, 1); GameObject obj = (GameObject) Instantiate (blankObj, vec, Quaternion.identity); Sprite sprite = Sprite.Create(tex, rec, new Vector2(0.5f, 0.5f), 16); PseudoRandom prnd = new PseudoRandom (seedX, seedY, seedZ); obj.GetComponent<SpriteRenderer> ().sprite = sprite; SceneObjects.AddObjectToScene (obj); for (int y = 0; y < grid.height; y++) { for (int x = 0; x < grid.width; x++) { // Create Invisible wall rigidbodys if (layout.CheckIfOnEdge(x,y)){ //GameObject objr = (GameObject)Instantiate (blockObj, new Vector3 (grid.GetCoords (x, y).x, grid.GetCoords (x, y).y, 0), Quaternion.identity); //SceneObjects.AddObjectToScene (objr); } else if (layout.CheckIfNextToEdge(x,y))// Create wall leaves. { int num = prnd.Next (1, 3); string nsewData = NSEWMap.GetNSEWString(layout.layout, x, y, 0); if (nsewData.Length > 3) { num = 1; } string spriteName = "Forest_Leaf" + nsewData + "_" + num.ToString (); if (CheckSpriteExists (spriteName)) { GameObject objr = (GameObject)Instantiate (blankObj, new Vector3 (grid.GetCoords (x, y).x, grid.GetCoords (x, y).y, 0), Quaternion.identity); objr.GetComponent<SpriteRenderer> ().sprite = GetSprite (spriteName); objr.GetComponent<SpriteRenderer> ().sortingOrder = 100; SceneObjects.AddObjectToScene (objr); } } if (layout.IsOpen (x, y)) { double chance = prnd.Next (); if (chance < 0.01) { int num = prnd.Next (1, 4); string spriteName = "Forest_Mush_" + num.ToString(); GameObject objr = (GameObject)Instantiate (blankObj, new Vector3 (grid.GetCoords (x, y).x, grid.GetCoords (x, y).y, 0), Quaternion.identity); objr.GetComponent<SpriteRenderer> ().sprite = GetSprite (spriteName); SceneObjects.AddObjectToScene (objr); } else if(chance >= 0.01 && chance < 0.02) { int num = prnd.Next (1, 5); string spriteName = "Forest_Rock_" + num.ToString(); GameObject objr = (GameObject)Instantiate (blankObj, new Vector3 (grid.GetCoords (x, y).x, grid.GetCoords (x, y).y, 0), Quaternion.identity); objr.GetComponent<SpriteRenderer> ().sprite = GetSprite (spriteName); SceneObjects.AddObjectToScene (objr); } } } } }
//================================================================// // CreateRandomPoint //================================================================// // Selects a random point on the map between (0,0) and (mapWidth,MapHeight) //================================================================// public int[] CreateRandomPoint(PseudoRandom rnd, int mapWidth, int mapHieght) { int[] point = new int[2]; point[0] = rnd.Next(0, mapWidth ); point[1] = rnd.Next(0, mapHieght); return point; }
//================================================================// // RandomiseListOrder //================================================================// // Randomises the order of the list by swaping the contents of the // list. //================================================================// List<int> RandomiseListOrder(List<int> initialList, PseudoRandom rnd) { for(int i = 0; i < initialList.Count; i++) { int swapWith = rnd.Next(0, initialList.Count); int holder = initialList[swapWith]; initialList[swapWith] = initialList[i]; initialList[i] = holder; } return initialList; }
static List<Quadrant> SpacialPartition(int mapSize, double splitChance, double roomChance, PseudoRandom rnd) { Quadrant first = new Quadrant(0,0, mapSize, mapSize, 0, 0); List<Quadrant> next = first.QuarterQuadrant(rnd, roomChance); List<Quadrant> final = new List<Quadrant>(); while(next.Count != 0) { int index = 0; double split = Math.Round(rnd.Next(), 2); if(split < splitChance) { List<Quadrant> subs = next[index].QuarterQuadrant(rnd, roomChance); if(subs.Count == 0) { final.Add(next[index]); } else { next.AddRange(subs); } } else { final.Add(next[index]); } next.RemoveAt(index); } return final; }
void ChangeDirection(PseudoRandom rnd) { if(rnd.Next(0,99) < directionChance*100) { Console.Write("Direction Change From: " + currentDirection + "\n"); currentDirection = NewDirection(currentDirection, rnd); Console.Write("Direction Change To: " + currentDirection + "\n"); directionChance = 0.05; } directionChance += directionRate; }
public int PickRandomX(PseudoRandom rnd) { return rnd.Next(X, X+width); }
static int[,] CreateDownSide(int[,] newMap, MapInfo mapInfo) { PseudoRandom downPRnd = new PseudoRandom (mapInfo.seedX, mapInfo.seedY-1, mapInfo.seedZ);//mapInfo.GetSeededPsuedoRnd (); int[] downDirection = new int[2]{0,1}; int numOfEntries = 0; for (int i = 0; i < mapInfo.maxNumOfEntries; i++) { int startPoint = downPRnd.Next(1, newMap.GetLength(1)-1-mapInfo.maxEntrySize); int entrySize = downPRnd.Next(2, mapInfo.maxEntrySize+1); int[] startCoord = new int[2]{startPoint, 0}; int[] connect = PathGenerator.FindEndPoint (startCoord, entrySize, downDirection, newMap); //Debug.Log ("Connection: " + i + " (" + connect[0] + "," + connect[1] + ")"); newMap = PathGenerator.CreateVerticalPath(startCoord, connect, entrySize, newMap, mapInfo); /*for (int x = startPoint; x <= (startPoint + entrySize); x++) { for (int y = 0; y <= connect[1]; y++) { newMap [y, x] = 0; } }*/ } return newMap; }
int SelectRoom(PseudoRandom rnd, double roomChance) { double room = Math.Round(rnd.Next(), 2); //Console.Write("Chance: " + roomChance + " - " + room + "\n"); int num = 0; if(room < roomChance) { num = stateRoom; } return num; }
public static List<string> GetMineSpriteNameForTile(int x, int y, string[,] nsewMap, Layout layout, Grid grid, PseudoRandom prnd) { List<string> nameList = new List<string> (); string nsewData = nsewMap [y, x]; if (!layout.IsOpen(x,y)) { nameList.Add("Mine_Empty"); string spriteName = "Mine_Edge" + nsewData; if (CheckSpriteExists (spriteName)) { nameList.Add("Mine_Edge" + nsewData); } else { char[] data = nsewData.ToCharArray (); for(int i = 1; i < data.Length; i++){ string newSpriteName = "Mine_Edge" + "_" + data[i].ToString(); Debug.Log (nsewData); if (CheckSpriteExists (newSpriteName)) { nameList.Add ("Mine_Edge" + "_" + data[i].ToString()); } } } if (layout.CheckIfOnEdge (x, y)) { if (x + 1 < grid.width && y + 1 < grid.height && !layout.IsOpen(x+1,y) && !layout.IsOpen(x,y+1) && layout.IsOpen(x+1,y+1)) { nameList.Add ("Mine_Edge_Corner" + "_NE"); } if (x - 1 >= 0 && y + 1 < grid.height && !layout.IsOpen(x-1,y) && !layout.IsOpen(x,y+1) && layout.IsOpen(x-1,y+1)) { nameList.Add ("Mine_Edge_Corner" + "_NW"); } if (x + 1 < grid.width && y - 1 >= 0 && !layout.IsOpen(x+1,y) && !layout.IsOpen(x,y-1) && layout.IsOpen(x+1,y-1)) { nameList.Add ("Mine_Edge_Corner" + "_SE"); } if (x - 1 >= 0 && y - 1 >= 0 && !layout.IsOpen(x-1,y) && !layout.IsOpen(x,y-1) && layout.IsOpen(x-1,y-1)) { nameList.Add ("Mine_Edge_Corner" + "_SW"); } } } else { string num = prnd.Next (1, 4).ToString (); nameList.Add("Mine_Floor_" + num); if (prnd.Next () < 0.1) { string decalNum = prnd.Next (1, 10).ToString (); nameList.Add("Mine_Floor_Decal_" + decalNum); } string insideNsewData = NSEWMap.GetNSEWString(layout.layout, x, y, 0); if (insideNsewData.Contains ("N")) { string shadowTile = "Mine_Floor_Shadow_"; if (insideNsewData.Contains ("E") && nsewData.Contains ("W")) { nameList.Add (shadowTile + "NE"); nameList.Add (shadowTile + "NW"); } else if (insideNsewData.Contains ("E")) { nameList.Add (shadowTile + "NE"); } else if (insideNsewData.Contains ("W")) { nameList.Add (shadowTile + "NW"); } else { nameList.Add (shadowTile + "N"); } } } return nameList; }