22
33import Constants .Directions ;
44import Map .NonRoad .House ;
5+ import Map .NonRoad .*;
56import Car .Car ;
67import Map .Road .Road ;
78import Map .Road .TrafficLight ;
@@ -45,7 +46,7 @@ public Map generateMap() {
4546 int start = (int ) (Math .random () * length / 2 ) * 2 ;
4647 int width = (int ) (Math .random () * (length - start - 6 ) / 2 ) * 2 + 6 ;
4748
48- m .createHorizontalRoad (start , i , width , 2 , 4 );
49+ m .createHorizontalRoad (start , i , width , 2 , 1 );
4950 }
5051 }
5152
@@ -54,13 +55,11 @@ public Map generateMap() {
5455 int start = (int ) (Math .random () * length / 2 ) * 2 ;
5556 int width = (int ) (Math .random () * (length - start - 6 ) / 2 ) * 2 + 6 ;
5657
57- m .createVerticalRoad (i , start , width , 2 , 4 );
58+ m .createVerticalRoad (i , start , width , 2 , 1 );
5859 }
5960 }
6061 }
6162
62- removeIslands (m );
63-
6463 ArrayList <Road > roads = new ArrayList <Road >();
6564
6665 for (Tile [] a : grid ) {
@@ -70,6 +69,8 @@ public Map generateMap() {
7069 }
7170 }
7271 }
72+ if (!assignSpeeds (m , roads ))
73+ for (int i = 0 ; i < 30 ; i ++) System .out .println ("MAPGEN DONE GOOFED" );
7374
7475 for (int i = 0 ; i < cars ; i ++) {
7576 Road start , end ;
@@ -85,7 +86,106 @@ public Map generateMap() {
8586
8687 return m ;
8788 }
89+ private static Tile [] getAdjTiles (Tile [][] grid , Tile x ){
90+ Tile [] y = new Tile [4 ];
91+ if (!(x .getX () == 0 ))
92+ y [3 ] = grid [x .getX () - 1 ][x .getY ()];
93+ if (!(x .getY () == 0 ))
94+ y [0 ] = grid [x .getX ()][x .getY () - 1 ];
95+ if (!(x .getX () == grid .length - 1 ))
96+ y [1 ] = grid [x .getX () + 1 ][x .getY ()];
97+ if (!(x .getY () == grid [0 ].length - 1 ))
98+ y [2 ] = grid [x .getX ()][x .getY () + 1 ];
99+
100+
101+ return y ;
102+ }
103+ private boolean assignSpeeds (Map m , ArrayList <Road > roads ){
104+ int [][] speeds = new int [m .getLengthX ()][m .getLengthY ()];
105+ int count = 0 ;
106+ for (Road r : roads ){
107+
108+ Tile [] adjTiles = getAdjTiles (m .grid , m .grid [r .getX ()][r .getY ()]);
109+ Road start = null ;
110+ for (int i = 0 ; i < 4 ; i ++){
111+ if (adjTiles [i ] instanceof TrafficLight ){
112+ start = r ;
113+ }
114+ if (adjTiles [i ] == null || adjTiles [i ] instanceof NonRoad ){
115+ if (i == r .getDirection () || i == Math .abs (r .getDirection () - 4 )){
116+ start = r ;
117+ }
118+ }
119+ }
120+
121+ if (start != null ){
122+ System .out .println ("Start point XY:" + start .getX () + " " + start .getY ());
123+ int dir = start .getDirection ();
124+ int length = 1 ;
125+ Tile t = r ;
126+ System .out .println ("XY:" + t .getX () + " " + t .getY ());
127+
128+ // if(m.getInDir(r,dir) != null){
129+ t = m .getInDir (r , dir );
130+
131+
132+
133+ if (t instanceof TrafficLight || t == null || t instanceof NonRoad ){
134+
135+ int reverseDir = (dir + 2 > 3 )? dir - 2 : dir + 2 ;
136+ if (m .getInDir (r , reverseDir ) != null )
137+ t = m .getInDir (r , reverseDir );
138+
139+ while (!(t == null || t instanceof TrafficLight || t instanceof NonRoad )){
140+ System .out .println ("Count: " + count + "XY:" + t .getX () + " " + t .getY ());
88141
142+ length ++;
143+ if (m .getInDir (t , reverseDir ) != null )
144+ t = m .getInDir (t , reverseDir );
145+ else
146+ break ;
147+ }
148+ System .out .println ("Length:" + length );
149+ for (int i = 0 ; i < length ; i ++){
150+ t = m .getInDir (t , dir ); // reverse direction
151+ speeds [t .getX ()][t .getY ()] = determineSpeed (length );
152+ }
153+
154+ } else {
155+
156+ while (!(t == null || t instanceof TrafficLight || t instanceof NonRoad )){
157+ System .out .println ("Count: " + count + "XY:" + t .getX () + " " + t .getY ());
158+
159+ length ++;
160+ if (m .getInDir (t , dir ) != null )
161+ t = m .getInDir (t , dir );
162+ else
163+ break ;
164+ }
165+ int reverseDir = (dir + 2 > 3 )? dir - 2 : dir + 2 ;
166+ System .out .println ("Length:" + length );
167+
168+ for (int i = 0 ; i < length ; i ++){
169+ t = m .getInDir (t , reverseDir ); //reverse directions
170+ speeds [t .getX ()][t .getY ()] = determineSpeed (length );
171+ }
172+
173+ }
174+ }
175+ // }
176+ count ++;
177+ }
178+ for (Road r : roads ){
179+ r .setSpeed (speeds [r .getX ()][r .getY ()]);
180+ }
181+ return true ;
182+ }
183+ private int determineSpeed (int length ){
184+ if (length <= 6 ) return 1 ;
185+ if (length <= 10 ) return 2 ;
186+ return 3 ;
187+
188+ }
89189 private void removeIslands (Map m ) {
90190 int [][] dp = new int [length ][length ];
91191
@@ -188,7 +288,7 @@ public String toString() {
188288 }
189289
190290 public static void main (String [] args ) {
191- MapGenerator mapGen = new RandomMapGenerator (30 , 1 );
291+ MapGenerator mapGen = new RandomMapGenerator (10 , 1 );
192292
193293 System .out .println (mapGen .generateMap ());
194294 }
0 commit comments