Guest User

Untitled

a guest
Mar 7th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 47.78 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <string>
  5. #include <fstream>
  6. #include <filesystem>
  7. #include <typeinfo>
  8. #include <iostream>
  9. #include <cstring>
  10. #include <random>
  11. #include <cstdio>
  12. #include <unistd.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <errno.h>
  16. #include <sys/wait.h>
  17. #include <cctype>
  18. #include <fcntl.h>
  19. #include <arpa/inet.h>
  20. #include <sys/types.h>
  21. #include <signal.h>
  22. #include <netinet/tcp.h>
  23.  
  24. using namespace std;
  25.  
  26.  
  27. struct coord
  28. {
  29.  
  30. int x;
  31. int y;
  32.  
  33. };
  34.  
  35. struct diplomatic
  36. {
  37. int n; //unconditioned reference diplomatics on game ( gdiplo )
  38. int ability;
  39. int zone; //start from 0
  40. int treaties;
  41. struct diplomatic *next;
  42. struct diplomatic *prev;
  43. };
  44.  
  45. struct diplomatic *dipl, *dipl2, *dipl3;
  46. struct recruitment
  47. {
  48. int duration; // 4 seasons/turns
  49. int type; // diplomatic 1, primitive 2
  50. struct recruitment *next;
  51. struct recruitment *prev;
  52. };
  53.  
  54. struct recruitment *recl_a, *recl_s, *recl_b, *recl_c;
  55.  
  56. struct event
  57. {
  58.  
  59. string descr;
  60. int type;
  61. int type2;
  62. int n;
  63. int ndiplo;
  64. int duration;
  65. int destination;
  66. struct event *next;
  67. struct event *prev;
  68.  
  69. };
  70.  
  71. struct event *ev_att, *ev_att2, *ev_att3, *ev_att4;
  72.  
  73. struct treaties
  74. {
  75.  
  76. string *agreements[23];
  77. int done[60][22];
  78.  
  79. };
  80.  
  81. struct neighbours
  82. {
  83.  
  84. int *neighbourhood;
  85. short int dim;
  86.  
  87. };
  88.  
  89. struct building
  90. {
  91.  
  92. int time_building;
  93. int x;
  94. int y;
  95. bool in_progress;
  96.  
  97. };
  98.  
  99. struct army
  100. {
  101.  
  102. int primitives;
  103. int lancers;
  104.  
  105. };
  106.  
  107. struct areas
  108. {
  109.  
  110. int nzone; // start from 0
  111. int type; // 1 aggressive, 2 neutral, 3 civil
  112. int *relationship; // it have to be equal to the territories number, maximum 60
  113. int ntreaties; // treaties reserved
  114. int residents;
  115. struct coord pos;
  116. struct recruitment *recl;
  117. struct diplomatic *dipl_z;
  118. struct event *ev;
  119. struct event *ev_inprogress;
  120. struct treaties *t; // 0 if null
  121. struct building *buildings;
  122.  
  123. /* civils characteristics */
  124.  
  125.  
  126. /* neutrals characteristics */
  127.  
  128.  
  129. /* agrressive characteristics */
  130.  
  131. struct army e;
  132.  
  133. };
  134.  
  135. struct areas *player, *territories;
  136. string str, str2, str3 /* only for diplay information rising village */, str4 /* only for loading */, str5 /* only for events */, pstr[22], str6;
  137. struct neighbours *atlas_neighbourhood;
  138. struct army *es;
  139.  
  140. int main()
  141. {
  142. char *message, *m, *s;
  143. int fd_start, opt, spid = 0, n, status, pid, labour = 0, zone, n_socket, i, i2, i3, i4, i5, nmess, l;
  144. short int narr = 0, proc = 0;
  145. bool calc = 0, esc = 0;
  146. struct sockaddr_in gate, gate2;
  147. struct sigaction n_act, v_act;
  148.  
  149. random_device rd;
  150. mt19937 producer( rd() ), producer2 ( rd() );
  151. uniform_int_distribution<int> arr( 1,60 ), arr2( 1,3 );
  152.  
  153. territories = (struct areas *) malloc( sizeof(struct areas) * 60 );
  154.  
  155. player = &territories[7];
  156.  
  157. /* I initialize areas of game */
  158. for( i = 0; i < 60; i++ )
  159. {
  160.  
  161. territories[i].nzone = i;
  162. territories[i].type = arr2(producer);
  163. territories[i].ntreaties = 0;
  164. territories[i].relationship= (int *) malloc( sizeof(int) * 60 );
  165.  
  166. if(territories[i].relationship == NULL)
  167. exit(0);
  168.  
  169. territories[i].ev = NULL;
  170. territories[i].ev_inprogress = NULL;
  171. territories[i].recl = NULL;
  172. territories[i].dipl_z = NULL;
  173. territories[i].buildings = NULL;
  174. territories[i].residents = 100;
  175.  
  176. /* civil characteristics */
  177.  
  178.  
  179. /* neutral characteristics */
  180.  
  181.  
  182. /* aggressive characteristics */
  183.  
  184. if(territories[i].type == 1)
  185. {
  186. territories[i].e.primitives = 0;
  187. territories[i].e.lancers = 0;
  188. }
  189.  
  190. territories[i].t = NULL;
  191.  
  192.  
  193. }
  194.  
  195. fd_start = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  196.  
  197. if( fd_start == -1 )
  198. exit(0);
  199.  
  200. memset(&gate, 0, sizeof(gate) );
  201. gate.sin_family = AF_INET;
  202. gate.sin_addr.s_addr = inet_addr("127.0.0.1");
  203. gate.sin_port = htons(10000);
  204.  
  205. opt = 1;
  206. setsockopt(fd_start, SOL_SOCKET, SO_REUSEADDR | SO_KEEPALIVE | SO_REUSEPORT, &opt, sizeof(opt) );
  207.  
  208. fcntl(fd_start, F_SETFL, O_NONBLOCK);
  209.  
  210. sigemptyset(&n_act.sa_mask);
  211. n_act.sa_flags = 0;
  212. sigaction(SIGPIPE, &n_act, &v_act);
  213.  
  214.  
  215. n = bind(fd_start, (struct sockaddr *)&gate, sizeof(gate) );
  216.  
  217. if( n == -1 )
  218. exit(0);
  219.  
  220. listen(fd_start, 60);
  221.  
  222. nmess = 29999;
  223. int nmess2 = 59999;
  224.  
  225. message = (char *)malloc( sizeof(char) * 60000 );
  226.  
  227. if(message == NULL)
  228. exit(0);
  229.  
  230. while(i < nmess2 )
  231. {
  232.  
  233. message[i] = '#';
  234. i++;
  235.  
  236. }
  237.  
  238. spid = getpid();
  239.  
  240. i = 0;
  241. i2 = 0;
  242.  
  243. while(i < 58)
  244. {
  245.  
  246. if(i == player->nzone)
  247. {
  248.  
  249. i++;
  250. continue;
  251.  
  252. }
  253.  
  254.  
  255. pid = fork();
  256.  
  257. // called processes
  258. if(getpid() != spid)
  259. {
  260.  
  261. int fd_arrivals, n, labour, opt;
  262. struct sockaddr_in gate;
  263. char pr[7] = "readyy", *message, *m, *s;
  264.  
  265. message = (char *)malloc( sizeof(char) * 30000 );
  266.  
  267. if(message == NULL)
  268. exit(0);
  269.  
  270. while(i < nmess )
  271. {
  272.  
  273. message[i] = '#';
  274. i++;
  275.  
  276. }
  277.  
  278. s = (char *) malloc( sizeof(char) * 20 );
  279.  
  280. if(s == NULL)
  281. exit(0);
  282.  
  283. fd_arrivals = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  284.  
  285. if(fd_arrivals == -1)
  286. exit(0);
  287.  
  288. memset(&gate, 0, sizeof(gate) );
  289. gate.sin_family = AF_INET;
  290. gate.sin_addr.s_addr = inet_addr("127.0.0.1");
  291. gate.sin_port = htons(10000);
  292.  
  293. //fcntl(fd_arrivals, F_SETFL, O_NONBLOCK);
  294.  
  295. opt = 1;
  296. int dim = 60000;
  297.  
  298. setsockopt(fd_arrivals, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT | SO_KEEPALIVE, &opt, sizeof(opt));
  299. setsockopt(fd_arrivals, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(dim));
  300.  
  301.  
  302. sigaction(SIGPIPE, &v_act, NULL);
  303.  
  304. n = connect(fd_arrivals, (struct sockaddr *)&gate, sizeof(gate) );
  305.  
  306. sigaction(SIGPIPE, &v_act, NULL);
  307. n = send(fd_arrivals, pr, sizeof(char) * 7, MSG_NOSIGNAL );
  308.  
  309. m = (char *) malloc( sizeof(char) * 20 );
  310.  
  311. if(m == NULL)
  312. exit(0);
  313.  
  314. n = 0;
  315. // Receive job from server
  316.  
  317. sigaction(SIGPIPE, &v_act, NULL);
  318.  
  319. while(1)
  320. {
  321.  
  322. n = recv(fd_arrivals, m, ( sizeof(char) * 20 ) - 1, MSG_NOSIGNAL );
  323.  
  324. if( n == 19 && atoi(m) > 0 )
  325. {
  326. labour = atoi(m);
  327. n = labour;
  328. i = labour;
  329. n++;
  330. i2 = 0;
  331. n = 0;
  332.  
  333. memset(message, '#', nmess );
  334.  
  335. while(i2 < 20)
  336. {
  337.  
  338. m[i2] = '#';
  339. i2++;
  340.  
  341. }
  342.  
  343. // Parallel processing
  344.  
  345. for(; i < n; i++)
  346. {
  347.  
  348. if(territories[i].nzone == player->nzone)
  349. goto fine;
  350.  
  351. if(territories[i].type == 1)
  352. {
  353.  
  354. // Primitives recruitment
  355.  
  356. if(territories[i].recl == NULL)
  357. {
  358.  
  359. territories[i].recl = (struct recruitment *) malloc( sizeof(struct recruitment) );
  360.  
  361. if(territories[i].recl == NULL)
  362. exit(0);
  363.  
  364. territories[i].recl->type = 2;
  365. territories[i].recl->duration = 2;
  366. territories[i].recl->next = NULL;
  367. territories[i].recl->prev = NULL;
  368.  
  369. continue;
  370.  
  371. }
  372.  
  373. // Continue recruitment
  374.  
  375. if(territories[i].recl != NULL )
  376. {
  377.  
  378. recl_a = territories[i].recl;
  379.  
  380.  
  381. while( recl_a != NULL )
  382. {
  383.  
  384. if(recl_a->duration > 0)
  385. recl_a->duration = recl_a->duration - 1;
  386.  
  387. if(recl_a->duration == 0)
  388. {
  389.  
  390. recl_b = recl_a->prev;
  391. recl_c = recl_a->next;
  392.  
  393. territories[i].e.primitives = territories[i].e.primitives + 1;
  394.  
  395. if( recl_b != NULL && recl_c != NULL )
  396. recl_c->prev = NULL;
  397.  
  398. if(recl_c != NULL && recl_b != NULL)
  399. recl_b->next = NULL;
  400.  
  401. if(recl_c != NULL && recl_b != NULL)
  402. {
  403.  
  404. recl_b->next = recl_c;
  405. recl_c->prev = recl_b;
  406.  
  407. free(recl_a);
  408. recl_a = recl_b;
  409.  
  410. }
  411.  
  412. }
  413.  
  414. recl_a = recl_a->next;
  415.  
  416. }
  417.  
  418. continue;
  419.  
  420. }
  421.  
  422. if(territories[i].recl != NULL && territories[i].recl->next == NULL )
  423. {
  424.  
  425. recl_a = territories[i].recl;
  426.  
  427. recl_b = (struct recruitment *) malloc( sizeof(struct recruitment) );
  428.  
  429. if(recl_b == NULL)
  430. exit(0);
  431.  
  432. recl_b->prev = recl_a;
  433. recl_a->next = recl_b;
  434. recl_b->next = NULL;
  435.  
  436. recl_b->type = 2;
  437. recl_b->duration = 2;
  438.  
  439. }
  440.  
  441. }
  442.  
  443. }
  444.  
  445. // saving territory
  446.  
  447. i = 0;
  448. sprintf( &message[i], "%d", territories[labour].nzone);
  449.  
  450. while(message[i] != '#')
  451. i++;
  452.  
  453. message[i] = ' ';
  454. i++;
  455.  
  456. sprintf(&message[i], "%d", territories[labour].type);
  457.  
  458. while(message[i] != '#')
  459. i++;
  460.  
  461. message[i] = ' ';
  462. i++;
  463.  
  464. if(territories[labour].relationship == NULL)
  465. {
  466.  
  467. message[i] = '-';
  468. i++;
  469.  
  470. }
  471. else
  472. {
  473.  
  474. i2 = 0;
  475.  
  476. while(i2 < 60)
  477. {
  478.  
  479. sprintf(&message[i], "%d", territories[labour].relationship[i2]);
  480.  
  481. while(message[i] != '#')
  482. i++;
  483.  
  484. message[i] = ' ';
  485. i++;
  486.  
  487. i2++;
  488.  
  489. }
  490.  
  491. message[i] = '#';
  492. i++;
  493.  
  494. }
  495.  
  496. sprintf(&message[i], "%d", territories[labour].ntreaties);
  497.  
  498. while(message[i] != '#')
  499. i++;
  500.  
  501. message[i] = ' ';
  502. i++;
  503.  
  504. sprintf(&message[i], "%d", territories[labour].residents);
  505.  
  506. while(message[i] != '#')
  507. i++;
  508.  
  509. message[i] = ' ';
  510. i++;
  511.  
  512.  
  513. if(territories[labour].recl == NULL)
  514. {
  515.  
  516. message[i] = '-';
  517. i++;
  518.  
  519. }
  520. else
  521. {
  522.  
  523. recl_a = territories[labour].recl;
  524.  
  525. while(recl_a != NULL)
  526. {
  527.  
  528. sprintf(&message[i], "%d", recl_a->duration);
  529.  
  530. while(message[i] != '#')
  531. i++;
  532.  
  533. message[i] = ' ';
  534. i++;
  535.  
  536. sprintf(&message[i], "%d", recl_a->type);
  537.  
  538. while(message[i] != '#')
  539. i++;
  540.  
  541. message[i] = ' ';
  542. i++;
  543.  
  544. recl_a = recl_a->next;
  545.  
  546. }
  547.  
  548. message[i] = '-';
  549. i++;
  550.  
  551. }
  552.  
  553. if(territories[labour].dipl_z == NULL)
  554. {
  555.  
  556. message[i] = '-';
  557. i++;
  558.  
  559. }
  560. else
  561. {
  562.  
  563. dipl = territories[labour].dipl_z;
  564.  
  565. while(dipl != NULL)
  566. {
  567.  
  568. sprintf(&message[i], "%d", dipl->n);
  569.  
  570. while(message[i] != '#')
  571. i++;
  572.  
  573. sprintf(&message[i], "%d", dipl->ability);
  574.  
  575. while(message[i] != '#')
  576. i++;
  577.  
  578. sprintf(&message[i], "%d", dipl->zone);
  579.  
  580. while(message[i] != '#')
  581. i++;
  582.  
  583. sprintf(&message[i], "%d", dipl->zone);
  584.  
  585. while(message[i] != '#')
  586. i++;
  587.  
  588. dipl = dipl->next;
  589.  
  590. }
  591.  
  592. message[i] = '-';
  593. i++;
  594.  
  595. }
  596.  
  597. if(territories[labour].ev == NULL)
  598. {
  599.  
  600. message[i] = '-';
  601. i++;
  602.  
  603. }
  604. else
  605. {
  606.  
  607. ev_att = territories[labour].ev;
  608.  
  609. while(ev_att != NULL)
  610. {
  611.  
  612. s = strdup(ev_att->descr.c_str() );
  613.  
  614. i2 = 0;
  615.  
  616. while(i2 < 20)
  617. {
  618.  
  619. message[i] = s[i2];
  620. i2++;
  621. i++;
  622.  
  623. }
  624.  
  625. message[i] = ' ';
  626. i++;
  627.  
  628. sprintf(&message[i], "%d", ev_att->type);
  629.  
  630. while(message[i] != '#')
  631. i++;
  632.  
  633. message[i] = ' ';
  634. i++;
  635.  
  636. sprintf(&message[i], "%d", ev_att->type2);
  637.  
  638. while(message[i] != '#')
  639. i++;
  640.  
  641. message[i] = ' ';
  642. i++;
  643.  
  644. sprintf(&message[i], "%d", ev_att->ndiplo);
  645.  
  646. while(message[i] != '#')
  647. i++;
  648.  
  649. message[i] = ' ';
  650. i++;
  651.  
  652. sprintf(&message[i], "%d", ev_att->duration);
  653.  
  654. while(message[i] != '#')
  655. i++;
  656.  
  657. message[i] = ' ';
  658. i++;
  659.  
  660. sprintf(&message[i], "%d", ev_att->destination);
  661.  
  662. while(message[i] != '#')
  663. i++;
  664.  
  665. message[i] = ' ';
  666. i++;
  667.  
  668. ev_att = ev_att->next;
  669.  
  670. }
  671.  
  672. message[i] = '-';
  673. i++;
  674.  
  675. }
  676.  
  677. if(territories[labour].ev_inprogress == NULL)
  678. {
  679.  
  680. message[i] = '-';
  681. i++;
  682.  
  683. }
  684. else
  685. {
  686.  
  687. ev_att = territories[labour].ev_inprogress;
  688.  
  689. while(ev_att != NULL)
  690. {
  691.  
  692. s = strdup(ev_att->descr.c_str() );
  693.  
  694. i2 = 0;
  695.  
  696. while(i2 < 20)
  697. {
  698.  
  699. message[i] = s[i2];
  700. i2++;
  701. i++;
  702.  
  703. }
  704.  
  705. message[i] = ' ';
  706. i++;
  707.  
  708. sprintf(&message[i], "%d", ev_att->type);
  709.  
  710. while(message[i] != '#')
  711. i++;
  712.  
  713. message[i] = ' ';
  714. i++;
  715.  
  716. sprintf(&message[i], "%d", ev_att->type2);
  717.  
  718. while(message[i] != '#')
  719. i++;
  720.  
  721. message[i] = ' ';
  722. i++;
  723.  
  724. sprintf(&message[i], "%d", ev_att->ndiplo);
  725.  
  726. while(message[i] != '#')
  727. i++;
  728.  
  729. message[i] = ' ';
  730. i++;
  731.  
  732. sprintf(&message[i], "%d", ev_att->duration);
  733.  
  734. while(message[i] != '#')
  735. i++;
  736.  
  737. message[i] = ' ';
  738. i++;
  739.  
  740. sprintf(&message[i], "%d", ev_att->destination);
  741.  
  742. while(message[i] != '#')
  743. i++;
  744.  
  745. message[i] = ' ';
  746. i++;
  747.  
  748. ev_att = ev_att->next;
  749.  
  750. message[i] = '-';
  751. i++;
  752.  
  753. }
  754.  
  755. }
  756.  
  757.  
  758. if(territories[labour].t == NULL)
  759. {
  760.  
  761. message[i] = '-';
  762. i++;
  763.  
  764. }
  765. else
  766. {
  767.  
  768. i2 = 0;
  769.  
  770. while(i2 < 23)
  771. {
  772.  
  773. s = strdup(territories[labour].t->agreements[i2]->c_str() );
  774. i4 = 0;
  775.  
  776. while(i4 < 20)
  777. {
  778.  
  779. message[i] = s[i4];
  780. i4++;
  781. i++;
  782.  
  783. }
  784. i2++;
  785. message[i] = ' ';
  786. i++;
  787.  
  788. }
  789.  
  790. while(message[i] != '#')
  791. i++;
  792.  
  793. message[i] = '-';
  794. i++;
  795.  
  796. i2 = 0;
  797. i3 = 0;
  798.  
  799. while(i2 < 60)
  800. {
  801.  
  802. while(i3 < 22)
  803. {
  804.  
  805. sprintf(&message[i], "%d", territories[labour].t->done[i2][i3]);
  806.  
  807. while(message[i] != '#')
  808. i++;
  809.  
  810. i3++;
  811. message[i] = ' ';
  812. i++;
  813.  
  814. }
  815.  
  816. i2++;
  817.  
  818. }
  819.  
  820. message[i] = '-';
  821. i++;
  822.  
  823. }
  824.  
  825. sprintf(&message[i], "%d", territories[labour].e.primitives);
  826.  
  827. message[i] = ' ';
  828. i++;
  829.  
  830. while(message[i] != '#')
  831. i++;
  832.  
  833. sprintf(&message[i], "%d", territories[labour].e.lancers);
  834.  
  835. message[i] = ' ';
  836. i++;
  837.  
  838. n = 0;
  839. sigaction(SIGPIPE, &v_act, NULL);
  840. n = send(fd_arrivals, message, (sizeof(char) * 30000 ) - 1, MSG_NOSIGNAL );
  841. n = 0;
  842. continue;
  843. }
  844.  
  845. fine:
  846.  
  847. if( atoi(m) == -1000 )
  848. {
  849. close(fd_arrivals);
  850. exit(1);
  851.  
  852. }
  853.  
  854. }
  855.  
  856. }
  857.  
  858. i++;
  859.  
  860. }
  861.  
  862. // caller process
  863. if(getpid() == spid)
  864. {
  865.  
  866. memset(&gate2, 0, sizeof(gate2) );
  867.  
  868. // delete zombies formation
  869. while(proc < 58)
  870. {
  871.  
  872. // Setting up server
  873.  
  874. l = sizeof(gate2);
  875.  
  876. sigaction(SIGPIPE, &v_act, NULL);
  877.  
  878. n_socket = accept(fd_start, (struct sockaddr *) &gate2, (socklen_t *)&l );
  879.  
  880. if(n_socket < 0 )
  881. continue;
  882.  
  883. sigaction(SIGPIPE, &v_act, NULL);
  884. n = recv(n_socket, message, ( sizeof(char) * 60000 ) - 1 , MSG_NOSIGNAL );
  885. cout<<n<<"\n";
  886. // Job request
  887. if(n == 7 && labour < 59 )
  888. {
  889. memset(message, '#', nmess2 );
  890.  
  891. if(labour == player->nzone)
  892. labour++;
  893.  
  894. m = (char *) malloc( sizeof(char) * 20 );
  895.  
  896. if(m == NULL)
  897. exit(0);
  898.  
  899. sprintf(m, "%d", labour);
  900.  
  901. sigaction(SIGPIPE, &v_act, NULL);
  902. n = send(n_socket, m, ( sizeof(char) * 20 ) - 1, MSG_NOSIGNAL );
  903. labour++;
  904. free(m);
  905.  
  906. memset(message, '#', nmess2 );
  907. continue;
  908.  
  909. }
  910.  
  911. // Loading territories array
  912. if( n > 8 )
  913. {
  914. cout<<"Sono qua\n";
  915. proc++;
  916. s = (char *) malloc( sizeof(char) * 20 );
  917. i = 0;
  918. i2 = 0;
  919. narr = 19;
  920.  
  921. while( message[i] != ' ' )
  922. {
  923.  
  924. if( isdigit(message[i]) )
  925. {
  926.  
  927.  
  928. if(i2 == narr)
  929. {
  930.  
  931. s = (char *) realloc(s, sizeof(char) * (narr + 1) * 2 );
  932.  
  933. if( s == NULL)
  934. exit(0);
  935.  
  936. narr = (narr + 1) * 2;
  937. narr--;
  938.  
  939. }
  940.  
  941. s[i2] = message[i];
  942. i2++;
  943.  
  944. }
  945.  
  946. i++;
  947.  
  948. }
  949.  
  950. zone = atoi(s);
  951. territories[zone].nzone = zone;
  952.  
  953. i2 = 0;
  954.  
  955. while(i2 < narr)
  956. {
  957.  
  958. s[i2] = '#';
  959. i2++;
  960.  
  961. }
  962.  
  963. i2 = 0;
  964.  
  965. while( message[i] != ' ' )
  966. {
  967.  
  968. if( isdigit(message[i]) )
  969. {
  970.  
  971. if(i2 == narr)
  972. {
  973.  
  974. s = (char *) realloc(s, sizeof(char) * (narr + 1) * 2 );
  975.  
  976. if( s == NULL)
  977. exit(0);
  978.  
  979. narr = (narr + 1) * 2;
  980. narr--;
  981.  
  982.  
  983. }
  984.  
  985. s[i2] = message[i];
  986. i2++;
  987.  
  988. }
  989.  
  990. i++;
  991.  
  992. }
  993.  
  994. i3 = atoi(s);
  995. territories[zone].type = i3;
  996.  
  997. while(message[i] != ' ')
  998. {
  999.  
  1000. i2 = 0;
  1001. i3 = 0;
  1002. i4 = 0;
  1003.  
  1004. while(i2 != ' ')
  1005. {
  1006.  
  1007. s[i3] = message[i];
  1008. i3++;
  1009. i2++;
  1010.  
  1011. if(i3 == narr)
  1012. {
  1013.  
  1014. s = (char *) realloc(s, sizeof(char) * (narr + 1) * 2 );
  1015.  
  1016. if( s == NULL)
  1017. exit(0);
  1018.  
  1019. narr = (narr + 1) * 2;
  1020. narr--;
  1021.  
  1022.  
  1023. }
  1024.  
  1025. }
  1026.  
  1027. territories[zone].relationship[i4] = atoi(s);
  1028.  
  1029. i4++;
  1030.  
  1031. i++;
  1032.  
  1033. i2 = 0;
  1034.  
  1035. while(i2 < narr)
  1036. {
  1037.  
  1038. s[i2] = '#';
  1039. i2++;
  1040.  
  1041. }
  1042.  
  1043. }
  1044.  
  1045. i++;
  1046. i2 = 0;
  1047.  
  1048. while( isdigit(message[i]) )
  1049. {
  1050.  
  1051. s[i2] = message[i];
  1052. i2++;
  1053. i++;
  1054.  
  1055. }
  1056.  
  1057. territories[zone].ntreaties = atoi(s);
  1058. i++;
  1059.  
  1060. i2 = 0;
  1061.  
  1062. while(i2 < narr)
  1063. {
  1064.  
  1065. s[i2] = '#';
  1066. i2++;
  1067.  
  1068. }
  1069.  
  1070. i2 = 0;
  1071.  
  1072. while( isdigit(message[i]) )
  1073. {
  1074.  
  1075. s[i2] = message[i];
  1076. i2++;
  1077. i++;
  1078.  
  1079. }
  1080.  
  1081. territories[zone].residents = atoi(s);
  1082. i++;
  1083.  
  1084. i2 = 0;
  1085.  
  1086. while(i2 < narr)
  1087. {
  1088.  
  1089. s[i2] = '#';
  1090. i2++;
  1091.  
  1092. }
  1093.  
  1094. i2 = 0;
  1095.  
  1096. while( isdigit(message[i]) )
  1097. {
  1098.  
  1099. s[i2] = message[i];
  1100. i2++;
  1101. i++;
  1102.  
  1103. }
  1104.  
  1105. territories[zone].pos.x = atoi(s);
  1106.  
  1107. i2 = 0;
  1108.  
  1109. while(i2 < narr)
  1110. {
  1111.  
  1112. s[i2] = '#';
  1113. i2++;
  1114.  
  1115. }
  1116.  
  1117. i2 = 0;
  1118. i++;
  1119.  
  1120. while( isdigit(message[i]) )
  1121. {
  1122.  
  1123. s[i2] = message[i];
  1124. i2++;
  1125. i++;
  1126.  
  1127. }
  1128.  
  1129. territories[zone].pos.y = atoi(s);
  1130.  
  1131. i2 = 0;
  1132.  
  1133. while(i2 < narr)
  1134. {
  1135.  
  1136. s[i2] = '#';
  1137. i2++;
  1138.  
  1139. }
  1140.  
  1141. i2 = 0;
  1142. i3 = 0;
  1143. i++;
  1144.  
  1145. while( message[i] != '-')
  1146. {
  1147.  
  1148. while( isdigit(message[i]) )
  1149. {
  1150.  
  1151. s[i2] = message[i];
  1152. i++;
  1153. i2++;
  1154.  
  1155. }
  1156.  
  1157. i3++;
  1158.  
  1159. recl_a = (struct recruitment *) malloc( sizeof(struct recruitment) );
  1160.  
  1161. if(recl_a == NULL)
  1162. exit(0);
  1163.  
  1164. if(i3 == 1 )
  1165. territories[zone].recl = recl_a;
  1166.  
  1167. recl_a->duration = atoi(s);
  1168.  
  1169. i2 = 0;
  1170.  
  1171. while(i2 < narr)
  1172. {
  1173.  
  1174. s[i2] = '#';
  1175. i2++;
  1176.  
  1177. }
  1178.  
  1179. i2 = 0;
  1180.  
  1181. while( isdigit(message[i]) )
  1182. {
  1183.  
  1184. s[i2] = message[i];
  1185. i++;
  1186. i2++;
  1187.  
  1188. }
  1189.  
  1190. recl_a->type = atoi(s);
  1191.  
  1192. i2 = 0;
  1193.  
  1194. while(i2 < narr)
  1195. {
  1196.  
  1197. s[i2] = '#';
  1198. i2++;
  1199.  
  1200. }
  1201.  
  1202. if(i3 == 1)
  1203. {
  1204. recl_a->next = NULL;
  1205. recl_a->prev = NULL;
  1206. territories[zone].recl = recl_a;
  1207. }
  1208. else
  1209. {
  1210.  
  1211. recl_a->prev = recl_b;
  1212. recl_b->next = recl_a;
  1213. recl_a->next = NULL;
  1214. recl_b = recl_a;
  1215.  
  1216. }
  1217.  
  1218. recl_b = recl_a;
  1219. i++;
  1220.  
  1221. }
  1222.  
  1223. i3 = 0;
  1224.  
  1225. while(message[i] != '-')
  1226. {
  1227.  
  1228. i2 = 0;
  1229.  
  1230. while(message[i] != ' ')
  1231. {
  1232.  
  1233. s[i2] = message[i];
  1234. i++;
  1235. i2++;
  1236.  
  1237. }
  1238.  
  1239. i3++;
  1240.  
  1241. dipl = (struct diplomatic *) malloc( sizeof(struct diplomatic) );
  1242.  
  1243. if(dipl == NULL)
  1244. exit(0);
  1245.  
  1246. if(i3 == 1)
  1247. territories[zone].dipl_z = dipl;
  1248.  
  1249. dipl->n = atoi(s);
  1250.  
  1251. i2 = 0;
  1252.  
  1253. while(i2 < narr)
  1254. {
  1255.  
  1256. s[i2] = '#';
  1257. i2++;
  1258.  
  1259. }
  1260.  
  1261. i2 = 0;
  1262.  
  1263. while(message[i] != ' ')
  1264. {
  1265.  
  1266. s[i2] = message[i];
  1267. i++;
  1268. i2++;
  1269.  
  1270. }
  1271.  
  1272. dipl->ability = atoi(s);
  1273.  
  1274. i2 = 0;
  1275.  
  1276. while(i2 < narr)
  1277. {
  1278.  
  1279. s[i2] = '#';
  1280. i2++;
  1281.  
  1282. }
  1283.  
  1284. i2 = 0;
  1285.  
  1286. while(message[i] != ' ')
  1287. {
  1288.  
  1289. s[i2] = message[i];
  1290. i++;
  1291. i2++;
  1292.  
  1293. }
  1294.  
  1295. dipl->zone = atoi(s);
  1296.  
  1297. i2 = 0;
  1298.  
  1299. while(i2 < narr)
  1300. {
  1301.  
  1302. s[i2] = '#';
  1303. i2++;
  1304.  
  1305. }
  1306.  
  1307. i2 = 0;
  1308.  
  1309. while(message[i] != ' ')
  1310. {
  1311.  
  1312. s[i2] = message[i];
  1313. i++;
  1314. i2++;
  1315.  
  1316. }
  1317.  
  1318. dipl->treaties = atoi(s);
  1319.  
  1320. i2 = 0;
  1321.  
  1322. while(i2 < narr)
  1323. {
  1324.  
  1325. s[i2] = '#';
  1326. i2++;
  1327.  
  1328. }
  1329.  
  1330. if(i3 == 1)
  1331. {
  1332.  
  1333. dipl->next = NULL;
  1334. dipl->prev = NULL;
  1335. territories[zone].dipl_z = dipl;
  1336.  
  1337. }
  1338. else
  1339. {
  1340.  
  1341. dipl->prev = dipl2;
  1342. dipl->next = dipl;
  1343. dipl->next = NULL;
  1344. dipl2 = dipl;
  1345.  
  1346. }
  1347.  
  1348. i++;
  1349.  
  1350. }
  1351.  
  1352. i3 = 0;
  1353.  
  1354. while(message[i] != '-')
  1355. {
  1356.  
  1357. i2 = 0;
  1358.  
  1359. while(message[i] != ' ')
  1360. {
  1361.  
  1362. s[i2] = message[i];
  1363. i++;
  1364. i2++;
  1365.  
  1366. }
  1367.  
  1368. i3++;
  1369.  
  1370. ev_att = (struct event *) malloc( sizeof(struct event) );
  1371.  
  1372. if(ev_att == NULL)
  1373. exit(0);
  1374.  
  1375. if(i3 == 1)
  1376. territories[zone].ev = ev_att;
  1377.  
  1378. ev_att->descr.clear();
  1379. ev_att->descr.append(s);
  1380.  
  1381. i2 = 0;
  1382.  
  1383. while(i2 < narr)
  1384. {
  1385.  
  1386. s[i2] = '#';
  1387. i2++;
  1388.  
  1389. }
  1390.  
  1391. i2 = 0;
  1392.  
  1393. while(message[i] != ' ')
  1394. {
  1395.  
  1396. s[i2] = message[i];
  1397. i++;
  1398. i2++;
  1399.  
  1400. }
  1401.  
  1402. ev_att->type = atoi(s);
  1403.  
  1404. i2 = 0;
  1405.  
  1406. while(i2 < narr)
  1407. {
  1408.  
  1409. s[i2] = '#';
  1410. i2++;
  1411.  
  1412. }
  1413.  
  1414. i2 = 0;
  1415.  
  1416. while(message[i] != ' ')
  1417. {
  1418.  
  1419. s[i2] = message[i];
  1420. i++;
  1421. i2++;
  1422.  
  1423. }
  1424.  
  1425. ev_att->type2 = atoi(s);
  1426.  
  1427. i2 = 0;
  1428.  
  1429. while(i2 < narr)
  1430. {
  1431.  
  1432. s[i2] = '#';
  1433. i2++;
  1434.  
  1435. }
  1436.  
  1437. i2 = 0;
  1438.  
  1439. while(message[i] != ' ')
  1440. {
  1441.  
  1442. s[i2] = message[i];
  1443. i++;
  1444. i2++;
  1445.  
  1446. }
  1447.  
  1448. ev_att->ndiplo = atoi(s);
  1449.  
  1450. i2 = 0;
  1451.  
  1452. while(i2 < narr)
  1453. {
  1454.  
  1455. s[i2] = '#';
  1456. i2++;
  1457.  
  1458. }
  1459.  
  1460. i2 = 0;
  1461.  
  1462. while(i2 < narr)
  1463. {
  1464.  
  1465. s[i2] = '#';
  1466. i2++;
  1467.  
  1468. }
  1469.  
  1470. ev_att->duration = atoi(s);
  1471.  
  1472. i2 = 0;
  1473.  
  1474. while(i2 < narr)
  1475. {
  1476.  
  1477. s[i2] = '#';
  1478. i2++;
  1479.  
  1480. }
  1481.  
  1482. ev_att->destination = atoi(s);
  1483.  
  1484. i2 = 0;
  1485.  
  1486. while(i2 < narr)
  1487. {
  1488.  
  1489. s[i2] = '#';
  1490. i2++;
  1491.  
  1492. }
  1493.  
  1494. if(i3 == 1)
  1495. territories[zone].ev = ev_att;
  1496.  
  1497. ev_att->prev = ev_att2;
  1498. ev_att2->next = ev_att;
  1499. ev_att->next = NULL;
  1500.  
  1501. i++;
  1502.  
  1503. }
  1504.  
  1505. i3 = 0;
  1506.  
  1507. while(message[i] != '-')
  1508. {
  1509.  
  1510. i2 = 0;
  1511.  
  1512. while(message[i] != ' ')
  1513. {
  1514.  
  1515. s[i2] = message[i];
  1516. i++;
  1517. i2++;
  1518.  
  1519. }
  1520.  
  1521. i3++;
  1522.  
  1523. ev_att = (struct event *) malloc( sizeof(struct event) );
  1524.  
  1525. if(ev_att == NULL)
  1526. exit(0);
  1527.  
  1528. if(i3 == 1)
  1529. territories[zone].ev = ev_att;
  1530.  
  1531. ev_att->descr.clear();
  1532. ev_att->descr.append(s);
  1533.  
  1534. i2 = 0;
  1535.  
  1536. while(i2 < narr)
  1537. {
  1538.  
  1539. s[i2] = '#';
  1540. i2++;
  1541.  
  1542. }
  1543.  
  1544. i2 = 0;
  1545.  
  1546. while(message[i] != ' ')
  1547. {
  1548.  
  1549. s[i2] = message[i];
  1550. i++;
  1551. i2++;
  1552.  
  1553. }
  1554.  
  1555. ev_att->type = atoi(s);
  1556.  
  1557. i2 = 0;
  1558.  
  1559. while(i2 < narr)
  1560. {
  1561.  
  1562. s[i2] = '#';
  1563. i2++;
  1564.  
  1565. }
  1566.  
  1567. i2 = 0;
  1568.  
  1569. while(message[i] != ' ')
  1570. {
  1571.  
  1572. s[i2] = message[i];
  1573. i++;
  1574. i2++;
  1575.  
  1576. }
  1577.  
  1578. ev_att->type2 = atoi(s);
  1579.  
  1580. i2 = 0;
  1581.  
  1582. while(i2 < narr)
  1583. {
  1584.  
  1585. s[i2] = '#';
  1586. i2++;
  1587.  
  1588. }
  1589.  
  1590. i2 = 0;
  1591.  
  1592. while(message[i] != ' ')
  1593. {
  1594.  
  1595. s[i2] = message[i];
  1596. i++;
  1597. i2++;
  1598.  
  1599. }
  1600.  
  1601. ev_att->ndiplo = atoi(s);
  1602.  
  1603. i2 = 0;
  1604.  
  1605. while(i2 < narr)
  1606. {
  1607.  
  1608. s[i2] = '#';
  1609. i2++;
  1610.  
  1611. }
  1612.  
  1613. i2 = 0;
  1614.  
  1615. while(i2 < narr)
  1616. {
  1617.  
  1618. s[i2] = '#';
  1619. i2++;
  1620.  
  1621. }
  1622.  
  1623. ev_att->duration = atoi(s);
  1624.  
  1625. i2 = 0;
  1626.  
  1627. while(i2 < narr)
  1628. {
  1629.  
  1630. s[i2] = '#';
  1631. i2++;
  1632.  
  1633. }
  1634.  
  1635. ev_att->destination = atoi(s);
  1636.  
  1637. i2 = 0;
  1638.  
  1639. while(i2 < narr)
  1640. {
  1641.  
  1642. s[i2] = '#';
  1643. i2++;
  1644.  
  1645. }
  1646.  
  1647. if(i3 == 1)
  1648. territories[zone].ev_inprogress = ev_att;
  1649.  
  1650. ev_att->prev = ev_att2;
  1651. ev_att2->next = ev_att;
  1652. ev_att->next = NULL;
  1653.  
  1654. i++;
  1655.  
  1656. }
  1657.  
  1658. while(message[i] != '-')
  1659. {
  1660.  
  1661. i4 = 0;
  1662.  
  1663. while(i4 < 23)
  1664. {
  1665.  
  1666. i2 = 0;
  1667.  
  1668. while(message[i] != ' ')
  1669. {
  1670.  
  1671. s[i2] = message[i];
  1672. i++;
  1673. i2++;
  1674.  
  1675. }
  1676.  
  1677. territories[zone].t->agreements[i4]->clear();
  1678. string str(s);
  1679. territories[zone].t->agreements[i4]->append(str);
  1680.  
  1681. i2 = 0;
  1682.  
  1683. while(i2 < narr)
  1684. {
  1685.  
  1686. s[i2] = '#';
  1687. i2++;
  1688.  
  1689. }
  1690.  
  1691. i4++;
  1692.  
  1693. }
  1694.  
  1695.  
  1696. i++;
  1697.  
  1698. }
  1699.  
  1700. i3 = 0;
  1701. i4 = 0;
  1702.  
  1703. while(message[i] != '-' )
  1704. {
  1705.  
  1706. i2 = 0;
  1707.  
  1708. while(message[i] != ' ')
  1709. {
  1710.  
  1711. s[i2] = message[i];
  1712. i++;
  1713. i2++;
  1714.  
  1715. }
  1716.  
  1717. territories[zone].t->done[i3][i4] = atoi(s);
  1718.  
  1719. if(i4 == 21)
  1720. {
  1721.  
  1722. i4 = 0;
  1723. i3++;
  1724.  
  1725. }
  1726.  
  1727. i2 = 0;
  1728.  
  1729. while(i2 < narr)
  1730. {
  1731.  
  1732. s[i2] = '#';
  1733. i2++;
  1734.  
  1735. }
  1736.  
  1737. i++;
  1738.  
  1739. }
  1740.  
  1741. territories[zone].buildings = NULL;
  1742. territories[zone].e.primitives = 0;
  1743.  
  1744. i2 = 0;
  1745.  
  1746. while(message[i] != ' ')
  1747. {
  1748.  
  1749. s[i2] = message[i];
  1750. i++;
  1751. i2++;
  1752.  
  1753. }
  1754.  
  1755. territories[zone].e.primitives = atoi(s);
  1756.  
  1757. i2 = 0;
  1758.  
  1759. while(i2 < narr)
  1760. {
  1761.  
  1762. s[i2] = '#';
  1763. i2++;
  1764.  
  1765. }
  1766.  
  1767. i2 = 0;
  1768.  
  1769. while(message[i] != ' ')
  1770. {
  1771.  
  1772. s[i2] = message[i];
  1773. i++;
  1774. i2++;
  1775.  
  1776. }
  1777.  
  1778. territories[zone].e.lancers = atoi(s);
  1779.  
  1780. i2 = 0;
  1781.  
  1782. while(i2 <= narr)
  1783. {
  1784.  
  1785. s[i2] = '#';
  1786. i2++;
  1787.  
  1788. }
  1789.  
  1790. memset(message, '#', nmess2 );
  1791. m = (char *) malloc( sizeof(char) * 20 );
  1792. i2 = -1000;
  1793. sprintf(m, "%d", i2);
  1794. n = send(n_socket, m, ( sizeof(char) * 20 ) - 1 , MSG_NOSIGNAL );
  1795. free(m);
  1796.  
  1797. }
  1798.  
  1799. // Parallel calculation
  1800.  
  1801. if(calc != 1)
  1802. {
  1803.  
  1804. calc = 1;
  1805.  
  1806. for(i = 0; i < 1; i++)
  1807. {
  1808.  
  1809. if(territories[i].nzone == player->nzone)
  1810. continue;
  1811.  
  1812. if(territories[i].type == 1)
  1813. {
  1814.  
  1815. // Primitives recriutment
  1816.  
  1817. if(territories[i].recl == NULL)
  1818. {
  1819.  
  1820. territories[i].recl = (struct recruitment *) malloc( sizeof(struct recruitment) );
  1821.  
  1822. if(territories[i].recl == NULL)
  1823. exit(0);
  1824.  
  1825. territories[i].recl->type = 2;
  1826. territories[i].recl->duration = 2;
  1827. territories[i].recl->next = NULL;
  1828. territories[i].recl->prev = NULL;
  1829.  
  1830. continue;
  1831.  
  1832. }
  1833.  
  1834. // Continue recruitment
  1835.  
  1836. if(territories[i].recl != NULL )
  1837. {
  1838.  
  1839. recl_a = territories[i].recl;
  1840.  
  1841.  
  1842. while( recl_a != NULL )
  1843. {
  1844.  
  1845. if(recl_a->duration > 0)
  1846. recl_a->duration = recl_a->duration - 1;
  1847.  
  1848. if(recl_a->duration == 0)
  1849. {
  1850.  
  1851. recl_b = recl_a->prev;
  1852. recl_c = recl_a->next;
  1853.  
  1854. territories[i].e.primitives = territories[i].e.primitives + 1;
  1855.  
  1856. if( recl_b != NULL && recl_c != NULL )
  1857. recl_c->prev = NULL;
  1858.  
  1859. if(recl_c != NULL && recl_b != NULL)
  1860. recl_b->next = NULL;
  1861.  
  1862. if(recl_c != NULL && recl_b != NULL)
  1863. {
  1864.  
  1865. recl_b->next = recl_c;
  1866. recl_c->prev = recl_b;
  1867.  
  1868. free(recl_a);
  1869. recl_a = recl_b;
  1870.  
  1871. }
  1872.  
  1873. }
  1874.  
  1875. recl_a = recl_a->next;
  1876.  
  1877. }
  1878.  
  1879. continue;
  1880.  
  1881. }
  1882.  
  1883. if(territories[i].recl != NULL && territories[i].recl->next == NULL )
  1884. {
  1885.  
  1886. recl_a = territories[i].recl;
  1887.  
  1888. recl_b = (struct recruitment *) malloc( sizeof(struct recruitment) );
  1889.  
  1890. if(recl_b == NULL)
  1891. exit(0);
  1892.  
  1893. recl_b->prev = recl_a;
  1894. recl_a->next = recl_b;
  1895. recl_b->next = NULL;
  1896.  
  1897. recl_b->type = 2;
  1898. recl_b->duration = 2;
  1899.  
  1900. }
  1901.  
  1902. }
  1903.  
  1904. }
  1905.  
  1906. }
  1907. memset(message, '#', nmess2 );
  1908.  
  1909. pid = waitpid(-1, &status, WNOHANG);
  1910.  
  1911. }
  1912.  
  1913. }
  1914.  
  1915. close(fd_start);
  1916. close(n_socket);
  1917. free(message);
  1918. return(1);
  1919. }
  1920.  
Advertisement
Add Comment
Please, Sign In to add comment