home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / GENTREE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-29  |  2.0 KB  |  79 lines

  1. #include "gentree.h"
  2. #include "voxel.h"
  3. #include "sprinter.h"
  4. #include "fixed.h"
  5. #include "maxmins.h"
  6. #include <time.h>
  7. #include <stdlib.h>
  8.  
  9. #define randomize()     srand((unsigned)time(NULL))
  10.  
  11. #define TREE_TYPE 0
  12. #define TREE_COUNT_SCALER 26000
  13. #define MAX_RAND 32767
  14. #define MIN_DIS_FROM_SEG 10
  15.  
  16. BOOL Within_Segs(pobject new_object);
  17.  
  18. inline long random(long range)
  19. {
  20. return (fixedmd((LONG)rand(), range, MAX_RAND));
  21. }
  22.  
  23. inline BOOL On_Seg_Right(pseg test_seg, long base_x, long base_y) {
  24.    long x1,x2,y1,y2;
  25.    x1=Vector_List[test_seg->v[0]].x;
  26.    y1=Vector_List[test_seg->v[0]].y;
  27.    x2=Vector_List[test_seg->v[1]].x;
  28.    y2=Vector_List[test_seg->v[1]].y;
  29.  
  30.    // this is different from other similar test because I want the trees to fit well into the sub sec
  31.    if ( ( ((x1-x2)*((base_y>>SHIFT)-y2)) - ((y1-y2)*((base_x>>SHIFT)-x2)) ) >= MIN_DIS_FROM_SEG) {
  32.       return TRUE;
  33.    } else {
  34.       return FALSE;
  35.    }
  36. }
  37.  
  38. void Gen_Tree() {
  39. long cur_x, cur_y;
  40. long min_x, min_y, max_x, max_y;
  41. long range_x, range_y, map_area;
  42. long tree_count;
  43. pobject new_tree;
  44.  
  45. Get_Map_Max_Mins(min_x, min_y, max_x, max_y);
  46. range_x=max_x-min_x;
  47. range_y=max_y-min_y;
  48.  
  49. map_area=range_x*range_y;
  50. tree_count=map_area/TREE_COUNT_SCALER;
  51. randomize();
  52. for (short cur_tree=0; cur_tree<tree_count; cur_tree++) {
  53.    cur_x=(random(range_x)+min_x)<<SHIFT;
  54.    cur_y=(random(range_y)+min_y)<<SHIFT;
  55.    new_tree=Create_Object(cur_x, cur_y, 0,0,TREE_TYPE, NULL, 0);
  56.    if (!new_tree->cur_ss->flags&VOXEL_SSECTOR) {
  57.       Kill_Object(new_tree);
  58.    } else {
  59.       if (!Within_Segs(new_tree)) {
  60.          Kill_Object(new_tree);
  61.       }
  62.    }
  63. } /* endfor */
  64.  
  65. }
  66.  
  67. BOOL Within_Segs(pobject new_object) {
  68.    short seg_start=new_object->cur_ss->seg_start;
  69.    short seg_end=new_object->cur_ss->seg_end;
  70.  
  71.    BOOL still_in=TRUE;
  72.    for (short cur_seg=seg_start; cur_seg<=seg_end; cur_seg++) {
  73.       if (!On_Seg_Right(Seg_List+cur_seg, new_object->x, new_object->y)) {
  74.          still_in=FALSE;
  75.       }
  76.    }
  77.    return still_in;
  78. }
  79.