Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tile.h"
- #include "cell.h"
- #include "render.h"
- #include <cstdlib>
- #include <iostream>
- #include <string>
- using namespace std;
- cell::cell()
- {
- for(int x=0; x<CELLW; x++)
- {
- for(int y=0; y<CELLH; y++)
- {
- tiles[x][y].type=0;
- tiles[x][y].elev=0;
- tiles[x][y].moist=0;
- tiles[x][y].syma='*';
- tiles[x][y].colr=0;
- tiles[x][y].colg=0;
- tiles[x][y].colb=0;
- tiles[x][y].prevtype=0; //the tile that existed prior to becoming water, so as to not turn turn stone into mud,etc
- //blank buffer
- newtiles[x][y].type=0;
- newtiles[x][y].elev=0;
- newtiles[x][y].moist=0;
- newtiles[x][y].syma='*';
- newtiles[x][y].colr=0;
- newtiles[x][y].colg=0;
- newtiles[x][y].colb=0;
- newtiles[x][y].prevtype=0;
- }
- }
- };
- int cell::getmoist(int x,int y)
- {
- if(tileinrange(x,y))
- {
- return tiles[x][y].moist;
- }
- else
- {
- return 0;
- }
- }
- int cell::getelev(int x,int y)
- {
- if(tileinrange(x,y))
- {
- return tiles[x][y].elev;
- }
- else
- {
- return 0;
- }
- }
- int cell::gettype(int x,int y)
- {
- if(tileinrange(x,y))
- {
- return tiles[x][y].type;
- }
- else
- {
- return -1; //there will never be a real tile with an index lower than 0.
- }
- }
- void cell::assigntype(int x,int y)
- {
- if(tileinrange(x,y))
- {
- switch(newtiles[x][y].type) //assign the correct character for the tile's type
- {
- case 0:
- newtiles[x][y].syma='D';
- setcolor(200,180,150,x,y);
- break;
- case 1:
- newtiles[x][y].syma='M';
- setcolor(190,150,110,x,y);
- break;
- case 2:
- newtiles[x][y].syma='W';
- setcolor(150,175,190,x,y);
- break;
- default:
- newtiles[x][y].syma='!';
- break;
- }
- }
- }
- void cell::sim()
- {
- for(int x=0; x<CELLW; x++)
- {
- for(int y=0; y<CELLH; y++)
- {
- moistflow(x,y);
- dirtmud(x,y);
- waterwet(x,y);
- waterdry(x,y);
- assigntype(x,y);
- }
- }
- flipbuffer();
- }
- void cell::flipbuffer()
- {
- for(int x=0; x<CELLW; x++)
- {
- for(int y=0; y<CELLH; y++)
- {
- tiles[x][y].type=newtiles[x][y].type;
- tiles[x][y].elev=newtiles[x][y].elev;
- tiles[x][y].moist= newtiles[x][y].moist;
- tiles[x][y].syma=newtiles[x][y].syma;
- tiles[x][y].colr=newtiles[x][y].colr;
- tiles[x][y].colg=newtiles[x][y].colg;
- tiles[x][y].colb=newtiles[x][y].colb;
- tiles[x][y].prevtype=newtiles[x][y].prevtype;
- }
- }
- }
- void cell::setcolor(int r,int g,int b,int x,int y)
- {
- if(tileinrange(x,y))
- {
- newtiles[x][y].colr=r;
- newtiles[x][y].colg=g;
- newtiles[x][y].colb=b;
- }
- }
- SDL_Rect Message_rect;
- void cell::render(viz visulizer)
- {
- for(int x=0; x<CELLW; x++)
- {
- for(int y=0; y<CELLH; y++)
- {
- Message_rect.x = x*16; //controls the rect's x coordinate
- Message_rect.y = y*16; // controls the rect's y coordinte
- Message_rect.w = 16; // controls the width of the rect
- Message_rect.h = 16; // controls the height of the rect
- //SDL_Surface* textout=visulizer.rtext(tiles[x][y].syma,Message_rect,tiles[x][y].colr,tiles[x][y].colg,tiles[x][y].colb);
- 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);
- visulizer.combosurf(&Message_rect,textout);
- SDL_FreeSurface(textout);
- }
- }
- }
- void cell::settile_type(int x,int y,int itype)
- {
- if(tileinrange(x,y))
- {
- newtiles[x][y].type=itype;
- }
- }
- void cell::settile_moist(int x,int y,int imoist)
- {
- if(tileinrange(x,y))
- {
- newtiles[x][y].moist=imoist;
- }
- }
- void cell::addtile_moist(int x,int y,int imoist)
- {
- if(tileinrange(x,y))
- {
- newtiles[x][y].moist+=imoist;
- }
- }
- void cell::addtile_elev(int x,int y,int ielev)
- {
- if(tileinrange(x,y))
- {
- newtiles[x][y].elev+=ielev;
- }
- }
- bool cell::tileinrange(int x,int y)
- {
- if(x< CELLW && y< CELLH)
- {
- if(x>-1 && y>-1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- //waterops
- void cell::dirtmud(int x,int y)
- {
- if(tileinrange(x,y))
- {
- if(tiles[x][y].moist>50 && tiles[x][y].type == 0)
- {
- newtiles[x][y].type=1;
- }
- if(tiles[x][y].moist<50 && tiles[x][y].type == 1)
- {
- newtiles[x][y].type=0;
- }
- }
- }
- void cell::waterdry(int x,int y)//detect when a tile is no longer wet and return it to the previous state
- {
- if(tileinrange(x,y))
- {
- if(tiles[x][y].moist<75 && tiles[x][y].type == 2)
- {
- newtiles[x][y].type=tiles[x][y].prevtype;
- }
- }
- }
- void cell::waterwet(int x,int y)
- {
- if(tileinrange(x,y))
- {
- if(tiles[x][y].moist>75)//todo: make a liquids group so things like lava are not affected
- {
- 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
- {
- newtiles[x][y].prevtype=tiles[x][y].type;
- newtiles[x][y].type=2;
- }
- }
- }
- }
- void cell::moistflow(int x,int y)
- {
- if(tileinrange(x,y))
- {
- if(tiles[x][y].moist>57)//start the area moist effect
- {
- for(int xo=-2; xo<2; xo++)
- {
- for(int yo=-2; yo<2; yo++)
- {
- if(newtiles[x][y].moist>57)//stop spreading after the current tile will become less than this
- {
- newtiles[x][y].moist-=2;
- addtile_moist(x+xo,y+yo,2);
- /*
- if(tiles[x+xo][y+yo].elev<=tiles[x][y].elev) //make it easier for water to flow into places lower than current
- {
- //and harder to flow uphill
- newtiles[x][y].moist-=2;
- addtile_moist(x+xo,y+yo,2);
- }
- if(tiles[x+xo][y+yo].elev>tiles[x][y].elev) //make it easier for water to flow into places lower than current
- {
- //and harder to flow uphill
- newtiles[x][y].moist-=1;
- addtile_moist(x+xo,y+yo,1);
- }
- */
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement