home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / wizchred.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  21.1 KB  |  632 lines

  1. #include        <quickdraw.h>
  2. #include        <menu.h>
  3. #include        <window.h>
  4. #include        <dialog.h>
  5. #include        <textedit.h>
  6. #include        <event.h>
  7. #include        <desk.h>
  8. #include        <control.h>
  9. #include        <inits.h>
  10. #include        <package.h>
  11. #include        <pb.h>
  12. #include        <syserr.h>
  13. #include        <resource.h>
  14.  
  15. #define maxControls     10
  16. #define sumControls     50
  17.  
  18. #define windowBase      200
  19. #define dialogBase      300
  20. #define menuBase        400
  21. #define controlBase     500
  22.  
  23.  
  24. #define numWindows      5
  25.  
  26. #define raceWind        0
  27. #define classWind       1
  28. #define statusWind      2
  29. #define alignWind       3
  30. #define statistWind     4
  31.  
  32.  
  33. #define numRace         6
  34.  
  35. #define norace          0
  36. #define human           1
  37. #define elf             2
  38. #define dwarf           3
  39. #define gnome           4
  40. #define hobbit          5
  41.  
  42.  
  43. #define numClass        8
  44.  
  45. #define fighter         0
  46. #define mage            1
  47. #define priest          2
  48. #define thief           3
  49. #define bishop          4
  50. #define samurai         5
  51. #define lord            6
  52. #define ninja           7
  53.  
  54.  
  55. #define numStatus       8
  56.  
  57. #define healthy         0
  58. #define afraid          1
  59. #define asleep          2
  60. #define paralysed       3
  61. #define stoned          4
  62. #define dead            5
  63. #define ashes           6
  64. #define lost            7
  65.  
  66.  
  67. #define numAlign        4
  68.  
  69. #define noalign         0
  70. #define nice            1
  71. #define neutral         2
  72. #define evil            3
  73.  
  74.  
  75. #define numStatist      10
  76.  
  77. #define Strength        0
  78. #define Intelligence    1
  79. #define Piety           2
  80. #define Vitality        3
  81. #define Agility         4
  82. #define Luck            5
  83. #define Gold            6
  84. #define Experience      7
  85. #define ActHits         8
  86. #define MaxHits         9
  87.  
  88.  
  89. #define numDialogs      5
  90.  
  91. #define aboutDialog     0
  92. #define saveDialog      1
  93. #define charDialog      2
  94. #define opWrDialog      3
  95. #define ioErrDialog     4
  96.  
  97.  
  98. #define saveItem        1
  99. #define discardItem     2
  100. #define cancelItem      3
  101.  
  102.  
  103. #define numMenus        3
  104.  
  105. #define apMenu          0
  106. #define fileMenu        1
  107. #define charMenu        2
  108.  
  109.                                         /* Start of character storage info */
  110.  
  111. #define charBlock       186             /* size of storage in file for each character */
  112.  
  113. #define nameLength      0
  114. #define charName        1
  115. #define raceLoc         33
  116. #define classLoc        34
  117. #define statusLoc       38
  118. #define alignLoc        39
  119. #define strengthLoc     40
  120. #define IQLoc           41
  121. #define pietyLoc        42
  122. #define vitalityLoc     43
  123. #define agilityLoc      44
  124. #define luckLoc         45
  125. #define goldLoc         54
  126. #define experienceLoc   106
  127. #define actHitsLoc      114
  128. #define maxHitsLoc      116
  129.  
  130.                                         /* Start of program info */
  131. #define DlgTop          100
  132. #define DlgLeft         85
  133.  
  134. MenuHandle      myMenu[numMenus];
  135. EventRecord     curEvent;
  136. WindowRecord    wRecord;
  137. WindowPtr       curWindow, myWindow[numWindows];
  138. ControlHandle   curControl, myControl[numWindows][maxControls];
  139. Rect            limitRect;
  140.  
  141. short           numControls[numWindows] = { numRace, numClass, numStatus, numAlign, numStatist };
  142. short           controlLoc[sumControls] = { raceLoc, classLoc, statusLoc, alignLoc, strengthLoc,
  143.                                             IQLoc, pietyLoc, vitalityLoc, agilityLoc,luckLoc,
  144.                                             goldLoc, experienceLoc, actHitsLoc, maxHitsLoc };
  145. short           curFile ;
  146. unsigned char   stat[charBlock];
  147. Str255          * chName;
  148. Boolean         Dirty, ErrorFlag, FOPENED = FALSE;
  149.  
  150.  
  151. setmenus()                      /* set up menus */
  152. {
  153.         int     menu;
  154.  
  155.         myMenu[apMenu]  = GetMenu(1);
  156.         AddResMenu(myMenu[apMenu], 'DRVR');
  157.         InsertMenu(myMenu[apMenu], 0);
  158.         for (menu = fileMenu; menu <= charMenu; menu++) {
  159.                 myMenu[menu]    = GetMenu(menuBase + menu);
  160.                 InsertMenu(myMenu[menu], 0);
  161.         }
  162.         for (menu = 1; menu <= 4; menu++)
  163.                 DisableItem(myMenu[charMenu], menu);
  164.         DisableItem(myMenu[fileMenu], 2);
  165.         DrawMenuBar();
  166. }
  167.  
  168.  
  169. initialize()
  170. {
  171.         InitGraf(&thePort);
  172.         InitFonts();
  173.         SetEventMask(everyEvent - keyUpMask);
  174.         FlushEvents(everyEvent, 0);
  175.         InitWindows();
  176.         InitMenus();
  177.         TEInit();
  178.         InitDialogs(0L);
  179.         SetCursor(&arrow);
  180.  
  181.         setmenus();
  182.         SetRect(&limitRect, 4, 24, 508, 338);
  183.         curWindow       = 0L;
  184. }
  185.  
  186.  
  187. IOCheck(resCode)                        /* check for IO errors */
  188.         OSErr   resCode;
  189. {
  190.         int     alertDlg, ignore;
  191.         long    longresult;
  192.         Str255  * errString;
  193.  
  194.         switch (resCode)        {
  195.                 case noErr      : return;
  196.                 case opWrErr    : alertDlg      = opWrDialog;
  197.                                   break;
  198.                 default         : alertDlg      = ioErrDialogg;
  199.                                   longresult    = resCode;
  200.                                   NumToString(longresult, errString);
  201.                                   ParamText(errString, "\P ", "\P ", "\P ");
  202.         }
  203.         InitCursor();
  204.         ignore  = StopAlert(dialogBase + alertDlg, 0L);
  205.  
  206.         ErrorFlag       = TRUE;
  207. }
  208.  
  209.         
  210. openfile()                              /* ask for file to open */
  211. {
  212.         Point           dlgOrigin;
  213.         char            fType[30];
  214.         SFReply         theReply;
  215.         OSErr           resultCode;
  216.  
  217.         SetPt(&dlgOrigin, DlgTop, DlgLeft);
  218.         strcpy(fType, "SAVE");
  219.  
  220.         SFGetFile(pass(dlgOrigin), "\P ", 0L, 1, fType, 0L, &theReply);
  221.  
  222.         if (theReply.good)      {
  223.                 resultCode      = FSOpen(theReply.fName, theReply.vRefNum, &curFile);
  224.                 IOCheck(resultCode);
  225.                 FOPENED = TRUE;
  226.                 EnableItem(myMenu[fileMenu], 2);
  227.                 DisableItem(myMenu[fileMenu], 1);
  228.                 EnableItem(myMenu[charMenu], 1);
  229.         }
  230. }
  231.  
  232.  
  233. closeDA()                       /* close a desk accesory */
  234. {
  235.         WindowPeek      whichWindow;
  236.         int             accNum;
  237.  
  238.         whichWindow     = (WindowPeek) FrontWindow();
  239.         accNum          = whichWindow->windowKind;
  240.         CloseDeskAcc(accNum);
  241. }
  242.  
  243.  
  244. closewindows()                          /* close all windows */
  245. {
  246.         int     window;
  247.  
  248.         for (window = raceWind; window <= statistWind; window++)
  249.                 DisposeWindow(myWindow[window]);
  250.         curWindow       = 0L;
  251. }
  252.  
  253.  
  254. closefile()                             /* close a file */
  255. {
  256.         int     theItem;
  257.         OSErr   resultCode;
  258.  
  259.         if ((curWindow != 0L) && (FrontWindow() == curWindow))
  260.                 closewindows();
  261.         if (FrontWindow() != 0L)
  262.                 closeDA();
  263.         resultCode      = FSClose(curFile);
  264.         FOPENED = FALSE;
  265.         DisableItem(myMenu[charMenu], 1);
  266.         EnableItem(myMenu[fileMenu], 1);
  267.         DisableItem(myMenu[fileMenu], 2);
  268. }
  269.  
  270.  
  271. getchname(theName)                      /* get character to edit */
  272.         Str255  * theName;
  273. {
  274.         DialogPtr       charDlg;
  275.         Handle          IHandle;
  276.         Rect            IRect;
  277.         int             itemNum, IType;
  278.  
  279.         charDlg = GetNewDialog(dialogBase + charDialog, 0L, -1L);
  280.         do
  281.                 ModalDialog(0L, &itemNum);
  282.         while ((itemNum != OK) && (itemNum != Cancel));
  283.         if (itemNum == Cancel)  {
  284.                         theName->length = 3;
  285.                         strcpy(&(theName->text), "???");
  286.         }
  287.         else    {
  288.                 GetDItem(charDlg, 4, &IType, &IHandle, &IRect);
  289.                 GetIText(IHandle, theName);
  290.         }
  291.         DisposDialog(charDlg);
  292. }
  293.  
  294.  
  295. updateStats()                           /* draws statistics on window */
  296. {
  297.         static  char    * slabel[10]    = {     "strength  ", "intellect ", "piety     ", "vitality  ", "agility   ",
  298.                                                 "luck      ", "gold      ", "experience", "actual HPs", "max HPs   "    };
  299.         
  300.         static  int     hp[10]  = { 15, 15, 15, 260, 260, 260, 15, 15, 260, 260 },
  301.                         vp[10]  = { 34, 59, 84, 34, 59, 84, 134, 159, 134, 159 };
  302.         int             label;
  303.  
  304.         SetPort(myWindow[statistWind]);
  305.         for (label = Strength; label <= MaxHits; label++)       {
  306.                 MoveTo(hp[label], vp[label]);
  307.                 DrawText(slabel[label], 0, 10);
  308.         }
  309. }
  310.                 
  311.  
  312. scale(ItoL, ip, lp)                     /* scales ints <-> longs */
  313.         unsigned char   ItoL;
  314.         unsigned int    * ip;
  315.         unsigned long   * lp;
  316. {
  317.         float           tmp;
  318.         
  319.         if (ItoL)       {
  320.                 tmp     = ((float) *ip)/32000;
  321.                 *lp     = 2147483000*tmp;
  322.         }
  323.         else    {
  324.                 tmp     = ((float) *lp)/2147483000;
  325.                 *ip     = 32000*tmp;
  326.         }
  327. }
  328.  
  329.  
  330. openwindows()                           /* opens all windows */
  331. {
  332.         int             wID, cID, * intPtr, tmp, ctlCount       = 0, localBase  = controlBase;
  333.         
  334.         for (wID = raceWind; wID <= statistWind; wID++) {
  335.                 myWindow[wID]   = GetNewWindow(windowBase + wID, 0L, -1L);
  336.                 for (cID = 0; cID < numControls[wID]; cID++)
  337.                         myControl[wID][cID]     = GetNewControl(localBase + cID, myWindow[wID]);
  338.                 if (wID < statistWind)
  339.                         SetCtlValue(myControl[wID][stat[controlLoc[wID]]], 1);
  340.                 else    {
  341.                         for (cID = 0; cID < numControls[wID]; cID++)
  342.                                 if (cID < Gold)
  343.                                         SetCtlValue(myControl[wID][cID], stat[controlLoc[wID + cID]]);
  344.                                 else if (cID != Experience)     {
  345.                                         intPtr  = &(stat[controlLoc[wID + cID]]);
  346.                                         SetCtlValue(myControl[wID][cID], *intPtr);
  347.                                 }
  348.                                 else    {
  349.                                         scale(0, &tmp, &(stat[experienceLoc]));
  350.                                         SetCtlValue(myControl[wID][cID], tmp);
  351.                                 }
  352.                         updateStats();
  353.                 }
  354.                 localBase       += numControls[wID];
  355.         }
  356.         curWindow       = myWindow[wID - 1];
  357. }
  358.  
  359.  
  360. rdchar()                                /* reads character stats from file */
  361. {
  362.         long    blocklen        = charBlock;
  363.         int     count, chindex;
  364.         
  365.         getchname(chName);
  366.         if (chName->text[0] == '?')
  367.                 return;
  368.         for (chindex = 0; chindex < 20; chindex++)      {
  369.                 SetFPos(curFile, fsFromStart, chindex*blocklen);
  370.                 FSRead(curFile, &blocklen, stat);
  371.                 for (count = 1; count <= stat[0]; count++)
  372.                         if (stat[count] != chName->text[count - 1])
  373.                                 break;
  374.                 if (count > stat[0])    {
  375.                         SetFPos(curFile, fsFromMark, -blocklen);
  376.                         break;
  377.                 }
  378.         }
  379.         if (chindex < 20)       {
  380.                 Dirty   = FALSE;
  381.                 openwindows();
  382.                 DisableItem(myMenu[fileMenu], 2);
  383.                 DisableItem(myMenu[charMenu], 1);
  384.                 for (count = 2; count <= 4; count++)
  385.                         EnableItem(myMenu[charMenu], count);
  386.         }
  387. }
  388.  
  389.  
  390.  
  391. wrchar()                                /* write character stats */
  392. {
  393.         long    blocklen        = charBlock;
  394.         long    curPos;
  395.  
  396.         FSWrite(curFile, &blocklen, stat);
  397.         SetFPos(curFile, fsFromMark, -blocklen);
  398.         Dirty   = FALSE;
  399. }
  400.  
  401.  
  402. clchar()                                /* close a character */
  403. {
  404.         int     theItem;
  405.  
  406.         if (Dirty)      {
  407.                 ParamText(chName, "\P ", "\P ", "\P ");
  408.                 theItem = CautionAlert(dialogBase + saveDialog, 0L);
  409.                 switch  (theItem)       {
  410.                         case saveItem           : wrchar();
  411.                                                   break;
  412.                         case discardItem        : break;
  413.                         case cancelItem         : return;
  414.                 }
  415.         }
  416.         closewindows();
  417.         EnableItem(myMenu[fileMenu], 2);
  418.         EnableItem(myMenu[charMenu], 1);
  419.         for (theItem = 2; theItem <= 4; theItem++)
  420.                 DisableItem(myMenu[charMenu], theItem);
  421. }
  422.  
  423.  
  424. brag()                                  /* do about stuff */
  425. {
  426.         int     ignore;
  427.         
  428.         ignore  = Alert(dialogBase + aboutDialog, 0L);
  429. }
  430.  
  431.  
  432. doCommand(eventloc)                     /* processes commands */
  433.         Point   eventloc;
  434. {
  435.         unsigned long   mResult;
  436.         char            name[30];
  437.         short           theMenu, theItem;
  438.         long            ticks;
  439.         
  440.         mResult = MenuSelect(pass(eventloc));
  441.         theMenu = mResult >> 16;
  442.         theItem = mResult;
  443.         switch (theMenu - menuBase)     {
  444.                 case -399       : if (theItem == 1)
  445.                                         brag();
  446.                                   else  {
  447.                                         GetItem(myMenu[apMenu], theItem, name);
  448.                                         OpenDeskAcc(name);
  449.                                   }
  450.                                   break;
  451.                 case fileMenu   : switch (theItem)      {
  452.                                         case 1  : openfile();
  453.                                                   break;
  454.                                         case 2  : closefile();
  455.                                                   break;
  456.                                         case 3  : if (Dirty)
  457.                                                         clchar();
  458.                                                   if (FOPENED)
  459.                                                         closefile();
  460.                                                   _exit();
  461.                                   }
  462.                                   break;
  463.                 case charMenu   : switch (theItem)      {
  464.                                         case 1  : rdchar();
  465.                                                   break;
  466.                                         case 2  : wrchar();
  467.                                                   break;
  468.                                         case 3  : clchar();
  469.                                                   break;
  470.                                         case 4  : clchar();
  471.                                                   break;
  472.                                   }
  473.                                   break;
  474.                 default         : break;
  475.         }
  476.         HiliteMenu(0);
  477. }
  478.  
  479.  
  480. pascal  void    scrollit(whichControl, whichPart)       /* pascal action procedure for scrolling */
  481.         ControlHandle   whichControl;
  482.         int             whichPart;
  483. {
  484.         int             change  = 0;
  485.         
  486.         switch (whichPart)      {
  487.                 case inUpButton         :;
  488.                 case inPageUp           : change--;
  489.                                           break;
  490.                 case inDownButton       :;
  491.                 case inPageDown         : change++;
  492.                                           break;
  493.                 default                 :;
  494.         }
  495.         if (whichPart)
  496.                 SetCtlValue(whichControl, GetCtlValue(whichControl) + change);
  497. }
  498.  
  499.  
  500. docontent()                             /* clicked in windows */
  501. {
  502.         int     controlPart, control, window, button, localBase = controlBase;
  503.         int     * intPtr, tmpInt;
  504.         
  505.         if (curWindow != FrontWindow()) {
  506.                 SelectWindow(curWindow);
  507.                 return;
  508.         }
  509.         else    {
  510.                 GlobalToLocal(&curEvent.where);
  511.                 if (controlPart = FindControl(pass(curEvent.where), curWindow, &curControl))
  512.                         if (curWindow == myWindow[statistWind]) {
  513.                                 Dirty   = TRUE;
  514.                                 if (controlPart == inThumb)
  515.                                         controlPart     = TrackControl(curControl, pass(curEvent.where), -1L);
  516.                                 else
  517.                                         controlPart     = TrackControl(curControl, pass(curEvent.where), scrollit);
  518.                                 for (control = Strength; control <= MaxHits; control++)
  519.                                         if (curControl == myControl[statistWind][control])
  520.                                                 if (control < Gold)
  521.                                                         stat[controlLoc[statistWind + control]] = GetCtlValue(curControl);
  522.                                                 else if (control != Experience) {
  523.                                                         intPtr  = &(stat[controlLoc[statistWind + control]]);
  524.                                                         *intPtr = GetCtlValue(curControl);
  525.                                                 }
  526.                                                 else    {
  527.                                                         tmpInt  = GetCtlValue(curControl);
  528.                                                         scale(1, &tmpInt, &(stat[experienceLoc]));
  529.                                                 }
  530.                         }
  531.                         else if (TrackControl(curControl, pass(curEvent.where), -1L) == inCheckBox)     {
  532.                                 Dirty   = TRUE;
  533.                                 SetCtlValue(curControl, 1);
  534.                                 for (window = raceWind; curWindow != myWindow[window]; window++)
  535.                                         ;
  536.                                 for (button = 0; button < numControls[window]; button++)
  537.                                         if (myControl[window][button] == curControl)
  538.                                                 stat[controlLoc[window]]        = button;
  539.                                         else
  540.                                                 SetCtlValue(myControl[window][button], 0);
  541.                         }
  542.         }
  543. }
  544.  
  545.  
  546. DoActivate()                            /* Handle activate events */
  547. {
  548.         WindowPeek      whichWindow;
  549.         WindowPtr       thisWindow;
  550.         ControlHandle   whichControl;
  551.         int             windnum;
  552.         
  553.         thisWindow = (WindowPtr) curEvent.message;
  554.         for (windnum = 0; windnum < numWindows; windnum++)
  555.                 if (thisWindow == myWindow[windnum])
  556.                         break;
  557.         if (windnum < numWindows)       {
  558.                 whichWindow     = (WindowPeek) thisWindow;
  559.                 SetPort(thisWindow);
  560.                 if (curEvent.modifiers & 1)     {
  561.                         curWindow       = thisWindow;
  562.                         for (whichControl = whichWindow->controlList; whichControl != 0L; whichControl = (*whichControl)->nextControl)
  563.                                 HiliteControl(whichControl, 0);
  564.                 }
  565.                 else    {
  566.                         for (whichControl = whichWindow->controlList; whichControl != 0L; whichControl = (*whichControl)->nextControl)
  567.                                 HiliteControl(whichControl, 255);
  568.                 }
  569.         }
  570. }
  571.  
  572.  
  573. DoUpdate()
  574. {
  575.         WindowPtr       thisWindow;
  576.         int             windnum;
  577.  
  578.         thisWindow = (WindowPtr) curEvent.message;
  579.         for (windnum = 0; windnum < numWindows; windnum++)
  580.                 if (thisWindow == myWindow[windnum])
  581.                         break;
  582.         if (windnum < numWindows)       {
  583.                 curWindow       = thisWindow;
  584.                 BeginUpdate(curWindow);
  585.                 DrawControls(curWindow);
  586.                 if (windnum == statistWind)
  587.                         updateStats();
  588.                 EndUpdate(curWindow);
  589.         }
  590. }
  591.  
  592.  
  593. main()
  594. {
  595.         int     code;
  596.  
  597.         initialize();
  598.         
  599.         for (;;)        {
  600.                 SystemTask();
  601.                 GetNextEvent(everyEvent, &curEvent);
  602.                 
  603.                 switch  (curEvent.what) {
  604.                         case mouseDown  :
  605.                                 code    = FindWindow(pass(curEvent.where), &curWindow);
  606.                                 switch  (code)  {
  607.                                         case inDesk     : break;
  608.                                         case inMenuBar  : doCommand(pass(curEvent.where));
  609.                                                           break;
  610.                                         case inSysWindow: SystemClick(&curEvent, curWindow);
  611.                                                           break;
  612.                                         case inDrag     : DragWindow(curWindow, pass(curEvent.where), &limitRect);
  613.                                                           break;
  614.                                         case inContent  : docontent();
  615.                                                           break;
  616.                                         default         : break;
  617.                                 }
  618.                                 break;
  619.                         case activateEvt        :
  620.                                 DoActivate();
  621.                                 break;
  622.                         case updateEvt  :
  623.                                 DoUpdate();
  624.                                 break;
  625.                         default :
  626.                                 break;
  627.                 }
  628.         }
  629. }
  630.  
  631.  
  632.