home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0109.zip / Timur / mech.c < prev    next >
Text File  |  1993-06-07  |  3KB  |  105 lines

  1. /* MECH.C - routines to control all the BattleMechs
  2.  
  3. Copyright (c) 1992-1993 Timur Tabi
  4. Copyright (c) 1992-1993 Fasa Corporation
  5.  
  6. The following trademarks are the property of Fasa Corporation:
  7. BattleTech, CityTech, AeroTech, MechWarrior, BattleMech, and 'Mech.
  8. The use of these trademarks should not be construed as a challenge to these marks.
  9.  
  10. */
  11.  
  12. #define INCL_WINWINDOWMGR
  13. #define INCL_GPIBITMAPS
  14. #include <os2.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17.  
  18. #define MECH_C
  19.  
  20. #include "header.h"
  21. #include "errors.h"
  22. #include "resource.h"
  23. #include "dialog.h"
  24. #include "hexes.h"
  25. #include "mech.h"
  26. #include "target.h"
  27. #include "bitmap.h"
  28. #include "window.h"
  29.  
  30. static void ShowPosition(void) {
  31. /* This function updates the "position" field in the Info Box
  32. */
  33.   char sz[20];
  34.  
  35.   sprintf(sz,"(%i,%i)",mech.hi.c,mech.hi.r);
  36.   WinSetDlgItemText(hwndInfoBox,IDD_POSITION,sz);
  37. }
  38.  
  39. ERROR MechInit(void) {
  40. /* This function initializes the MECH module and loads all the mech bitmaps.
  41. */
  42.   mech.hi.c=0;
  43.   mech.hi.r=0;
  44.   mech.iDirection=HEXDIR_NORTH;
  45.   RETURN(BitmapLoad(IDB_MECH_SE,&mech.hbm[0]));
  46.   RETURN(BitmapLoad(IDB_MECH_NE,&mech.hbm[1]));
  47.   RETURN(BitmapLoad(IDB_MECH_N,&mech.hbm[2]));
  48.   RETURN(BitmapLoad(IDB_MECH_NW,&mech.hbm[3]));
  49.   RETURN(BitmapLoad(IDB_MECH_SW,&mech.hbm[4]));
  50.   RETURN(BitmapLoad(IDB_MECH_S,&mech.hbm[5]));
  51.   ShowPosition();
  52.   return ERR_NOERROR;
  53. }
  54.  
  55. void MechErase(void) {
  56. /* Erases the current 'Mech from the screen.
  57. */
  58.   HPS hps=WinGetPS(hwndClient);
  59.  
  60.   HexFillDraw(hps,mech.hi);
  61.   WinReleasePS(hps);
  62. }
  63.  
  64. void MechDraw(void) {
  65. /* Draws the current 'Mech.
  66. */
  67.   POINTL ptl;
  68.  
  69.   ptl=HexCoord(mech.hi);
  70.   ptl.x-=HEX_EXT;
  71.  
  72.   BitmapDraw(mech.hbm[mech.iDirection],hbmHexMask,ptl);
  73. }
  74.  
  75.  
  76. void MechMove(HEXINDEX hi) {
  77. /* This routine moves the 'Mech to position 'hi', if that position is adjacent
  78.    to the 'Mech's current position.
  79.    Future enhancement: allow the mech to change direction only by 60 degrees each turn.
  80. */
  81.   int dx=hi.c-mech.hi.c;
  82.   int dy=hi.r-mech.hi.r;
  83.   int iDir;                            // Direction from mech.hi to hi
  84.  
  85.   if (abs(dx) > 1) return;             // +/- one column?
  86.   if (abs(dy) > 2) return;             // +/- two rows?
  87.   if HI_EQUAL(hi,mech.hi) return;      // same row/column?
  88.  
  89. // Calculate direction based on dx and dy
  90.   switch (dy) {
  91.     case -2: iDir=5; break;
  92.     case -1: iDir= dx>0 ? 0 : 4; break;
  93.     case 1:  iDir= dx>0 ? 1 : 3; break;
  94.     case 2:  iDir=2; break;
  95.   }
  96.  
  97.   if (iDir==mech.iDirection) {         // Are we already facing the way we want to go?
  98.     MechErase();                       // Yes, we can move that way
  99.     mech.hi=hi;
  100.     ShowPosition();
  101.   } else
  102.     mech.iDirection=iDir;              // No, let's turn to that direction instead
  103.   MechDraw();
  104. }
  105.