Difference for random_maps/style.c from version 1.12 to 1.13


version 1.12 version 1.13
Line 1
 
Line 1
 /*  /*
  * static char *rcsid_style_c =   * static char *rcsid_style_c =
  *   "$Id: style.c,v 1.12 2001/04/06 19:08:28 michtoen Exp $";   *   "$Id: style.c,v 1.13 2001/05/08 07:11:26 mwedel Exp $";
  */   */
   
 /*  /*
Line 138
 
Line 138
   
 #endif  #endif
   
 object *style_map_object_list[2048];  
 int nrofstyle_map_objects;  
   
   
 /* the warning here is because I've declared it "const", the  /* the warning here is because I've declared it "const", the
Line 151
 
Line 149
 }  }
      
 /* this function loads and returns the map requested.  /* this function loads and returns the map requested.
   dirname, for example, is "/styles/wallstyles", stylename, is,   * dirname, for example, is "/styles/wallstyles", stylename, is,
 for example, "castle", difficulty is -1 when difficulty is   * for example, "castle", difficulty is -1 when difficulty is
 irrelevant to the style.  If dirname is given, but stylename   * irrelevant to the style.  If dirname is given, but stylename
 isn't, and difficult is -1, it returns a random style map.   * isn't, and difficult is -1, it returns a random style map.
 Otherwise, it tries to match the difficulty given with a style   * Otherwise, it tries to match the difficulty given with a style
 file, named style_name_# where # is an integer */   * file, named style_name_# where # is an integer
    */
   
   /* remove extern, so visible to command_style_map_info function */
   mapstruct *styles=NULL;
   
 static mapstruct *styles=NULL;  
   
 mapstruct *load_style_map(char *style_name)  mapstruct *load_style_map(char *style_name)
 {  {
Line 191
 
Line 192
   char style_file_full_path[256];    char style_file_full_path[256];
   mapstruct *style_map = NULL;    mapstruct *style_map = NULL;
   struct stat file_stat;    struct stat file_stat;
       int i;
      
   /* if stylename exists, set style_file_path to that file.*/    /* if stylename exists, set style_file_path to that file.*/
   if(stylename && strlen(stylename)>0)    if(stylename && strlen(stylename)>0)
Line 206
 
Line 207
   
   if(! (S_ISDIR(file_stat.st_mode))) {    if(! (S_ISDIR(file_stat.st_mode))) {
     style_map=load_style_map(style_file_path);      style_map=load_style_map(style_file_path);
   
   }    }
   if(style_map == NULL)  /* maybe we were given a directory! */    if(style_map == NULL)  /* maybe we were given a directory! */
     {      {
Line 227
 
Line 227
  }   }
  else {  /* find the map closest in difficulty */   else {  /* find the map closest in difficulty */
     int min_dist=32000,min_index=-1;      int min_dist=32000,min_index=-1;
     int i;  
     for(i=0;i<n;i++) {      for(i=0;i<n;i++) {
       int dist;        int dist;
       char *mfile_name = strrchr(namelist[i]->d_name,'_')+1;        char *mfile_name = strrchr(namelist[i]->d_name,'_')+1;
   
       if((mfile_name-1) == NULL) { /* since there isn't a sequence, */        if((mfile_name-1) == NULL) { /* since there isn't a sequence, */
        int q;
         /*pick one at random to recurse */          /*pick one at random to recurse */
         return find_style(style_file_path,       style_map= find_style(style_file_path,
  namelist[RANDOM()%n]->d_name,difficulty);   namelist[RANDOM()%n]->d_name,difficulty);
       }       for (q=0; q<n; q++)
            free(namelist[q]);
        free(namelist);
        return style_map;
    } else {
       dist = abs(difficulty-atoi(mfile_name));        dist = abs(difficulty-atoi(mfile_name));
       if(dist<min_dist) {        if(dist<min_dist) {
         min_dist = dist;          min_dist = dist;
         min_index = i;          min_index = i;
       }        }
     }      }
        }
     /* presumably now we've found the "best" match for the      /* presumably now we've found the "best" match for the
  difficulty. */   difficulty. */
     strcat(style_file_path,"/");      strcat(style_file_path,"/");
     strcat(style_file_path,namelist[min_index]->d_name);      strcat(style_file_path,namelist[min_index]->d_name);
     style_map = load_style_map(style_file_path);      style_map = load_style_map(style_file_path);
   
  }   }
     for (i=0; i<n; i++)
        free(namelist[i]);
    free(namelist);
     }      }
   
   return style_map;    return style_map;
   
 }  }
   
 /* picks a random object from a style map.  
  it maintains a data stucture so that if the same stylemap is  
  called several times, it uses the old datastucture. */  
   
   /* picks a random object from a style map.
    * Redone by MSW so it should be faster and not use static
    * variables to generate tables.
    */
 object *pick_random_object(mapstruct *style) {  object *pick_random_object(mapstruct *style) {
   static mapstruct *laststyle=0;      int x,y, i;
     
   /* if this isn't the current style, make the style_map_object_list array  
      for easy searching */  
   if(laststyle!=style)  
  {  
  int i,j;  
  int maxx,maxy;  
  object *new_obj;   object *new_obj;
   
  laststyle=style;      /* If someone makes a style map that is empty, this will loop forever,
  maxx = style->map_object->x;       * but the callers will crash if we return a null object, so either
  maxy = style->map_object->y;       * way is not good.
  nrofstyle_map_objects = 0;       */
  for(i=0;i<maxx;i++) {      do {
    for(j=0;j<maxy;j++) {   i = RANDOM () % (style->map_object->x * style->map_object->y);
  new_obj = get_map_ob(style,i,j);  
  if(new_obj) { /* make sure it's the head: if it is, add it */   x = i / style->map_object->y;
  if(!new_obj->head ) {   y = i % style->map_object->y;
    style_map_object_list[nrofstyle_map_objects] = new_obj;   new_obj = get_map_ob(style,x,y);
    nrofstyle_map_objects++;      } while (new_obj == NULL);
  }      if (new_obj->head) return new_obj->head;
  }      else return new_obj;
    }  
  }  
  }  
   if(nrofstyle_map_objects > 0)  
   return style_map_object_list[RANDOM() % nrofstyle_map_objects];  
   else return NULL;  
 }  }
    
    


Legend:
line(s) removed in v.1.12 
line(s) changed
 line(s) added in v.1.13

File made using version 1.98 of cvs2html by leaf at 2011-07-21 17:58