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

  1. // entities.c
  2.  
  3. #include "light.h"
  4.  
  5. entity_t    entities[MAX_MAP_ENTITIES];
  6. int            num_entities;
  7.  
  8. /*
  9. ==============================================================================
  10.  
  11. ENTITY FILE PARSING
  12.  
  13. If a light has a targetname, generate a unique style in the 32-63 range
  14. ==============================================================================
  15. */
  16.  
  17. int        numlighttargets;
  18. char    lighttargets[32][64];
  19.  
  20. int LightStyleForTargetname (char *targetname, qboolean alloc)
  21. {
  22.     int        i;
  23.     
  24.     for (i=0 ; i<numlighttargets ; i++)
  25.         if (!strcmp (lighttargets[i], targetname))
  26.             return 32 + i;
  27.     if (!alloc)
  28.         return -1;
  29.     strcpy (lighttargets[i], targetname);
  30.     numlighttargets++;
  31.     return numlighttargets-1 + 32;
  32. }
  33.  
  34.  
  35. /*
  36. ==================
  37. MatchTargets
  38. ==================
  39. */
  40. void MatchTargets (void)
  41. {
  42.     int        i,j;
  43.     
  44.     for (i=0 ; i<num_entities ; i++)
  45.     {
  46.         if (!entities[i].target[0])
  47.             continue;
  48.             
  49.         for (j=0 ; j<num_entities ; j++)
  50.             if (!strcmp(entities[j].targetname, entities[i].target))
  51.             {
  52.                 entities[i].targetent = &entities[j];
  53.                 break;
  54.             }
  55.         if (j==num_entities)
  56.         {
  57.             printf ("WARNING: entity at (%i,%i,%i) (%s) has unmatched target\n", (int)entities[i].origin[0], (int)entities[i].origin[1], (int)entities[i].origin[2], entities[i].classname);
  58.             continue;
  59.         }
  60.         
  61. // set the style on the source ent for switchable lights
  62.         if (entities[j].style)
  63.         {
  64.             char    s[16];
  65.             
  66.             entities[i].style = entities[j].style;
  67.             sprintf (s,"%i", entities[i].style);
  68.             SetKeyValue (&entities[i], "style", s);
  69.         }
  70.     }    
  71. }
  72.  
  73.  
  74. /*
  75. ==================
  76. LoadEntities
  77. ==================
  78. */
  79. void LoadEntities (void)
  80. {
  81.     char         *data;
  82.     entity_t    *entity;
  83.     char        key[64];    
  84.     epair_t        *epair;
  85.     
  86.     data = dentdata;
  87. //
  88. // start parsing
  89. //
  90.     num_entities = 0;
  91.     
  92. // go through all the entities
  93.     while (1)
  94.     {
  95.     // parse the opening brace    
  96.         data = COM_Parse (data);
  97.         if (!data)
  98.             break;
  99.         if (com_token[0] != '{')
  100.             Error ("LoadEntities: found %s when expecting {",com_token);
  101.  
  102.         if (num_entities == MAX_MAP_ENTITIES)
  103.             Error ("LoadEntities: MAX_MAP_ENTITIES");
  104.         entity = &entities[num_entities];
  105.         num_entities++;
  106.         
  107.     // go through all the keys in this entity
  108.         while (1)
  109.         {
  110.             int        c;
  111.  
  112.         // parse key
  113.             data = COM_Parse (data);
  114.             if (!data)
  115.                 Error ("LoadEntities: EOF without closing brace");
  116.             if (!strcmp(com_token,"}"))
  117.                 break;
  118.             strcpy (key, com_token);
  119.  
  120.         // parse value
  121.             data = COM_Parse (data);
  122.             if (!data)
  123.                 Error ("LoadEntities: EOF without closing brace");
  124.             c = com_token[0];
  125.             if (c == '}')
  126.                 Error ("LoadEntities: closing brace without data");
  127.             
  128.             epair = malloc (sizeof(epair_t));
  129.             memset (epair, 0, sizeof(epair));
  130.             strcpy (epair->key, key);
  131.             strcpy (epair->value, com_token);
  132.             epair->next = entity->epairs;
  133.             entity->epairs = epair;
  134.             
  135.             if (!strcmp(key, "classname"))
  136.                 strcpy (entity->classname, com_token);
  137.             else if (!strcmp(key, "target"))
  138.                 strcpy (entity->target, com_token);            
  139.             else if (!strcmp(key, "targetname"))
  140.                 strcpy (entity->targetname, com_token);
  141.             else if (!strcmp(key, "origin"))
  142.             {
  143.                 if (sscanf(com_token, "%lf %lf %lf",
  144.                         &entity->origin[0],
  145.                         &entity->origin[1],
  146.                         &entity->origin[2]) != 3)
  147.                     Error ("LoadEntities: not 3 values for origin");
  148.             }
  149.             else if (!strncmp(key, "light", 5))
  150.             {
  151.                 entity->light = atof(com_token);
  152.             }
  153.             else if (!strcmp(key, "style"))
  154.             {
  155.                 entity->style = atof(com_token);
  156.                 if ((unsigned)entity->style > 254)
  157.                     Error ("Bad light style %i (must be 0-254)", entity->style);
  158.             }
  159.             else if (!strcmp(key, "angle"))
  160.             {
  161.                 entity->angle = atof(com_token);
  162.             }
  163.         
  164.         }
  165.  
  166.     // all fields have been parsed
  167.         if (!strncmp (entity->classname, "light", 5) && !entity->light)
  168.             entity->light = DEFAULTLIGHTLEVEL;
  169.  
  170.         if (!strcmp (entity->classname, "light"))
  171.         {
  172.             if (entity->targetname[0] && !entity->style)
  173.             {
  174.                 char    s[16];
  175.                 
  176.                 entity->style = LightStyleForTargetname (entity->targetname, true);
  177.                 sprintf (s,"%i", entity->style);
  178.                 SetKeyValue (entity, "style", s);
  179.             }
  180.         }
  181.     }
  182.  
  183.     printf ("%d entities read\n", num_entities);
  184.  
  185.     MatchTargets ();
  186. }
  187.  
  188. char     *ValueForKey (entity_t *ent, char *key)
  189. {
  190.     epair_t    *ep;
  191.     
  192.     for (ep=ent->epairs ; ep ; ep=ep->next)
  193.         if (!strcmp (ep->key, key) )
  194.             return ep->value;
  195.     return "";
  196. }
  197.  
  198. void     SetKeyValue (entity_t *ent, char *key, char *value)
  199. {
  200.     epair_t    *ep;
  201.     
  202.     for (ep=ent->epairs ; ep ; ep=ep->next)
  203.         if (!strcmp (ep->key, key) )
  204.         {
  205.             strcpy (ep->value, value);
  206.             return;
  207.         }
  208.     ep = malloc (sizeof(*ep));
  209.     ep->next = ent->epairs;
  210.     ent->epairs = ep;
  211.     strcpy (ep->key, key);
  212.     strcpy (ep->value, value);
  213. }
  214.  
  215. float    FloatForKey (entity_t *ent, char *key)
  216. {
  217.     char    *k;
  218.     
  219.     k = ValueForKey (ent, key);
  220.     return atof(k);
  221. }
  222.  
  223. void     GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
  224. {
  225.     char    *k;
  226.     
  227.     k = ValueForKey (ent, key);
  228.     sscanf (k, "%lf %lf %lf", &vec[0], &vec[1], &vec[2]);
  229. }
  230.  
  231.  
  232.  
  233. /*
  234. ================
  235. WriteEntitiesToString
  236. ================
  237. */
  238. void WriteEntitiesToString (void)
  239. {
  240.     char    *buf, *end;
  241.     epair_t    *ep;
  242.     char    line[128];
  243.     int        i;
  244.     
  245.     buf = dentdata;
  246.     end = buf;
  247.     *end = 0;
  248.     
  249.     printf ("%i switchable light styles\n", numlighttargets);
  250.     
  251.     for (i=0 ; i<num_entities ; i++)
  252.     {
  253.         ep = entities[i].epairs;
  254.         if (!ep)
  255.             continue;    // ent got removed
  256.         
  257.         strcat (end,"{\n");
  258.         end += 2;
  259.                 
  260.         for (ep = entities[i].epairs ; ep ; ep=ep->next)
  261.         {
  262.             sprintf (line, "\"%s\" \"%s\"\n", ep->key, ep->value);
  263.             strcat (end, line);
  264.             end += strlen(line);
  265.         }
  266.         strcat (end,"}\n");
  267.         end += 2;
  268.  
  269.         if (end > buf + MAX_MAP_ENTSTRING)
  270.             Error ("Entity text too long");
  271.     }
  272.     entdatasize = end - buf + 1;
  273. }
  274.  
  275.