Share Pastebin
Guest
Public paste!

Epitaph64

By: a guest | Oct 18th, 2009 | Syntax: C++ | Size: 7.21 KB | Hits: 93 | Expires: Never
Copy text to clipboard
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <curses.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int map[80][25];
  10. bool rM[80][25];
  11. bool checked[80][25];
  12. int tally;
  13. int px = 0;
  14. int py = 0;
  15.  
  16. void expand(int x, int y)
  17. {
  18.         if (x >= 0 && y >= 0 && x < 80 && y < 25)
  19.         {
  20.                 if (!checked[x][y])
  21.                 {
  22.                         checked[x][y] = true;
  23.                         tally ++;
  24.                         if (map[x-1][y] != 1)
  25.                         {
  26.                                 expand(x-1,y);
  27.                         }
  28.                         if (map[x+1][y] != 1)
  29.                         {
  30.                                 expand(x+1,y);
  31.                         }
  32.                         if (map[x][y-1] != 1)
  33.                         {
  34.                                 expand(x,y-1);
  35.                         }
  36.                         if (map[x][y+1] != 1)
  37.                         {
  38.                                 expand(x,y+1);
  39.                         }
  40.                 }
  41.         }
  42. }
  43.  
  44. int sizeOfArea(int x, int y)
  45. {
  46.         int size = 0;
  47.         tally = 0;
  48.         expand(x, y);
  49.         size = tally;
  50.         return size;
  51. }
  52.  
  53. void gen()
  54. {
  55.         for (int x = 0; x < 80; x++)
  56.         {
  57.                 for (int y = 0; y < 25; y++)
  58.                 {
  59.                         checked[x][y] = false;
  60.                         rM[x][y] = false;
  61.                         map[x][y] = 1;
  62.                 }
  63.         }
  64.         bool working = true;
  65.         int roomsMade = 0;
  66.         int att = 0;
  67.         while(working)
  68.         {
  69.                 int nx = rand()%73+1;
  70.                 int ny = rand()%18+1;
  71.                 int w = 7 + rand() % 10;
  72.                 int h = 7 + rand() % 7;
  73.                 bool allowed = true;
  74.                 if (nx + w > 79 || ny + h > 24)
  75.                 {
  76.                         allowed = false;
  77.                 }
  78.                 if (allowed)
  79.                 {
  80.                         for (int x = nx; x < nx + w; x++)
  81.                         {
  82.                                 for (int y = ny; y < ny + h; y++)
  83.                                 {
  84.                                         if (rM[x][y])
  85.                                         {
  86.                                                 allowed = false;
  87.                                         }
  88.                                 }
  89.                         }
  90.                 }
  91.                 if (allowed)
  92.                 {
  93.                         roomsMade ++;
  94.                         for (int x = nx + 1; x < nx + w - 1; x++)
  95.                         {
  96.                                 for (int y = ny + 1; y < ny + h - 1; y++)
  97.                                 {
  98.                                         rM[x][y] = true;
  99.                                         map[x][y] = 0;
  100.                                 }
  101.                         }
  102.                         map[nx+(w/2)][ny+(h/2)] = 2;
  103.                 }
  104.                 att++;
  105.                 if (att > 250)
  106.                 {
  107.                         working = false;
  108.                 }
  109.         }
  110.         for (int x = 1; x < 79; x++)
  111.         {
  112.                 for (int y = 1; y < 24; y++)
  113.                 {
  114.                         if (map[x][y] == 2)
  115.                         {
  116.                                 bool success = false;
  117.                                 int att = 0;
  118.                                 while(!success)
  119.                                 {
  120.                                         att ++;
  121.                                         if (att > 5)
  122.                                         {
  123.                                                 success = true;
  124.                                         }
  125.                                         bool r = false;
  126.                                         for (int i = x+1; i < x + 20; i++)
  127.                                         {
  128.                                                 if (i < 80)
  129.                                                 {
  130.                                                         if (map[i][y] == 0)
  131.                                                         {
  132.                                                                 success = true;
  133.                                                                 r = true;
  134.                                                                 for (int j = x; j < i; j++)
  135.                                                                 {
  136.                                                                         if (map[j][y] != 2)
  137.                                                                         {
  138.                                                                                 map[j][y] = 0;
  139.                                                                         }
  140.                                                                 }
  141.                                                         }
  142.                                                 }
  143.                                         }
  144.                                         for (int i = y+1; i < y + 20; i++)
  145.                                         {
  146.                                                 if (i < 25)
  147.                                                 {
  148.                                                         if (map[x][i] == 0)
  149.                                                         {
  150.                                                                 success = true;
  151.                                                                 r = true;
  152.                                                                 for (int j = y; j < i; j++)
  153.                                                                 {
  154.                                                                         if (map[x][j] != 2)
  155.                                                                         {
  156.                                                                                 map[x][j] = 0;
  157.                                                                         }
  158.                                                                         else
  159.                                                                         {
  160.                                                                                 px = x;
  161.                                                                                 py = j;
  162.                                                                         }
  163.                                                                 }
  164.                                                         }
  165.                                                 }
  166.                                         }
  167.                                         if (!r)
  168.                                         {
  169.                                                 for (int i = x-1; x > x - 20; x--)
  170.                                                 {
  171.                                                         if (i >= 0)
  172.                                                         {
  173.                                                                 if (map[i][y] == 0)
  174.                                                                 {
  175.                                                                         success = true;
  176.                                                                         for (int j = x; j > i; j--)
  177.                                                                         {
  178.                                                                                 if (map[j][y] != 2)
  179.                                                                                 {
  180.                                                                                         map[j][y] = 0;
  181.                                                                                 }
  182.                                                                         }
  183.                                                                 }
  184.                                                         }
  185.                                                 }
  186.                                                 for (int i = y-1; y > y - 20; y--)
  187.                                                 {
  188.                                                         if (i >= 0)
  189.                                                         {
  190.                                                                 if (map[x][i] == 0)
  191.                                                                 {
  192.                                                                         success = true;
  193.                                                                         for (int j = y; j > i; j--)
  194.                                                                         {
  195.                                                                                 if (map[x][j] != 2)
  196.                                                                                 {
  197.                                                                                         map[x][j] = 0;
  198.                                                                                 }
  199.                                                                         }
  200.                                                                 }
  201.                                                         }
  202.                                                 }
  203.                                         }
  204.                                 }
  205.                                 map[x][y] = 0;
  206.                         }
  207.                 }
  208.         }
  209.         //Chance to generate decrepit dungeon floor
  210.         if (rand()%100 > 95)
  211.         {
  212.                 for (int x = 1; x < 79; x++)
  213.                 {
  214.                         for (int y = 1; y < 24; y++)
  215.                         {
  216.                                 if (map[x][y] == 1)
  217.                                 {
  218.                                         if (map[x-1][y] == 0 || map[x-1][y-1] == 0 || map[x][y-1] == 0 || map[x+1][y] == 0)
  219.                                         {
  220.                                                 if (map[x+1][y+1] == 0 || map[x+1][y-1] == 0 || map[x][y+1] == 0 || map[x-1][y+1] == 0)
  221.                                                 {
  222.                                                         map[x][y] = 5;
  223.                                                 }
  224.                                         }
  225.                                 }
  226.                         }
  227.                 }
  228.                 for (int x = 1; x < 79; x++)
  229.                 {
  230.                         for (int y = 1; y < 24; y++)
  231.                         {
  232.                                 if (map[x][y] == 5)
  233.                                 {
  234.                                         if (rand()%100 > 75)
  235.                                         {
  236.                                                 map[x][y] = 0;
  237.                                         }
  238.                                         else
  239.                                         {
  240.                                                 map[x][y] = 1;
  241.                                         }
  242.                                 }
  243.                         }
  244.                 }
  245.         }
  246.         //Check map is acceptable size
  247.         if (sizeOfArea(px,py) < 500)
  248.         {
  249.                 gen();
  250.         }
  251.         else
  252.         {
  253.                 //Fill in the areas (for now) that aren't connected to the main chamber
  254.                 //Later on, they will be flagged as vaults or side areas (so as to avoid stair placement at the very least)
  255.                 for (int x = 0; x < 80; x++)
  256.                 {
  257.                         for (int y = 0; y < 25; y++)
  258.                         {
  259.                                 if (!checked[x][y])
  260.                                 {
  261.                                         map[x][y] = 1;
  262.                                 }
  263.                                 else
  264.                                 {
  265.                                         if (map[x][y] == 0)
  266.                                         {
  267.                                                 if (px == 0 || py == 0)
  268.                                                 {
  269.                                                         px = x;
  270.                                                         py = y;
  271.                                                 }
  272.                                                 else
  273.                                                 {
  274.                                                         if (rand()%1000 > 995)
  275.                                                         {
  276.                                                                 px = x;
  277.                                                                 py = y;
  278.                                                         }
  279.                                                 }
  280.                                         }
  281.                                 }
  282.                         }
  283.                 }
  284.         }
  285.         //Generate doors
  286.         for (int x = 0; x < 80; x++)
  287.         {
  288.                 for (int y = 0; y < 25; y++)
  289.                 {
  290.                         if (map[x][y] == 0)
  291.                         {
  292.                                 if (y - 1 > 0 && y + 1 < 25 && x - 1 > 0 && x + 1 < 80)
  293.                                 {
  294.                                         if (map[x][y-1] == 1 && map[x][y+1] == 1)
  295.                                         {
  296.                                                 if (map[x-1][y-1] == 0 && map[x-1][y+1] == 0 && map[x+1][y] == 0)
  297.                                                 {
  298.                                                         if (rand()%10>7)
  299.                                                         map[x][y] = 2;
  300.                                                 }
  301.                                                 if (map[x+1][y-1] == 0 && map[x+1][y+1] == 0 && map[x-1][y] == 0)
  302.                                                 {
  303.                                                         if (rand()%10>7)
  304.                                                         map[x][y] = 2;
  305.                                                 }
  306.                                         }
  307.                                         if (map[x-1][y] == 1 && map[x+1][y] == 1)
  308.                                         {
  309.                                                 if (map[x-1][y-1] == 0 && map[x+1][y-1] == 0 && map[x][y+1] == 0)
  310.                                                 {
  311.                                                         if (rand()%10>7)
  312.                                                         map[x][y] = 2;
  313.                                                 }
  314.                                                 if (map[x-1][y+1] == 0 && map[x+1][y+1] == 0 && map[x][y-1] == 0)
  315.                                                 {
  316.                                                         if (rand()%10>7)
  317.                                                         map[x][y] = 2;
  318.                                                 }
  319.                                         }
  320.                                 }
  321.                         }
  322.                 }
  323.         }
  324. }
  325.  
  326. void draw(int x1, int y1, int w, int h)
  327. {
  328.         for (int x = x1; x < x1+w; x++)
  329.         {
  330.                 for (int y = y1; y < y1+h; y++)
  331.                 {
  332.                         if (x >= 0 && y >= 0 && x < 80 && y < 25)
  333.                         {
  334.                                 move(y, x);
  335.                                 if (map[x][y] == 0)
  336.                                 {
  337.                                         attron(COLOR_PAIR(1));
  338.                                         addch(' ');
  339.                                 }
  340.                                 if (map[x][y] == 1)
  341.                                 {
  342.                                         attroff(A_BOLD);
  343.                                         attron(COLOR_PAIR(1));
  344.                                         addch('#');
  345.                                 }
  346.                                 if (map[x][y] == 2)
  347.                                 {
  348.                                         attron(COLOR_PAIR(2) | A_BOLD);
  349.                                         addch('+');
  350.                                         attroff(A_BOLD);
  351.                                 }
  352.                         }
  353.                 }
  354.         }
  355. }
  356.  
  357. int main()
  358. {      
  359.         PDC_set_title("Roguelike");
  360.         srand ((unsigned int) time (NULL));
  361.         int ch = 0;
  362.         initscr();
  363.         curs_set(0);
  364.  
  365.         if(has_colors() == FALSE || can_change_color() == FALSE)
  366.         {      
  367.                 endwin();
  368.                 printf("You need color support to play this game.\n");
  369.                 exit(1);
  370.         }
  371.  
  372.         start_color();
  373.         init_pair(1, COLOR_WHITE, COLOR_BLACK);
  374.         init_pair(2, COLOR_RED, COLOR_BLACK);
  375.         init_pair(3, COLOR_GREEN, COLOR_BLACK);
  376.  
  377.         raw();
  378.         keypad(stdscr, TRUE);
  379.         noecho();
  380.         gen();
  381.         draw(0,0,80,25);
  382.  
  383.         while (ch != 27)
  384.         {      
  385.                 move(py, px);
  386.                 attron(COLOR_PAIR(1) | A_BOLD);
  387.                 addch('@');
  388.                 ch = getch();
  389.                 if (ch == 'r')
  390.                 {
  391.                         gen();
  392.                         draw(0,0,80,25);
  393.                 }
  394.                 if (ch == 'o')
  395.                 {
  396.                         addch('o');
  397.                         draw(px-2,py-2,3,3);
  398.                 }
  399.                 if (ch == KEY_LEFT)
  400.                 {
  401.                         if (map[px-1][py] == 0)
  402.                         {
  403.                                 px--;
  404.                                 draw(px, py, 2, 1);
  405.                         }
  406.                 }
  407.                 if (ch == KEY_RIGHT)
  408.                 {
  409.                         if (map[px+1][py] == 0)
  410.                         {
  411.                                 px++;
  412.                                 draw(px - 1, py, 2, 1);
  413.                         }
  414.                 }
  415.                 if (ch == KEY_UP)
  416.                 {
  417.                         if (map[px][py-1] == 0)
  418.                         {
  419.                                 py--;
  420.                                 draw(px, py, 1, 2);
  421.                         }
  422.                 }
  423.                 if (ch == KEY_DOWN)
  424.                 {
  425.                         if (map[px][py+1] == 0)
  426.                         {
  427.                                 py++;
  428.                                 draw(px, py-1, 1, 2);
  429.                         }
  430.                 }
  431.                 refresh();
  432.         }
  433.         endwin();
  434. }