home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xconq55.zip / xc5.5 / per2c.c < prev    next >
C/C++ Source or Header  |  1991-09-18  |  9KB  |  309 lines

  1. /* Copyright (c) 1987, 1988  Stanley T. Shebs. */
  2. /* This program may be used, copied, modified, and redistributed freely */
  3. /* for noncommercial purposes, so long as this notice remains intact. */
  4.  
  5. /* This is a mini-tool that converts a period description file into a piece */
  6. /* of C code that can be linked in as xconq's default period.  It uses the */
  7. /* normal xconq period reader, and defines the output routine.  Works from */
  8. /* stdio, only option is to turn on debugging if any args present. */
  9. /* The code is simple but lengthy. */
  10.  
  11. #include "config.h"
  12. #include "misc.h"
  13. #include "period.h"
  14.  
  15. /* Declarations for variables set in the period file. */
  16.  
  17. Period period;
  18.  
  19. Utype utypes[MAXUTYPES];
  20.  
  21. Rtype rtypes[MAXRTYPES];
  22.  
  23. Ttype ttypes[MAXTTYPES];
  24.  
  25. char *snames[MAXSNAMES];
  26.  
  27. char *unames[MAXUNAMES];
  28.  
  29. int Debug = FALSE;
  30.  
  31. /* Very simple main program. We do need to soak up the first line of the *
  32. /* period description, which is almost certainly a mapfile header. */
  33. /* Any argument will enable debugging prints in the period reader. */
  34.  
  35. main(argc, argv)
  36. int argc;
  37. char *argv[];
  38. {
  39.     char dummy[BUFSIZE];
  40.  
  41.     if (argc > 1) Debug = TRUE;
  42.     fgets(dummy, BUFSIZE-1, stdin);
  43.     read_period(stdin);
  44.     print_period();
  45.     print_utypes();
  46.     print_rtypes();
  47.     print_ttypes();
  48.     print_snames();
  49.     print_unames();
  50.     exit(0);
  51. }
  52.  
  53. print_period()
  54. {
  55.     printf("#include \"config.h\"\n");
  56.     printf("#include \"period.h\"\n");
  57.     printf("\nPeriod period = { \"%s\", NULL, \"%s\", %d, %d, %d,\n",
  58.        period.name, /* period.notes */ period.fontname, 
  59.        period.countrysize, period.mindistance, period.maxdistance);
  60.     printf(" %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d,\n",
  61.        period.numutypes, period.numrtypes, period.numttypes,
  62.        period.numsnames, period.numunames,
  63.        period.firstutype, period.firstptype,
  64.        period.knownradius, period.allseen,
  65.        period.counterattack, period.capturemoves, period.nukehit, period.neutrality,
  66.        period.efficiency);
  67.     printf(" %d, %d, %d, %d, %d, %d, %d, %d, %d };\n",
  68.        period.altroughness, period.wetroughness, period.machineadvantage,
  69.        period.defaultterrain, period.edgeterrain,
  70.        period.spychance, period.spyquality,
  71.        period.leavemap, period.repairscale);
  72. }
  73.  
  74. print_utypes()
  75. {
  76.     int u, t, u2, r;
  77.  
  78.     printf("\nUtype utypes[MAXUTYPES] = {\n");
  79.     for_all_unit_types(u) {
  80.     printf("{ '%c', \"%s\", \"%s\", NULL, \"%s\",\n",
  81.            utypes[u].uchar, utypes[u].name, utypes[u].help, /* notes, */
  82.            (utypes[u].bitmapname ? utypes[u].bitmapname : ""));
  83.     printf("  %d, %d, %d, %d,\n",
  84.            utypes[u].incountry, utypes[u].density,
  85.            utypes[u].named, utypes[u].alreadyseen);
  86.     printf("  { ");
  87.     for_all_terrain_types(t) printf("%d, ", utypes[u].favored[t]);
  88.     printf(" },\n");
  89.     printf("  { ");
  90.     for_all_resource_types(r) printf("%d, ", utypes[u].stockpile[r]);
  91.     printf(" },\n");
  92.     printf("  %d, %d, %d, %d, \"%s\", \"%s\",\n",
  93.            utypes[u].revolt, utypes[u].surrender, utypes[u].siege,
  94.            utypes[u].attdamage, utypes[u].attritionmsg,
  95.            utypes[u].accidentmsg);
  96.     printf("  { ");
  97.     for_all_terrain_types(t) printf("%d, ", utypes[u].attrition[t]);
  98.     printf(" },\n");
  99.     printf("  { ");
  100.     for_all_terrain_types(t) printf("%d, ", utypes[u].accident[t]);
  101.     printf(" },\n");
  102.     printf("  %d, %d, %d, %d, \n",
  103.            utypes[u].maker, utypes[u].occproduce, utypes[u].startup, utypes[u].research);
  104.     printf("  { ");
  105.     for_all_unit_types(u2) printf("%d, ", utypes[u].research_contrib[u2]);
  106.     printf(" },\n");
  107.     printf("  { ");
  108.     for_all_unit_types(u2) printf("%d, ", utypes[u].make[u2]);
  109.     printf(" },\n");
  110.     printf("  { ");
  111.     for_all_resource_types(r) printf("%d, ", utypes[u].tomake[r]);
  112.     printf(" },\n");
  113.     printf("  { ");
  114.     for_all_unit_types(u2) printf("%d, ", utypes[u].repair[u2]);
  115.     printf(" },\n");
  116.     printf("  %d, \"%s\",\n", utypes[u].survival, utypes[u].starvemsg);
  117.     printf("  { ");
  118.     for_all_resource_types(r) printf("%d, ", utypes[u].produce[r]);
  119.     printf(" },\n");
  120.     printf("  { ");
  121.     for_all_terrain_types(t) printf("%d, ", utypes[u].productivity[t]);
  122.     printf(" },\n");
  123.     printf("  { ");
  124.     for_all_resource_types(r) printf("%d, ", utypes[u].storage[r]);
  125.     printf(" },\n");
  126.     printf("  { ");
  127.     for_all_resource_types(r) printf("%d, ", utypes[u].consume[r]);
  128.     printf(" },\n");
  129.     printf("  { ");
  130.     for_all_resource_types(r) printf("%d, ", utypes[u].inlength[r]);
  131.     printf(" },\n");
  132.     printf("  { ");
  133.     for_all_resource_types(r) printf("%d, ", utypes[u].outlength[r]);
  134.     printf(" },\n");
  135.     printf(" %d, \n", utypes[u].consume_as_occupant);
  136.     printf("  %d,\n",
  137.            utypes[u].speed);
  138.     printf("  { ");
  139.     for_all_terrain_types(t) printf("%d, ", utypes[u].moves[t]);
  140.     printf(" },\n");
  141.     printf("  { ");
  142.     for_all_terrain_types(t) printf("%d, ", utypes[u].randommove[t]);
  143.     printf(" },\n");
  144.     printf("  { ");
  145.     for_all_resource_types(r) printf("%d, ", utypes[u].tomove[r]);
  146.     printf(" },\n");
  147.     printf("  %d, %d,\n", utypes[u].volume, utypes[u].holdvolume);
  148.     printf("  { ");
  149.     for_all_unit_types(u2) printf("%d, ", utypes[u].capacity[u2]);
  150.     printf(" },\n");
  151.     printf("  { ");
  152.     for_all_unit_types(u2) printf("%d, ", utypes[u].entertime[u2]);
  153.     printf(" },\n");
  154.     printf("  { ");
  155.     for_all_unit_types(u2) printf("%d, ", utypes[u].leavetime[u2]);
  156.     printf(" },\n");
  157.     printf("  { ");
  158.     for_all_unit_types(u2) printf("%d, ", utypes[u].bridge[u2]);
  159.     printf(" },\n");
  160.     printf("  { ");
  161.     for_all_unit_types(u2) printf("%d, ", utypes[u].mobility[u2]);
  162.     printf(" },\n");
  163.     printf("  %d, %d, %d, %d, %d,\n",
  164.            utypes[u].seealways, utypes[u].seebest, utypes[u].seeworst,
  165.            utypes[u].seerange, utypes[u].visibility);
  166.     printf("  { ");
  167.     for_all_terrain_types(t) printf("%d, ", utypes[u].conceal[t]);
  168.     printf(" },\n");
  169.     printf(" %d, %d, %d, %d, %d, %d, %d, %d, \"%s\",\n",
  170.            utypes[u].hp, utypes[u].crippled,
  171.            utypes[u].selfdestruct, utypes[u].changeside,
  172.            utypes[u].hittime, utypes[u].retreat,
  173.            utypes[u].counterable, utypes[u].arearadius,
  174.            utypes[u].destroymsg);
  175.     printf("  { ");
  176.     for_all_unit_types(u2) printf("%d, ", utypes[u].hit[u2]);
  177.     printf(" },\n");
  178.     printf("  { ");
  179.     for_all_terrain_types(t) printf("%d, ", utypes[u].defense[t]);
  180.     printf(" },\n");
  181.     printf("  { ");
  182.     for_all_unit_types(u2) printf("%d, ", utypes[u].damage[u2]);
  183.     printf(" },\n");
  184.     printf("  { ");
  185.     for_all_resource_types(r) printf("%d, ", utypes[u].hitswith[r]);
  186.     printf(" },\n");
  187.     printf("  { ");
  188.     for_all_resource_types(r) printf("%d, ", utypes[u].hitby[r]);
  189.     printf(" },\n");
  190.     printf("  { ");
  191.     for_all_unit_types(u2) printf("%d, ", utypes[u].capture[u2]);
  192.     printf(" },\n");
  193.     printf("  { ");
  194.     for_all_unit_types(u2) printf("%d, ", utypes[u].guard[u2]);
  195.     printf(" },\n");
  196.     printf("  { ");
  197.     for_all_unit_types(u2) printf("%d, ", utypes[u].protect[u2]);
  198.     printf(" },\n");
  199.     printf("  %d, %d, %d,\n",
  200.            utypes[u].territory, utypes[u].isneutral, 
  201.            utypes[u].disband);
  202.     printf("  %d, %d, %d, %d,\n",
  203.            utypes[u].attack_worth, utypes[u].defense_worth,
  204.            utypes[u].explore_worth, utypes[u].min_region_size);
  205.     printf("  %d, %d, %d, %d \n",
  206.            utypes[u].is_base, utypes[u].is_base_builder,
  207.            utypes[u].is_transport, utypes[u].is_carrier);
  208.     printf(" },\n");
  209.     }
  210.     printf("};\n");
  211. }
  212.  
  213. print_rtypes()
  214. {
  215.     int r;
  216.  
  217.     if (period.numrtypes > 0) {
  218.     printf("\nRtype rtypes[MAXRTYPES] = {\n");
  219.     for_all_resource_types(r) {
  220.         printf("{ '%c', \"%s\", \"%s\" },\n",
  221.            rtypes[r].rchar, rtypes[r].name, rtypes[r].help);
  222.     }
  223.     printf("};\n");
  224.     } else {
  225.     printf("\nRtype rtypes[MAXRTYPES];\n");
  226.     }
  227. }
  228.  
  229. print_ttypes()
  230. {
  231.     int t;
  232.  
  233.     printf("\nTtype ttypes[MAXTTYPES] = {\n");
  234.     for_all_terrain_types(t) {
  235.     printf("{ '%c', \"%s\", \"%s\",\n",
  236.            ttypes[t].tchar, ttypes[t].name, ttypes[t].color);
  237.     printf("  %d, %d, %d, %d, %d, %d },\n",
  238.            ttypes[t].dark, ttypes[t].nuked,
  239.            ttypes[t].minalt, ttypes[t].maxalt,
  240.            ttypes[t].minwet, ttypes[t].maxwet);
  241.     }
  242.     printf("};\n");
  243. }
  244.  
  245. print_snames()
  246. {
  247.     int i;
  248.  
  249.     if (period.numsnames > 0) {
  250.     printf("\nchar *snames[MAXSNAMES] = {");
  251.     for (i = 0; i < period.numsnames; ++i) {
  252.         if ((i % 5) == 0) printf("\n");
  253.         printf("\"%s\", ", snames[i]);
  254.     }
  255.     printf("};\n");
  256.     }
  257. }
  258.  
  259. print_unames()
  260. {
  261.     int i;
  262.  
  263.     if (period.numunames > 0) {
  264.     printf("\nchar *unames[MAXUNAMES] = {");
  265.     for (i = 0; i < period.numunames; ++i) {
  266.         if ((i % 5) == 0) printf("\n");
  267.         printf("\"%s\", ", unames[i]);
  268.     }
  269.     printf("};\n");
  270.     }
  271. }
  272.  
  273. /* Read a line and save it away.  This routine should be used sparingly, */
  274. /* since the malloced space is never freed. */
  275.  
  276. char *
  277. read_line(fp)
  278. FILE *fp;
  279. {
  280.     char tmp[BUFSIZE], *line;
  281.  
  282.     fgets(tmp, BUFSIZE-1, fp); tmp[strlen(tmp)-1] = '\0';
  283.     line = (char *) malloc(strlen(tmp)+2);
  284.     strcpy(line, tmp);
  285.     return line;
  286. }
  287.  
  288. char *
  289. copy_string(str)
  290. char *str;
  291. {
  292.     char *rslt;
  293.  
  294.     rslt = (char *) malloc(strlen(str)+1);
  295.     strcpy(rslt, str);
  296.     return rslt;
  297. }
  298.  
  299. /* This little routine goes at the end of all case statements on internal */
  300. /* data that shouldn't go out of bounds.  We want a core dump to debug. */
  301.  
  302. case_panic(str, var)
  303. char *str;
  304. int var;
  305. {
  306.     fprintf(stderr, "Panic! Unknown %s %d\n", str, var);
  307.     abort();
  308. }
  309.