home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / server / battlegame / piper.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  4.8 KB  |  178 lines

  1. /*
  2.  * @(#)Piper.java
  3.  */
  4. package games.Battle.server.BattleGame;
  5.  
  6. import java.lang.*;
  7. import java.io.*;
  8.  
  9. import games.Battle.shared.comm.*;
  10. import games.Battle.shared.sys.*;
  11. import games.Battle.server.ServerBoard.*;
  12.  
  13. /** 
  14.  * Piper watches the client for input and executes the client's requests for
  15.  * pipes, guns, paratroops, and whatever else the client wants to do.
  16.  *
  17.  * @version 1.00
  18.  * @author Alex Nicolaou
  19.  * @author Jay Steeele
  20.  */
  21.  
  22. class Piper implements Runnable {
  23.     /**
  24.      * the thread that the piper uses to run itself
  25.      */
  26.     Thread piperThread;
  27.  
  28.     /**
  29.      * the board that the player is playing on
  30.      */
  31.     ServerBoard board = null;
  32.     /**
  33.      * the stream of input from the player
  34.      */
  35.     InputStream is = null;
  36.     /**
  37.      * the game id of the game the player is playing
  38.      */
  39.     int gameId;
  40.     /**
  41.      * the number of the player in this game
  42.      */
  43.     int p;
  44.  
  45.     /**
  46.      * the log service
  47.      */
  48.     Logger logfile;
  49.  
  50.     /**
  51.      * constructs a piper object, and starts a thread in it to run it.
  52.      * @param board the board that the player is playing on
  53.      * @param is the input stream frmo the player
  54.      * @param g the game id of the game the player is in
  55.      * @param playernum the number of the player
  56.      * @param l the log file service
  57.      */
  58.     Piper(ServerBoard board, InputStream is, int g, int playernum, Logger l) {
  59.         this.board = board;
  60.         this.is = is;
  61.         this.logfile = l;
  62.         gameId = g;
  63.         p = playernum;
  64.         piperThread = new Thread(this);
  65.         piperThread.start();
  66.     }
  67.  
  68.     /**
  69.      * The run method for this thread. Waits for commands,
  70.      * verifies their validity, and executes them.
  71.      */
  72.     public void run() {
  73.         try {
  74.             while (true) {
  75.                 Command cmd = new Command();
  76.                 try {
  77.                     cmd.readFrom(is);
  78.                 } catch (Exception e) {
  79.                     logfile.log("Game #"+gameId+" - player "+p+" vanished.");
  80.                     return;
  81.                 }
  82.  
  83.                 if (cmd.getId() >= Symbols.NORTH_PIPE && 
  84.                     cmd.getId() <= Symbols.WEST_PIPE) {
  85.  
  86.                     Cell cell = board.getCell(cmd.getRow(), cmd.getCol());
  87.                     if (cell.getOccupancy() == Symbols.PLAYER_SYMBOL[p]) {
  88.                         if (cell.getPipe(cmd.getId())) {
  89.                             cell.setPipe(cmd.getId(), false);
  90.                         } else {
  91.                             cell.setPipe(cmd.getId(), true);
  92.                         }
  93.                     }
  94.                 }
  95.                 else if (cmd.getId() == Symbols.CLEAR_PIPES) {
  96.                     Cell cell = board.getCell(cmd.getRow(), cmd.getCol());
  97.                     if (cell.getOccupancy() == Symbols.PLAYER_SYMBOL[p]) {
  98.                         cell.clearPipes();
  99.                     }
  100.                 }
  101.                 else if (cmd.getId() == Symbols.GUN) {
  102.                     // first confirm that the source is owned by this player
  103.                     int srcr = cmd.getRow();
  104.                     int srcc = cmd.getCol();
  105.                     int dstr = cmd.getX();
  106.                     int dstc = cmd.getY();
  107.                     int offr = srcr - dstr;
  108.                     int offc = srcc - dstc;
  109.                     ServerCell src = (ServerCell)board.getCell(srcr, srcc);
  110.                     if (src.getOccupancy() == Symbols.PLAYER_SYMBOL[p]
  111.                         && Math.abs(offr) <= Rules.gunRange
  112.                         && Math.abs(offc) <= Rules.gunRange) 
  113.                     {
  114.                         // we have a valid gun command, so start a-gunning
  115.                         try {
  116.                             ServerCell dest = (ServerCell)board.getCell(dstr, dstc);
  117.                             if (src.getTroops() > 15 
  118.                                 && dest.getOccupancy() != Symbols.UNOCCUPIED) 
  119.                             {
  120.                                 int numSent = dest.artillery(src.getOccupancy(), 1);
  121.                                 src.setTroops(src.getTroops() - numSent);
  122.  
  123.                                 if (src.getTroops() <= 0)
  124.                                     src.setTroops(1);
  125.                             }
  126.                         }
  127.                         catch (Exception e) {}
  128.                     }
  129.                 }
  130.                 else if (cmd.getId() == Symbols.PARATROOP) {
  131.                     // first confirm that the source is owned by this player
  132.                     int srcr = cmd.getRow();
  133.                     int srcc = cmd.getCol();
  134.                     int dstr = cmd.getX();
  135.                     int dstc = cmd.getY();
  136.                     int offr = srcr - dstr;
  137.                     int offc = srcc - dstc;
  138.                     ServerCell src = (ServerCell)board.getCell(srcr, srcc);
  139.                     if (src.getOccupancy() == Symbols.PLAYER_SYMBOL[p]
  140.                         && Math.abs(offr) <= Rules.parRange
  141.                         && Math.abs(offc) <= Rules.parRange) 
  142.                     {
  143.                         // we have a valid paratroop command so start a-paratroopin
  144.                         try {
  145.                             ServerCell dest = (ServerCell)board.getCell(dstr, dstc);
  146.                             if (src.getTroops() > 15)
  147.                             {
  148.                                 int numSent = dest.paratroop(src.getOccupancy(), 1);
  149.                                 src.setTroops(src.getTroops() - numSent);
  150.  
  151.                                 if (src.getTroops() <= 0)
  152.                                     src.setTroops(1);
  153.                             }
  154.                         }
  155.                         catch (Exception e) {}
  156.                     }
  157.                 }
  158.                 else if (cmd.getId() == Symbols.SURRENDER) {
  159.                     logfile.log("Player "+Symbols.PLAYER_SYMBOL[p]+" Surrendered.");
  160.                     board.killPlayer(Symbols.PLAYER_SYMBOL[p]);
  161.  
  162.                     // stop the piper thread, the player is no longer allowed
  163.                     // to continue playing
  164.                     piperThread.stop();
  165.                 }
  166.                 else {
  167.                     logfile.log("Command ID: " + cmd.getId() + " not used!");
  168.                     logfile.log("** " + cmd);
  169.                 }
  170.             }
  171.         }
  172.         catch (Exception e) {
  173.             logfile.log("PIPER DIED: Exception: "+e.toString());
  174.         }
  175.     }
  176.  
  177. }
  178.