Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <fstream>
- #include <time.h>
- #include <SDL.h>
- #include <SDL_ttf.h>
- //#include <Windows.h>
- #include <iostream>
- #include <sstream>
- using namespace std;
- SDL_Surface *screen;
- string bts(bool b)
- {
- if(b)
- return "True";
- return "False";
- }
- void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination ) {
- //Make a temporary rectangle to hold the offsets
- SDL_Rect offset;
- //Give the offsets to the rectangle
- offset.x = x; offset.y = y;
- //Blit the surface
- SDL_BlitSurface( source, NULL, destination, &offset ); }
- string is(int m)
- {
- stringstream bob;
- bob<<m;
- return bob.str();
- }
- int zcheck(int ch)
- {
- if(ch<0){return 0;}
- else{return ch;}
- }
- /*
- void em(string txt)
- {
- MessageBox(0,txt.c_str(),"Azjherben",0);
- }*/
- int fomap = 0;
- void applysurface(int x,int y,SDL_Surface *source)
- {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface( source, NULL, screen, &offset );
- }
- SDL_Surface *load_image( std::string filename )
- {
- SDL_Surface *loadedImage = SDL_LoadBMP( filename.c_str() );
- loadedImage = SDL_DisplayFormat( loadedImage );
- Uint32 colorkey = SDL_MapRGB( loadedImage->format, 255, 255, 255);
- SDL_SetColorKey( loadedImage, SDL_SRCCOLORKEY, colorkey );
- return loadedImage;
- }
- Uint32 getacolor(int r,int g,int b) //gets a color and returns handle
- {
- return SDL_MapRGB(screen->format,r,g,b);
- }
- void applyapixel(int x,int y,Uint32 color)
- {
- SDL_Rect off;
- off.x = x;
- off.y = y;
- off.w = 1;
- off.h = 1;
- SDL_FillRect(screen,&off,color);
- }
- int rcs = 0;
- int rq = 0;
- int randa(int min,int max)
- {
- if(rq > 10000000)
- {
- rq = 1;
- }
- srand((time(NULL)*rcs)+rq);
- int diff = max-min;
- int reta = rand() %diff;
- //em("Max is "+is(max)+" min is "+is(min)+". Number gened is "+is(reta+min));
- rcs=reta+min;
- rq+=21728;
- return reta+min;
- }
- SDL_Event event;
- int lightr = 255;
- int lightg = 255;
- int lightb = 255;
- class food
- {
- public:
- int x,y;
- bool u;
- void show();
- };
- void food::show()
- {
- applyapixel(x,y,getacolor(255,255,0));
- applyapixel(x,y+1,getacolor(255,255,0));
- applyapixel(x+1,y,getacolor(255,255,0));
- applyapixel(x+1,y+1,getacolor(255,255,0));
- }
- food foods[600];
- void makefood(int x,int y)
- {
- for(int qr = 0; qr<600; qr++)
- {
- if(foods[qr].u == false)
- {
- fomap += 1;
- foods[qr].u = true;
- foods[qr].x = x;
- foods[qr].y = y;
- return;
- }
- }
- }
- class cell
- {
- public:
- int x,y,r,g,b,food,waitforkids,sff,fff,use,ka,po,pfc,pff,phold,kanwait,ckw,por,pog,pob,dr;
- bool dead,eats,conz,ec;
- void makefood();
- void produce();
- void dieout();
- void starv();
- void show();
- void canna();
- void checkandeat();
- void pop();
- };
- void cell::show()
- {
- if(use !=-1){
- //em("Showing... "+is(r)+" "+is(g)+" "+is(b));
- applyapixel(x,y,getacolor(r,g,b));
- if(ckw > 1){ckw -= 1;}
- }}
- int foodat(int ax,int ay)
- {
- for(int nf = 0; nf<1000; nf++)
- {
- if(foods[nf].x == ax && foods[nf].y == ay)
- {
- if(foods[nf].u == true)
- {
- return nf;
- }
- }
- }
- return -1;
- }
- void cell::checkandeat()
- {
- if(eats == true)
- {
- int fae = -1;
- bool taf = false;
- if(foodat(x+1,y) != -1 ){po += pff; fae = foodat(x+1,y); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x,y+1) != -1){po += pff; fae = foodat(x,y+1); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x+1,y+1) != -1){po += pff; fae = foodat(x+1,y+1); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x+2,y+2) != -1){po += pff; fae = foodat(x+2,y+2); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x+2,y+1) != -1){po += pff; fae = foodat(x+2,y+1); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x+1,y+2) != -1){po += pff; fae = foodat(x+1,y+2); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x+2,y) != -1){po += pff; fae = foodat(x+2,y); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x,y+2) != -1){po += pff; fae = foodat(x,y+2); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x,y-1) != -1){po += pff; fae = foodat(x,y-1); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x-1,y) != -1){po += pff; fae = foodat(x-1,y); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- if(foodat(x-1,y-1) != -1){po += pff; fae = foodat(x-1,y-1); fomap -= 1; foods[fae].u = false; food+=fff; foods[fae].x = -1; foods[fae].y = -1; return; taf = true;}
- }
- }
- const int maxcells = 1000;
- int timetowait = 2;
- cell cells[maxcells];
- bool nocellat(int ax,int ay)
- {
- for(int nc = 0; nc<maxcells; nc++)
- {
- if(cells[nc].x == ax && cells[nc].y == ay)
- {
- return false;
- }
- else{
- }
- }
- return true;
- }
- class pots
- {
- public:
- int xx,yy,ro,used,r,g,b;
- void show();
- void remove();
- };
- bool rper(int percent)
- {
- int rrp = randa(0,100);
- for(int rcrp = 0; rcrp<percent; rcrp++)
- {
- if(rrp == rcrp){return true;}
- }
- return false;
- }
- pots poops[200];
- void pots::remove()
- {
- if(ro < 1){used = false;}
- else{
- ro -= 1;}
- }
- void pots::show()
- {
- if(used == true)
- {
- applyapixel(xx,yy,getacolor(r,g,b));
- applyapixel(xx,yy+1,getacolor(r,g,b));
- applyapixel(xx+1,yy,getacolor(r,g,b));
- applyapixel(xx+1,yy+1,getacolor(r,g,b));
- }
- }
- void makepo(int x,int y,int rat,int r,int g,int b)
- {
- for(int poche = 0; poche<200; poche++)
- {
- if(poops[poche].used == false)
- {
- poops[poche].used = true;
- poops[poche].ro = rat*10;
- poops[poche].xx = x;
- poops[poche].yy = y;
- poops[poche].r = r;
- poops[poche].g = g;
- poops[poche].b = b;
- }
- }
- }
- void cell::pop()
- {
- if(po > phold)
- {
- makepo(x,y,po,por,pog,pob);
- po = 0;
- }
- }
- int cellat(int ax,int ay)
- {
- for(int nc = 0; nc<maxcells; nc++)
- {
- if(cells[nc].x == ax && cells[nc].y == ay)
- {
- return nc;
- }
- }
- return -1;
- }
- void cell::canna()
- {
- if(conz != false){
- if(ckw < 2){
- int ccel = -1;
- for(int vx = 0; vx<6; vx++)
- {
- for(int vy = 0; vy<6; vy++)
- {
- if(cellat(x+vx-3,y+vy-3)!=-1){ccel = cellat(x+vx-3,y+vy-3);}
- }
- }
- if(ka<-100)
- {
- ka = 0;
- }
- if(ccel !=-1)
- {
- //if(cells[ccel].conz != true)
- //{
- if(ccel != use){
- int tqf = (((cells[ccel].food)/2)-ka);
- po += pfc;
- food += tqf;
- cells[ccel].dead = true;
- cells[ccel].use = -1;
- ckw = kanwait;
- //}
- }
- }
- }}
- }
- void makeacell(int x,int y,int r,int g,int b,int w,int s,bool eats,int faw,bool e,int k,int qpfc,int qpff,int qphold,int canawt,
- int por,int pog,int pob,int dnarepair)
- {
- for(int chk = 0; chk<maxcells; chk++)
- {
- if (cells[chk].use == -1)
- {
- if(nocellat(x,y)){
- cells[chk].use = chk;
- cells[chk].dead = false;
- cells[chk].x = x;
- cells[chk].y = y;
- cells[chk].r = r;
- cells[chk].g = g;
- cells[chk].b = b;
- cells[chk].waitforkids = w;
- cells[chk].sff = s;
- cells[chk].food = s;
- cells[chk].eats = eats;
- cells[chk].conz = e;
- cells[chk].ka = k;
- cells[chk].kanwait = canawt;
- cells[chk].pfc = qpfc;
- cells[chk].pff = qpff;
- cells[chk].po = 0;
- cells[chk].por = por;
- cells[chk].pog = pog;
- cells[chk].pob = pob;
- cells[chk].dr = dnarepair;
- if(eats == false){faw = 0;}
- cells[chk].fff = faw;
- return;
- }
- }
- }
- }
- void cell::dieout()
- {
- if(use != -1)
- {
- if(rper(1))
- {
- if(rper(50)){
- dead = true;
- use = -1;
- //em("Cell died.");
- }}
- }
- }
- void cell::makefood()
- {
- if(use != -1)
- {
- int ivr = 255-r;
- int ivg = 255-g;
- int ivb = 255-b;
- if(ivr > lightr){ivr = lightr;}
- if(ivg > lightg){ivg = lightg;}
- if(ivb > lightb){ivb = lightb;}
- food += (ivr+ivg+ivb)/10;
- //em("Produced "+is((ivr+ivg+ivb)/10)+" units of food. "+is(lightr)+" "+is(lightg)+" "+is(lightb));
- }
- }
- void cell::starv()
- {
- if(use!=-1)
- {
- food -= 20;
- if(dr>0)
- {
- food-=4;
- }
- if(food < 5)
- {
- dead = true;
- use = -1;
- }
- }
- }
- void cell::produce()
- {
- if(use!=-1)
- {
- if(food > waitforkids)
- {
- food -= 1000+zcheck(sff);
- if(food > 5)
- {
- int v = 100;
- if(dr == 1){v = 2;}
- int pr,pg,pb;
- if(rper(v)){
- pr = r+randa(-10,10);
- pg = g+randa(-10,10);
- pb = b+randa(-10,10);
- }
- else{
- pr = r;
- pg = g;
- pb = b;
- }
- if(pr < 1){pr = abs(pr)+1;}
- if(pg < 1){pg = abs(pg)+1;}
- if(pb < 1){pb = abs(pb)+1;}
- if(pr > 254){pr = 253;}
- if(pg > 254){pg = 253;}
- if(pb > 254){pb = 253;} // :D
- int px = x+randa(-5,5);
- int py = y+randa(-5,5);
- if(px < 1){px = 1;}
- if(py < 1){py = 1;}
- if(px > 498){px = 497;}
- if(py > 498){py = 497;}
- int st = sff + randa(-10,10);
- int wfk = waitforkids + randa(-10,10); //second gene to go in
- //em(is(wfk));
- int ffa = fff + randa(-10,10);
- if(rper(4) && dr == 0){ if(rper(4)){ dr = 1; food -= 400;}}
- if(rper(1) && rper(5) && rper(5) && dr == 1){dr = 0;}
- int kat = + randa(-10,10);
- if(eats == false){if(randa(0,300) == 50){eats=true; ffa = 100; fff = 100;}}
- if(eats == true){if(randa(0,300) == 50){eats=false; ffa = 0; fff = 0;}}
- if(conz == false){if(randa(0,300) == 50){conz=true; ka = 10; ka = 10;}}
- if(conz == true){if(randa(0,300) == 50){conz=false; ka = 0; ka = 0;}}
- makeacell(px,py,pr,pg,pb,wfk,st,eats,ffa,conz,kat,pfc,pff,phold,kanwait,por,pog,pob,dr);
- }
- }
- }
- }
- void drawspectrum()
- {
- for(int dsx = 0; dsx<255; dsx++)
- {
- for(int dsy = 0; dsy<10; dsy++)
- {
- applyapixel(dsx,dsy,getacolor(256,dsx,256));
- }
- for(int dsy = 0; dsy<10; dsy++)
- {
- applyapixel(dsx,dsy+10,getacolor(256,256,dsx));
- }
- for(int dsy = 0; dsy<10; dsy++)
- {
- applyapixel(dsx,dsy+20,getacolor(dsx,256,256));
- }
- for(int dsy = 0; dsy<10; dsy++)
- {
- applyapixel(dsx,dsy+30,getacolor(dsx,dsx,dsx));
- }
- }
- }
- Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
- {
- //Convert the pixels to 32 bit
- Uint32 *pixels = (Uint32 *)surface->pixels;
- //Get the requested pixel
- return pixels[ ( y * surface->w ) + x ];
- }
- int main ( int argc, char** argv )
- {
- SDL_Init( SDL_INIT_EVERYTHING ); //Inital Init
- screen = SDL_SetVideoMode(500,500,32,0); //500 by 500 screen
- SDL_Rect txt;//hello
- txt.x = 400;
- txt.y = 100;
- txt.w = 1;
- txt.h = 1;
- TTF_Init();
- TTF_Font* tmpfont = NULL;
- tmpfont = TTF_OpenFont("a.ttf", 18);
- // if(tmpfont == NULL){em("Font did not load.");}
- SDL_Color black = {0,0,0};
- for(int aset = 0; aset<maxcells; aset++)
- {
- cells[aset].use = -1;
- cells[aset].dead = true;
- }
- //makeacell(300,201,255,0,0,1500,400,false,100,false,10,25,20,35,50,169,39,19,1);
- //makeacell(300,200,255,0,0,1500,400,false,100,false,10,25,20,35,50,169,39,19,1);
- //makeacell(125,201,0,255,0,1000,200,false,100,true,10,25,20,35,5,169,39,19,0);
- //makeacell(125,200,0,255,0,1000,200,false,100,true,10,25,20,35,5,169,39,19,0);
- /*
- makeacell(100,200,0,255,0,800,200,true);
- makeacell(300,200,0,0,255,800,200,true);
- makeacell(100,201,0,255,0,800,200,true);
- makeacell(300,201,0,0,255,800,200); //tests for color
- makeacell(200,350,150,150,150,800,200);
- makeacell(200,351,150,150,150,800,200);
- */
- /*
- makeacell(100,200,0,255,0,1200);
- makeacell(300,200,0,255,0,600); //tests for waitforkid values
- makeacell(100,201,0,255,0,1200);
- makeacell(300,201,0,255,0,600);
- *///int size; char * memblock;
- string fir,fig,fib,fdata;
- int posf = 0;
- int numberfound = 0;
- int positions[100];
- int ptrk = 0;
- /*
- ifstream file("startcell.txt", ios::in|ios::binary|ios::ate);
- if(file.is_open())
- {
- size = file.tellg();
- memblock = new char [size];
- file.seekg (0, ios::beg);
- for(int sizecheck = 0; sizecheck<size; sizecheck++){
- file.read (memblock, 1);
- if(memblock!=NULL && memblock!="" && memblock!=" "){
- fdata += memblock;
- }
- }
- for(int p = 0; p<100; p++){positions[p] = -1;}
- while(!(posf>fdata.length()))
- {
- if(fdata.substr(posf,1) == "|")
- {
- positions[ptrk] = posf;
- //em("Positions["+is(ptrk)+"] = "+is(posf));
- ptrk++;
- numberfound++;
- }
- posf++;
- }
- }
- else{
- //em("Couldn't find cell start data, defaulting.");
- makeacell(100,200,1,255,1,1500,400,false,100,false,10,25,20,35,50,169,39,19,0);
- makeacell(100,201,1,255,1,1500,400,false,100,false,10,25,20,35,50,169,39,19,0);
- }
- file.close();
- string fwa,fsf;
- fir = fdata.substr(0,positions[0]);
- fig = fdata.substr(positions[0]+1,positions[1]-positions[0]-1);
- fib = fdata.substr(positions[1]+1,positions[2]-positions[1]-1);
- fwa = fdata.substr(positions[2]+1,positions[3]-positions[2]-1);
- fsf = fdata.substr(positions[3]+1,positions[4]-positions[3]-1);
- makeacell(100,200,atoi(fir.c_str()),atoi(fig.c_str()),atoi(fib.c_str()),atoi(fwa.c_str()),atoi(fsf.c_str()),false,100,false,10,25,20,35,50,169,39,19,0);
- makeacell(100,201,atoi(fir.c_str()),atoi(fig.c_str()),atoi(fib.c_str()),atoi(fwa.c_str()),atoi(fsf.c_str()),false,100,false,10,25,20,35,50,169,39,19,0);
- */
- makeacell(100,201,0,255,0,1400,400,false,100,false,10,25,20,35,50,169,39,19,0);
- makeacell(100,200,0,255,0,1400,400,false,100,false,10,25,20,35,50,169,39,19,0);
- // program main loop
- bool done = false;
- while (!done)
- {
- while(SDL_PollEvent(&event)) //run though events
- {
- if(event.type == SDL_QUIT) //if the user wants to quit
- {
- done = true;
- }
- if(event.type == SDL_MOUSEBUTTONDOWN)
- {
- if(event.button.button == SDL_BUTTON_LEFT)
- {
- if(cellat(event.motion.x,event.motion.y) == -1)
- {
- Uint32 clickcolor = get_pixel32(screen,event.motion.x,event.motion.y);
- Uint8 ra,ga,ba;
- SDL_GetRGB(clickcolor,screen->format,&ra,&ga,&ba);
- lightr = ra;
- lightg = ga;
- lightb = ba;
- }
- else{
- int cnum = cellat(event.motion.x,event.motion.y);
- /*em("Clicked cell number: "+is(cnum)+" has "+is(cells[cnum].food)+
- " food. || Genes || Wfk: "+is(cells[cnum].waitforkids)+" Sff: "+is(cells[cnum].sff)+ " Red: "+is(cells[cnum].r)+" Green: "+is(cells[cnum].g)+
- " Blue: "+is(cells[cnum].b)+" fff: "+is(cells[cnum].fff)+" ka: "+is(cells[cnum].ka)+" pfc: "+is(cells[cnum].pfc)+" pff: "+is(cells[cnum].pff)+
- " phold: "+is(cells[cnum].phold)+" kwy: "+is(cells[cnum].kanwait)+" conz: "+bts(cells[cnum].conz)+" eats: "+bts(cells[cnum].eats)+ " por: "+is(cells[cnum].por)
- +" pog: "+is(cells[cnum].pog)+" pob: "+is(cells[cnum].pob)+" dr: "+is(cells[cnum].dr) );*/
- }
- }
- if(event.button.button == SDL_BUTTON_RIGHT)
- {
- makefood(event.motion.x,event.motion.y);
- }
- }
- }
- //start draws
- // DRAWING ENDS HERE
- if(fomap < 500){
- makefood(randa(0,500),randa(0,500));
- makefood(randa(0,500),randa(0,500));
- makefood(randa(0,500),randa(0,500));
- makefood(randa(0,500),randa(0,500));
- makefood(randa(0,500),randa(0,500));
- }
- else{
- }
- //make a random food
- SDL_FillRect(screen,&screen->clip_rect,getacolor(lightr,lightg,lightb));
- drawspectrum();
- int hgfd = 0;
- for(int fud = 0; fud<600; fud++)
- {
- if(foods[fud].u == true)
- {
- hgfd += 1;
- foods[fud].show();
- }
- }
- for(int pud = 0; pud<200; pud++){
- if(poops[pud].used == true)
- {
- poops[pud].show();
- poops[pud].remove();
- }
- }
- int hgce = 0;
- int hgdr = 0;
- for(int showcells = 0; showcells<maxcells; showcells++)
- {
- if(cells[showcells].use != -1){
- cells[showcells].produce();
- cells[showcells].makefood();
- cells[showcells].show();
- cells[showcells].checkandeat();
- cells[showcells].dieout();
- cells[showcells].canna();
- cells[showcells].pop();
- cells[showcells].starv();
- hgce += 1;
- if(cells[showcells].dr > 0){hgdr++;}
- }
- }
- string ah = "Cells: "+is(hgce);
- SDL_Surface* tp = TTF_RenderText_Solid(tmpfont, ah.c_str(), black);
- apply_surface(350,100,tp,screen);
- string at = "Food cubes: "+is(hgfd);
- SDL_Surface* to = TTF_RenderText_Solid(tmpfont, at.c_str(), black);
- apply_surface(350,120,to,screen);
- string aq = "Cells with dr: "+is(hgdr);
- SDL_Surface* tq = TTF_RenderText_Solid(tmpfont, aq.c_str(), black);
- apply_surface(350,140,tq,screen);
- // finally, update the screen :)
- SDL_Flip(screen);
- SDL_Delay(timetowait/2);
- // Sleep(timetowait/2);
- } // end main loop
- // all is well ;)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement