Guest User

Untitled

a guest
May 8th, 2017
9,838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 41.92 KB | None | 0 0
  1. /*
  2.   |================================================================|
  3.   |                LifeSim Artificial Life Simulator               |
  4.   |                     version 1.0.0 Final                        |
  5.   |                                                                |
  6.   |                Coded by DarthRhonin and Sh3llc0d3r             |
  7.   |                Thanks to: hackingfantasy.it, Google            |
  8.   |                                                                |
  9.   |  LifeSim is a complex artificial life simulator that simulate  |
  10.   |  an artificial creature in an artificial enviroment.           |
  11.   |  The creature can do many things, and can interact with others |
  12.   |  creatures of the other sex. There are two races and two sex   |
  13.   |  for each race. There are many others features, discover them  |
  14.   |  playing with the Sim!! Enjoy ^_^                              |
  15. */
  16. #include <iostream>
  17. #include <fstream>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #define BRIGHT 1
  24. #define GREEN 32
  25. #define RED 31
  26. #define BO 34
  27. #define BG_BLACK 40
  28. using namespace std;
  29.  
  30. char* nofilo;
  31. char* nomefile;
  32. void defcar(), sopravvivi(), riproduzione();
  33. bool predatore;
  34. int sesso;
  35. int temperatura;
  36. int malattia, vita, stamina;
  37. int anticorpo[5][10];
  38. int avm, amm, astm, atm, atmmin, atmmax;
  39. int evento;
  40. time_t rawtime;
  41. struct tm *timeinfo;
  42. int minuti;
  43.  
  44.  
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.  
  49.  
  50.  
  51.     int cnt;
  52.     char cnts[10];
  53.     time ( &rawtime );
  54.     timeinfo = localtime ( &rawtime );
  55.     ofstream count;
  56.     count.open("/tmp/count.txt");
  57.     count << cnt;
  58.     cnt++;
  59.     count.close();
  60.     ifstream countera;
  61.     countera.open("/tmp/count.txt");
  62.     countera >> cnt;
  63.     countera.close();
  64.     nomefile = argv[0];
  65.     sprintf(cnts, "%d", cnt);
  66.     nofilo = (char *) malloc(sizeof(char) * (strlen("/tmp/Log")+strlen(cnts) + strlen(".txt") + 1));
  67.     sprintf(nofilo, "%s%s%s",  "/tmp/Log", cnts, ".txt");
  68.     cout <<"\nIl codice e' nato in: " << argv[0] << " \nin data "<<asctime(timeinfo) << endl;
  69.  
  70.    ofstream log;
  71.     log.open(nofilo, ios::app);
  72.     log <<   nomefile << "\n  Nascita in data: " << asctime(timeinfo) << endl;;
  73.  
  74.  
  75.  
  76.     log.close();
  77.     cout << nomefile <<endl;
  78.  
  79.     defcar();
  80.     sleep(2);
  81.     sopravvivi();
  82.     return 0;
  83. }
  84. void defcar()
  85. {
  86.     int pgen;
  87.     fstream control;
  88.     fstream getatm;
  89.     fstream getev;
  90.     srand((unsigned)time(NULL));
  91.     getatm.open("/tmp/tempinfo.txt");
  92.     getatm >> temperatura;
  93.     if (!getatm)
  94.     {
  95.         temperatura = rand() % 40 + 5;
  96.         getatm << temperatura;
  97.     }
  98.  
  99.     else
  100.     {
  101.         if (temperatura > 45)
  102.         {
  103.             temperatura = 45;
  104.         }
  105.         else if (temperatura < -10)
  106.         {
  107.             temperatura = -10;
  108.         };
  109.     };
  110.     getatm.close();
  111.     getev.open("/tmp/evinfo.txt");
  112.     getev >> evento;
  113.  
  114.     if (!getev)
  115.     {
  116.         evento = rand() % 1 + 10;
  117.         getev<< evento;
  118.  
  119.     }
  120.     getev.close();
  121.  
  122.     fstream insert;
  123.     insert.open("/tmp/tempinfo.txt");
  124.     insert << temperatura;
  125.     insert.close();
  126.     pgen = rand() % 7;
  127.     control.open("/tmp/avpinfo.txt");
  128.     control <<  avm;
  129.     control.close();
  130.     if (!control)
  131.     {
  132.         predatore == false;
  133.     }
  134.     else
  135.     {
  136.         predatore == true;
  137.     };
  138.     if (pgen == 4)
  139.     {
  140.         predatore = true;
  141.     };
  142.     if (predatore == true)
  143.     {
  144.  
  145.         ifstream allelep;
  146.         allelep.open("/tmp/avpinfo.txt");
  147.         allelep >> avm;
  148.         allelep.close();
  149.         allelep.open("/tmp/ampinfo.txt");
  150.         allelep >> amm;
  151.         allelep.close();
  152.         allelep.open("/tmp/astpinfo.txt");
  153.         allelep >> astm;
  154.         allelep.close();
  155.         allelep.open("/tmp/atmpinfo.txt");
  156.         allelep >> atm;
  157.         allelep.close();
  158.         while (!allelep)
  159.         {
  160.             astm = rand() % 25 + 100;
  161.             avm = rand() % 799 + 100;
  162.             amm = rand() % 10 + 40;
  163.             atm = rand() % 30 + 10;
  164.             break;
  165.         }
  166.         system("rm /tmp/avpinfo.txt");
  167.         system("rm /tmp/ampinfo.txt");
  168.         system("rm /tmp/astpinfo.txt");
  169.         system("rm /tmp/atminfo.txt");
  170.     }
  171.     else
  172.     {
  173.  
  174.         ifstream allele;
  175.         allele.open("/tmp/avminfo.txt");
  176.         allele >> avm;
  177.         allele.close();
  178.         allele.open("/tmp/amminfo.txt");
  179.         allele >> amm;
  180.         allele.close();
  181.         allele.open("/tmp/astminfo.txt");
  182.         allele >> astm;
  183.         allele.close();
  184.         allele.open("/tmp/atminfo.txt");
  185.         allele >> atm;
  186.         allele.close();
  187.         while (!allele)
  188.         {
  189.             astm = rand() % 25 + 100;
  190.             avm = rand() % 799 + 100;
  191.             amm = rand() % 10 + 40;
  192.             atm = rand() % 30 + 10;
  193.             break;
  194.         }
  195.         system("rm /tmp/avminfo.txt");
  196.         system("rm /tmp/amminfo.txt");
  197.         system("rm /tmp/astminfo.txt");
  198.     };
  199.     sesso = rand() % 2;
  200.     vita = rand() % avm + 100;
  201.     stamina = rand() % astm + 50;
  202.     cout << "Generazione caratteri basici in corso." ;
  203.     atmmin = atm - 23;
  204.     atmmax = atm + 27;
  205.     sleep(1);
  206.     cout << "." ;
  207.     sleep(1);
  208.     cout << "." <<endl;
  209.     sleep(1);
  210.     cout << " " <<endl;
  211.     cout << "Nascita completata!" << endl;
  212.     sleep(1);
  213.     if (sesso == 0)
  214.     {
  215.         if (predatore == true)
  216.         {
  217.  
  218.             printf("%c[%d;%d;%dm", 0x1B, BRIGHT,GREEN,BG_BLACK);
  219.             cout << "Il figlio e' maschio PREDATORE, mangier� le uova per sopravvivere" << endl;
  220.             ofstream log;
  221.             log.open(nofilo, ios::app);
  222.             log << "Caratteri generati: Maschio Predatore \n";
  223.             log.close();
  224.         }
  225.         else
  226.         {
  227.  
  228.             printf("%c[%d;%d;%dm", 0x1B, BRIGHT,RED,BG_BLACK);
  229.             cout << "Il figlio e' maschio, produrra' le uova per la riproduzione" << endl;
  230.             ofstream log;
  231.             log.open(nofilo, ios::app);
  232.             log << "Caratteri generati: Maschio Normale \n";
  233.             log.close();
  234.         };
  235.     }
  236.     else
  237.     {
  238.         if (predatore == true)
  239.         {
  240.  
  241.             printf("%c[%d;%d;%dm", 0x1B, BRIGHT,RED,GREEN);
  242.             cout << "Il figlio e' femmina PREDATRICE, mangier� le uova per sopravvivere" << endl;
  243.             ofstream log;
  244.             log.open(nofilo, ios::app);
  245.             log << "Caratteri generati: Femmina Predatore \n";
  246.             log.close();
  247.         }
  248.         else
  249.         {
  250.  
  251.             printf("%c[%d;%d;%dm", 0x1B, BRIGHT,GREEN,RED);
  252.             cout << "Il figlio e' femmina, mangera' il cibo vivendo" << endl;
  253.             ofstream log;
  254.             log.open(nofilo, ios::app);
  255.             log << "Caratteri generati: Femmina Normale \n";
  256.             log.close();
  257.         };
  258.     };
  259.     cout << "La nascita a dato vita a uno zigote con le seguenti informazioni genetiche: IGAVM " << avm << " IGAMM " << amm << " IGASTM " << astm << endl;
  260.     ofstream log;
  261.     log.open(nofilo, ios::app);
  262.     log << "Caratteri generati: AVM " << avm << " AMM " << amm << " astm " << astm << " \n";
  263.     log.close();
  264.     cout << "La temperatura ideale del sim e' " << atmmin << " = " << atmmax << endl;
  265.     log.open(nofilo, ios::app);
  266.     log << "Temperatura ideale tra " << atmmin << " e " << atmmax << " \n";
  267.     log.close();
  268.     cout << "La temperatura ambiente e' " << temperatura << endl;
  269.     sleep(1);
  270.     cout << "Avvio delle funzioni vitali di sopravvivenza..." << endl;
  271. }
  272. void sopravvivi()
  273. {
  274.     int presta;
  275.     int alim;
  276.     int done;
  277.     int guarigione;
  278.     int quanto;
  279.     srand((unsigned)time(NULL));
  280.     int mature;
  281.     mature = (rand() % 40) + 40;
  282.     cout << "Il Sim raggiungera' la maturita' all'eta' di " << mature << " cicli." << endl;
  283.     sleep(1);
  284.     int fenomeno;
  285.     int counter = 0;
  286.     ofstream alimentazione;
  287.     ofstream insert;
  288.     ofstream death;
  289.     alimentazione.open("/tmp/alimentazione.txt", ios::app);
  290.  
  291.     cout << "\aFunzioni vitali attivate, inizio routine di sopravvivenza..." << endl;
  292.     for (quanto = 0; quanto<101; quanto++)
  293.     {
  294.         srand((unsigned)time(NULL));
  295.         minuti = timeinfo->tm_min;
  296.         if ((minuti<=5)|(minuti>20&minuti<=25)|(minuti>40&minuti<=45))
  297.         {
  298.             if(presta!=1)
  299.             {
  300.             cout << "\nSiamo in estate: le temperature si fanno piu alte\n";
  301.             temperatura=rand() % 22 + 22;
  302.  
  303.             presta = 1;
  304.             }
  305.  
  306.         }
  307.         else if ((minuti>5&minuti<=10)|(minuti>25&minuti<=30)|(minuti>45&minuti<=50))
  308.         {
  309.             if(presta!=2)
  310.             {
  311.             cout << "\nSiamo in autunno: le temperature sono tiepide\n";
  312.             temperatura=rand() % 16 + 17;
  313.             presta=2;
  314.             }
  315.         }
  316.         else if ((minuti>10&minuti<=15)|(minuti>30&minuti<=35)|(minuti>50&minuti<=55))
  317.         {
  318.             if(presta!=3)
  319.             {
  320.             cout << "\nSiamo in inverno: le temperature si fanno piu basse\n";
  321.            temperatura=rand() % 8 + 9;
  322.         presta = 3;
  323.             }
  324.         }
  325.         else if ((minuti>15&minuti<=20)|(minuti>35&minuti<=40)|(minuti>55&minuti<=59))
  326.         {
  327.             if(presta!=4)
  328.             {
  329.             cout << "\nSiamo in primavera: le temperature sono tiepide\n";
  330.         temperatura=rand() % 13 + 13;
  331.         presta=4;
  332.             }
  333.         }
  334.         srand((unsigned)time(NULL));
  335.         int rtemp = rand() % 15;
  336.         fenomeno = rand() % 20;
  337.         if (fenomeno == 5)
  338.         {
  339.             int tipo = rand() % 6;
  340.             if (tipo == 1)
  341.             {
  342.                 cout << "Sta piovendo, la temperatura si abbassa moderatamente..." << endl;
  343.                 ofstream log;
  344.                 log.open(nofilo, ios::app);
  345.  
  346.                 log << "  ";
  347.  
  348.                 log << "  Ambiente: Pioggia\n";
  349.                 log.close();
  350.                 temperatura = temperatura - 4;
  351.                 insert.open("/tmp/tempinfo.txt");
  352.                 insert << temperatura;
  353.                 insert.close();
  354.                 sleep(1);
  355.             }
  356.             else if (tipo == 2)
  357.             {
  358.                 cout << "Sta nevicando, la temperatura si abbassa drasticamente..." << endl;
  359.                 ofstream log;
  360.                 log.open(nofilo, ios::app);
  361.  
  362.                 log << "  ";
  363.  
  364.                 log << "  Ambiente: Neve\n";
  365.                 log.close();
  366.                 temperatura = temperatura - 10;
  367.                 insert.open("/tmp/tempinfo.txt");
  368.                 insert << temperatura;
  369.                 insert.close();
  370.                 sleep(1);
  371.             }
  372.             else if (tipo == 3)
  373.             {
  374.                 cout << "Sta venendo nebbia, la temperatura si abbassa lievemente" << endl;
  375.                 ofstream log;
  376.                 log.open(nofilo, ios::app);
  377.  
  378.                 log << "  ";
  379.  
  380.                 log << "  Ambiente: Nebbia\n";
  381.                 log.close();
  382.                 temperatura = temperatura - 3;
  383.                 insert.open("/tmp/tempinfo.txt");
  384.                 insert << temperatura;
  385.                 insert.close();
  386.                 sleep(1);
  387.             }
  388.             else if (tipo == 4)
  389.             {
  390.                 temperatura = temperatura + 5;
  391.                 cout << "E' uscito il sole, la temperatura si alza moderatamente" << endl;
  392.                 ofstream log;
  393.                 log.open(nofilo, ios::app);
  394.  
  395.                 log << "  ";
  396.  
  397.                 log << "  Ambiente: Sole\n";
  398.                 log.close();
  399.                 insert.open("/tmp/tempinfo.txt");
  400.                 insert << temperatura;
  401.                 insert.close();
  402.                 sleep(1);
  403.             }
  404.             else if (tipo == 5)
  405.             {
  406.                 temperatura = temperatura + 8;
  407.                 cout << "C'e' afa, la temperatura si alza notevolmente..." << endl;
  408.                 ofstream log;
  409.                 log.open(nofilo, ios::app);
  410.  
  411.                 log << "  ";
  412.  
  413.                 log << "  Ambiente: Afoso\n";
  414.                 log.close();
  415.                 insert.open("/tmp/tempinfo.txt");
  416.                 insert << temperatura;
  417.                 insert.close();
  418.                 sleep(1);
  419.             };
  420.         }
  421.         if (rtemp == 5)
  422.         {
  423.             int rdtemp = rand() % 2;
  424.             if (rdtemp == 1)
  425.             {
  426.                 temperatura = temperatura + 1;
  427.             }
  428.             else
  429.             {
  430.                 temperatura = temperatura - 1;
  431.             };
  432.             insert.open("/tmp/tempinfo.txt");
  433.             insert << temperatura;
  434.             insert.close();
  435.             if (!insert)
  436.             {
  437.                 cout << "error" << endl;
  438.             }
  439.         }
  440.         fstream getev;
  441.         getev.open("/tmp/evinfo.txt");
  442.         int yon;
  443.         yon = rand() % 1 + 2;
  444.         if (yon==1)
  445.         {
  446.             getev >> evento;
  447.         }
  448.         else
  449.         {
  450.             evento = rand() % 1 + 10;
  451.             getev << evento;
  452.         }
  453.         if (!getev)
  454.         {
  455.             evento = rand() % 1 + 10;
  456.             getev << evento;
  457.  
  458.         }
  459.         getev.close();
  460.  
  461.         ifstream getatm;
  462.         getatm.open("/tmp/tempinfo.txt");
  463.         getatm >> temperatura;
  464.         getatm.close();
  465.         while ((temperatura < atmmin - 4) || (temperatura > atmmax + 4))
  466.         {
  467.             counter += 1;
  468.             if (counter == 6)
  469.             {
  470.                 cout << "Il Sim e' morto per temperatura inadatta prolungata..." << endl;
  471.                 cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  472.                 ofstream log;
  473.                 log.open(nofilo, ios::app);
  474.  
  475.                 log << "  ";
  476.  
  477.                 log << "  Evento: morte causa temperatura inadatta\n";
  478.                 log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  479.                 log.close();
  480.                 sleep(1);
  481.                 death.open("/tmp/death.txt");
  482.                 death << nomefile;
  483.                 death.close();
  484.  
  485.                 exit(0);
  486.             }
  487.             cout << "Il Sim e' indebolito per la temperatura non idonea -> " << temperatura << " gradi." << endl;
  488.             ofstream log;
  489.             log.open(nofilo, ios::app);
  490.  
  491.             log << "  ";
  492.  
  493.             log << "  Evento: ATTENZIONE temperatura inadeguata\n";
  494.             log.close();
  495.             ifstream getatm;
  496.             getatm.open("/tmp/tempinfo.txt");
  497.             getatm >> temperatura;
  498.             getatm.close();
  499.             sleep(4);
  500.             if(temperatura < atmmin - 4)
  501.             {
  502.                 int periodo = rand() % 12;
  503.                 if(periodo==3){
  504.                     temperatura = temperatura + 2;
  505.                 }
  506.             }
  507.             else if(temperatura > atmmax + 4)
  508.             {
  509.                 int periodo = rand() % 12;
  510.                 if(periodo==6){
  511.                     temperatura = temperatura - 2;
  512.                 }
  513.             }
  514.         }
  515.         if (evento==2)
  516.         {
  517.             cout << "\nUn terremoto sconvolge la terra!\n\a";
  518.             ofstream log;
  519.             log.open(nofilo, ios::app);
  520.  
  521.             log << "  ";
  522.  
  523.             log << "  \nEvento: Terremoto\n";
  524.  
  525.             guarigione = rand() %4;
  526.             if (guarigione==3)
  527.             {
  528.                 cout << "Il sim � sopravvissuto alla catastrofe...\n";
  529.                 log << "   Terremoto Passato\n";
  530.             }
  531.             else if (guarigione==2)
  532.             {
  533.                 cout << "Il sim � stato ferito ma � sopravvissuto...\n";
  534.                 vita-=1;
  535.                 stamina-=2;
  536.                 log << "   Ferite riportate a causa del terremoto\n";
  537.             }
  538.             else
  539.             {
  540.                 cout << "Il sim � stato ucciso dal terremoto\n";
  541.                 cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  542.                 log << "Il sim � morto a causa del terremoto\n";
  543.                 log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  544.                 death.open("/tmp/death.txt");
  545.                 death << nomefile;
  546.                 death.close();
  547.                 sleep(2);
  548.                 exit(1);
  549.             }
  550.             log.close();
  551.         }
  552.         if (evento==3)
  553.         {
  554.             cout << "Sul terreno sono cresciuti alberi dai frutti miracolosi!\n";
  555.             ofstream log;
  556.             log.open(nofilo, ios::app);
  557.             log << "   Trovato un frutto dalle propriet� benefiche\n";
  558.             vita+=2;
  559.             stamina+=2;
  560.             atmmin-=2;
  561.             atmmax+=2;
  562.             sleep(1);
  563.             log.close();
  564.             cout << "Grazie al frutto ora il sim � piu resistente\n";
  565.         }
  566.         if (evento==4)
  567.         {
  568.             cout << "Il terreno � arido, in questo periodo...i sim si indeboliscono\n";
  569.             ofstream log;
  570.             log.open(nofilo, ios::app);
  571.             log << "   Terreno arido: indebolimento della specie\n";
  572.             stamina-=1;
  573.             atmmin+=1;
  574.             log.close();
  575.         }
  576.  
  577.  
  578.         counter = 0;
  579.         malattia = rand() % amm;
  580.         if (malattia == 10)
  581.         {
  582.             int t_malattia = rand() % 7;
  583.             while (t_malattia == 5)
  584.             {
  585.                 if (temperatura > 15)
  586.                 {
  587.                     t_malattia = rand() % 7;
  588.                 };
  589.             }
  590.             int t_variante;
  591.             if (t_malattia == 1)
  592.             {
  593.                 cout << "Il codice si e' ammalato di Disformismo Circuitale..." << endl;
  594.                 t_variante = rand() % 7;
  595.                 ofstream log;
  596.                 log.open(nofilo, ios::app);
  597.  
  598.                 log << "  ";
  599.  
  600.                 log << "  Evento: il codice si � ammalato di Disformismo Circuitale\n";
  601.                 log.close();
  602.             }
  603.             else if (t_malattia == 2)
  604.             {
  605.                 cout << "Il codice si e' ammalato di Interfezione di Sintesi..." << endl;
  606.                 t_variante = rand() % 5;
  607.                 ofstream log;
  608.                 log.open(nofilo, ios::app);
  609.  
  610.                 log << "  ";
  611.  
  612.                 log << "  Evento: il codice si � ammalato di Interfezione di Sintesi\n";
  613.                 log.close();
  614.             }
  615.             else if (t_malattia == 3)
  616.             {
  617.                 cout << "Il codice si e' ammalato di Positronismo Virale..." << endl;
  618.                 t_variante = rand() % 7;
  619.                 ofstream log;
  620.                 log.open(nofilo, ios::app);
  621.  
  622.                 log << "  ";
  623.  
  624.                 log << "  Evento: il codice si � ammalato di Positronismo Virale\n";
  625.                 log.close();
  626.             }
  627.             else if (t_malattia == 4)
  628.             {
  629.                 cout << "Il codice si e' ammalato di Paresi Ricettiva..." << endl;
  630.                 t_variante = rand() % 4;
  631.                 ofstream log;
  632.                 log.open(nofilo, ios::app);
  633.  
  634.                 log << "  ";
  635.  
  636.                 log << "  Evento: il codice si � ammalato di Paresi Ricettiva\n";
  637.                 log.close();
  638.             }
  639.             else if (t_malattia == 5)
  640.             {
  641.                 cout << "Il codice si e' ammalato di Infezione UV ai fotoricettori..." << endl;
  642.                 t_variante = rand() % 3;
  643.                 ofstream log;
  644.                 log.open(nofilo, ios::app);
  645.  
  646.                 log << "  ";
  647.  
  648.                 log << "  Evento: il codice si � ammalato di Infezione UV ai fotoricettori\n";
  649.                 log.close();
  650.             }
  651.             else
  652.             {
  653.                 cout << "Il codice si e' ammalato di una malattia sconosciuta..." << endl;
  654.                 t_variante = rand() % 8;
  655.                 ofstream log;
  656.                 log.open(nofilo, ios::app);
  657.                 log << "  ";
  658.                 log << " Evento: il codice si � ammalato di una malattia sconosciuta\n";
  659.                 log.close();
  660.             };
  661.             sleep(5);
  662.             srand((unsigned)time(NULL));
  663.             bool difesa = false;
  664.             bool pre_difesa = false;
  665.             int v = 0;
  666.             int i;
  667.             for (i = 0; i < 5; i++)
  668.             {
  669.                 v++;
  670.                 if (anticorpo[i][v]!=0)
  671.                 {
  672.                     if ((i == t_malattia) && (v == t_variante))
  673.                     {
  674.                         difesa = true;
  675.                     }
  676.                     if (i == t_malattia)
  677.                     {
  678.                         pre_difesa = true;
  679.                     }
  680.                 }
  681.             }
  682.             if (difesa == true)
  683.             {
  684.                 guarigione = 0;
  685.             }
  686.             else
  687.             {
  688.                 guarigione = rand() % 2;
  689.                 if (pre_difesa == true)
  690.                 {
  691.                     if (guarigione != 0)
  692.                     {
  693.                         guarigione = rand() % 2;
  694.                     }
  695.                 }
  696.             }
  697.             if (guarigione == 0)
  698.             {
  699.  
  700.                 anticorpo[i][v] = 1;
  701.                 ofstream log;
  702.                 log.open(nofilo, ios::app);
  703.  
  704.                 log << "  ";
  705.  
  706.                 if (difesa == true)
  707.                 {
  708.                     log << "  malattia superata, il codice � guarito grazie agli anticorpi\n";
  709.                 }
  710.                 else
  711.                 {
  712.                     log << "  malattia superata, il codice � guarito\n";
  713.                 }
  714.                 log.close();
  715.                 sleep(2);
  716.             }
  717.             else
  718.             {
  719.                 cout << "il codice e' morto per la grave malattia..." << endl;
  720.                 cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  721.                 ofstream log;
  722.                 log.open(nofilo, ios::app);
  723.  
  724.                 log << "  ";
  725.  
  726.                 log << "  malattia mortale: il codice � morto.\n";
  727.                 log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  728.                 log.close();
  729.                 death.open("/tmp/death.txt");
  730.                 death << nomefile;
  731.                 death.close();
  732.                 sleep(2);
  733.  
  734.                 exit(0);
  735.             };
  736.         };
  737.         if (sesso == 0)
  738.         {
  739.             if (predatore == true)
  740.             {
  741.                 cout << "Sistema di caccia attivo e funzionante, ciclo numero " << quanto << " temperatura " << temperatura << " gradi." << endl;
  742.                 sleep(1);
  743.             }
  744.             else
  745.             {
  746.                 alimentazione << "Cibo \n";
  747.                 cout << "Sistema produttivo attivo e funzionante, ciclo numero " << quanto << " temperatura " << temperatura << " gradi." << endl;
  748.                 sleep(1);
  749.             }
  750.         }
  751.         else
  752.         {
  753.             if (predatore == true)
  754.             {
  755.                 cout << "Sistema di caccia attivo e funzionante, ciclo numero " << quanto << " temperatura " << temperatura << " gradi." << endl;
  756.                 sleep(1);
  757.             }
  758.             else
  759.             {
  760.                 alimentazione << "Cibo\n";
  761.                 cout << "Sistema alimentare attivo e funzionante, ciclo numero " << quanto<< " temperatura " << temperatura << " gradi." << endl;
  762.                 sleep(1);
  763.             }
  764.         };
  765.         while (!alimentazione)
  766.         {
  767.             alim++;
  768.             if (alim==3)
  769.             {
  770.                 cout << "Il codice e' morto di fame in quanto non � riuscito a procurarsi il cibo..." << endl;
  771.                 cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  772.                 ofstream log;
  773.                 log.open(nofilo, ios::app);
  774.  
  775.                 log << "  ";
  776.  
  777.                 log << "  Evento: il codice � morto per assenza di cibo!\n";
  778.                 log << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  779.                 log.close();
  780.                 death.open("/tmp/death.txt");
  781.                 death << nomefile;
  782.                 death.close();
  783.                 sleep(2);
  784.  
  785.                 exit(0);
  786.             }
  787.             else
  788.             {
  789.                 cout << "Il codice non ha trovato cibo per nutrirsi, ed inizia ad indebolirsi..." << endl;
  790.                 ofstream log;
  791.                 log.open(nofilo, ios::app);
  792.  
  793.                 log << "  ";
  794.  
  795.                 log << "  Evento: il cibo scarseggia, il codice si indebolisce\n";
  796.                 log.close();
  797.                 stamina = stamina - 1;
  798.             }
  799.         }
  800.         vita = vita - 1;
  801.         stamina = stamina - 1;
  802.         if (vita == 0)
  803.         {
  804.             cout << "Il codice e' morto di vecchiaia... " << endl;
  805.             cout << "Il codice e' morto in data: " << asctime(timeinfo) << endl;
  806.             ofstream log;
  807.             log.open(nofilo, ios::app);
  808.  
  809.             log << "  ";
  810.  
  811.             log << "  Evento: il codice ha finito il suo ciclo vitale, ed ora muore in pace :)\n";
  812.             log << " Codice morto in data: " << asctime(timeinfo) << endl;
  813.             log.close();
  814.             death.open("/tmp/death.txt");
  815.             death << nomefile;
  816.             death.close();
  817.             sleep(2);
  818.  
  819.             exit(0);
  820.         };
  821.         if (stamina == 0)
  822.         {
  823.             srand((unsigned)time(NULL));
  824.             cout << "Il codice e' stanco e deve riposare..." << endl;
  825.             ofstream log;
  826.             log.open(nofilo, ios::app);
  827.  
  828.             log << "  ";
  829.  
  830.             log << "  Evento: il codice � stanco e si ferma a riposare...\n";
  831.             log.close();
  832.             sleep(5);
  833.             stamina = rand () % astm + 50;
  834.         };
  835.         if (predatore == true)
  836.         {
  837.             if (quanto == 40)
  838.             {
  839.                 cout << "Il codice e' affamato e tenta di assalire delle uova..." << endl;
  840.                 ofstream log;
  841.                 log.open(nofilo, ios::app);
  842.  
  843.                 log << "  ";
  844.  
  845.                 log << "  Evento Predatore: Il predatore cerca di assalire le uova di un predatore normale\n";
  846.                 log.close();
  847.                 sleep(2);
  848.                 ifstream udc;
  849.                 int cnt;
  850.                 bool avmvero, ammvero, astmvero;
  851.                 udc.open("/tmp/avminfo.txt", ios::app);
  852.                 if (udc.is_open())
  853.                 {
  854.                     avmvero = true;
  855.                 }
  856.                 else
  857.                 {
  858.                     avmvero = false;
  859.                 }
  860.                 udc.close();
  861.  
  862.                 if (avmvero==true)
  863.                 {
  864.                     system("rm /tmp/avminfo.txt");
  865.                     system("rm /tmp/amminfo.txt");
  866.                     system("rm /tmp/astminfo.txt");
  867.                 }
  868.                 else
  869.                 {
  870.                     cnt++;
  871.                     if (cnt==3)
  872.                     {
  873.                         cout << "Il codice � morto di fame, non avendo trovato pi� uova da mangiare" << endl;
  874.                         ofstream log;
  875.                         log.open(nofilo, ios::app);
  876.  
  877.                         log << "  ";
  878.                         log << "  Evento Predatore: non avendo trovado uova per molto tempo, il predatore � morto di fame\n";
  879.                         log.close();
  880.                         death.open("/tmp/death.txt");
  881.                         death << nomefile;
  882.                         death.close();
  883.                         sleep(2);
  884.  
  885.                         exit(0);
  886.  
  887.  
  888.                     }
  889.                     else
  890.                     {
  891.                         cout << "Il codice non ha trovato uova e digiuna, indebolendosi" <<endl;
  892.                         ofstream log;
  893.                         log.open(nofilo, ios::app);
  894.  
  895.                         log << "  ";
  896.  
  897.                         log << "  Evento Predatore: Le uova scarseggiano, il digiuno indebolisce il predatore\n";
  898.                         log.close();
  899.                         stamina = stamina -1;
  900.                         sleep(2);
  901.                     }
  902.                 }
  903.  
  904.             };
  905.             if (quanto > mature)
  906.             {
  907.                 quanto = 0;
  908.                 cout << "Il codice ha cacciato abbastanza per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
  909.                 ofstream log;
  910.                 log.open(nofilo, ios::app);
  911.  
  912.                 log << "  ";
  913.  
  914.                 log << "  Evento: Passaggio alla fase riproduttiva\n";
  915.                 log.close();
  916.                 if (sesso == 0)
  917.                 {
  918.                     cout << "Il codice ha prodotto abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
  919.                     ofstream log;
  920.                     log.open(nofilo, ios::app);
  921.  
  922.                     log << "  ";
  923.  
  924.                     log << "  Evento: Passaggio alla fase riproduttiva\n";
  925.                     log.close();
  926.                     cout << "Il codice sta per produrre le uova di un figlio. " << endl;
  927.                     sleep(2);
  928.                     riproduzione();
  929.                 }
  930.                 else
  931.                 {
  932.                     cout << "Il codice si e' alimentato abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
  933.                     ofstream log;
  934.                     log.open(nofilo, ios::app);
  935.  
  936.                     log << "  ";
  937.  
  938.                     log << "  Evento: Passaggio alla fase riproduttiva\n";
  939.                     log.close();
  940.                     cout << "Se il programma trovera' le uova fara' nascere il figlio..." << endl;
  941.                     sleep(2);
  942.                     riproduzione();
  943.                 };
  944.             };
  945.         }
  946.         else
  947.         {
  948.             if (quanto > mature)
  949.             {
  950.                 quanto = 0;
  951.                 if (sesso == 0)
  952.                 {
  953.                     cout << "Il codice ha prodotto abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
  954.                     ofstream log;
  955.                     log.open(nofilo, ios::app);
  956.  
  957.                     log << "  ";
  958.  
  959.                     log << "  Evento: Passaggio alla fase riproduttiva\n";
  960.                     log.close();
  961.                     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;
  962.                     sleep(2);
  963.                     riproduzione();
  964.                 }
  965.                 else
  966.                 {
  967.                     cout << "Il codice si e' alimentato abbastanza, per ora." << endl << "Passaggio alla fase riproduttiva..." << endl;
  968.                     ofstream log;
  969.                     log.open(nofilo, ios::app);
  970.  
  971.                     log << "  ";
  972.  
  973.                     log << "  Evento: Passaggio alla fase riproduttiva\n";
  974.                     log.close();
  975.                     cout << "Se il programma trovera' le uova fara' nascere il figlio..." << endl;
  976.                     sleep(2);
  977.                     riproduzione();
  978.                 };
  979.             };
  980.         };
  981.     }
  982. }
  983. void riproduzione()
  984. {
  985.     char* comfin;
  986.     int newavm, newamm, newastm, newatm;
  987.     int complicazioni;
  988.     char ch[10];
  989.     srand((unsigned)time(NULL));
  990.     complicazioni = rand() % 15;
  991.     if (complicazioni == 10)
  992.     {
  993.         cout << endl;
  994.         cout << "ci sono state complicazioni nella riproduzione..." << endl;
  995.         cout << "Il figlio e' morto..." << endl;
  996.         ofstream log;
  997.         log.open(nofilo, ios::app);
  998.  
  999.         log << "  ";
  1000.  
  1001.         log << "  Riproduzione: Complicazioni riproduttive, il figlio � morto\n";
  1002.         log.close();
  1003.         sleep(2);
  1004.         sopravvivi();
  1005.     }
  1006.     else
  1007.     {
  1008.         if (sesso == 0)
  1009.         {
  1010.             if (predatore == true)
  1011.             {
  1012.                 ofstream fp;
  1013.                 fp.open("/tmp/avpinfo.txt");
  1014.                 fp << avm;
  1015.                 fp.close();
  1016.                 fp.open("/tmp/ampinfo.txt");
  1017.                 fp << amm;
  1018.                 fp.close();
  1019.                 fp.open("/tmp/astpinfo.txt");
  1020.                 fp << astm;
  1021.                 fp.close();
  1022.                 fp.open("/tmp/atmpinfo.txt");
  1023.                 fp << atm;
  1024.                 fp.close();
  1025.                 while (!fp)
  1026.                 {
  1027.                     cout << "Il codice non � riuscito a depositare le uova..." << endl;
  1028.                     ofstream log;
  1029.                     log.open(nofilo, ios::app);
  1030.  
  1031.                     log << "  ";
  1032.  
  1033.                     log << "  Riproduzione: Uova non deposte\n";
  1034.                     log.close();
  1035.                     sleep(2);
  1036.                     sopravvivi();
  1037.                 }
  1038.             }
  1039.             else
  1040.             {
  1041.                 ofstream fd;
  1042.                 fd.open("/tmp/avminfo.txt");
  1043.                 fd << avm;
  1044.                 fd.close();
  1045.                 fd.open("/tmp/amminfo.txt");
  1046.                 fd << amm;
  1047.                 fd.close();
  1048.                 fd.open("/tmp/astminfo.txt");
  1049.                 fd << astm;
  1050.                 fd.close();
  1051.                 fd.open("/tmp/atminfo.txt");
  1052.                 fd << atm;
  1053.                 fd.close();
  1054.                 while (!fd)
  1055.                 {
  1056.                     cout << "Il codice non � riuscito a depositare le uova..." << endl;
  1057.                     ofstream log;
  1058.                     log.open(nofilo, ios::app);
  1059.  
  1060.                     log << "  ";
  1061.  
  1062.                     log << "  Riproduzione: Uova non deposte\n";
  1063.                     log.close();
  1064.                     sleep(2);
  1065.                     sopravvivi();
  1066.                 }
  1067.                 cout << "Dopo aver deposto le uova il programma deve riposarsi..." << endl;
  1068.                 ofstream log;
  1069.                 log.open(nofilo, ios::app);
  1070.  
  1071.                 log << "  ";
  1072.  
  1073.                 log << "  Riproduzione: Uova deposte, riposo\n";
  1074.                 log.close();
  1075.                 sleep(3);
  1076.                 sopravvivi();
  1077.             };
  1078.         }
  1079.         else
  1080.         {
  1081.             int i = rand() % 301;
  1082.             int* nnrand;
  1083.             sprintf(ch,"%d",i);
  1084.             ifstream elabavm;
  1085.             ifstream elabamm;
  1086.             ifstream elabastm;
  1087.             ifstream elabatm;
  1088.             ofstream save;
  1089.             if (predatore == false)
  1090.             {
  1091.                 elabavm.open("/tmp/avminfo.txt");
  1092.                 elabavm >> newavm;
  1093.                 elabavm.close();
  1094.             }
  1095.             else
  1096.             {
  1097.                 elabavm.open("/tmp/avpinfo.txt");
  1098.                 elabavm >> newavm;
  1099.                 elabavm.close();
  1100.             };
  1101.             if (!elabavm)
  1102.             {
  1103.                 cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
  1104.                 ofstream log;
  1105.                 log.open(nofilo, ios::app);
  1106.  
  1107.                 log << "  ";
  1108.  
  1109.                 log << "  Riproduzione: Uova non trovate, riproduzione annullata\n";
  1110.                 log.close();
  1111.                 sleep(2);
  1112.                 sopravvivi();
  1113.             };
  1114.             if (predatore == false)
  1115.             {
  1116.                 elabamm.open("/tmp/avminfo.txt");
  1117.                 elabamm >> newamm;
  1118.                 elabamm.close();
  1119.             }
  1120.             else
  1121.             {
  1122.                 elabamm.open("/tmp/avpinfo.txt");
  1123.                 elabamm >> newamm;
  1124.                 elabamm.close();
  1125.             };
  1126.             if (!elabamm)
  1127.             {
  1128.                 cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
  1129.                 ofstream log;
  1130.                 log.open(nofilo, ios::app);
  1131.  
  1132.                 log << "  ";
  1133.  
  1134.                 log << "  Riproduzione: Uova non trovate, riproduzione annullata\n";
  1135.                 log.close();
  1136.                 sleep(2);
  1137.                 sopravvivi();
  1138.             };
  1139.             if (predatore == false)
  1140.             {
  1141.                 elabavm.open("/tmp/avminfo.txt");
  1142.                 elabavm >> newastm;
  1143.                 elabavm.close();
  1144.             }
  1145.             else
  1146.             {
  1147.                 elabastm.open("/tmp/avpinfo.txt");
  1148.                 elabastm >> newastm;
  1149.                 elabastm.close();
  1150.             };
  1151.             if (!elabastm)
  1152.             {
  1153.                 cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
  1154.                 ofstream log;
  1155.                 log.open(nofilo, ios::app);
  1156.                 log << "  ";
  1157.  
  1158.                 log << "  Riproduzione: Uova non trovate, riproduzione annullata\n";
  1159.                 log.close();
  1160.                 sleep(2);
  1161.                 sopravvivi();
  1162.             };
  1163.             if (predatore == false)
  1164.             {
  1165.                 elabatm.open("/tmp/atminfo.txt");
  1166.                 elabatm >> newatm;
  1167.                 elabatm.close();
  1168.             }
  1169.             else
  1170.             {
  1171.                 elabastm.open("/tmp/atminfo.txt");
  1172.                 elabatm >> newatm;
  1173.                 elabatm.close();
  1174.             };
  1175.             if (!elabatm)
  1176.             {
  1177.                 cout << "Non sono state trovate le uova... riproduzione annullata." << endl;
  1178.                 ofstream log;
  1179.                 log.open(nofilo, ios::app);
  1180.  
  1181.                 log << "  ";
  1182.  
  1183.                 log << "  Riproduzione: Uova non trovate, riproduzione annullata\n";
  1184.                 log.close();
  1185.                 sleep(2);
  1186.                 sopravvivi();
  1187.             };
  1188.             if (newamm < amm)
  1189.             {
  1190.                 nnrand = new int;
  1191.                 *nnrand = rand() % 15 + 10;
  1192.                 newamm = ((newamm / amm * amm) + (rand() % *nnrand));
  1193.                 delete nnrand;
  1194.             }
  1195.             else
  1196.             {
  1197.                 nnrand = new int;
  1198.                 *nnrand = rand() % 15 + 10;
  1199.                 newamm = ((newamm / amm * amm) - (rand() % *nnrand));
  1200.                 delete nnrand;
  1201.             };
  1202.             if (newavm < avm)
  1203.             {
  1204.                 nnrand = new int;
  1205.                 *nnrand = rand() % 50 + 100;
  1206.                 newavm = ((newavm / avm * avm) + (rand() % *nnrand));
  1207.                 delete nnrand;
  1208.             }
  1209.             else
  1210.             {
  1211.                 nnrand = new int;
  1212.                 *nnrand = rand() % 50 + 100;
  1213.                 newavm = ((newavm / avm * avm) - (rand() % *nnrand));
  1214.                 delete nnrand;
  1215.             };
  1216.             if (newastm < astm)
  1217.             {
  1218.                 nnrand = new int;
  1219.                 *nnrand = rand() % 15 + 30;
  1220.                 newastm = ((newastm / astm * astm) + (rand() % *nnrand));
  1221.                 delete nnrand;
  1222.             }
  1223.             else
  1224.             {
  1225.                 nnrand = new int;
  1226.                 *nnrand = rand() % 15 + 30;
  1227.                 newastm = ((newastm / astm * astm) - (rand() % *nnrand));
  1228.                 delete nnrand;
  1229.             };
  1230.             if (newatm < atm)
  1231.             {
  1232.                 nnrand = new int;
  1233.                 *nnrand = rand() % 10 + 5;
  1234.                 newastm = ((newatm / atm * atm) + (rand() % *nnrand));
  1235.                 delete nnrand;
  1236.             }
  1237.             else
  1238.             {
  1239.                 nnrand = new int;
  1240.                 *nnrand = rand() % 10 + 5;
  1241.                 newatm = ((newatm / atm * atm) - (rand() % *nnrand));
  1242.                 delete nnrand;
  1243.             };
  1244.             if (predatore == true)
  1245.             {
  1246.                 save.open("/tmp/atmpinfo.txt", ios::out);
  1247.                 save << newavm;
  1248.                 save.close();
  1249.                 save.open("/tmp/atmpinfo.txt", ios::out);
  1250.                 save << newamm;
  1251.                 save.close();
  1252.                 save.open("/tmp/atmpinfo.txt", ios::out);
  1253.                 save << newastm;
  1254.                 save.close();
  1255.                 save.open("/tmp/atmpinfo.txt", ios::out);
  1256.                 save << newastm;
  1257.                 save.close();
  1258.                 while (!save)
  1259.                 {
  1260.                     cout << "Errore nella fecondazione del gamete..." << endl;
  1261.                     ofstream log;
  1262.                     log.open(nofilo, ios::app);
  1263.  
  1264.                     log << "  ";
  1265.  
  1266.                     log << "  Riproduzione: Gamete non fecondato correttamente\n";
  1267.                     log.close();
  1268.                     sleep(2);
  1269.                     sopravvivi();
  1270.                     break;
  1271.                 }
  1272.             }
  1273.             else
  1274.             {
  1275.                 save.open("/tmp/atminfo.txt", ios::out);
  1276.                 save << newavm;
  1277.                 save.close();
  1278.                 save.open("/tmp/atminfo.txt", ios::out);
  1279.                 save << newamm;
  1280.                 save.close();
  1281.                 save.open("/tmp/atminfo.txt", ios::out);
  1282.                 save << newastm;
  1283.                 save.close();
  1284.                 save.open("/tmp/atminfo.txt", ios::out);
  1285.                 save << newastm;
  1286.                 save.close();
  1287.                 while (!save)
  1288.                 {
  1289.                     cout << "Errore nella fecondazione del gamete..." << endl;
  1290.                     ofstream log;
  1291.                     log.open(nofilo, ios::app);
  1292.  
  1293.                     log << "  ";
  1294.  
  1295.                     log << "  Riproduzione: Gamete non fecondato correttamente\n";
  1296.                     log.close();
  1297.                     sleep(2);
  1298.                     sopravvivi();
  1299.                     break;
  1300.                 }
  1301.             };
  1302.  
  1303.             char* nomefiglio;
  1304.  
  1305.             comfin = (char *) malloc(sizeof(char) * (strlen("cp ") + strlen("\"") + strlen(nomefile) + strlen("\"")+ strlen(" figlio") + strlen(ch)  + 3));
  1306.             sprintf(comfin, "%s%s%s%s%s%s", "cp ", "\"", nomefile, "\"", " figlio", ch);
  1307.             FILE* out;
  1308.             out = popen(comfin, "w");
  1309.             pclose(out);
  1310.             nomefiglio = (char *) malloc(sizeof(char) * (strlen("xterm -e ./figlio") + strlen(ch)  + 2));
  1311.             sprintf(nomefiglio, "%s%s", "xterm -e ./figlio", ch);
  1312.             out = popen(nomefiglio, "w");
  1313.             pclose(out);
  1314.             cout << "Dopo aver partorito il programma deve riposarsi..." << endl;
  1315.             ofstream log;
  1316.             log.open(nofilo, ios::app);
  1317.  
  1318.             log << "  ";
  1319.  
  1320.  
  1321.             log << "  Riproduzione: Il codice ha partorito, riposo post parto ^_^\n";
  1322.             log.close();
  1323.             free(comfin);
  1324.             free(nomefiglio);
  1325.             free(nofilo);
  1326.             sleep(3);
  1327.             sopravvivi();
  1328.         };
  1329.     };
  1330. }
Advertisement
Add Comment
Please, Sign In to add comment