001         package com.croftsoft.apps.mars.net;
002    
003         import java.awt.Color;
004         import java.io.*;
005    
006         import com.croftsoft.core.beans.XmlBeanCoder;
007         import com.croftsoft.core.lang.NullArgumentException;
008         import com.croftsoft.core.lang.Testable;
009    
010         import com.croftsoft.apps.mars.model.seri.SeriAmmoDump;
011    
012         /*********************************************************************
013         * Initializer for Mars Game.
014         *
015         * @version
016         *   2003-06-12
017         * @since
018         *   2003-04-30
019         * @author
020         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
021         *********************************************************************/
022    
023         public final class  GameInit
024           implements GameInitAccessor, Serializable, Testable
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028    
029         private static final long  serialVersionUID = 0L;
030    
031         //
032    
033         public static final double  DEFAULT_TIME_FACTOR         = 1.0;
034    
035         public static final long    DEFAULT_RANDOM_SEED         = 1968L;
036    
037         public static final double  DEFAULT_INITIAL_PLAYER_X    = 20.0;
038    
039         public static final double  DEFAULT_INITIAL_PLAYER_Y    = 20.0;
040    
041         public static final int     DEFAULT_AMMO_DUMPS          = 3;
042    
043         public static final int     DEFAULT_OBSTACLES           = 6;
044    
045         public static final int     DEFAULT_WORLD_WIDTH         = 600;
046    
047         public static final int     DEFAULT_WORLD_HEIGHT        = 400;
048    
049         public static final Color   DEFAULT_FRIEND_COLOR        = Color.BLUE;
050    
051         public static final Color   DEFAULT_ENEMY_COLOR         = Color.RED;
052    
053         public static final double  DEFAULT_TIME_DELTA_MAX      = 0.2;
054    
055         public static final int     DEFAULT_ATTEMPTS_MAX        = 10;
056    
057         public static final double  DEFAULT_OBSTACLE_RADIUS_MAX = 60.0;
058    
059         public static final double  DEFAULT_OBSTACLE_RADIUS_MIN = 10.0;
060    
061         public static final long    DEFAULT_PLAYER_TIMEOUT      = 10 * 1000;
062    
063         public static final double  DEFAULT_AMMO_DUMP_GROWTH
064           = SeriAmmoDump.Shared.DEFAULT_AMMO_GROWTH_RATE;
065    
066         public static final double  DEFAULT_AMMO_DUMP_MAX
067           = SeriAmmoDump.Shared.DEFAULT_AMMO_MAX;
068    
069         public static final double  DEFAULT_AMMO_DUMP_EXPLOSION
070           = SeriAmmoDump.Shared.DEFAULT_EXPLOSION_FACTOR;
071    
072         public static final double  DEFAULT_AMMO_DUMP_Z
073           = SeriAmmoDump.Shared.DEFAULT_Z;
074    
075         //
076    
077         private double               timeFactorDefault;
078    
079         private long                 randomSeed;
080    
081         private double               initialPlayerX;
082    
083         private double               initialPlayerY;
084    
085         private int                  ammoDumps;
086    
087         private int                  obstacles;
088    
089         private int                  worldWidth;
090    
091         private int                  worldHeight;
092    
093         private Color                friendColor;
094    
095         private Color                enemyColor;
096    
097         private double               timeDeltaMax;
098    
099         private int                  attemptsMax;
100    
101         private double               obstacleRadiusMax;
102    
103         private double               obstacleRadiusMin;
104    
105         private long                 playerTimeout;
106    
107         private double               ammoDumpGrowth;
108    
109         private double               ammoDumpMax;
110    
111         private double               ammoDumpExplosion;
112    
113         private double               ammoDumpZ;
114    
115         //////////////////////////////////////////////////////////////////////
116         // test methods
117         //////////////////////////////////////////////////////////////////////
118    
119         public static void  main ( String [ ]  args )
120           throws Exception
121         //////////////////////////////////////////////////////////////////////
122         {
123           if ( args.length > 0 )
124           {
125             createTemplateXmlFile ( args [ 0 ] );
126           }
127           else
128           {
129             System.out.println ( test ( args ) );
130           }
131         }
132    
133         public static boolean  test ( String [ ]  args )
134         //////////////////////////////////////////////////////////////////////
135         {
136           try
137           {
138             final int  TEST_WORLD_WIDTH = 2 * DEFAULT_WORLD_WIDTH;
139    
140             GameInit  gameInit1 = createDefaultGameInit ( );
141    
142             System.out.println (
143               "world width = " + gameInit1.getWorldWidth ( ) );
144    
145             gameInit1.setWorldWidth ( TEST_WORLD_WIDTH );
146    
147             ByteArrayOutputStream  byteArrayOutputStream
148               = new ByteArrayOutputStream ( );
149    
150             XmlBeanCoder.encodeAsXml ( gameInit1, byteArrayOutputStream );
151    
152             byte [ ]  xmlBytes = byteArrayOutputStream.toByteArray ( );
153    
154             System.out.println ( new String ( xmlBytes ) );
155    
156             GameInit  gameInit2
157               = ( GameInit ) XmlBeanCoder.decodeFromXml ( xmlBytes );
158    
159             System.out.println (
160               "world width = " + gameInit2.getWorldWidth ( ) );
161    
162             return gameInit2.equals ( gameInit1 );
163           }
164           catch ( Exception  ex )
165           {
166             ex.printStackTrace ( );
167    
168             return false;
169           }
170         }
171    
172         //////////////////////////////////////////////////////////////////////
173         // other static methods
174         //////////////////////////////////////////////////////////////////////
175    
176         public static GameInit  createDefaultGameInit ( )
177         //////////////////////////////////////////////////////////////////////
178         {
179           GameInit  gameInit = new GameInit ( );
180    
181           gameInit.setTimeFactorDefault  ( DEFAULT_TIME_FACTOR           );
182    
183           gameInit.setRandomSeed         ( DEFAULT_RANDOM_SEED           );
184    
185           gameInit.setInitialPlayerX     ( DEFAULT_INITIAL_PLAYER_X      );
186    
187           gameInit.setInitialPlayerY     ( DEFAULT_INITIAL_PLAYER_Y      );
188    
189           gameInit.setAmmoDumps          ( DEFAULT_AMMO_DUMPS            );
190    
191           gameInit.setObstacles          ( DEFAULT_OBSTACLES             );
192    
193           gameInit.setWorldWidth         ( DEFAULT_WORLD_WIDTH           );
194    
195           gameInit.setWorldHeight        ( DEFAULT_WORLD_HEIGHT          );
196    
197           gameInit.setFriendColor        ( DEFAULT_FRIEND_COLOR          );
198    
199           gameInit.setEnemyColor         ( DEFAULT_ENEMY_COLOR           );
200    
201           gameInit.setTimeDeltaMax       ( DEFAULT_TIME_DELTA_MAX        );
202    
203           gameInit.setAttemptsMax        ( DEFAULT_ATTEMPTS_MAX          );
204    
205           gameInit.setObstacleRadiusMax  ( DEFAULT_OBSTACLE_RADIUS_MAX   );
206    
207           gameInit.setObstacleRadiusMin  ( DEFAULT_OBSTACLE_RADIUS_MIN   );
208    
209           gameInit.setPlayerTimeout      ( DEFAULT_PLAYER_TIMEOUT        );
210    
211           gameInit.setAmmoDumpGrowth     ( DEFAULT_AMMO_DUMP_GROWTH      );
212    
213           gameInit.setAmmoDumpMax        ( DEFAULT_AMMO_DUMP_MAX         );
214    
215           gameInit.setAmmoDumpExplosion  ( DEFAULT_AMMO_DUMP_EXPLOSION   );
216    
217           gameInit.setAmmoDumpZ          ( DEFAULT_AMMO_DUMP_Z           );
218    
219           return gameInit;
220         }
221    
222         public static void createTemplateXmlFile ( String  filename )
223           throws IOException
224         //////////////////////////////////////////////////////////////////////
225         {
226           XmlBeanCoder.saveToXmlFile ( createDefaultGameInit ( ), filename );
227         }
228    
229         //////////////////////////////////////////////////////////////////////
230         // constructor methods
231         //////////////////////////////////////////////////////////////////////
232    
233         public  GameInit ( )
234         //////////////////////////////////////////////////////////////////////
235         {
236         }
237    
238         public  GameInit ( GameInitAccessor  gameInitAccessor )
239         //////////////////////////////////////////////////////////////////////
240         {
241           setTimeFactorDefault ( gameInitAccessor.getTimeFactorDefault  ( ) );
242    
243           setRandomSeed        ( gameInitAccessor.getRandomSeed         ( ) );
244    
245           setInitialPlayerX    ( gameInitAccessor.getInitialPlayerX     ( ) );
246    
247           setInitialPlayerY    ( gameInitAccessor.getInitialPlayerY     ( ) );
248    
249           setAmmoDumps         ( gameInitAccessor.getAmmoDumps          ( ) );
250    
251           setObstacles         ( gameInitAccessor.getObstacles          ( ) );
252    
253           setWorldWidth        ( gameInitAccessor.getWorldWidth         ( ) );
254    
255           setWorldHeight       ( gameInitAccessor.getWorldHeight        ( ) );
256    
257           setFriendColor       ( gameInitAccessor.getFriendColor        ( ) );
258    
259           setEnemyColor        ( gameInitAccessor.getEnemyColor         ( ) );
260    
261           setTimeDeltaMax      ( gameInitAccessor.getTimeDeltaMax       ( ) );
262    
263           setAttemptsMax       ( gameInitAccessor.getAttemptsMax        ( ) );
264    
265           setObstacleRadiusMax ( gameInitAccessor.getObstacleRadiusMax  ( ) );
266    
267           setObstacleRadiusMin ( gameInitAccessor.getObstacleRadiusMin  ( ) );
268    
269           setPlayerTimeout     ( gameInitAccessor.getPlayerTimeout      ( ) );
270    
271           setAmmoDumpGrowth    ( gameInitAccessor.getAmmoDumpGrowth     ( ) );
272    
273           setAmmoDumpMax       ( gameInitAccessor.getAmmoDumpMax        ( ) );
274    
275           setAmmoDumpExplosion ( gameInitAccessor.getAmmoDumpExplosion  ( ) );
276    
277           setAmmoDumpZ         ( gameInitAccessor.getAmmoDumpZ          ( ) );
278         }
279    
280         //////////////////////////////////////////////////////////////////////
281         // accessor methods
282         //////////////////////////////////////////////////////////////////////
283    
284         public double  getTimeFactorDefault ( ) { return timeFactorDefault; }
285    
286         public long    getRandomSeed        ( ) { return randomSeed;        }
287    
288         public double  getInitialPlayerX    ( ) { return initialPlayerX;    }
289    
290         public double  getInitialPlayerY    ( ) { return initialPlayerY;    }
291    
292         public int     getAmmoDumps         ( ) { return ammoDumps;         }
293    
294         public int     getObstacles         ( ) { return obstacles;         }
295    
296         public int     getWorldWidth        ( ) { return worldWidth;        }
297    
298         public int     getWorldHeight       ( ) { return worldHeight;       }
299    
300         public Color   getFriendColor       ( ) { return friendColor;       }
301    
302         public Color   getEnemyColor        ( ) { return enemyColor;        }
303    
304         public double  getTimeDeltaMax      ( ) { return timeDeltaMax;      }
305    
306         public int     getAttemptsMax       ( ) { return attemptsMax;       }
307    
308         public double  getObstacleRadiusMax ( ) { return obstacleRadiusMax; }
309    
310         public double  getObstacleRadiusMin ( ) { return obstacleRadiusMin; }
311    
312         public long    getPlayerTimeout     ( ) { return playerTimeout;     }
313    
314         public double  getAmmoDumpGrowth    ( ) { return ammoDumpGrowth;    }
315    
316         public double  getAmmoDumpMax       ( ) { return ammoDumpMax;       }
317    
318         public double  getAmmoDumpExplosion ( ) { return ammoDumpExplosion; }
319    
320         public double  getAmmoDumpZ         ( ) { return ammoDumpZ;         }
321    
322         //////////////////////////////////////////////////////////////////////
323         // mutator methods
324         //////////////////////////////////////////////////////////////////////
325    
326         public void  setTimeFactorDefault ( double  timeFactorDefault )
327         //////////////////////////////////////////////////////////////////////
328         {
329           this.timeFactorDefault = timeFactorDefault;
330         }
331    
332         public void  setRandomSeed ( long  randomSeed )
333         //////////////////////////////////////////////////////////////////////
334         {
335           this.randomSeed = randomSeed;
336         }
337    
338         public void  setInitialPlayerX ( double  initialPlayerX )
339         //////////////////////////////////////////////////////////////////////
340         {
341           this.initialPlayerX = initialPlayerX;
342         }
343    
344         public void  setInitialPlayerY ( double  initialPlayerY )
345         //////////////////////////////////////////////////////////////////////
346         {
347           this.initialPlayerY = initialPlayerY;
348         }
349    
350         public void  setAmmoDumps ( int  ammoDumps )
351         //////////////////////////////////////////////////////////////////////
352         {
353           this.ammoDumps = ammoDumps;
354         }
355    
356         public void  setObstacles ( int  obstacles )
357         //////////////////////////////////////////////////////////////////////
358         {
359           this.obstacles = obstacles;
360         }
361    
362         public void  setWorldWidth ( int  worldWidth )
363         //////////////////////////////////////////////////////////////////////
364         {
365           this.worldWidth = worldWidth;
366         }
367    
368         public void  setWorldHeight ( int  worldHeight )
369         //////////////////////////////////////////////////////////////////////
370         {
371           this.worldHeight = worldHeight;
372         }
373    
374         public void  setFriendColor ( Color  friendColor )
375         //////////////////////////////////////////////////////////////////////
376         {
377           NullArgumentException.check ( this.friendColor = friendColor );
378         }
379    
380         public void  setEnemyColor ( Color  enemyColor )
381         //////////////////////////////////////////////////////////////////////
382         {
383           NullArgumentException.check ( this.enemyColor = enemyColor );
384         }
385    
386         public void  setTimeDeltaMax ( double  timeDeltaMax )
387         //////////////////////////////////////////////////////////////////////
388         {
389           this.timeDeltaMax = timeDeltaMax;
390         }
391    
392         public void  setAttemptsMax ( int  attemptsMax )
393         //////////////////////////////////////////////////////////////////////
394         {
395           this.attemptsMax = attemptsMax;
396         }
397    
398         public void  setObstacleRadiusMax ( double  obstacleRadiusMax )
399         //////////////////////////////////////////////////////////////////////
400         {
401           this.obstacleRadiusMax = obstacleRadiusMax;
402         }
403    
404         public void  setObstacleRadiusMin ( double  obstacleRadiusMin )
405         //////////////////////////////////////////////////////////////////////
406         {
407           this.obstacleRadiusMin = obstacleRadiusMin;
408         }
409    
410         public void  setPlayerTimeout ( long  playerTimeout )
411         //////////////////////////////////////////////////////////////////////
412         {
413           this.playerTimeout = playerTimeout;
414         }
415    
416         public void  setAmmoDumpGrowth ( double  ammoDumpGrowth )
417         //////////////////////////////////////////////////////////////////////
418         {
419           this.ammoDumpGrowth = ammoDumpGrowth;
420         }
421    
422         public void  setAmmoDumpMax ( double  ammoDumpMax )
423         //////////////////////////////////////////////////////////////////////
424         {
425           this.ammoDumpMax = ammoDumpMax;
426         }
427    
428         public void  setAmmoDumpExplosion ( double  ammoDumpExplosion )
429         //////////////////////////////////////////////////////////////////////
430         {
431           this.ammoDumpExplosion = ammoDumpExplosion;
432         }
433    
434         public void  setAmmoDumpZ ( double  ammoDumpZ )
435         //////////////////////////////////////////////////////////////////////
436         {
437           this.ammoDumpZ = ammoDumpZ;
438         }
439    
440         //////////////////////////////////////////////////////////////////////
441         // overridden object methods
442         //////////////////////////////////////////////////////////////////////
443    
444         public boolean  equals ( Object  other )
445         //////////////////////////////////////////////////////////////////////
446         {
447           if ( other == null )
448           {
449             return false;
450           }
451    
452           if ( other.getClass ( ) != getClass ( ) )
453           {
454             return false;
455           }
456    
457           GameInit  otherGameInit = ( GameInit ) other;
458    
459           return
460                ( timeFactorDefault == otherGameInit.timeFactorDefault )
461             && ( randomSeed        == otherGameInit.randomSeed        )
462             && ( initialPlayerX    == otherGameInit.initialPlayerX    )
463             && ( initialPlayerY    == otherGameInit.initialPlayerY    )
464             && ( ammoDumps         == otherGameInit.ammoDumps         )
465             && ( obstacles         == otherGameInit.obstacles         )
466             && ( worldWidth        == otherGameInit.worldWidth        )
467             && ( worldHeight       == otherGameInit.worldHeight       )
468             && friendColor.equals ( otherGameInit.friendColor )
469             && enemyColor.equals  ( otherGameInit.enemyColor  )
470             && ( timeDeltaMax      == otherGameInit.timeDeltaMax      )
471             && ( attemptsMax       == otherGameInit.attemptsMax       )
472             && ( obstacleRadiusMax == otherGameInit.obstacleRadiusMax )
473             && ( obstacleRadiusMin == otherGameInit.obstacleRadiusMin )
474             && ( playerTimeout     == otherGameInit.playerTimeout     )
475             && ( ammoDumpGrowth    == otherGameInit.ammoDumpGrowth    )
476             && ( ammoDumpMax       == otherGameInit.ammoDumpMax       )
477             && ( ammoDumpExplosion == otherGameInit.ammoDumpExplosion )
478             && ( ammoDumpZ         == otherGameInit.ammoDumpZ         );
479         }
480    
481         //////////////////////////////////////////////////////////////////////
482         //////////////////////////////////////////////////////////////////////
483         }