package { import ca.struct.dungo.*; import assets.*; import flash.events.*; import flash.ui.*; import flash.display.*; public class DungoSWF extends Sprite { private var _grid:Grid; private var _stepInput:StepInputAsset; public function DungoSWF(){ super(); Config.stage = stage; addChild((this._stepInput = new StepInputAsset())); this._stepInput.x = Config.stage.stageWidth; this._stepInput.y = 0; this._stepInput.generateButton.mouseChildren = false; this._stepInput.generateButton.addEventListener(MouseEvent.CLICK, this.onGenerateClick); this._stepInput.roomSizeTextField.text = "4"; this._stepInput.roomSizeTextField.restrict = "0-9"; this._stepInput.zonesTextField.text = "10"; this._stepInput.zonesTextField.restrict = "0-9"; this._stepInput.colsTextField.text = "8"; this._stepInput.colsTextField.restrict = "0-9"; this._stepInput.rowsTextField.text = "8"; this._stepInput.rowsTextField.restrict = "0-9"; this.reset(); Config.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.onKeyDown); } private function onGenerateClick(event:MouseEvent):void{ this.reset(); } private function onKeyDown(event:KeyboardEvent):void{ if ((((event.keyCode == Keyboard.R)) || ((event.keyCode == Keyboard.G)))){ this.reset(); }; } private function reset():void{ if (this._grid){ this._grid.kill(); removeChild(this._grid); }; var roomSizeInput:int = parseInt(this._stepInput.roomSizeTextField.text); var zonesInput:int = parseInt(this._stepInput.zonesTextField.text); var colInput:int = parseInt(this._stepInput.colsTextField.text); var rowInput:int = parseInt(this._stepInput.rowsTextField.text); if (((isNaN(roomSizeInput)) || ((roomSizeInput <= 0)))){ roomSizeInput = 1; }; if (((isNaN(zonesInput)) || ((zonesInput <= 0)))){ zonesInput = 1; }; if (((isNaN(colInput)) || ((colInput <= 0)))){ colInput = 1; }; if (((isNaN(rowInput)) || ((rowInput <= 0)))){ rowInput = 1; }; roomSizeInput = Math.min(roomSizeInput, 20); this._grid = new Grid(roomSizeInput, zonesInput, colInput, rowInput); addChild(this._grid); this._grid.x = (stage.stageWidth / 2); this._grid.y = (stage.stageHeight / 2); } } }//package package ca.struct.dungo { public class Edge { private var _startCol:int; private var _startRow:int; private var _endCol:int; private var _endRow:int; public function Edge(startCol:int, startRow:int, endCol:int, endRow:int){ super(); this._startCol = startCol; this._startRow = startRow; this._endCol = endCol; this._endRow = endRow; } public function get startCol():int{ return (this._startCol); } public function get startRow():int{ return (this._startRow); } public function get endCol():int{ return (this._endCol); } public function get endRow():int{ return (this._endRow); } } }//package ca.struct.dungo package ca.struct.dungo { import __AS3__.vec.*; public class Zone { private var _color:uint; private var _connections:Vector.; private var _hasBeenReached:Boolean = false; public function Zone(color:uint){ this._connections = new Vector.(); super(); this._color = color; } public function get color():uint{ return (this._color); } public function connectToZone(otherZone:Zone, startCol:int, startRow:int, endCol:int, endRow:int):void{ var connection:Connection; var newConnection:Connection; for each (connection in this._connections) { if ((((connection.zoneA == otherZone)) || ((connection.zoneB == otherZone)))){ connection.addEdge(new Edge(startCol, startRow, endCol, endRow)); return; }; }; newConnection = new Connection(this, otherZone); newConnection.addEdge(new Edge(startCol, startRow, endCol, endRow)); this.addConnection(newConnection); otherZone.addConnection(newConnection); Grid.instance.addConnection(newConnection); } public function addConnection(connection:Connection):void{ this._connections.push(connection); } public function get hasBeenReached():Boolean{ return (this._hasBeenReached); } public function set hasBeenReached(hasBeenReached:Boolean):void{ this._hasBeenReached = hasBeenReached; } public function get connections():Vector.{ return (this._connections); } } }//package ca.struct.dungo package ca.struct.dungo { import __AS3__.vec.*; import assets.*; import flash.display.*; import flash.events.*; public class Grid extends Sprite { public static var PIXEL_SIZE:Number; public static var instance:Grid; private var _roomSize:int; private var _zoneCount:int; private var _cols:int; private var _rows:int; private var _stepIndex:int = 0; private var _stepLabel:StepLabelAsset; private var _gridBMD:BitmapData; private var _gridBitmap:Bitmap; private var _zones:Vector.; private var _blackPixels:Vector.; private var _connections:Vector.; private var _solidBMD:BitmapData; private var _solidBitmap:Bitmap; public function Grid(roomSize:int, zoneCount:int, cols:int, rows:int){ this._zones = new Vector.(); this._blackPixels = new Vector.(); this._connections = new Vector.(); super(); instance = this; this._roomSize = roomSize; this._zoneCount = zoneCount; this._cols = cols; this._rows = rows; addChild((this._stepLabel = new StepLabelAsset())); this._stepLabel.x = (-(Config.stage.stageWidth) / 2); this._stepLabel.y = (-(Config.stage.stageHeight) / 2); this._stepLabel.textField.text = "Generating cells..."; this._gridBMD = new BitmapData(this._cols, this._rows, false, 0); addChild((this._gridBitmap = new Bitmap(this._gridBMD))); var colrow:Number = Math.max(this._cols, this._rows); var scale:Number = (500 / colrow); this._gridBitmap.scaleX = (this._gridBitmap.scaleY = scale); this._gridBitmap.x = (-(this._gridBitmap.width) / 2); this._gridBitmap.y = (-(this._gridBitmap.height) / 2); Config.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown); this.doNextStep(); } public function addConnection(connection:Connection):void{ this._connections.push(connection); } private function createZones():void{ var possibleColor:uint; var setCol:int; var setRow:int; var zoneColor:uint; var zone:Zone; var blackIndex:int; this._stepLabel.textField.text = "Creating zones..."; var zoneColors:Array = []; while (zoneColors.length < this._zoneCount) { possibleColor = (Math.random() * 0xFFFFFF); if (zoneColors.indexOf(possibleColor) == -1){ zoneColors.push(possibleColor); }; }; var z:int; while (z < this._zoneCount) { setCol = (Math.random() * this._cols); setRow = (Math.random() * this._rows); if (this._gridBMD.getPixel(setCol, setRow) == 0){ zoneColor = zoneColors[z]; this._gridBMD.setPixel(setCol, setRow, zoneColor); zone = new Zone(zoneColor); this._zones.push(zone); blackIndex = this._blackPixels.indexOf(((setRow * this._cols) + setCol)); if (blackIndex != -1){ this._blackPixels.splice(blackIndex, 1); }; this.addEdgePixel((setCol - 1), setRow); this.addEdgePixel((setCol + 1), setRow); this.addEdgePixel(setCol, (setRow - 1)); this.addEdgePixel(setCol, (setRow + 1)); }; z++; }; } private function replaceBlackPixel(col:int, row:int, newColor:uint):void{ this._gridBMD.setPixel(col, row, newColor); var blackIndex:int = this._blackPixels.indexOf(((row * this._cols) + col)); if (blackIndex != -1){ this._blackPixels.splice(blackIndex, 1); }; this.addEdgePixel((col - 1), row); this.addEdgePixel((col + 1), row); this.addEdgePixel(col, (row - 1)); this.addEdgePixel(col, (row + 1)); } private function addEdgePixel(col:int, row:int):void{ var pos:int = ((row * this._cols) + col); if ((((((((col >= 0)) && ((col < this._cols)))) && ((row >= 0)))) && ((row < this._rows)))){ if (this._gridBMD.getPixel(col, row) == 0){ if (this._blackPixels.indexOf(pos) == -1){ this._blackPixels.push(pos); }; }; }; } private function getZoneForColor(color:uint):Zone{ var zone:Zone; for each (zone in this._zones) { if (zone.color == color){ return (zone); }; }; return (null); } private function onMouseDown(event:MouseEvent):void{ this.doNextStep(); } private function doNextStep():void{ this._stepIndex++; if (this._stepIndex == 1){ this.createZones(); this.doNextStep(); } else { if (this._stepIndex == 2){ addEventListener(Event.ENTER_FRAME, this.onEnterFrameExpand); } else { if (this._stepIndex == 3){ this.createZoneConnections(); this.doNextStep(); } else { if (this._stepIndex == 4){ this.traverseConnections(); this.doNextStep(); } else { if (this._stepIndex == 5){ this.createSolid(); this.doNextStep(); }; }; }; }; }; } private function createSolid():void{ var colorA:uint; var colorB:uint; var colorC:uint; var colorD:uint; var punchC:int; var punchR:int; var connection:Connection; var i:int; var edge:Edge; var solidWidth:int = (((this._cols * this._roomSize) + this._cols) + 1); var solidHeight:int = (((this._rows * this._roomSize) + this._cols) + 1); var wallColor:uint = 0xFFFFFF; var floorColor:uint; this._solidBMD = new BitmapData(solidWidth, solidHeight, false, floorColor); addChild((this._solidBitmap = new Bitmap(this._solidBMD))); this._solidBitmap.alpha = 1; var bigSide:Number = Math.max(this._solidBMD.width, this._solidBMD.height); var scale:Number = (500 / bigSide); this._solidBitmap.scaleX = (this._solidBitmap.scaleY = scale); this._solidBitmap.x = (-(this._solidBitmap.width) / 2); this._solidBitmap.y = (-(this._solidBitmap.height) / 2); var col:int; var row:int; row = 0; while (row < solidHeight) { col = 0; while (col < solidWidth) { if ((row % (this._roomSize + 1)) == 0){ this._solidBMD.setPixel(col, row, wallColor); } else { if ((col % (this._roomSize + 1)) == 0){ this._solidBMD.setPixel(col, row, wallColor); }; }; col++; }; row++; }; var p:int; row = 0; while (row < this._rows) { col = 1; while (col < this._cols) { colorA = this._gridBMD.getPixel((col - 1), row); colorB = this._gridBMD.getPixel(col, row); colorC = this._gridBMD.getPixel((col - 1), (row + 1)); colorD = this._gridBMD.getPixel(col, (row + 1)); if (colorA == colorB){ punchC = (col * (this._roomSize + 1)); punchR = (row * (this._roomSize + 1)); p = 0; while (p < this._roomSize) { this._solidBMD.setPixel(punchC, ((punchR + p) + 1), floorColor); p++; }; if ((((colorA == colorC)) && ((colorA == colorD)))){ punchC = (col * (this._roomSize + 1)); punchR = ((row + 1) * (this._roomSize + 1)); this._solidBMD.setPixel(punchC, punchR, floorColor); }; }; col++; }; row++; }; col = 0; while (col < this._cols) { row = 1; while (row < this._rows) { colorA = this._gridBMD.getPixel(col, (row - 1)); colorB = this._gridBMD.getPixel(col, row); if (colorA == colorB){ punchC = (col * (this._roomSize + 1)); punchR = (row * (this._roomSize + 1)); p = 0; while (p < this._roomSize) { this._solidBMD.setPixel(((punchC + p) + 1), punchR, floorColor); p++; }; }; row++; }; col++; }; for each (connection in this._connections) { if (connection.hasBeenUsed){ if (((connection.hasBeenUsed) || ((Math.random() < 0.1)))){ i = 0; while (i < 1) { edge = connection.edges[int((Math.random() * connection.edges.length))]; if (edge.startRow == edge.endRow){ punchC = ((edge.startCol + 1) * (this._roomSize + 1)); punchR = (edge.startRow * (this._roomSize + 1)); this._solidBMD.setPixel(punchC, ((punchR + 2) + (Math.random() * (this._roomSize - 2))), floorColor); } else { if (edge.startCol == edge.endCol){ punchC = (edge.startCol * (this._roomSize + 1)); punchR = ((edge.startRow + 1) * (this._roomSize + 1)); this._solidBMD.setPixel(((punchC + 2) + (Math.random() * (this._roomSize - 2))), punchR, floorColor); }; }; i++; }; }; }; }; } private function traverseConnections():void{ var resetConnection:Connection; var resetZone:Zone; var haveAllZonesBeenReached:Boolean; var currentZone:Zone; var attempts:int; var conn:Connection; var connection:Connection; var zone:Zone; for each (resetConnection in this._connections) { resetConnection.hasBeenUsed = false; }; for each (resetZone in this._zones) { resetZone.hasBeenReached = false; }; haveAllZonesBeenReached = false; currentZone = this._zones[0]; currentZone.hasBeenReached = true; attempts = 100000; while (((!(haveAllZonesBeenReached)) && ((attempts > 0)))) { connection = currentZone.connections[int((Math.random() * currentZone.connections.length))]; if (connection.zoneA == currentZone){ currentZone = connection.zoneB; } else { if (connection.zoneB == currentZone){ currentZone = connection.zoneA; }; }; if (!(currentZone.hasBeenReached)){ connection.hasBeenUsed = true; currentZone.hasBeenReached = true; }; haveAllZonesBeenReached = true; for each (zone in this._zones) { if (!(zone.hasBeenReached)){ haveAllZonesBeenReached = false; break; }; }; }; var notNeeded:int; for each (conn in this._connections) { if (!(conn.hasBeenUsed)){ notNeeded++; }; }; } private function createZoneConnections():void{ var col:int; var row:int; var colorA:uint; var colorB:uint; row = 0; while (row < this._rows) { col = 1; while (col < this._cols) { colorA = this._gridBMD.getPixel((col - 1), row); colorB = this._gridBMD.getPixel(col, row); if (colorA != colorB){ this.getZoneForColor(colorA).connectToZone(this.getZoneForColor(colorB), (col - 1), row, col, row); }; col++; }; row++; }; col = 0; while (col < this._cols) { row = 1; while (row < this._rows) { colorA = this._gridBMD.getPixel(col, (row - 1)); colorB = this._gridBMD.getPixel(col, row); if (colorA != colorB){ this.getZoneForColor(colorA).connectToZone(this.getZoneForColor(colorB), col, (row - 1), col, row); }; row++; }; col++; }; } private function onEnterFrameExpand(event:Event):void{ this._stepIndex = 1; this._stepLabel.textField.text = "Expanding zones..."; var e:int = 2500; do { var _temp1 = e; e = (e - 1); } while ((((_temp1 > 0)) && (this.expandZones()))); } private function expandZones():Boolean{ var extend:uint; if (this._blackPixels.length == 0){ this._stepLabel.textField.text = "Expansion complete."; this._stepIndex = 2; removeEventListener(Event.ENTER_FRAME, this.onEnterFrameExpand); this.doNextStep(); return (false); }; var blackIndex:int = (Math.random() * this._blackPixels.length); var blackPos:int = this._blackPixels[blackIndex]; var checkCol:int = (blackPos % this._cols); var checkRow:int = (blackPos / this._cols); var expandCol:int = checkCol; var expandRow:int = checkRow; var dir:int = (Math.random() * 4); if (dir == 0){ expandCol--; } else { if (dir == 1){ expandRow++; } else { if (dir == 2){ expandCol++; } else { if (dir == 3){ expandRow--; }; }; }; }; if ((((((((expandCol < 0)) || ((expandCol >= this._cols)))) || ((expandRow < 0)))) || ((expandRow >= this._rows)))){ } else { extend = this._gridBMD.getPixel(expandCol, expandRow); if (extend != 0){ this._gridBMD.setPixel(checkCol, checkRow, extend); this._blackPixels.splice(blackIndex, 1); this.addEdgePixel((checkCol - 1), checkRow); this.addEdgePixel((checkCol + 1), checkRow); this.addEdgePixel(checkCol, (checkRow - 1)); this.addEdgePixel(checkCol, (checkRow + 1)); }; }; return (true); } public function getPixelAt(col:int, row:int):uint{ return (this._gridBMD.getPixel(col, row)); } public function kill():void{ removeEventListener(Event.ENTER_FRAME, this.onEnterFrameExpand); } } }//package ca.struct.dungo package ca.struct.dungo { import flash.display.*; public class Config { public static var stage:Stage; } }//package ca.struct.dungo package ca.struct.dungo { import __AS3__.vec.*; public class Connection { private var _zoneA:Zone; private var _zoneB:Zone; private var _edges:Vector.; private var _hasBeenUsed:Boolean = false; public function Connection(zoneA:Zone, zoneB:Zone){ this._edges = new Vector.(); super(); this._zoneA = zoneA; this._zoneB = zoneB; } public function addEdge(edge:Edge):void{ this._edges.push(edge); } public function get zoneA():Zone{ return (this._zoneA); } public function set zoneA(zoneA:Zone):void{ this._zoneA = zoneA; } public function get zoneB():Zone{ return (this._zoneB); } public function set zoneB(zoneB:Zone):void{ this._zoneB = zoneB; } public function get hasBeenUsed():Boolean{ return (this._hasBeenUsed); } public function set hasBeenUsed(hasBeenUsed:Boolean):void{ this._hasBeenUsed = hasBeenUsed; } public function get edges():Vector.{ return (this._edges); } } }//package ca.struct.dungo package assets { import flash.display.*; public dynamic class GenerateButtonAsset extends MovieClip { } }//package assets package assets { import flash.text.*; import flash.display.*; public dynamic class StepInputAsset extends MovieClip { public var roomSizeTextField:TextField; public var generateButton:GenerateButtonAsset; public var rowsTextField:TextField; public var zonesTextField:TextField; public var colsTextField:TextField; } }//package assets package assets { import flash.text.*; import flash.display.*; public dynamic class StepLabelAsset extends MovieClip { public var textField:TextField; } }//package assets