home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 115 / af115sub.adf / yahzee.lzx / yahzee / tooltypes.c < prev    next >
C/C++ Source or Header  |  1998-06-03  |  7KB  |  233 lines

  1. /*
  2.  * tooltypes.c
  3.  * ===========
  4.  * Handles tooltypes.
  5.  *
  6.  * Copyright (C) 1994-1998 Håkan L. Younes (lorens@hem.passagen.se)
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <workbench/startup.h>
  11. #include <workbench/workbench.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "draw.h"
  15. #include "tooltypes.h"
  16.  
  17. #include <clib/dos_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/icon_protos.h>
  20.  
  21.  
  22. #define NUM_TOOLTYPES   4
  23.  
  24.  
  25. struct Library  *IconBase;
  26.  
  27. /* filename and dirlock */
  28. static BPTR   dir_lock = NULL;
  29. static char   prg_name[256];
  30.  
  31. /* where to put the toolvalues */
  32. extern char    pubscr_name[];
  33. extern UBYTE   chosen_num_players;
  34. extern UBYTE   chosen_scoring;
  35. extern UBYTE   chosen_gametype;
  36. extern UWORD   board_pens[];
  37. extern BOOL    locked_pens[];
  38.  
  39.  
  40. /* reads the tooltypes */
  41. static void
  42. read_tooltypes (void)
  43. {
  44.    struct DiskObject  *disk_obj;
  45.    char  *tool_val;
  46.    BPTR   old_dir = -1;
  47.    
  48.    if (IconBase = OpenLibrary ("icon.library", 33L))
  49.    {
  50.       if (dir_lock != NULL)
  51.          old_dir = CurrentDir (dir_lock);
  52.       
  53.       if (disk_obj = GetDiskObject (prg_name))
  54.       {
  55.          if (tool_val = FindToolType (disk_obj->do_ToolTypes, "PUBSCREEN"))
  56.             strncpy (pubscr_name, tool_val, 128);
  57.          if (tool_val = FindToolType (disk_obj->do_ToolTypes, "PLAYERS"))
  58.             chosen_num_players = atoi (tool_val);
  59.          if (tool_val = FindToolType (disk_obj->do_ToolTypes, "RULES"))
  60.          {
  61.             if (MatchToolValue (tool_val, "Traditional"))
  62.                chosen_scoring = 0;
  63.             else if (MatchToolValue (tool_val, "American"))
  64.                chosen_scoring = 1;
  65.          }
  66.          if (tool_val = FindToolType (disk_obj->do_ToolTypes, "GAMETYPE"))
  67.          {
  68.             if (MatchToolValue (tool_val, "Original"))
  69.                chosen_gametype = 0;
  70.             else if (MatchToolValue (tool_val, "Maxi"))
  71.                chosen_gametype = 1;
  72.          }
  73.          if (tool_val = FindToolType (disk_obj->do_ToolTypes, "FILLPEN"))
  74.          {
  75.             board_pens[BOARD_FILLPEN] = atoi (tool_val);
  76.             locked_pens[BOARD_FILLPEN] = TRUE;
  77.          }
  78.          FreeDiskObject (disk_obj);
  79.       }
  80.       
  81.       if (old_dir != -1)
  82.          CurrentDir (old_dir);
  83.       
  84.       CloseLibrary (IconBase);
  85.    }
  86. }
  87.  
  88. void
  89. save_tooltypes (void)
  90. {
  91.    int    i, j, n = 0;
  92.    char **new_toolarray;
  93.    char **old_toolarray;
  94.    char  *old_toolval[NUM_TOOLTYPES];
  95.    char   tooltypes[NUM_TOOLTYPES][129];
  96.    char   num_str[7];
  97.    BPTR   old_dir = -1;
  98.    struct DiskObject  *disk_obj;
  99.    
  100.    if (IconBase = OpenLibrary ("icon.library", 33L))
  101.    {
  102.       if (dir_lock != NULL)
  103.          old_dir = CurrentDir (dir_lock);
  104.       
  105.       if (disk_obj = GetDiskObject (prg_name))
  106.       {
  107.          old_toolarray = disk_obj->do_ToolTypes;
  108.          while (old_toolarray[n] != NULL)
  109.             ++n;
  110.          
  111.          old_toolval[0] = FindToolType (old_toolarray, "PUBSCREEN");
  112.          old_toolval[1] = FindToolType (old_toolarray, "PLAYERS");
  113.          old_toolval[2] = FindToolType (old_toolarray, "RULES");
  114.          old_toolval[3] = FindToolType (old_toolarray, "GAMETYPE");
  115.          for (i = 0; i < NUM_TOOLTYPES; ++i)
  116.          {
  117.             if (old_toolval[i] == NULL)
  118.                ++n;
  119.          }
  120.          if (new_toolarray = malloc ((n + 1) * sizeof (*new_toolarray)))
  121.          {
  122.             if (pubscr_name[0] != '\0')
  123.             {
  124.                strncpy (tooltypes[0], "PUBSCREEN=", 128);
  125.                strncat (tooltypes[0], pubscr_name, 128);
  126.             }
  127.             else
  128.             {
  129.                strncpy (tooltypes[0],
  130.                         "(PUBSCREEN=<name of public screen>)", 128);
  131.             }
  132.             strncpy (tooltypes[1], "PLAYERS=", 128);
  133.             stci_d (num_str, chosen_num_players);
  134.             strncat (tooltypes[1], num_str, 128);         
  135.             strncpy (tooltypes[2], "RULES=", 128);
  136.             if (chosen_scoring == 0)
  137.                strncat (tooltypes[2], "Traditional", 128);
  138.             else if (chosen_scoring == 1)
  139.                strncat (tooltypes[2], "American", 128);
  140.             strncpy (tooltypes[3], "GAMETYPE=", 128);
  141.             if (chosen_gametype == 0)
  142.                strncat (tooltypes[3], "Original", 128);
  143.             else if (chosen_gametype == 1)
  144.                strncat (tooltypes[3], "Maxi", 128);
  145.             
  146.             i = 0;
  147.             while (old_toolarray[i] != NULL)
  148.             {
  149.                if (strstr (old_toolarray[i], "PUBSCREEN=") ==
  150.                    old_toolarray[i] ||
  151.                    strstr (old_toolarray[i], "(PUBSCREEN=") ==
  152.                    old_toolarray[i])
  153.                {
  154.                   new_toolarray[i] = tooltypes[0];
  155.                   old_toolval[0] = tooltypes[0];
  156.                }
  157.                else if (strstr (old_toolarray[i], "PLAYERS=") ==
  158.                         old_toolarray[i] ||
  159.                         strstr (old_toolarray[i], "(PLAYERS=") ==
  160.                         old_toolarray[i])
  161.                {
  162.                   new_toolarray[i] = tooltypes[1];
  163.                   old_toolval[1] = tooltypes[1];
  164.                }
  165.                else if (strstr (old_toolarray[i], "RULES=") ==
  166.                         old_toolarray[i] ||
  167.                         strstr (old_toolarray[i], "(RULES=") ==
  168.                         old_toolarray[i])
  169.                {
  170.                   new_toolarray[i] = tooltypes[2];
  171.                   old_toolval[2] = tooltypes[2];
  172.                }
  173.                else if (strstr (old_toolarray[i], "GAMETYPE=") ==
  174.                         old_toolarray[i] ||
  175.                         strstr (old_toolarray[i], "(GAMETYPE=") ==
  176.                         old_toolarray[i])
  177.                {
  178.                   new_toolarray[i] = tooltypes[3];
  179.                   old_toolval[3] = tooltypes[3];
  180.                }
  181.                else
  182.                   new_toolarray[i] = old_toolarray[i];
  183.                
  184.                ++i;
  185.             }
  186.             if (n > i)
  187.             {
  188.                for (j = 0; j < NUM_TOOLTYPES; ++j)
  189.                {
  190.                   if (old_toolval[j] == NULL)
  191.                   {
  192.                      new_toolarray[i] = tooltypes[j];
  193.                      ++i;
  194.                   }
  195.                }
  196.             }
  197.             new_toolarray[i] = NULL;
  198.             
  199.             disk_obj->do_ToolTypes = new_toolarray;
  200.             PutDiskObject (prg_name, disk_obj);
  201.             disk_obj->do_ToolTypes = old_toolarray;
  202.             
  203.             free (new_toolarray);
  204.          }
  205.          FreeDiskObject (disk_obj);
  206.       }
  207.       
  208.       if (old_dir != -1)
  209.          CurrentDir (old_dir);
  210.       
  211.       CloseLibrary (IconBase);
  212.    }
  213. }
  214.  
  215. void
  216. handle_startup_msg (
  217.    char **args,
  218.    BOOL   from_wb)
  219. {
  220.    struct WBArg  *wb_arg;
  221.    
  222.    if (from_wb)
  223.    {
  224.       wb_arg = ((struct WBStartup *)args)->sm_ArgList;
  225.       dir_lock = wb_arg->wa_Lock;
  226.       strncpy (prg_name, wb_arg->wa_Name, 255);
  227.    }
  228.    else
  229.       strncpy (prg_name, args[0], 255);
  230.    
  231.    read_tooltypes ();
  232. }
  233.