Advertisement
zDeveloper

Cell simulation

Nov 29th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 KB | None | 0 0
  1. #include "tile.h"
  2. #include "cell.h"
  3. #include "render.h"
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8. cell::cell()
  9. {
  10.     for(int x=0; x<CELLW; x++)
  11.     {
  12.         for(int y=0; y<CELLH; y++)
  13.         {
  14.             tiles[x][y].type=0;
  15.             tiles[x][y].elev=0;
  16.             tiles[x][y].moist=0;
  17.             tiles[x][y].syma='*';
  18.             tiles[x][y].colr=0;
  19.             tiles[x][y].colg=0;
  20.             tiles[x][y].colb=0;
  21.             tiles[x][y].prevtype=0; //the tile that existed prior to becoming water, so as to not turn turn stone into mud,etc
  22.             //blank buffer
  23.             newtiles[x][y].type=0;
  24.             newtiles[x][y].elev=0;
  25.             newtiles[x][y].moist=0;
  26.             newtiles[x][y].syma='*';
  27.             newtiles[x][y].colr=0;
  28.             newtiles[x][y].colg=0;
  29.             newtiles[x][y].colb=0;
  30.             newtiles[x][y].prevtype=0;
  31.         }
  32.     }
  33.  
  34. };
  35. int cell::getmoist(int x,int y)
  36. {
  37.     if(tileinrange(x,y))
  38.     {
  39.         return tiles[x][y].moist;
  40.     }
  41.     else
  42.     {
  43.         return 0;
  44.     }
  45. }
  46. int cell::getelev(int x,int y)
  47. {
  48.     if(tileinrange(x,y))
  49.     {
  50.         return tiles[x][y].elev;
  51.     }
  52.     else
  53.     {
  54.         return 0;
  55.     }
  56. }
  57. int cell::gettype(int x,int y)
  58. {
  59.     if(tileinrange(x,y))
  60.     {
  61.         return tiles[x][y].type;
  62.     }
  63.     else
  64.     {
  65.         return -1; //there will never be a real tile with an index lower than 0.
  66.     }
  67. }
  68.  
  69. void cell::assigntype(int x,int y)
  70. {
  71.     if(tileinrange(x,y))
  72.     {
  73.         switch(newtiles[x][y].type) //assign the correct character for the tile's type
  74.         {
  75.         case 0:
  76.             newtiles[x][y].syma='D';
  77.             setcolor(200,180,150,x,y);
  78.             break;
  79.         case 1:
  80.             newtiles[x][y].syma='M';
  81.             setcolor(190,150,110,x,y);
  82.             break;
  83.         case 2:
  84.             newtiles[x][y].syma='W';
  85.             setcolor(150,175,190,x,y);
  86.             break;
  87.         default:
  88.             newtiles[x][y].syma='!';
  89.             break;
  90.         }
  91.     }
  92. }
  93.  
  94. void cell::sim()
  95. {
  96.     for(int x=0; x<CELLW; x++)
  97.     {
  98.         for(int y=0; y<CELLH; y++)
  99.         {
  100.             moistflow(x,y);
  101.             dirtmud(x,y);
  102.             waterwet(x,y);
  103.             waterdry(x,y);
  104.  
  105.             assigntype(x,y);
  106.         }
  107.     }
  108. flipbuffer();
  109. }
  110. void cell::flipbuffer()
  111. {
  112.     for(int x=0; x<CELLW; x++)
  113.     {
  114.         for(int y=0; y<CELLH; y++)
  115.         {
  116.             tiles[x][y].type=newtiles[x][y].type;
  117.             tiles[x][y].elev=newtiles[x][y].elev;
  118.             tiles[x][y].moist= newtiles[x][y].moist;
  119.             tiles[x][y].syma=newtiles[x][y].syma;
  120.             tiles[x][y].colr=newtiles[x][y].colr;
  121.             tiles[x][y].colg=newtiles[x][y].colg;
  122.             tiles[x][y].colb=newtiles[x][y].colb;
  123.             tiles[x][y].prevtype=newtiles[x][y].prevtype;
  124.         }
  125.     }
  126. }
  127.  
  128. void cell::setcolor(int r,int g,int b,int x,int y)
  129. {
  130.     if(tileinrange(x,y))
  131.     {
  132.         newtiles[x][y].colr=r;
  133.         newtiles[x][y].colg=g;
  134.         newtiles[x][y].colb=b;
  135.     }
  136. }
  137. SDL_Rect Message_rect;
  138. void cell::render(viz visulizer)
  139. {
  140.     for(int x=0; x<CELLW; x++)
  141.     {
  142.         for(int y=0; y<CELLH; y++)
  143.         {
  144.             Message_rect.x = x*16;  //controls the rect's x coordinate
  145.             Message_rect.y = y*16; // controls the rect's y coordinte
  146.             Message_rect.w = 16; // controls the width of the rect
  147.             Message_rect.h = 16; // controls the height of the rect
  148.             //SDL_Surface* textout=visulizer.rtext(tiles[x][y].syma,Message_rect,tiles[x][y].colr,tiles[x][y].colg,tiles[x][y].colb);
  149.             SDL_Surface* textout=visulizer.rtext(tiles[x][y].syma,Message_rect,tiles[x][y].colr,tiles[x][y].colg,tiles[x][y].colb,tiles[x][y].moist);
  150.             visulizer.combosurf(&Message_rect,textout);
  151.             SDL_FreeSurface(textout);
  152.         }
  153.     }
  154. }
  155.  
  156. void cell::settile_type(int x,int y,int itype)
  157. {
  158.     if(tileinrange(x,y))
  159.     {
  160.         newtiles[x][y].type=itype;
  161.     }
  162. }
  163. void cell::settile_moist(int x,int y,int imoist)
  164. {
  165.     if(tileinrange(x,y))
  166.     {
  167.         newtiles[x][y].moist=imoist;
  168.     }
  169. }
  170.  
  171. void cell::addtile_moist(int x,int y,int imoist)
  172. {
  173.     if(tileinrange(x,y))
  174.     {
  175.         newtiles[x][y].moist+=imoist;
  176.     }
  177. }
  178. void cell::addtile_elev(int x,int y,int ielev)
  179. {
  180.     if(tileinrange(x,y))
  181.     {
  182.         newtiles[x][y].elev+=ielev;
  183.     }
  184.  
  185. }
  186. bool cell::tileinrange(int x,int y)
  187. {
  188.     if(x< CELLW && y< CELLH)
  189.     {
  190.         if(x>-1 && y>-1)
  191.         {
  192.             return true;
  193.         }
  194.         else
  195.         {
  196.             return false;
  197.         }
  198.     }
  199.     else
  200.     {
  201.         return false;
  202.     }
  203. }
  204. //waterops
  205. void cell::dirtmud(int x,int y)
  206. {
  207.     if(tileinrange(x,y))
  208.     {
  209.  
  210.         if(tiles[x][y].moist>50 && tiles[x][y].type == 0)
  211.         {
  212.             newtiles[x][y].type=1;
  213.         }
  214.         if(tiles[x][y].moist<50 && tiles[x][y].type == 1)
  215.         {
  216.             newtiles[x][y].type=0;
  217.         }
  218.     }
  219. }
  220. void cell::waterdry(int x,int y)//detect when a tile is no longer wet and return it to the previous state
  221. {
  222.     if(tileinrange(x,y))
  223.     {
  224.         if(tiles[x][y].moist<75 && tiles[x][y].type == 2)
  225.         {
  226.             newtiles[x][y].type=tiles[x][y].prevtype;
  227.         }
  228.     }
  229. }
  230. void cell::waterwet(int x,int y)
  231. {
  232.     if(tileinrange(x,y))
  233.     {
  234.         if(tiles[x][y].moist>75)//todo: make a liquids group so things like lava are not affected
  235.         {
  236.             if(tiles[x][y].type!=2) //make sure it does not try to convert water to water and end up with water as a prevtype
  237.             {
  238.                 newtiles[x][y].prevtype=tiles[x][y].type;
  239.                 newtiles[x][y].type=2;
  240.             }
  241.  
  242.         }
  243.     }
  244. }
  245. void cell::moistflow(int x,int y)
  246. {
  247.     if(tileinrange(x,y))
  248.     {
  249.         if(tiles[x][y].moist>57)//start the area moist effect
  250.         {
  251.             for(int xo=-2; xo<2; xo++)
  252.             {
  253.                 for(int yo=-2; yo<2; yo++)
  254.                 {
  255.                     if(newtiles[x][y].moist>57)//stop spreading after the current tile will become less than this
  256.         {
  257.              newtiles[x][y].moist-=2;
  258.                             addtile_moist(x+xo,y+yo,2);
  259.                             /*
  260.                         if(tiles[x+xo][y+yo].elev<=tiles[x][y].elev) //make it easier for water to flow into places lower than current
  261.                         {
  262.                             //and harder to flow uphill
  263.                             newtiles[x][y].moist-=2;
  264.                             addtile_moist(x+xo,y+yo,2);
  265.                         }
  266.                         if(tiles[x+xo][y+yo].elev>tiles[x][y].elev) //make it easier for water to flow into places lower than current
  267.                         {
  268.                             //and harder to flow uphill
  269.                             newtiles[x][y].moist-=1;
  270.                             addtile_moist(x+xo,y+yo,1);
  271.                         }
  272.                         */
  273.         }
  274.                 }
  275.             }
  276.         }
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement