home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / game / think / chaos / src / project.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  8.9 KB  |  377 lines

  1. /*  Chaos:            The Chess HAppening Organisation System    V5.2
  2.     Copyright (C)   1993    Jochen Wiedmann
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     $RCSfile: Project.c,v $
  20.     $Revision: 2.2 $
  21.     $Date: 1993/12/12 13:22:15 $
  22.  
  23.     This file contains the functions of the Project-menu.
  24.  
  25.     Computer:    Amiga 1200            Compiler:    Dice 2.07.54 (3.0)
  26.  
  27.     Author:    Jochen Wiedmann
  28.         Am Eisteich 9
  29.       72555 Metzingen
  30.         Tel. 07123 / 14881
  31.         Internet: wiedmann@mailserv.zdv.uni-tuebingen.de
  32. */
  33.  
  34.  
  35. #ifndef CHAOS_H
  36. #include "chaos.h"
  37. #endif
  38.  
  39.  
  40.  
  41. static char FILEVERSION[8] = "CHAOS5.0";
  42.  
  43.  
  44.  
  45.  
  46. /*
  47.     DeleteTournament creates a new tournament with default settings.
  48.  
  49.     Inputs: name    pointer to a string containing the new tournaments
  50.             name.
  51. */
  52. void *TrnMem = NULL;
  53. void DeleteTournament(char *name)
  54.  
  55. {
  56.   /*
  57.       Deallocate data used by the old tournament.
  58.   */
  59.   PutMemList (&TrnMem);
  60.  
  61.   /*
  62.       Initialize the new tournament.
  63.   */
  64.   NewList(&PlayerList);
  65.   PlayerList.lh_Type = -1;
  66.   *TrnFileName = '\0';
  67.   NumRounds = 0;
  68.   NumPlayers = 0;
  69.   if (name == NULL)
  70.   { *TrnName = '\0';
  71.   }
  72.   else
  73.   { strcpy (TrnName, name);
  74.   }
  75.   TrnMode = 0;
  76.   IsSaved = TRUE;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. /*
  83.     SaveTournament() saves a tournament.
  84.  
  85.     Inputs: name    Name of the tournament file; FileRequest() is called,
  86.             if name is NULL or the empty string.
  87.  
  88.     Result: TRUE, if successfull, FALSE otherwise
  89. */
  90. int SaveTournament(char *name)
  91.  
  92.   { FILE *fh;
  93.     struct Player *tn;
  94.     struct Game *gm;
  95.  
  96.     if (name == NULL  ||  *name == '\0')
  97.       { if ((name = FileRequest(TrnFileName, NULL, NULL, TRUE))  ==  NULL)
  98.       { return(FALSE);
  99.       }
  100.       }
  101.  
  102.     if ((fh = fopen(name, "w"))  ==  NULL)
  103.       { ShowError((char *) GetChaosString(MSG_NO_WRITE_FILE), name, IoErr());
  104.     return(FALSE);
  105.       }
  106.  
  107.     if (fwrite(FILEVERSION, sizeof(FILEVERSION), 1, fh) != 1)
  108.       { goto WriteError;
  109.       }
  110.  
  111.     if (fwrite(&NumRounds, sizeof(NumRounds), 1, fh) != 1                  ||
  112.     fwrite(&RankingFirst, sizeof(RankingFirst), 1, fh) != 1    ||
  113.     fwrite(&NumPlayers, sizeof(NumPlayers), 1, fh) != 1          ||
  114.     fwrite(&NumGamesMissing, sizeof(NumGamesMissing), 1, fh) != 1  ||
  115.     fwrite(TrnName, sizeof(TrnName), 1, fh) != 1               ||
  116.     fwrite(&TrnMode, sizeof(TrnMode), 1, fh) != 1)
  117.       { goto WriteError;
  118.       }
  119.  
  120.     for (tn = (struct Player *) PlayerList.lh_Head;
  121.      tn->Tn_Node.ln_Succ != NULL;
  122.      tn = (struct Player *) tn->Tn_Node.ln_Succ)
  123.       { tn->Helpptr = tn;
  124.     if (fwrite(tn, sizeof(*tn), 1, fh)  !=  1)
  125.       { goto WriteError;
  126.       }
  127.     for (gm = tn->First_Game;  gm != NULL;  gm = gm->Next)
  128.       { if (fwrite(gm, sizeof(*gm), 1, fh)  !=  1)
  129.           { goto WriteError;
  130.           }
  131.       }
  132.       }
  133.  
  134.     fclose(fh);
  135.     IsSaved = TRUE;
  136.     strcpy(TrnFileName, name);
  137.     CreateIcon(name);
  138.     return(TRUE);
  139.  
  140. WriteError:
  141.     fclose(fh);
  142.     ShowError((char *) GetChaosString(MSG_WRITE_ERROR), IoErr(), name);
  143.     return(FALSE);
  144.   }
  145.  
  146.  
  147.  
  148.  
  149. /*
  150.     The function NewAddress() initializes a pointer after loading.
  151. */
  152. static void NewAddress(struct Player **tptr)
  153.  
  154. { struct Player *tn;
  155.  
  156.   for (tn = (struct Player *) PlayerList.lh_Head;
  157.        tn->Tn_Node.ln_Succ != NULL;
  158.        tn = (struct Player *) tn->Tn_Node.ln_Succ)
  159.   { if (tn->Helpptr == *tptr)
  160.     { *tptr = tn;
  161.       return;
  162.     }
  163.   }
  164. }
  165.  
  166.  
  167.  
  168.  
  169. /*
  170.     LoadTournament() loads a tournament from disk.
  171.  
  172.     Inputs: name    - Name of the tournament file; FileRequest() is called,
  173.               if name is NULL or the empty string.
  174.         memlist - a pointer to the memlist, where the player should
  175.               be inserted; this may be NULL, in which case TrnMem
  176.               is used
  177.         plrlist - a pointer to a list, where the new player should be
  178.               inserted; this may be NULL, in which case PlayerList
  179.               is used
  180.  
  181.  
  182.     Result: RETURN_OK, if successful, RETURN_WARN or RETURN_ERROR otherwise
  183. */
  184. int LoadTournament(char *name, void **memlist, struct List *plrlist)
  185.  
  186. { FILE *fh;
  187.   struct Player *tn;
  188.   struct Game *gm;
  189.   int i, j;
  190.   char fileversion[sizeof(FILEVERSION)];
  191.   int import;
  192.   int numplayers, numrounds;
  193.  
  194.   if (plrlist == NULL)
  195.   { plrlist = &PlayerList;
  196.   }
  197.   if (memlist == NULL)
  198.   { memlist = &TrnMem;
  199.   }
  200.   import = (plrlist != &PlayerList);
  201.  
  202.   if (name == NULL  ||  *name == '\0')
  203.   { if ((name = FileRequest(TrnFileName, NULL, NULL, FALSE))  ==  NULL)
  204.     { return(RETURN_WARN);
  205.     }
  206.   }
  207.  
  208.   if ((fh = fopen(name, "r"))  ==  NULL)
  209.   { ShowError((char *) GetChaosString(MSG_NO_READ_FILE), name, IoErr());
  210.     return(RETURN_ERROR);
  211.   }
  212.  
  213.   if (fread(fileversion, sizeof(FILEVERSION), 1, fh)  !=  1)
  214.   { goto ReadError;
  215.   }
  216.   if (strncmp(fileversion, FILEVERSION, sizeof(FILEVERSION)) != 0)
  217.   { ShowError((char *) GetChaosString(MSG_NO_CHAOS_FILE), name);
  218.     fclose(fh);
  219.     return(RETURN_ERROR);
  220.   }
  221.  
  222.   if (!import)
  223.   { DeleteTournament(NULL);
  224.     NewList(&PlayerList);
  225.     if (fread(&NumRounds, sizeof(NumRounds), 1, fh) != 1              ||
  226.     fread(&RankingFirst, sizeof(RankingFirst), 1, fh) != 1        ||
  227.     fread(&NumPlayers, sizeof(NumPlayers), 1, fh) != 1            ||
  228.     fread(&NumGamesMissing, sizeof(NumGamesMissing), 1, fh) != 1  ||
  229.     fread(TrnName, sizeof(TrnName), 1, fh) != 1                   ||
  230.     fread(&TrnMode, sizeof(TrnMode), 1, fh) != 1)
  231.     { goto ReadError;
  232.     }
  233.     numrounds = NumRounds;
  234.     numplayers = NumPlayers;
  235.   }
  236.   else
  237.   { NewList(plrlist);
  238.     if (fread(&numrounds, sizeof(numrounds), 1, fh) != 1              ||
  239.     fseek(fh, sizeof(RankingFirst), SEEK_CUR)                     ||
  240.     fread(&numplayers, sizeof(numplayers), 1, fh) != 1            ||
  241.     fseek(fh, sizeof(NumGamesMissing)+sizeof(TrnName)+sizeof(TrnMode),
  242.           SEEK_CUR))
  243.     { goto ReadError;
  244.     }
  245.   }
  246.  
  247.  
  248.   for (i = 0;  i < numplayers;  i++)
  249.   { int readsize = sizeof(*tn) + (import ? 0 : sizeof(*gm)*numrounds);
  250.     int seeksize = import ? sizeof(*gm)*numrounds : 0;
  251.  
  252.     if ((tn = GetMem(memlist, readsize))  ==  NULL)
  253.     { goto Error;
  254.     }
  255.  
  256.     if (fread (tn, readsize, 1, fh)  !=  1   ||
  257.     (seeksize != 0  &&  fseek(fh, seeksize, SEEK_CUR)))
  258.     { goto ReadError;
  259.     }
  260.  
  261.     tn->Tn_Node.ln_Name = tn->Name;
  262.     AddTail(plrlist, (struct Node *) tn);
  263.  
  264.     if (!import)
  265.     { if (NumRounds != 0)
  266.       tn->First_Game = gm = (struct Game *)(tn+1);
  267.       for (j = 0;  j < NumRounds-1;  j++, gm++)
  268.       { gm->Next = gm+1;
  269.       }
  270.     }
  271.   }
  272.  
  273.   if (!import)
  274.   { for (tn = (struct Player *) PlayerList.lh_Head;
  275.      tn->Tn_Node.ln_Succ != NULL;
  276.      tn = (struct Player *) tn->Tn_Node.ln_Succ)
  277.     { NewAddress(&tn->RankNext);
  278.       for (gm = tn->First_Game;  gm != NULL;  gm = gm->Next)
  279.       { NewAddress(&gm->Opponent);
  280.       }
  281.     }
  282.     NewAddress(&RankingFirst);
  283.  
  284.     IsSaved = TRUE;
  285.     strcpy(TrnFileName, name);
  286.   }
  287.  
  288.  
  289.   fclose(fh);
  290.   return(RETURN_OK);
  291.  
  292. ReadError:
  293.   ShowError((char *) GetChaosString(MSG_READ_ERROR), IoErr(), name);
  294. Error:
  295.   fclose(fh);
  296.   if (!import)
  297.   { DeleteTournament(NULL);
  298.   }
  299.   return(RETURN_ERROR);
  300. }
  301.  
  302.  
  303.  
  304.  
  305. /*
  306.     TestSaved() checks, if the current tournament has changed since the
  307.     last load or save. The user is asked for saving, if it has.
  308.  
  309.     Result: FALSE, if the user cancels, TRUE otherwise.
  310. */
  311. int TestSaved(void)
  312.  
  313.   { if (IsSaved)
  314.       { return(TRUE);
  315.       }
  316.  
  317.     return(AskSave());
  318.   }
  319.  
  320.  
  321.  
  322.  
  323. /*
  324.     NewTournament() brings up a window asking for new tournament data.
  325.     The old tournament gets removed, if successfull.
  326. */
  327. void NewTournament(void)
  328.  
  329. { char NewTrnName[TRNNAME_LEN+1];
  330.   int running = 1;
  331.  
  332.   NewTrnName[0] = '\0';
  333.   if (InitTrnWnd(NewTrnName))
  334.   { while (running == 1)
  335.     { running = ProcessTrnWnd(NewTrnName);
  336.     }
  337.     TerminateTrnWnd();
  338.     if (running == -1)
  339.     { DeleteTournament(NewTrnName);
  340.       IsSaved = FALSE;
  341.     }
  342.   }
  343. }
  344.  
  345.  
  346.  
  347.  
  348. /*
  349.     My beloved routine: The About() function... :-)
  350. */
  351. void About(void)
  352.  
  353. { static char *txt = "\n"
  354.              "Chaos V5.2     The Chess HAppening Organisation "
  355.             "System\n"
  356.              "\n"
  357.              "Copyright © 1993 by       Jochen Wiedmann\n"
  358.              "                          Am Eisteich 9\n"
  359.              "                    72555 Metzingen (Germany)\n"
  360.              "                          Tel. 07123 / 14881\n"
  361.              "                          "
  362.             "Internet:wiedmann@mailserv.zdv.uni-tuebingen.de\n"
  363.              "\n\n";
  364.  
  365.   InitOutput((char *) GetChaosString(MSG_ABOUT_TITLE), "", "", "", NULL,
  366.          DEVICE_Screen, 0, 0);
  367.  
  368.   if (longlprint(txt)  &&
  369.       longlprint((char *) GetChaosString(MSG_ABOUT_PERMISSION))  &&
  370.       longlprint((char *) GetChaosString(MSG_ABOUT_THANKS1))  &&
  371.       longlprint((char *) GetChaosString(MSG_ABOUT_THANKS2)))
  372.   { ProcessOutput();
  373.   }
  374.  
  375.   TerminateOutput();
  376. }
  377.