Week9

http://www.halcyon.com/amalmin/java.html

Note: This weeks code won't run off the web. It is no longer an applet. Download the code and the two GIF files available on the week6 page, compile it, and run it with the appletviewer if you want to see it work. In the future I hope to be able to inbed the graphics into one of the class files...

/*****************************************************************************/
/*                             Acts of God(esses)                            */
/*****************************************************************************/
/* By Audin Malmin (amalmin@halcyon.com),                                    */
/*    Chris Corwin (),                                                       */
/*    and Onder Ceyhun ().                                                   */
/*                                                                           */
/* Copywrite (c) 1998 Audin Malmin, Ondar Ceyhun, and Chris Corwin.          */
/* This code is made available under the GNU licence.                        */
/*                                                                           */
/* If you make use of this code please drop us a line.                       */
/*****************************************************************************/
/*****************************************************************************/
/* History                                                                   *
 *****************************************************************************
 * ANM	5-7-98	Added splashscreen.
 *              Explicit Frame creation.  Can't get the damned thing to close,
 *                  though.
 *              no more applet code.
 *		5-8-98	More work on exiting properly...
 *				Animated splash screen.
 *				Zapped one un-used frame.
 *		5-9-98	DrawPanel renamed MainPanel.
 *				More fiddling with the splash screen.
 *				General code cleanup.
 * ANM	5-13-98	New movement code.  (the dudes now use a modal movement method.)
 * & CC	6 hour	Better Cathedral display.
 * & OC	block	Cathedral "area of influance" or Holy Ground displayed.
 *				LocalDude reproduction implimented.  (PopulationExplosion thread)
 *				Added menubar code.
 * ANM	5-17-98	Cathedral.isCathedral(), Cathedral.isCathZone(), expanded Cathedral.test().
 *				LocalUtil.waitforimg()
 *				Cathedral constructior now waits for image to load befor displaying it.
 *				Source code split up: Colony, LocalUtil, Splash, AboutDialog
 *				Window title.
 * ANM	5-20-98	Enhanced cathedral code.
 * & CC			Enhanced PopulationExplosion
 *				Implimented "Popup-Zap"...which just kills a dude (removing him from the list.)
 * ANM	5-21-98	Network code.
 * & CC			Network code.
 * & OC			Network code.
 * ANM	5-22-98 More network code work.
 * ANM	5-23-98	Network code finally works!!!!!!!!!
 */
/*
 * Audin Malmin
 * 5-4-98
 * Java/OS week 7 (maybe) (I've lost count).
 *
 * This week has seen some major work.  I started out on my own by fiddling with the animation
 * code.  However, this work was not at all successful, so we moved back to the original code
 * when we made further changes.  This is why you won't see it mentioned in the history.
 *
 * When we all got together the first thing we did was splice in Chris's menubar code.  Then
 * we set ourselves to the task of writing real Dude movement code.  The movement code that
 * has been used up until now was only written for debugging purposes.  This original code
 * wasn't nearly smart enough to do even half of what we asked of it.  The new code is far
 * more capable.  It also took several hours of thought and a few hours of coding to produce.
 *
 * After we got the new movement code working, we tackeled the Cathedral code.  This operation
 * was basically the same: replacing temp. code with final production code.  First we put in
 * the "sphere of influance" code.  (This required some other changes...as the influance of a
 * cathedral is based on it's design, which is based on the size of the population which
 * build it.)  Along with this we added support for 5 different cathedral images, each with
 * different spheres of influance.
 *
 * Finally, around 10:00pm we started work on Dude reproduction.  This functionality is
 * encased in the PopulationExplosion thread.
 *
 * At a later date I expanded the Cathedral code a bit, adding functions to test if a point
 * was inside both the cathedral structure itself and if the point was inside the cathedral's
 * zone of influance.  I also added a LocalUtil.waitforimg() function, which is used in the
 * cathedral constructor to pause while the image loads...so as not to let it strobe into
 * existance in the main window.
 *
 * At this time I also worked on the splash screen a bit...it now consistently comes up BEFOR
 * the main program window (it kept popping up after the main window in my 6x86MX200)...
 *
 * This evening also brought the first source code split.  The new source files: LocalUtil,
 * Splash, and AboutDialog were created.  The main source file was renamed "Colony" (from
 * "ColonyApplet"), since the "Applet" part no longer fit.
 *
 * Finally I played around with the javadoc program a bit...and determined that I don't much
 * care for it...  It seems to (at least based on the JDK API documentation) encourage poor
 * documentation of code.  (ie: the JDK documentation is just about as terse as one can possibly
 * get.  Function descriptions that provide no more information than the function name itself
 * are not at all helpful.)  And after seeing just how ugly the comments make the source code,
 * I can see why the JDK docs are so bad...  Javadoc is no replacement for normal, manually
 * written, professional documentation.
 *
 * Current layout (has-a relationships):
 *
 * ColonyApplet --+
 *  |      |      |
 *  |      |  MainPanel --+--------+---------+---------------+-------------+-----+-------+
 *  |      |              |        |         |               |             |     |       |
 *  |   Splash*        LocalDL RemoteDL   LocalCL        %RemoteCL 	       |     |   PopupMenu
 *  |      |            |   |    |   |     |  |            |    |          |     |
 *  |    Window        Dude |   Dude |     | CathUpdate*%  |  CathUpdate*% | PopupMenu
 *  |      |                |        |     |               |               |
 *  |  ImgCanvas        DudeUpdate*  | Cathedral       Cathedral%          |
 *  |   |     |                      |                                     |
 *  | Image Label               DudeUpdate*                       PopulationExplosion*
 *  |
 * MenuBar
 *  |    |
 * Menu Menu
 *
 * Key:		*: thread		%: Not yet implimented
 *
 * (is-a relationships):
 *
 * Frame -> ColonyApplet	: The overall program object.  Contains main(), and one
 *                            MainPanel object (which does all the work).
 *
 * Thread-> Splash			: The splash screen thread.  It just throws up a borderless window
 *                            and paints a graphic in it.  It then sleeps for 10 seconds, after
 *                            which time it just zaps the window and exits.
 *
 * Frame -> MainPanel		: The heavy working object...Does everything.
 *                            The main event loop is here...
 *
 * Menu -> PopupMenu		: Just a normal multi-level popup menu.
 *
 * LocalDL -> RemoteDL
 *
 * LocalCL -> RemoteCL	(At least in the future)
 *
 * Thread  -> PopulationExplosion
 *
 * Thread  -> DudeUpdate
 *
 * Thread  -> CathUpdate
 *
 * So basically, MainPanel is the main program loop.  It sets everything up and
 * handles events.  MainPanel contains two popup menus, two CL objects, two DL objects,
 * and one PopulationExplosion thread.
 *
 * LocalDL is kind of a mutant linked list container.  It does contain a linked list
 * of Dude objects.  In addition it contains a DudeUpdate object.
 *
 * RemoteDL is derived from LocalDL.  It is identical except for the fact that, in the future,
 * it RemoteDL will get it's movement, creation, and destruction information from the remote
 * machine.
 *
 * Dude is the basic dude object.  It contains the dudes color, position, a move() function,
 * a test(x,y) function and a few other little functions.  The most interesting function is
 * move():
 *
 * A dude has two states which it can be in: moving and non-moving.  Everyone starts out as
 * non-moving.  This means that when the move() function is called the dude goes through a
 * little routine in which it decides a) if it WANTS to move, b) which direction it wants to
 * go, and c) how far it wants to go.  If it completes these three steps correctly then it
 * changes it's state to moving.  After figuring out the desination pixel, the movement
 * vecter needed to get there and the number of steps required, the move() function then
 * returns.  On the next call, the MOVING part of the move() function gets called, which
 * actually moves the dude one step, and decrements the total-movement variable.  When the
 * total-movements variable is 0, the dude sets itself back to NON-MOVING and the process can
 * start again.  The final resting place may not in fact be the exact Destination Point, but
 * it will be within 1 or 2 pixels.  In the future a call will be added to the decision making
 * process to test the Destination Point against a list of Dangerous Areas.  The probability
 * of the dude deciding to move into a dangerous area will be less than the probability of
 * moving into a friendly area.
 *
 * DudeUpdate is a thread.  It's job is to animate each of the Dudes.  Basically it
 * runs through the linked list on it's own (it gets a pointer to the "first" Dude
 * object), calling the Dude.move() function for each one.  The DudeUpdate constructor
 * also gets a pointer to the main MainPanel object, so that it can get ahold of the
 * Graphics Context and the background color.
 *
 * LocalCL is another bastard linked list container.  It contains a list of Cathedral objects.
 * RemoteCL will be derived from it; again adding a network interfact.
 *
 * Cathedral contains an image, a position at which to display the image, and a influance
 * variable which controls the size of the influance circle.  In the future I plan to have it
 * animate a small string of images (this depends on our having time to come up with images
 * to animate, though)...  The type of cathedral created depends on the size of the local
 * population at the time of the CL.add() call.  This population size also controls the area
 * of influance of the cathedral (which in turn affects the overall power of the player as
 * the God).  Finally the class contains a find(x,y) function, which returns true if the
 * point defined is within the cathedral image boundary.  Still to be added is an
 * influance(x,y) function which will return either a boolean based on whether the point is
 * within the influance circle, or (this would be neater) an int based on how close the point
 * is to the very center of the influance circle.  A god would therefor grow in power as it
 * approached one of it's temples.
 *
 * PopulationExplosion is a thread that takes care of reproduction and cathedral creation.
 * Basically it is a perpetual loop.  At the top of the loop is a Sleep, whos time is based on
 * the size of the population.  After the sleep is a for loop which increases the population
 * size by 10%.  The sleep time is based on population so that we don't end up with an ehtirely
 * exponental population explosion.  The sleep time grows as the population grows.  It's
 * basically 1 second delay per dude.  This seems to work pretty well.  Although I have the
 * feeling that this will be increased in the future.
 *
 * So what does it all do???????????????
 *
 * * It compiles and runs.
 *
 * * It displays a splash screen for a few seconds, during which time the rest of the program
 *   loads and sets itself up.  (The splash screen is in the forground, so you can watch the
 *   main program window set itself up in the background.)
 *
 * * Optionally, the splash screen can slide around the screen, bouncing off the edges of the
 *   monitor.  This doesn't work very well, though..at least on my mid-range PCI video
 *   hardware (it flickers quite a bit).
 *
 * * Two cathedrals are created and displayed.  These are currently created with temp. code.
 *   PopulationExplosion should be creating these...but it isn't.
 *
 * * 10 dudes are created automatically by the PopulationExplosion constructor.  PE then goes
 *   on to run in the background, creating new dudes every once in a while.
 *
 * * Left-clicking on the screen will create one red Dude directly under the mouse
 *   cursor, and one blue Dude will be created randomly somewhere else.  This will probably
 *   have been removed by the time anyone reads this.
 *
 * * The Dudes (only local dudes are created automatically) will move around the window as
 *   befor, except the movement pattern has changed drastically.  Also Dudes now have the
 *   ability to stand still (which they do a quite a bit, to save CPU cycles).
 *
 * * The Dudes will vary slightly in color.  (Ie: all red dudes are red, but some are
 *   brighter and some are darker.)
 *
 * * Right-clicking on an unoccupied section of screen you will get a popup menu
 *   with a list of acts you can perform on the general area.  These acts will have
 *   a circle of influance on which they will be active.  They will also most likely
 *   "taint" the area with an aura of fear.  (ie: the Dudes should try to stay away
 *   from these areas.)  UPDATE: the new movement code is designed to make this easy to
 *   impliment.
 *
 * * Right-clicking in a small area around a Dude will give you a popup menu with both
 *   the normal options and acts you can inflict on the Dude itself.  The menu also
 *   gives you the Dude's number.  Currently the only working option is "Mutate", which
 *   causes the dude to double in size.
 *
 * * Right-clicking on a cathedral will try to identify the cathedral.  In the future it will
 *   bring up a menu with cathedral options.
 *
 * So what doesn't it do???
 *
 * * You can't currently kill a Dude.  This isn't hard to add, though.
 *
 * * There's a problem with erasing the first drawing of a Dude...so you get some
 *   fluff left over on the background.  UPDATE: The problem has finally been discovered, but
 *   I have yet to fix it.
 *
 * * The above fix will not, however, solve the repainting problem.  I don't know how to fix
 *   this problem without using Double Buffering...which I don't want to get into.  So it will
 *   probably never be fixed.
 *
 *
 * Wow, you really read this far?  I'm stunned....  :-)
 */
 

You'll need all 8 of the files below to compile and run the code. You will also need to start up two instances of the program and network them together...

ie: start the first copy of the program, and set it to "server"...(File->NetworkGame->Server)

then start the second copy, select File->NetworkGame->Client, the program will then ask you for the server's address...(on the console window)...just leave it blank and hit Enter to use the default loop-back address... The game should then start up.

The source code.

The source code.

The source code.

The source code.

The source code.

The source code.

The source code.

The source code.


Audin Malmin