home *** CD-ROM | disk | FTP | other *** search
/ Superpower (Alt) / SUPERPOWER.iso / q / source / mbq198 / entmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-26  |  6.1 KB  |  371 lines

  1. // entmap.c -- a program for remapping entity key/value pairs
  2.  
  3. #include "bsp5.h"
  4.  
  5.  
  6.  
  7. #define    MAXTOKEN    128
  8.  
  9. char    token[MAXTOKEN];
  10. qboolean    unget;
  11. char    *script_p;
  12. int        scriptline;
  13. FILE    *f;
  14.  
  15. void    StartTokenParsing (char *data)
  16. {
  17.     scriptline = 1;
  18.     script_p = data;
  19.     unget = false;
  20. }
  21.  
  22. qboolean GetToken (qboolean crossline)
  23. {
  24.     char    *token_p;
  25.  
  26.     if (unget)                         // is a token allready waiting?
  27.     {
  28.         unget = false;
  29.         return true;
  30.     }
  31.  
  32. //
  33. // skip space
  34. //
  35. skipspace:
  36.     while (*script_p <= 32)
  37.     {
  38.         if (!*script_p)
  39.         {
  40.             if (!crossline)
  41.                 Error ("Line %i is incomplete",scriptline);
  42.             return false;
  43.         }
  44.         if (*script_p++ == '\n')
  45.         {
  46.             if (!crossline)
  47.                 Error ("Line %i is incomplete",scriptline);
  48.             scriptline++;
  49.         }
  50.     }
  51.  
  52.     if (script_p[0] == '/' && script_p[1] == '/')    // comment field
  53.     {
  54.         if (!crossline)
  55.             Error ("Line %i is incomplete\n",scriptline);
  56.         while (*script_p++ != '\n')
  57.             if (!*script_p)
  58.             {
  59.                 if (!crossline)
  60.                     Error ("Line %i is incomplete",scriptline);
  61.                 return false;
  62.             }
  63.         goto skipspace;
  64.     }
  65.  
  66. //
  67. // copy token
  68. //
  69.     token_p = token;
  70.  
  71.     if (*script_p == '"')
  72.     {
  73.         script_p++;
  74.         while ( *script_p != '"' )
  75.         {
  76.             if (!*script_p)
  77.                 Error ("EOF inside quoted token");
  78.             *token_p++ = *script_p++;
  79.             if (token_p == &token[MAXTOKEN])
  80.                 Error ("Token too large on line %i",scriptline);
  81.         }
  82.         script_p++;
  83.     }
  84.     else while ( *script_p > 32 )
  85.     {
  86.         *token_p++ = *script_p++;
  87.         if (token_p == &token[MAXTOKEN])
  88.             Error ("Token too large on line %i",scriptline);
  89.     }
  90.  
  91.     *token_p = 0;
  92.     
  93.     return true;
  94. }
  95.  
  96. void UngetToken ()
  97. {
  98.     unget = true;
  99. }
  100.  
  101.  
  102. //============================================================================
  103.  
  104. int            num_entities;
  105. entity_t    entity;
  106. entity_t    *ent = &entity;
  107.  
  108. char     *ValueForKey (entity_t *ent, char *key)
  109. {
  110.     epair_t    *ep;
  111.     
  112.     for (ep=ent->epairs ; ep ; ep=ep->next)
  113.         if (!strcmp (ep->key, key) )
  114.             return ep->value;
  115.     return "";
  116. }
  117.  
  118. /*
  119. =================
  120. MapEntity
  121. =================
  122. */
  123. void MapEntity (void)
  124. {
  125. #if 0
  126.     char    *class;
  127.     
  128.     class = ValueForKey (ent, "classname");
  129.     
  130.     if (!strcmp(class, "path_runcorner")
  131.     || !strcmp(class, "path_stand")
  132.     || !strcmp(class, "path_walkcorner")
  133.     || !strcmp(class, "path_bow") )
  134.     {
  135.         printf ("remapped %s\n", class);
  136.         strcpy (class, "path_corner");
  137.         return;
  138.     }
  139. #endif
  140. }
  141.  
  142. /*
  143. =================
  144. ParseEpair
  145. =================
  146. */
  147. void ParseEpair (void)
  148. {
  149.     epair_t    *e;
  150.     
  151.     e = malloc (sizeof(epair_t));
  152.     memset (e, 0, sizeof(epair_t));
  153.     e->next = ent->epairs;
  154.     ent->epairs = e;
  155.     
  156.     strcpy (e->key, token);
  157.     GetToken (false);
  158.     strcpy (e->value, token);
  159. }
  160.  
  161.  
  162. /*
  163. =================
  164. CopyBrush
  165.  
  166. Just copy the brush to the output file
  167. =================
  168. */
  169. qboolean CopyBrush (void)
  170. {
  171.     int        i, j;
  172.     int        pts[3][3];
  173.     char    texname[32];
  174.     int        sofs, tofs, flags;
  175.     int        scale[2], shift[2], rotate;
  176.  
  177.     fprintf (f,"{\n");
  178.     while (1)
  179.     {
  180.     //
  181.     // read it in the old way
  182.     //
  183.         GetToken (true);
  184.         if (token[0] == '}')
  185.             break;
  186.         UngetToken ();
  187.         
  188.         for (i=0 ; i<3 ; i++)
  189.         {
  190.             GetToken (false);
  191.             if (token[0] != '(')
  192.                 return false; //Error ("CopyBrush: couldn't parse");
  193.             for (j=0 ; j<3 ; j++)
  194.             {
  195.                 GetToken (false);
  196.                 pts[i][j] = atoi(token);
  197.             }
  198.             GetToken (false);
  199.             if (token[0] != ')')
  200.                 return false; //Error ("CopyBrush: couldn't parse");
  201.         }
  202.         
  203.         GetToken (false);
  204.         strcpy (texname, token);
  205.         GetToken (false);
  206.         sofs = atoi(token);
  207.         GetToken (false);
  208.         tofs = atoi(token);
  209.         GetToken (false);
  210.         flags = atoi(token);
  211.  
  212. //
  213. // convert to new definitions
  214. //        
  215.         flags &= 7;
  216.  
  217.         shift[0] = sofs;
  218.         shift[1] = tofs;
  219.  
  220.         rotate = 0;
  221.         scale[0] = 1;
  222.         scale[1] = 1;
  223.  
  224. #define    TEX_FLIPAXIS    1
  225. #define    TEX_FLIPS        2
  226. #define    TEX_FLIPT        4
  227.  
  228.         if (flags & TEX_FLIPAXIS)
  229.         {
  230.             rotate = 90;
  231.             if ( !(flags & TEX_FLIPT) )
  232.                 scale[0] = -1;
  233.             if (flags & TEX_FLIPS)
  234.                 scale[1] = -1;
  235.         }
  236.         else
  237.         {
  238.             if (flags & TEX_FLIPS)
  239.                 scale[0] = -1;
  240.             if (flags & TEX_FLIPT)
  241.                 scale[1] = -1;
  242.         }
  243.             
  244.  
  245.     //
  246.     // write it out the new way
  247.     //
  248.         for (i=0 ; i<3 ; i++)
  249.             fprintf (f, "( %d %d %d ) ", pts[i][0], pts[i][1], pts[i][2]);
  250.         fprintf (f, "%s %d %d %d %d %d\n", texname, shift[0], shift[1], rotate, scale[0], scale[1]);
  251.     }
  252.  
  253.     fprintf (f,"}\n");
  254.     
  255.     return true;
  256. }
  257.  
  258. /*
  259. ================
  260. ParseEntity
  261. ================
  262. */
  263. int    ParseEntity (void)
  264. {
  265.     epair_t    *ep;
  266.     
  267.     if (!GetToken (true))
  268.         return false;
  269.  
  270.     if (strcmp (token, "{") )
  271.         Error ("ParseEntity: { not found");
  272.     
  273.     fprintf (f,"{\n");
  274.     
  275.     ent->epairs = NULL;
  276.     
  277.     do
  278.     {
  279.         if (!GetToken (true))
  280.             Error ("ParseEntity: EOF without closing brace");
  281.         if (!strcmp (token, "}") )
  282.             break;
  283.         if (!strcmp (token, "{") )
  284.         {
  285.             if (!CopyBrush ())
  286.                 return -1;
  287.         }
  288.         else
  289.             ParseEpair ();
  290.     } while (1);
  291.     
  292. // map the epairs
  293.     MapEntity ();
  294.     
  295. // write the epairs
  296.     for (ep = ent->epairs ; ep ; ep=ep->next)
  297.         fprintf (f,"\"%s\" \"%s\"\n", ep->key, ep->value);
  298.     
  299.     fprintf (f,"}\n");
  300.  
  301.     return true;
  302. }
  303.  
  304. /*
  305. ================
  306. Remap
  307. ================
  308. */
  309. void Remap (char *filename)
  310. {
  311.     char    *buf;
  312.     char    backname[1024];
  313.     char    workname[1024];
  314.     int        r;
  315.  
  316.     printf ("--------------\n%s\n", filename);
  317.     
  318.     LoadFile (filename, (void **)&buf);
  319.     strcpy (backname, filename);
  320.     StripExtension (backname);
  321.     strcpy (workname, backname);
  322.     strcat (backname, ".old");
  323.     strcat (workname, ".tmp");
  324.  
  325.     remove (workname);
  326.  
  327.     f = fopen (workname, "w");
  328.     if (!f)
  329.         Error ("Couldn't write to %s", backname);
  330.  
  331.     StartTokenParsing (buf);
  332.     
  333.     num_entities = 0;
  334.     
  335.     while (1)
  336.     {
  337.         r = ParseEntity ();
  338.         if (r != 1)
  339.             break;    
  340.         num_entities++;
  341.     }
  342.     
  343.     free (buf);
  344.  
  345.     fclose (f);
  346.     
  347.     if (r == -1)
  348.     {
  349.         printf ("Error parsing %s\n", filename);
  350.         return;
  351.     }
  352.     
  353.     printf ("%5i entities\n", num_entities);
  354.     remove (backname);
  355.     rename (filename, backname);
  356.     rename (workname, filename);
  357. }
  358.  
  359.  
  360. void main (int argc, char **argv)
  361. {
  362.     int        i;
  363.     
  364.     if (argc<2)
  365.         Error ("USAGE: entmap [mapfile]\nRenames the map to .old, then remaps entity values");
  366.         
  367.     for (i=1 ; i<argc ; i++)
  368.         Remap (argv[i]);
  369. }
  370.  
  371.