Advertisement
Pieman7373

early generation code (possible infinite loop)

Oct 14th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <tice.h>
  5.  
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <graphx.h>
  11. #include <keypadc.h>
  12. #include <debug.h>
  13.  
  14. #include "structs.h"
  15.  
  16. /* Put your function prototypes here */
  17. void mapgen(void);
  18. void genrooms(void);
  19. void randroom(int,int,int,int);
  20. bool placeroom(int,int);
  21. bool doesroomfit(int,int,int,int);
  22. void mazeworm(void);
  23. void drawmap(void);
  24.  
  25. /* Put all your globals here */
  26. int _x;
  27. int _y;
  28. int offset = 10;
  29. int xmin = 0;
  30. int xmax = 30;
  31. int ymin = 0;
  32. int ymax = 20;
  33. int treecolor = 0x03;
  34. int floorcolor = 0xFF;
  35. int wallcolor = 0xE0;
  36. int min_w = 3;
  37. int min_h = 3;
  38. int max_w=10;
  39. int max_h=10;
  40. int num_rcands;
  41. int room_x;
  42. int room_y;
  43. int room_w;
  44. int room_h;
  45.  
  46. room_t r[600];
  47.  
  48. int map[30][20];
  49.  
  50. void main(void) {
  51.     gfx_Begin();
  52.    
  53.     mapgen();
  54.     drawmap();
  55.    
  56.     do {
  57.         kb_Scan();
  58.     } while (!(kb_Data[6] & kb_Clear));
  59.    
  60.     gfx_End();
  61. }
  62.  
  63. /* Put other functions here */
  64. void drawmap() {
  65.     gfx_FillScreen(0x00);
  66.     for (_x=xmin;_x<xmax;_x++){
  67.         for (_y=ymin;_y<ymax;_y++){
  68.             if (map[_x][_y] == 1){
  69.                 gfx_SetColor(wallcolor);
  70.             }
  71.             else if (map[_x][_y] == 0){
  72.                 gfx_SetColor(floorcolor);
  73.             }
  74.             gfx_SetTextXY((_x*8)+offset,(_y*8)+offset);
  75.             gfx_PrintStringXY("o",(_x*8)+offset,(_y*8)+offset);
  76.         }
  77.     }
  78. }
  79.  
  80. void mapgen() {
  81.     genrooms();
  82.     //mazeworm();
  83.     //placeflags();
  84. }
  85.  
  86. void genrooms() {
  87.     int fmax=10;
  88.     int rmax=10;
  89.     max_w = 10;
  90.     max_h = 10;
  91.     randroom(min_w,max_w,min_h,max_h);
  92.     do {
  93.         if (placeroom(room_w, room_h)) {
  94.             rmax--;
  95.         }
  96.         else {
  97.             fmax--;
  98.         }
  99.     } while (fmax > 0 || rmax > 0);
  100. }
  101.  
  102. void randroom(int mn_w,int mx_w,int mn_h,int mx_h) {
  103.     room_w = randInt(mn_w,mx_w);
  104.     room_h = randInt(mn_h,mx_h);
  105. }
  106.  
  107. bool placeroom(int w,int h) {
  108.     int r_x;
  109.     int r_y;
  110.     int R;
  111.     int rp_x;
  112.     int rp_y;
  113.     int mx;
  114.     int my;
  115.     num_rcands = 0;
  116.     for (r_x=xmin;r_x<(xmax-w);r_x++){
  117.         for (r_y=ymin;r_y<(ymax-h);r_y++){
  118.             if (doesroomfit(r_x,r_y,w,h)) {
  119.                 r[num_rcands].x=r_x;
  120.                 r[num_rcands].y=r_y;
  121.                 r[num_rcands].w=w;
  122.                 r[num_rcands].h=h;
  123.                 num_rcands++;
  124.             }
  125.         }
  126.     }
  127.     if (num_rcands==0){
  128.         return false;
  129.     }
  130.     R=randInt(0,num_rcands);
  131.     for (rp_x=r[R].x;rp_x<(r[R].x+r[R].w);rp_x++){
  132.         for (rp_y=r[R].y;rp_y<(r[R].y+r[R].h);rp_y++){
  133.             mx=r[R].x;
  134.             my=r[R].y;
  135.             map[mx][my]=0;
  136.         }
  137.     }
  138.     return true;
  139. }
  140.  
  141. bool doesroomfit(int x,int y,int w,int h) {
  142.     int r_x;
  143.     int r_y;
  144.     for (r_x=(x-1);r_x<x+(w+1);r_x++){
  145.         for (r_y=(y-1);r_y<y+(h+1);r_y++){
  146.             if (map[r_x][r_y]==0){
  147.                 if (w>=h) {
  148.                     max_w--;
  149.                 }
  150.                 else {
  151.                     max_h--;
  152.                 }
  153.                 return false;
  154.             }
  155.         }
  156.     }
  157.     return true;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement