Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- |================================================================|
- | LifeSim Artificial Life Simulator |
- | version 1.0.0 Final |
- | |
- | Coded by DarthRhonin and Sh3llc0d3r |
- | Thanks to: hackingfantasy.it, Google |
- | |
- | LifeSim is a complex artificial life simulator that simulate |
- | an artificial creature in an artificial enviroment. |
- | The creature can do many things, and can interact with others |
- | creatures of the other sex. There are two races and two sex |
- | for each race. There are many others features, discover them |
- | playing with the Sim!! Enjoy ^_^ |
- */
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <unistd.h>
- #define BRIGHT 1
- #define GREEN 32
- #define RED 31
- #define BO 34
- #define BG_BLACK 40
- using namespace std;
- char* nofilo;
- char* nomefile;
- void defcar(), sopravvivi(), riproduzione();
- bool predatore;
- int sesso;
- int temperatura;
- int malattia, vita, stamina;
- int anticorpo[5][10];
- int avm, amm, astm, atm, atmmin, atmmax;
- int evento;
- time_t rawtime;
- struct tm *timeinfo;
- int minuti;
- int main(int argc, char *argv[])
- {
- int cnt;
- char cnts[10];
- time ( &rawtime );
- timeinfo = localtime ( &rawtime );
- ofstream count;
- count.open("/tmp/count.txt");
- count << cnt;
- cnt++;
- count.close();
- ifstream countera;
- countera.open("/tmp/count.txt");
- countera >> cnt;
- countera.close();
- nomefile = argv[0];
- sprintf(cnts, "%d", cnt);
- nofilo = (char *) malloc(sizeof(char) * (strlen("/tmp/Log")+strlen(cnts) + strlen(".txt") + 1));
- sprintf(nofilo, "%s%s%s", "/tmp/Log", cnts, ".txt");
- cout <<"\nIl codice e' nato in: " << argv[0] << " \nin data "<<asctime(timeinfo) << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << nomefile << "\n Nascita in data: " << asctime(timeinfo) << endl;;
- log.close();
- cout << nomefile <<endl;
- defcar();
- sleep(2);
- sopravvivi();
- return 0;
- }
- void defcar()
- {
- int pgen;
- fstream control;
- fstream getatm;
- fstream getev;
- srand((unsigned)time(NULL));
- getatm.open("/tmp/tempinfo.txt");
- getatm >> temperatura;
- if (!getatm)
- {
- temperatura = rand() % 40 + 5;
- getatm << temperatura;
- }
- else
- {
- if (temperatura > 45)
- {
- temperatura = 45;
- }
- else if (temperatura < -10)
- {
- temperatura = -10;
- };
- };
- getatm.close();
- getev.open("/tmp/evinfo.txt");
- getev >> evento;
- if (!getev)
- {
- evento = rand() % 1 + 10;
- getev<< evento;
- }
- getev.close();
- fstream insert;
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- pgen = rand() % 7;
- control.open("/tmp/avpinfo.txt");
- control << avm;
- control.close();
- if (!control)
- {
- predatore == false;
- }
- else
- {
- predatore == true;
- };
- if (pgen == 4)
- {
- predatore = true;
- };
- if (predatore == true)
- {
- ifstream allelep;
- allelep.open("/tmp/avpinfo.txt");
- allelep >> avm;
- allelep.close();
- allelep.open("/tmp/ampinfo.txt");
- allelep >> amm;
- allelep.close();
- allelep.open("/tmp/astpinfo.txt");
- allelep >> astm;
- allelep.close();
- allelep.open("/tmp/atmpinfo.txt");
- allelep >> atm;
- allelep.close();
- while (!allelep)
- {
- astm = rand() % 25 + 100;
- avm = rand() % 799 + 100;
- amm = rand() % 10 + 40;
- atm = rand() % 30 + 10;
- break;
- }
- system("rm /tmp/avpinfo.txt");
- system("rm /tmp/ampinfo.txt");
- system("rm /tmp/astpinfo.txt");
- system("rm /tmp/atminfo.txt");
- }
- else
- {
- ifstream allele;
- allele.open("/tmp/avminfo.txt");
- allele >> avm;
- allele.close();
- allele.open("/tmp/amminfo.txt");
- allele >> amm;
- allele.close();
- allele.open("/tmp/astminfo.txt");
- allele >> astm;
- allele.close();
- allele.open("/tmp/atminfo.txt");
- allele >> atm;
- allele.close();
- while (!allele)
- {
- astm = rand() % 25 + 100;
- avm = rand() % 799 + 100;
- amm = rand() % 10 + 40;
- atm = rand() % 30 + 10;
- break;
- }
- system("rm /tmp/avminfo.txt");
- system("rm /tmp/amminfo.txt");
- system("rm /tmp/astminfo.txt");
- };
- sesso = rand() % 2;
- vita = rand() % avm + 100;
- stamina = rand() % astm + 50;
- cout << "Generazione caratteri basici in corso." ;
- atmmin = atm - 23;
- atmmax = atm + 27;
- sleep(1);
- cout << "." ;
- sleep(1);
- cout << "." <<endl;
- sleep(1);
- cout << " " <<endl;
- cout << "Nascita completata!" << endl;
- sleep(1);
- if (sesso == 0)
- {
- if (predatore == true)
- {
- printf("%c[%d;%d;%dm", 0x1B, BRIGHT,GREEN,BG_BLACK);
- cout << "Il figlio e' maschio PREDATORE, mangier� le uova per sopravvivere" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << "Caratteri generati: Maschio Predatore \n";
- log.close();
- }
- else
- {
- printf("%c[%d;%d;%dm", 0x1B, BRIGHT,RED,BG_BLACK);
- cout << "Il figlio e' maschio, produrra' le uova per la riproduzione" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << "Caratteri generati: Maschio Normale \n";
- log.close();
- };
- }
- else
- {
- if (predatore == true)
- {
- printf("%c[%d;%d;%dm", 0x1B, BRIGHT,RED,GREEN);
- cout << "Il figlio e' femmina PREDATRICE, mangier� le uova per sopravvivere" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << "Caratteri generati: Femmina Predatore \n";
- log.close();
- }
- else
- {
- printf("%c[%d;%d;%dm", 0x1B, BRIGHT,GREEN,RED);
- cout << "Il figlio e' femmina, mangera' il cibo vivendo" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << "Caratteri generati: Femmina Normale \n";
- log.close();
- };
- };
- cout << "La nascita a dato vita a uno zigote con le seguenti informazioni genetiche: IGAVM " << avm << " IGAMM " << amm << " IGASTM " << astm << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << "Caratteri generati: AVM " << avm << " AMM " << amm << " astm " << astm << " \n";
- log.close();
- cout << "La temperatura ideale del sim e' " << atmmin << " = " << atmmax << endl;
- log.open(nofilo, ios::app);
- log << "Temperatura ideale tra " << atmmin << " e " << atmmax << " \n";
- log.close();
- cout << "La temperatura ambiente e' " << temperatura << endl;
- sleep(1);
- cout << "Avvio delle funzioni vitali di sopravvivenza..." << endl;
- }
- void sopravvivi()
- {
- int presta;
- int alim;
- int done;
- int guarigione;
- int quanto;
- srand((unsigned)time(NULL));
- int mature;
- mature = (rand() % 40) + 40;
- cout << "Il Sim raggiungera' la maturita' all'eta' di " << mature << " cicli." << endl;
- sleep(1);
- int fenomeno;
- int counter = 0;
- ofstream alimentazione;
- ofstream insert;
- ofstream death;
- alimentazione.open("/tmp/alimentazione.txt", ios::app);
- cout << "\aFunzioni vitali attivate, inizio routine di sopravvivenza..." << endl;
- for (quanto = 0; quanto<101; quanto++)
- {
- srand((unsigned)time(NULL));
- minuti = timeinfo->tm_min;
- if ((minuti<=5)|(minuti>20&minuti<=25)|(minuti>40&minuti<=45))
- {
- if(presta!=1)
- {
- cout << "\nSiamo in estate: le temperature si fanno piu alte\n";
- temperatura=rand() % 22 + 22;
- presta = 1;
- }
- }
- else if ((minuti>5&minuti<=10)|(minuti>25&minuti<=30)|(minuti>45&minuti<=50))
- {
- if(presta!=2)
- {
- cout << "\nSiamo in autunno: le temperature sono tiepide\n";
- temperatura=rand() % 16 + 17;
- presta=2;
- }
- }
- else if ((minuti>10&minuti<=15)|(minuti>30&minuti<=35)|(minuti>50&minuti<=55))
- {
- if(presta!=3)
- {
- cout << "\nSiamo in inverno: le temperature si fanno piu basse\n";
- temperatura=rand() % 8 + 9;
- presta = 3;
- }
- }
- else if ((minuti>15&minuti<=20)|(minuti>35&minuti<=40)|(minuti>55&minuti<=59))
- {
- if(presta!=4)
- {
- cout << "\nSiamo in primavera: le temperature sono tiepide\n";
- temperatura=rand() % 13 + 13;
- presta=4;
- }
- }
- srand((unsigned)time(NULL));
- int rtemp = rand() % 15;
- fenomeno = rand() % 20;
- if (fenomeno == 5)
- {
- int tipo = rand() % 6;
- if (tipo == 1)
- {
- cout << "Sta piovendo, la temperatura si abbassa moderatamente..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Ambiente: Pioggia\n";
- log.close();
- temperatura = temperatura - 4;
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- sleep(1);
- }
- else if (tipo == 2)
- {
- cout << "Sta nevicando, la temperatura si abbassa drasticamente..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Ambiente: Neve\n";
- log.close();
- temperatura = temperatura - 10;
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- sleep(1);
- }
- else if (tipo == 3)
- {
- cout << "Sta venendo nebbia, la temperatura si abbassa lievemente" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Ambiente: Nebbia\n";
- log.close();
- temperatura = temperatura - 3;
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- sleep(1);
- }
- else if (tipo == 4)
- {
- temperatura = temperatura + 5;
- cout << "E' uscito il sole, la temperatura si alza moderatamente" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Ambiente: Sole\n";
- log.close();
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- sleep(1);
- }
- else if (tipo == 5)
- {
- temperatura = temperatura + 8;
- cout << "C'e' afa, la temperatura si alza notevolmente..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Ambiente: Afoso\n";
- log.close();
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- sleep(1);
- };
- }
- if (rtemp == 5)
- {
- int rdtemp = rand() % 2;
- if (rdtemp == 1)
- {
- temperatura = temperatura + 1;
- }
- else
- {
- temperatura = temperatura - 1;
- };
- insert.open("/tmp/tempinfo.txt");
- insert << temperatura;
- insert.close();
- if (!insert)
- {
- cout << "error" << endl;
- }
- }
- fstream getev;
- getev.open("/tmp/evinfo.txt");
- int yon;
- yon = rand() % 1 + 2;
- if (yon==1)
- {
- getev >> evento;
- }
- else
- {
- evento = rand() % 1 + 10;
- getev << evento;
- }
- if (!getev)
- {
- evento = rand() % 1 + 10;
- getev << evento;
- }
- getev.close();
- ifstream getatm;
- getatm.open("/tmp/tempinfo.txt");
- getatm >> temperatura;
- getatm.close();
- while ((temperatura < atmmin - 4) || (temperatura > atmmax + 4))
- {
- counter += 1;
- if (counter == 6)
- {
- cout << "Il Sim e' morto per temperatura inadatta prolungata..." << endl;
- cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: morte causa temperatura inadatta\n";
- log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- log.close();
- sleep(1);
- death.open("/tmp/death.txt");
- death << nomefile;
- death.close();
- exit(0);
- }
- cout << "Il Sim e' indebolito per la temperatura non idonea -> " << temperatura << " gradi." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: ATTENZIONE temperatura inadeguata\n";
- log.close();
- ifstream getatm;
- getatm.open("/tmp/tempinfo.txt");
- getatm >> temperatura;
- getatm.close();
- sleep(4);
- if(temperatura < atmmin - 4)
- {
- int periodo = rand() % 12;
- if(periodo==3){
- temperatura = temperatura + 2;
- }
- }
- else if(temperatura > atmmax + 4)
- {
- int periodo = rand() % 12;
- if(periodo==6){
- temperatura = temperatura - 2;
- }
- }
- }
- if (evento==2)
- {
- cout << "\nUn terremoto sconvolge la terra!\n\a";
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " \nEvento: Terremoto\n";
- guarigione = rand() %4;
- if (guarigione==3)
- {
- cout << "Il sim � sopravvissuto alla catastrofe...\n";
- log << " Terremoto Passato\n";
- }
- else if (guarigione==2)
- {
- cout << "Il sim � stato ferito ma � sopravvissuto...\n";
- vita-=1;
- stamina-=2;
- log << " Ferite riportate a causa del terremoto\n";
- }
- else
- {
- cout << "Il sim � stato ucciso dal terremoto\n";
- cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- log << "Il sim � morto a causa del terremoto\n";
- log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- death.open("/tmp/death.txt");
- death << nomefile;
- death.close();
- sleep(2);
- exit(1);
- }
- log.close();
- }
- if (evento==3)
- {
- cout << "Sul terreno sono cresciuti alberi dai frutti miracolosi!\n";
- ofstream log;
- log.open(nofilo, ios::app);
- log << " Trovato un frutto dalle propriet� benefiche\n";
- vita+=2;
- stamina+=2;
- atmmin-=2;
- atmmax+=2;
- sleep(1);
- log.close();
- cout << "Grazie al frutto ora il sim � piu resistente\n";
- }
- if (evento==4)
- {
- cout << "Il terreno � arido, in questo periodo...i sim si indeboliscono\n";
- ofstream log;
- log.open(nofilo, ios::app);
- log << " Terreno arido: indebolimento della specie\n";
- stamina-=1;
- atmmin+=1;
- log.close();
- }
- counter = 0;
- malattia = rand() % amm;
- if (malattia == 10)
- {
- int t_malattia = rand() % 7;
- while (t_malattia == 5)
- {
- if (temperatura > 15)
- {
- t_malattia = rand() % 7;
- };
- }
- int t_variante;
- if (t_malattia == 1)
- {
- cout << "Il codice si e' ammalato di Disformismo Circuitale..." << endl;
- t_variante = rand() % 7;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice si � ammalato di Disformismo Circuitale\n";
- log.close();
- }
- else if (t_malattia == 2)
- {
- cout << "Il codice si e' ammalato di Interfezione di Sintesi..." << endl;
- t_variante = rand() % 5;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice si � ammalato di Interfezione di Sintesi\n";
- log.close();
- }
- else if (t_malattia == 3)
- {
- cout << "Il codice si e' ammalato di Positronismo Virale..." << endl;
- t_variante = rand() % 7;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice si � ammalato di Positronismo Virale\n";
- log.close();
- }
- else if (t_malattia == 4)
- {
- cout << "Il codice si e' ammalato di Paresi Ricettiva..." << endl;
- t_variante = rand() % 4;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice si � ammalato di Paresi Ricettiva\n";
- log.close();
- }
- else if (t_malattia == 5)
- {
- cout << "Il codice si e' ammalato di Infezione UV ai fotoricettori..." << endl;
- t_variante = rand() % 3;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice si � ammalato di Infezione UV ai fotoricettori\n";
- log.close();
- }
- else
- {
- cout << "Il codice si e' ammalato di una malattia sconosciuta..." << endl;
- t_variante = rand() % 8;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice si � ammalato di una malattia sconosciuta\n";
- log.close();
- };
- sleep(5);
- srand((unsigned)time(NULL));
- bool difesa = false;
- bool pre_difesa = false;
- int v = 0;
- int i;
- for (i = 0; i < 5; i++)
- {
- v++;
- if (anticorpo[i][v]!=0)
- {
- if ((i == t_malattia) && (v == t_variante))
- {
- difesa = true;
- }
- if (i == t_malattia)
- {
- pre_difesa = true;
- }
- }
- }
- if (difesa == true)
- {
- guarigione = 0;
- }
- else
- {
- guarigione = rand() % 2;
- if (pre_difesa == true)
- {
- if (guarigione != 0)
- {
- guarigione = rand() % 2;
- }
- }
- }
- if (guarigione == 0)
- {
- anticorpo[i][v] = 1;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- if (difesa == true)
- {
- log << " malattia superata, il codice � guarito grazie agli anticorpi\n";
- }
- else
- {
- log << " malattia superata, il codice � guarito\n";
- }
- log.close();
- sleep(2);
- }
- else
- {
- cout << "il codice e' morto per la grave malattia..." << endl;
- cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " malattia mortale: il codice � morto.\n";
- log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- log.close();
- death.open("/tmp/death.txt");
- death << nomefile;
- death.close();
- sleep(2);
- exit(0);
- };
- };
- if (sesso == 0)
- {
- if (predatore == true)
- {
- cout << "Sistema di caccia attivo e funzionante, ciclo numero " << quanto << " temperatura " << temperatura << " gradi." << endl;
- sleep(1);
- }
- else
- {
- alimentazione << "Cibo \n";
- cout << "Sistema produttivo attivo e funzionante, ciclo numero " << quanto << " temperatura " << temperatura << " gradi." << endl;
- sleep(1);
- }
- }
- else
- {
- if (predatore == true)
- {
- cout << "Sistema di caccia attivo e funzionante, ciclo numero " << quanto << " temperatura " << temperatura << " gradi." << endl;
- sleep(1);
- }
- else
- {
- alimentazione << "Cibo\n";
- cout << "Sistema alimentare attivo e funzionante, ciclo numero " << quanto<< " temperatura " << temperatura << " gradi." << endl;
- sleep(1);
- }
- };
- while (!alimentazione)
- {
- alim++;
- if (alim==3)
- {
- cout << "Il codice e' morto di fame in quanto non � riuscito a procurarsi il cibo..." << endl;
- cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice � morto per assenza di cibo!\n";
- log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- log.close();
- death.open("/tmp/death.txt");
- death << nomefile;
- death.close();
- sleep(2);
- exit(0);
- }
- else
- {
- cout << "Il codice non ha trovato cibo per nutrirsi, ed inizia ad indebolirsi..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il cibo scarseggia, il codice si indebolisce\n";
- log.close();
- stamina = stamina - 1;
- }
- }
- vita = vita - 1;
- stamina = stamina - 1;
- if (vita == 0)
- {
- cout << "Il codice e' morto di vecchiaia... " << endl;
- cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice ha finito il suo ciclo vitale, ed ora muore in pace :)\n";
- log << " Codice morto in data: " << asctime(timeinfo) << endl;
- log.close();
- death.open("/tmp/death.txt");
- death << nomefile;
- death.close();
- sleep(2);
- exit(0);
- };
- if (stamina == 0)
- {
- srand((unsigned)time(NULL));
- cout << "Il codice e' stanco e deve riposare..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: il codice � stanco e si ferma a riposare...\n";
- log.close();
- sleep(5);
- stamina = rand () % astm + 50;
- };
- if (predatore == true)
- {
- if (quanto == 40)
- {
- cout << "Il codice e' affamato e tenta di assalire delle uova..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento Predatore: Il predatore cerca di assalire le uova di un predatore normale\n";
- log.close();
- sleep(2);
- ifstream udc;
- int cnt;
- bool avmvero, ammvero, astmvero;
- udc.open("/tmp/avminfo.txt", ios::app);
- if (udc.is_open())
- {
- avmvero = true;
- }
- else
- {
- avmvero = false;
- }
- udc.close();
- if (avmvero==true)
- {
- system("rm /tmp/avminfo.txt");
- system("rm /tmp/amminfo.txt");
- system("rm /tmp/astminfo.txt");
- }
- else
- {
- cnt++;
- if (cnt==3)
- {
- cout << "Il codice � morto di fame, non avendo trovato pi� uova da mangiare" << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento Predatore: non avendo trovado uova per molto tempo, il predatore � morto di fame\n";
- log.close();
- death.open("/tmp/death.txt");
- death << nomefile;
- death.close();
- sleep(2);
- exit(0);
- }
- else
- {
- cout << "Il codice non ha trovato uova e digiuna, indebolendosi" <<endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento Predatore: Le uova scarseggiano, il digiuno indebolisce il predatore\n";
- log.close();
- stamina = stamina -1;
- sleep(2);
- }
- }
- };
- if (quanto > mature)
- {
- quanto = 0;
- cout << "Il codice ha cacciato abbastanza per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: Passaggio alla fase riproduttiva\n";
- log.close();
- if (sesso == 0)
- {
- cout << "Il codice ha prodotto abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: Passaggio alla fase riproduttiva\n";
- log.close();
- cout << "Il codice sta per produrre le uova di un figlio. " << endl;
- sleep(2);
- riproduzione();
- }
- else
- {
- cout << "Il codice si e' alimentato abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: Passaggio alla fase riproduttiva\n";
- log.close();
- cout << "Se il programma trovera' le uova fara' nascere il figlio..." << endl;
- sleep(2);
- riproduzione();
- };
- };
- }
- else
- {
- if (quanto > mature)
- {
- quanto = 0;
- if (sesso == 0)
- {
- cout << "Il codice ha prodotto abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: Passaggio alla fase riproduttiva\n";
- log.close();
- cout << "Il codice sta per produrre le uova di un figlio. Le uova conterranno il materiale genetico paterno e attenderanno una femmina che le incubi" <<endl;
- sleep(2);
- riproduzione();
- }
- else
- {
- cout << "Il codice si e' alimentato abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Evento: Passaggio alla fase riproduttiva\n";
- log.close();
- cout << "Se il programma trovera' le uova fara' nascere il figlio..." << endl;
- sleep(2);
- riproduzione();
- };
- };
- };
- }
- }
- void riproduzione()
- {
- char* comfin;
- int newavm, newamm, newastm, newatm;
- int complicazioni;
- char ch[10];
- srand((unsigned)time(NULL));
- complicazioni = rand() % 15;
- if (complicazioni == 10)
- {
- cout << endl;
- cout << "ci sono state complicazioni nella riproduzione..." << endl;
- cout << "Il figlio e' morto..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Complicazioni riproduttive, il figlio � morto\n";
- log.close();
- sleep(2);
- sopravvivi();
- }
- else
- {
- if (sesso == 0)
- {
- if (predatore == true)
- {
- ofstream fp;
- fp.open("/tmp/avpinfo.txt");
- fp << avm;
- fp.close();
- fp.open("/tmp/ampinfo.txt");
- fp << amm;
- fp.close();
- fp.open("/tmp/astpinfo.txt");
- fp << astm;
- fp.close();
- fp.open("/tmp/atmpinfo.txt");
- fp << atm;
- fp.close();
- while (!fp)
- {
- cout << "Il codice non � riuscito a depositare le uova..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova non deposte\n";
- log.close();
- sleep(2);
- sopravvivi();
- }
- }
- else
- {
- ofstream fd;
- fd.open("/tmp/avminfo.txt");
- fd << avm;
- fd.close();
- fd.open("/tmp/amminfo.txt");
- fd << amm;
- fd.close();
- fd.open("/tmp/astminfo.txt");
- fd << astm;
- fd.close();
- fd.open("/tmp/atminfo.txt");
- fd << atm;
- fd.close();
- while (!fd)
- {
- cout << "Il codice non � riuscito a depositare le uova..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova non deposte\n";
- log.close();
- sleep(2);
- sopravvivi();
- }
- cout << "Dopo aver deposto le uova il programma deve riposarsi..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova deposte, riposo\n";
- log.close();
- sleep(3);
- sopravvivi();
- };
- }
- else
- {
- int i = rand() % 301;
- int* nnrand;
- sprintf(ch,"%d",i);
- ifstream elabavm;
- ifstream elabamm;
- ifstream elabastm;
- ifstream elabatm;
- ofstream save;
- if (predatore == false)
- {
- elabavm.open("/tmp/avminfo.txt");
- elabavm >> newavm;
- elabavm.close();
- }
- else
- {
- elabavm.open("/tmp/avpinfo.txt");
- elabavm >> newavm;
- elabavm.close();
- };
- if (!elabavm)
- {
- cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova non trovate, riproduzione annullata\n";
- log.close();
- sleep(2);
- sopravvivi();
- };
- if (predatore == false)
- {
- elabamm.open("/tmp/avminfo.txt");
- elabamm >> newamm;
- elabamm.close();
- }
- else
- {
- elabamm.open("/tmp/avpinfo.txt");
- elabamm >> newamm;
- elabamm.close();
- };
- if (!elabamm)
- {
- cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova non trovate, riproduzione annullata\n";
- log.close();
- sleep(2);
- sopravvivi();
- };
- if (predatore == false)
- {
- elabavm.open("/tmp/avminfo.txt");
- elabavm >> newastm;
- elabavm.close();
- }
- else
- {
- elabastm.open("/tmp/avpinfo.txt");
- elabastm >> newastm;
- elabastm.close();
- };
- if (!elabastm)
- {
- cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova non trovate, riproduzione annullata\n";
- log.close();
- sleep(2);
- sopravvivi();
- };
- if (predatore == false)
- {
- elabatm.open("/tmp/atminfo.txt");
- elabatm >> newatm;
- elabatm.close();
- }
- else
- {
- elabastm.open("/tmp/atminfo.txt");
- elabatm >> newatm;
- elabatm.close();
- };
- if (!elabatm)
- {
- cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Uova non trovate, riproduzione annullata\n";
- log.close();
- sleep(2);
- sopravvivi();
- };
- if (newamm < amm)
- {
- nnrand = new int;
- *nnrand = rand() % 15 + 10;
- newamm = ((newamm / amm * amm) + (rand() % *nnrand));
- delete nnrand;
- }
- else
- {
- nnrand = new int;
- *nnrand = rand() % 15 + 10;
- newamm = ((newamm / amm * amm) - (rand() % *nnrand));
- delete nnrand;
- };
- if (newavm < avm)
- {
- nnrand = new int;
- *nnrand = rand() % 50 + 100;
- newavm = ((newavm / avm * avm) + (rand() % *nnrand));
- delete nnrand;
- }
- else
- {
- nnrand = new int;
- *nnrand = rand() % 50 + 100;
- newavm = ((newavm / avm * avm) - (rand() % *nnrand));
- delete nnrand;
- };
- if (newastm < astm)
- {
- nnrand = new int;
- *nnrand = rand() % 15 + 30;
- newastm = ((newastm / astm * astm) + (rand() % *nnrand));
- delete nnrand;
- }
- else
- {
- nnrand = new int;
- *nnrand = rand() % 15 + 30;
- newastm = ((newastm / astm * astm) - (rand() % *nnrand));
- delete nnrand;
- };
- if (newatm < atm)
- {
- nnrand = new int;
- *nnrand = rand() % 10 + 5;
- newastm = ((newatm / atm * atm) + (rand() % *nnrand));
- delete nnrand;
- }
- else
- {
- nnrand = new int;
- *nnrand = rand() % 10 + 5;
- newatm = ((newatm / atm * atm) - (rand() % *nnrand));
- delete nnrand;
- };
- if (predatore == true)
- {
- save.open("/tmp/atmpinfo.txt", ios::out);
- save << newavm;
- save.close();
- save.open("/tmp/atmpinfo.txt", ios::out);
- save << newamm;
- save.close();
- save.open("/tmp/atmpinfo.txt", ios::out);
- save << newastm;
- save.close();
- save.open("/tmp/atmpinfo.txt", ios::out);
- save << newastm;
- save.close();
- while (!save)
- {
- cout << "Errore nella fecondazione del gamete..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Gamete non fecondato correttamente\n";
- log.close();
- sleep(2);
- sopravvivi();
- break;
- }
- }
- else
- {
- save.open("/tmp/atminfo.txt", ios::out);
- save << newavm;
- save.close();
- save.open("/tmp/atminfo.txt", ios::out);
- save << newamm;
- save.close();
- save.open("/tmp/atminfo.txt", ios::out);
- save << newastm;
- save.close();
- save.open("/tmp/atminfo.txt", ios::out);
- save << newastm;
- save.close();
- while (!save)
- {
- cout << "Errore nella fecondazione del gamete..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Gamete non fecondato correttamente\n";
- log.close();
- sleep(2);
- sopravvivi();
- break;
- }
- };
- char* nomefiglio;
- comfin = (char *) malloc(sizeof(char) * (strlen("cp ") + strlen("\"") + strlen(nomefile) + strlen("\"")+ strlen(" figlio") + strlen(ch) + 3));
- sprintf(comfin, "%s%s%s%s%s%s", "cp ", "\"", nomefile, "\"", " figlio", ch);
- FILE* out;
- out = popen(comfin, "w");
- pclose(out);
- nomefiglio = (char *) malloc(sizeof(char) * (strlen("xterm -e ./figlio") + strlen(ch) + 2));
- sprintf(nomefiglio, "%s%s", "xterm -e ./figlio", ch);
- out = popen(nomefiglio, "w");
- pclose(out);
- cout << "Dopo aver partorito il programma deve riposarsi..." << endl;
- ofstream log;
- log.open(nofilo, ios::app);
- log << " ";
- log << " Riproduzione: Il codice ha partorito, riposo post parto ^_^\n";
- log.close();
- free(comfin);
- free(nomefiglio);
- free(nofilo);
- sleep(3);
- sopravvivi();
- };
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment