home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / kernel / generic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  7.6 KB  |  296 lines  |  [TEXT/R*ch]

  1. /* Low-level support for variables and properties in Xconq GDL.
  2.    Copyright (C) 1991, 1992, 1993, 1994, 1995 Stanley T. Shebs.
  3.  
  4. Xconq 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, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "config.h"
  10. #include "misc.h"
  11. #include "lisp.h"
  12. #include "game.h"
  13.  
  14. /* The total number of unit/material/terrain types. */
  15.  
  16. short numutypes;
  17. short nummtypes;
  18. short numttypes;
  19.  
  20. /* These variables indicate whether new types can still be defined.
  21.    Once a table or list of types is manipulated, these are turned off. */
  22.  
  23. short canaddutype = TRUE;
  24. short canaddmtype = TRUE;
  25. short canaddttype = TRUE;
  26.  
  27. /* It would be useful for these to mention the slot or table access that
  28.    resulted in the error, but we don't want to have to pass a bunch of
  29.    parameters to these routines, since they're referenced a lot. */
  30.  
  31. void
  32. utype_error(u)
  33. int u;
  34. {
  35.     run_warning("Bad utype %d", u);
  36. }
  37.  
  38. void
  39. mtype_error(m)
  40. int m;
  41. {
  42.     run_warning("Bad mtype %d", m);
  43. }
  44.  
  45. void
  46. ttype_error(t)
  47. int t;
  48. {
  49.     run_warning("Bad ttype %d", t);
  50. }
  51.  
  52. #ifdef SPECIAL
  53.  
  54. /* When specially compiled, all the types and globals are statically
  55.    initialized. */
  56.  
  57. void
  58. init_types()
  59. {
  60. }
  61.  
  62. void
  63. init_globals()
  64. {
  65. }
  66.  
  67. #else /* SPECIAL */
  68.  
  69. /* Declarations of the type definitions themselves. */
  70.  
  71. Utype *utypes;
  72.  
  73. Mtype *mtypes;
  74.  
  75. Ttype *ttypes;
  76.  
  77. Globals globals;
  78.  
  79. /* This prepares the type definitions to be filled in, doing initial
  80.    allocations, etc. */
  81.  
  82. int curmaxutypes = MAXUTYPES;
  83. int curmaxmtypes = MAXMTYPES;
  84. int curmaxttypes = MAXTTYPES;
  85.  
  86. void
  87. init_types()
  88. {
  89.     numutypes = nummtypes = numttypes = 0;
  90.  
  91.     Dprintf("Utype is %d bytes, mtype is %d bytes, ttype is %d bytes.\n",
  92.         sizeof(Utype), sizeof(Mtype), sizeof(Ttype));
  93.  
  94.     utypes = (Utype *) xmalloc(sizeof(Utype) * curmaxutypes);
  95.     mtypes = (Mtype *) xmalloc(sizeof(Mtype) * curmaxmtypes);
  96.     ttypes = (Ttype *) xmalloc(sizeof(Ttype) * curmaxttypes);
  97.  
  98.     memset(utypes, 0, sizeof(Utype) * curmaxutypes);
  99.     memset(mtypes, 0, sizeof(Mtype) * curmaxmtypes);
  100.     memset(ttypes, 0, sizeof(Ttype) * curmaxttypes);
  101. }
  102.  
  103. VarDefn vardefns[] = {
  104.  
  105. #undef  DEF_VAR_I
  106. #define DEF_VAR_I(NAME,FNAME,SETFNAME,DOC,var,LO,DFLT,HI)  \
  107.     { NAME, FNAME, NULL, NULL, SETFNAME, NULL, NULL, DOC, DFLT, NULL, NULL, LO, HI },
  108. #undef  DEF_VAR_S
  109. #define DEF_VAR_S(NAME,FNAME,SETFNAME,DOC,var,DFLT)  \
  110.     { NAME, NULL, FNAME, NULL, NULL, SETFNAME, NULL, DOC,    0, DFLT, NULL,  0,  0 },
  111. #undef  DEF_VAR_L
  112. #define DEF_VAR_L(NAME,FNAME,SETFNAME,DOC,var,DFLT)  \
  113.     { NAME, NULL, NULL, FNAME, NULL, NULL, SETFNAME, DOC,    0, NULL, DFLT,  0,  0 },
  114.  
  115. #include "gvar.def"
  116.  
  117.     { NULL }
  118. };
  119.  
  120. /* Define all the global-getting and -setting functions. */
  121.  
  122. #undef  DEF_VAR_I
  123. #define DEF_VAR_I(str,FNAME,SETFNAME,doc,VAR,lo,dflt,hi)  \
  124.   int FNAME() { return globals.VAR; }  \
  125.   void SETFNAME(v) int v; { globals.VAR = v; }
  126. #undef  DEF_VAR_S
  127. #define DEF_VAR_S(str,FNAME,SETFNAME,doc,VAR,dflt)  \
  128.   char *FNAME() { return globals.VAR; }  \
  129.   void SETFNAME(v) char *v; { globals.VAR = v; }
  130. #undef  DEF_VAR_L
  131. #define DEF_VAR_L(str,FNAME,SETFNAME,doc,VAR,DFLT)  \
  132.   Obj *FNAME() {  \
  133.       void (*fn) PARAMS ((void)) = (DFLT);  \
  134.       if (fn != NULL && globals.VAR == NULL) (*fn)();  \
  135.       return globals.VAR;  \
  136.   }  \
  137.   void SETFNAME(v) Obj *v; { globals.VAR = v; }
  138.  
  139. #include "gvar.def"
  140.  
  141. /* Set the globals to their default values. */
  142.  
  143. void
  144. init_globals()
  145. {
  146. #undef  DEF_VAR_I
  147. #define DEF_VAR_I(str,fname,SETFNAME,doc,var,lo,DFLT,hi)  \
  148.     SETFNAME(DFLT);
  149. #undef  DEF_VAR_S
  150. #define DEF_VAR_S(str,fname,SETFNAME,doc,var,DFLT)  \
  151.     SETFNAME(DFLT);
  152. #undef  DEF_VAR_L
  153. #define DEF_VAR_L(str,fname,SETFNAME,doc,var,DFLT)  \
  154.     if ((DFLT) == NULL) SETFNAME(lispnil);
  155.  
  156. #include "gvar.def"
  157.  
  158. }
  159.  
  160. #define TYPEPROP(TYPES, N, DEFNS, I, TYPE)  \
  161.   ((TYPE *) &(((char *) (&(TYPES[N])))[DEFNS[I].offset]))[0]
  162.  
  163. /* MPW C always loses on multiply-nested array references, so expand them
  164.    into pointer-arithmetic equivalents, which seem to work fine. */
  165.  
  166. #ifdef MPW_C
  167. #undef TYPEPROP
  168. #define TYPEPROP(TYPES, N, DEFNS, I, TYPE)  \
  169.   ((TYPE *) &(((char *) (&(TYPES[N])))[(*((DEFNS)+(I))).offset]))[0]
  170. #endif
  171.  
  172. /* This sets all the defaults in a unit type definition.  Note that all
  173.    type structures get blasted with zeros initially, so we really only
  174.    need to do default settings of nonzero values, thus the test.  (It
  175.    helps if the compiler is smart enough to remove dead code.) */
  176.  
  177. void
  178. default_unit_type(u)
  179. int u;
  180. {
  181.     int i;
  182.  
  183.     for (i = 0; utypedefns[i].name != NULL; ++i) {
  184.     if (utypedefns[i].intgetter) {
  185.         if (utypedefns[i].dflt != 0)
  186.           TYPEPROP(utypes, u, utypedefns, i, short) = utypedefns[i].dflt;
  187.     } else if (utypedefns[i].strgetter) {
  188.         if (utypedefns[i].dfltstr != NULL)
  189.           TYPEPROP(utypes, u, utypedefns, i, char *) =
  190.         (char *) utypedefns[i].dfltstr;
  191.     } else {
  192.         TYPEPROP(utypes, u, utypedefns, i, Obj *) = lispnil;
  193.     }
  194.     }
  195. }
  196.  
  197. /* This sets all the defaults in a material type definition. */
  198.  
  199. void
  200. default_material_type(m)
  201. int m;
  202. {
  203.     int i;
  204.     
  205.     for (i = 0; mtypedefns[i].name != NULL; ++i) {
  206.     if (mtypedefns[i].intgetter) {
  207.         if (mtypedefns[i].dflt != 0)
  208.           TYPEPROP(mtypes, m, mtypedefns, i, short) = mtypedefns[i].dflt;
  209.     } else if (mtypedefns[i].strgetter) {
  210.         if (mtypedefns[i].dfltstr != NULL)
  211.           TYPEPROP(mtypes, m, mtypedefns, i, char *) = (char *) mtypedefns[i].dfltstr;
  212.     } else {
  213.         TYPEPROP(mtypes, m, mtypedefns, i, Obj *) = lispnil;
  214.     }
  215.     }
  216. }
  217.  
  218. /* This sets all the defaults in a terrain type definition. */
  219.  
  220. void
  221. default_terrain_type(t)
  222. int t;
  223. {
  224.     int i;
  225.     
  226.     for (i = 0; ttypedefns[i].name != NULL; ++i) {
  227.     if (ttypedefns[i].intgetter) {
  228.         if (ttypedefns[i].dflt != 0)
  229.           TYPEPROP(ttypes, t, ttypedefns, i, short) = ttypedefns[i].dflt;
  230.     } else if (ttypedefns[i].strgetter) {
  231.         if (ttypedefns[i].dfltstr != 0)
  232.           TYPEPROP(ttypes, t, ttypedefns, i, char *) = (char *) ttypedefns[i].dfltstr;
  233.     } else {
  234.         TYPEPROP(ttypes, t, ttypedefns, i, Obj *) = lispnil;
  235.     }
  236.     }
  237. }
  238.  
  239. #endif /* SPECIAL */
  240.  
  241. char *
  242. index_type_name(x)
  243. int x;
  244. {
  245.     return ((x) == UTYP ? "unit" : ((x) == MTYP ? "material" : "terrain"));
  246. }
  247.  
  248. /* This function allocates a parameter table and fills it with a default. */
  249.  
  250. void
  251. allocate_table(tbl, reset)
  252. int tbl, reset;
  253. {
  254.     int i, lim1, lim2, dflt = tabledefns[tbl].dflt;
  255.     short *rslt;
  256.  
  257.     if (reset) *(tabledefns[tbl].table) = NULL;
  258.     if (*tabledefns[tbl].table == NULL) {
  259.     lim1 = numtypes_from_index_type(tabledefns[tbl].index1);
  260.     lim2 = numtypes_from_index_type(tabledefns[tbl].index2);
  261.     if (lim1 == 0) {
  262.         run_warning("Can't allocate the %s table, no %s types defined",
  263.             tabledefns[tbl].name, index_type_name(tabledefns[tbl].index1));
  264.         return;
  265.     }
  266.     if (lim2 == 0) {
  267.         run_warning("Can't allocate the %s table, no %s types defined",
  268.             tabledefns[tbl].name, index_type_name(tabledefns[tbl].index2));
  269.         return;
  270.     }
  271.     /* Allocate the table itself. */
  272.     rslt = (short *) xmalloc(lim1 * lim2 * sizeof(short));
  273.     /* Put the table's default everywhere in the table. */
  274.     for (i = 0; i < lim1 * lim2; ++i) rslt[i] = dflt;
  275.     *(tabledefns[tbl].table) = rslt;
  276.     /* For each index, flag that no more types of that sort allowed. */
  277.     switch (tabledefns[tbl].index1) {
  278.       case UTYP: canaddutype = FALSE;  break;
  279.       case MTYP: canaddmtype = FALSE;  break;
  280.       case TTYP: canaddttype = FALSE;  break;
  281.     }
  282.     switch (tabledefns[tbl].index2) {
  283.       case UTYP: canaddutype = FALSE;  break;
  284.       case MTYP: canaddmtype = FALSE;  break;
  285.       case TTYP: canaddttype = FALSE;  break;
  286.     }
  287.     }
  288. }
  289.  
  290. int
  291. numtypes_from_index_type(x)
  292. int x;
  293. {
  294.     return ((x) == UTYP ? numutypes : ((x) == MTYP ? nummtypes : numttypes));
  295. }
  296.