Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cmath>
- #include <ctime>
- #include <string>
- #include <fstream>
- #include <filesystem>
- #include <typeinfo>
- #include <iostream>
- #include <cstring>
- #include <random>
- #include <cstdio>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <errno.h>
- #include <sys/wait.h>
- #include <cctype>
- #include <fcntl.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <netinet/tcp.h>
- using namespace std;
- struct coord
- {
- int x;
- int y;
- };
- struct diplomatic
- {
- int n; //unconditioned reference diplomatics on game ( gdiplo )
- int ability;
- int zone; //start from 0
- int treaties;
- struct diplomatic *next;
- struct diplomatic *prev;
- };
- struct diplomatic *dipl, *dipl2, *dipl3;
- struct recruitment
- {
- int duration; // 4 seasons/turns
- int type; // diplomatic 1, primitive 2
- struct recruitment *next;
- struct recruitment *prev;
- };
- struct recruitment *recl_a, *recl_s, *recl_b, *recl_c;
- struct event
- {
- string descr;
- int type;
- int type2;
- int n;
- int ndiplo;
- int duration;
- int destination;
- struct event *next;
- struct event *prev;
- };
- struct event *ev_att, *ev_att2, *ev_att3, *ev_att4;
- struct treaties
- {
- string *agreements[23];
- int done[60][22];
- };
- struct neighbours
- {
- int *neighbourhood;
- short int dim;
- };
- struct building
- {
- int time_building;
- int x;
- int y;
- bool in_progress;
- };
- struct army
- {
- int primitives;
- int lancers;
- };
- struct areas
- {
- int nzone; // start from 0
- int type; // 1 aggressive, 2 neutral, 3 civil
- int *relationship; // it have to be equal to the territories number, maximum 60
- int ntreaties; // treaties reserved
- int residents;
- struct coord pos;
- struct recruitment *recl;
- struct diplomatic *dipl_z;
- struct event *ev;
- struct event *ev_inprogress;
- struct treaties *t; // 0 if null
- struct building *buildings;
- /* civils characteristics */
- /* neutrals characteristics */
- /* agrressive characteristics */
- struct army e;
- };
- struct areas *player, *territories;
- string str, str2, str3 /* only for diplay information rising village */, str4 /* only for loading */, str5 /* only for events */, pstr[22], str6;
- struct neighbours *atlas_neighbourhood;
- struct army *es;
- int main()
- {
- char *message, *m, *s;
- int fd_start, opt, spid = 0, n, status, pid, labour = 0, zone, n_socket, i, i2, i3, i4, i5, nmess, l;
- short int narr = 0, proc = 0;
- bool calc = 0, esc = 0;
- struct sockaddr_in gate, gate2;
- struct sigaction n_act, v_act;
- random_device rd;
- mt19937 producer( rd() ), producer2 ( rd() );
- uniform_int_distribution<int> arr( 1,60 ), arr2( 1,3 );
- territories = (struct areas *) malloc( sizeof(struct areas) * 60 );
- player = &territories[7];
- /* I initialize areas of game */
- for( i = 0; i < 60; i++ )
- {
- territories[i].nzone = i;
- territories[i].type = arr2(producer);
- territories[i].ntreaties = 0;
- territories[i].relationship= (int *) malloc( sizeof(int) * 60 );
- if(territories[i].relationship == NULL)
- exit(0);
- territories[i].ev = NULL;
- territories[i].ev_inprogress = NULL;
- territories[i].recl = NULL;
- territories[i].dipl_z = NULL;
- territories[i].buildings = NULL;
- territories[i].residents = 100;
- /* civil characteristics */
- /* neutral characteristics */
- /* aggressive characteristics */
- if(territories[i].type == 1)
- {
- territories[i].e.primitives = 0;
- territories[i].e.lancers = 0;
- }
- territories[i].t = NULL;
- }
- fd_start = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if( fd_start == -1 )
- exit(0);
- memset(&gate, 0, sizeof(gate) );
- gate.sin_family = AF_INET;
- gate.sin_addr.s_addr = inet_addr("127.0.0.1");
- gate.sin_port = htons(10000);
- opt = 1;
- setsockopt(fd_start, SOL_SOCKET, SO_REUSEADDR | SO_KEEPALIVE | SO_REUSEPORT, &opt, sizeof(opt) );
- fcntl(fd_start, F_SETFL, O_NONBLOCK);
- sigemptyset(&n_act.sa_mask);
- n_act.sa_flags = 0;
- sigaction(SIGPIPE, &n_act, &v_act);
- n = bind(fd_start, (struct sockaddr *)&gate, sizeof(gate) );
- if( n == -1 )
- exit(0);
- listen(fd_start, 60);
- nmess = 29999;
- int nmess2 = 59999;
- message = (char *)malloc( sizeof(char) * 60000 );
- if(message == NULL)
- exit(0);
- while(i < nmess2 )
- {
- message[i] = '#';
- i++;
- }
- spid = getpid();
- i = 0;
- i2 = 0;
- while(i < 58)
- {
- if(i == player->nzone)
- {
- i++;
- continue;
- }
- pid = fork();
- // called processes
- if(getpid() != spid)
- {
- int fd_arrivals, n, labour, opt;
- struct sockaddr_in gate;
- char pr[7] = "readyy", *message, *m, *s;
- message = (char *)malloc( sizeof(char) * 30000 );
- if(message == NULL)
- exit(0);
- while(i < nmess )
- {
- message[i] = '#';
- i++;
- }
- s = (char *) malloc( sizeof(char) * 20 );
- if(s == NULL)
- exit(0);
- fd_arrivals = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if(fd_arrivals == -1)
- exit(0);
- memset(&gate, 0, sizeof(gate) );
- gate.sin_family = AF_INET;
- gate.sin_addr.s_addr = inet_addr("127.0.0.1");
- gate.sin_port = htons(10000);
- //fcntl(fd_arrivals, F_SETFL, O_NONBLOCK);
- opt = 1;
- int dim = 60000;
- setsockopt(fd_arrivals, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT | SO_KEEPALIVE, &opt, sizeof(opt));
- setsockopt(fd_arrivals, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(dim));
- sigaction(SIGPIPE, &v_act, NULL);
- n = connect(fd_arrivals, (struct sockaddr *)&gate, sizeof(gate) );
- sigaction(SIGPIPE, &v_act, NULL);
- n = send(fd_arrivals, pr, sizeof(char) * 7, MSG_NOSIGNAL );
- m = (char *) malloc( sizeof(char) * 20 );
- if(m == NULL)
- exit(0);
- n = 0;
- // Receive job from server
- sigaction(SIGPIPE, &v_act, NULL);
- while(1)
- {
- n = recv(fd_arrivals, m, ( sizeof(char) * 20 ) - 1, MSG_NOSIGNAL );
- if( n == 19 && atoi(m) > 0 )
- {
- labour = atoi(m);
- n = labour;
- i = labour;
- n++;
- i2 = 0;
- n = 0;
- memset(message, '#', nmess );
- while(i2 < 20)
- {
- m[i2] = '#';
- i2++;
- }
- // Parallel processing
- for(; i < n; i++)
- {
- if(territories[i].nzone == player->nzone)
- goto fine;
- if(territories[i].type == 1)
- {
- // Primitives recruitment
- if(territories[i].recl == NULL)
- {
- territories[i].recl = (struct recruitment *) malloc( sizeof(struct recruitment) );
- if(territories[i].recl == NULL)
- exit(0);
- territories[i].recl->type = 2;
- territories[i].recl->duration = 2;
- territories[i].recl->next = NULL;
- territories[i].recl->prev = NULL;
- continue;
- }
- // Continue recruitment
- if(territories[i].recl != NULL )
- {
- recl_a = territories[i].recl;
- while( recl_a != NULL )
- {
- if(recl_a->duration > 0)
- recl_a->duration = recl_a->duration - 1;
- if(recl_a->duration == 0)
- {
- recl_b = recl_a->prev;
- recl_c = recl_a->next;
- territories[i].e.primitives = territories[i].e.primitives + 1;
- if( recl_b != NULL && recl_c != NULL )
- recl_c->prev = NULL;
- if(recl_c != NULL && recl_b != NULL)
- recl_b->next = NULL;
- if(recl_c != NULL && recl_b != NULL)
- {
- recl_b->next = recl_c;
- recl_c->prev = recl_b;
- free(recl_a);
- recl_a = recl_b;
- }
- }
- recl_a = recl_a->next;
- }
- continue;
- }
- if(territories[i].recl != NULL && territories[i].recl->next == NULL )
- {
- recl_a = territories[i].recl;
- recl_b = (struct recruitment *) malloc( sizeof(struct recruitment) );
- if(recl_b == NULL)
- exit(0);
- recl_b->prev = recl_a;
- recl_a->next = recl_b;
- recl_b->next = NULL;
- recl_b->type = 2;
- recl_b->duration = 2;
- }
- }
- }
- // saving territory
- i = 0;
- sprintf( &message[i], "%d", territories[labour].nzone);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", territories[labour].type);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- if(territories[labour].relationship == NULL)
- {
- message[i] = '-';
- i++;
- }
- else
- {
- i2 = 0;
- while(i2 < 60)
- {
- sprintf(&message[i], "%d", territories[labour].relationship[i2]);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- i2++;
- }
- message[i] = '#';
- i++;
- }
- sprintf(&message[i], "%d", territories[labour].ntreaties);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", territories[labour].residents);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- if(territories[labour].recl == NULL)
- {
- message[i] = '-';
- i++;
- }
- else
- {
- recl_a = territories[labour].recl;
- while(recl_a != NULL)
- {
- sprintf(&message[i], "%d", recl_a->duration);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", recl_a->type);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- recl_a = recl_a->next;
- }
- message[i] = '-';
- i++;
- }
- if(territories[labour].dipl_z == NULL)
- {
- message[i] = '-';
- i++;
- }
- else
- {
- dipl = territories[labour].dipl_z;
- while(dipl != NULL)
- {
- sprintf(&message[i], "%d", dipl->n);
- while(message[i] != '#')
- i++;
- sprintf(&message[i], "%d", dipl->ability);
- while(message[i] != '#')
- i++;
- sprintf(&message[i], "%d", dipl->zone);
- while(message[i] != '#')
- i++;
- sprintf(&message[i], "%d", dipl->zone);
- while(message[i] != '#')
- i++;
- dipl = dipl->next;
- }
- message[i] = '-';
- i++;
- }
- if(territories[labour].ev == NULL)
- {
- message[i] = '-';
- i++;
- }
- else
- {
- ev_att = territories[labour].ev;
- while(ev_att != NULL)
- {
- s = strdup(ev_att->descr.c_str() );
- i2 = 0;
- while(i2 < 20)
- {
- message[i] = s[i2];
- i2++;
- i++;
- }
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->type);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->type2);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->ndiplo);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->duration);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->destination);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- ev_att = ev_att->next;
- }
- message[i] = '-';
- i++;
- }
- if(territories[labour].ev_inprogress == NULL)
- {
- message[i] = '-';
- i++;
- }
- else
- {
- ev_att = territories[labour].ev_inprogress;
- while(ev_att != NULL)
- {
- s = strdup(ev_att->descr.c_str() );
- i2 = 0;
- while(i2 < 20)
- {
- message[i] = s[i2];
- i2++;
- i++;
- }
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->type);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->type2);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->ndiplo);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->duration);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- sprintf(&message[i], "%d", ev_att->destination);
- while(message[i] != '#')
- i++;
- message[i] = ' ';
- i++;
- ev_att = ev_att->next;
- message[i] = '-';
- i++;
- }
- }
- if(territories[labour].t == NULL)
- {
- message[i] = '-';
- i++;
- }
- else
- {
- i2 = 0;
- while(i2 < 23)
- {
- s = strdup(territories[labour].t->agreements[i2]->c_str() );
- i4 = 0;
- while(i4 < 20)
- {
- message[i] = s[i4];
- i4++;
- i++;
- }
- i2++;
- message[i] = ' ';
- i++;
- }
- while(message[i] != '#')
- i++;
- message[i] = '-';
- i++;
- i2 = 0;
- i3 = 0;
- while(i2 < 60)
- {
- while(i3 < 22)
- {
- sprintf(&message[i], "%d", territories[labour].t->done[i2][i3]);
- while(message[i] != '#')
- i++;
- i3++;
- message[i] = ' ';
- i++;
- }
- i2++;
- }
- message[i] = '-';
- i++;
- }
- sprintf(&message[i], "%d", territories[labour].e.primitives);
- message[i] = ' ';
- i++;
- while(message[i] != '#')
- i++;
- sprintf(&message[i], "%d", territories[labour].e.lancers);
- message[i] = ' ';
- i++;
- n = 0;
- sigaction(SIGPIPE, &v_act, NULL);
- n = send(fd_arrivals, message, (sizeof(char) * 30000 ) - 1, MSG_NOSIGNAL );
- n = 0;
- continue;
- }
- fine:
- if( atoi(m) == -1000 )
- {
- close(fd_arrivals);
- exit(1);
- }
- }
- }
- i++;
- }
- // caller process
- if(getpid() == spid)
- {
- memset(&gate2, 0, sizeof(gate2) );
- // delete zombies formation
- while(proc < 58)
- {
- // Setting up server
- l = sizeof(gate2);
- sigaction(SIGPIPE, &v_act, NULL);
- n_socket = accept(fd_start, (struct sockaddr *) &gate2, (socklen_t *)&l );
- if(n_socket < 0 )
- continue;
- sigaction(SIGPIPE, &v_act, NULL);
- n = recv(n_socket, message, ( sizeof(char) * 60000 ) - 1 , MSG_NOSIGNAL );
- cout<<n<<"\n";
- // Job request
- if(n == 7 && labour < 59 )
- {
- memset(message, '#', nmess2 );
- if(labour == player->nzone)
- labour++;
- m = (char *) malloc( sizeof(char) * 20 );
- if(m == NULL)
- exit(0);
- sprintf(m, "%d", labour);
- sigaction(SIGPIPE, &v_act, NULL);
- n = send(n_socket, m, ( sizeof(char) * 20 ) - 1, MSG_NOSIGNAL );
- labour++;
- free(m);
- memset(message, '#', nmess2 );
- continue;
- }
- // Loading territories array
- if( n > 8 )
- {
- cout<<"Sono qua\n";
- proc++;
- s = (char *) malloc( sizeof(char) * 20 );
- i = 0;
- i2 = 0;
- narr = 19;
- while( message[i] != ' ' )
- {
- if( isdigit(message[i]) )
- {
- if(i2 == narr)
- {
- s = (char *) realloc(s, sizeof(char) * (narr + 1) * 2 );
- if( s == NULL)
- exit(0);
- narr = (narr + 1) * 2;
- narr--;
- }
- s[i2] = message[i];
- i2++;
- }
- i++;
- }
- zone = atoi(s);
- territories[zone].nzone = zone;
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while( message[i] != ' ' )
- {
- if( isdigit(message[i]) )
- {
- if(i2 == narr)
- {
- s = (char *) realloc(s, sizeof(char) * (narr + 1) * 2 );
- if( s == NULL)
- exit(0);
- narr = (narr + 1) * 2;
- narr--;
- }
- s[i2] = message[i];
- i2++;
- }
- i++;
- }
- i3 = atoi(s);
- territories[zone].type = i3;
- while(message[i] != ' ')
- {
- i2 = 0;
- i3 = 0;
- i4 = 0;
- while(i2 != ' ')
- {
- s[i3] = message[i];
- i3++;
- i2++;
- if(i3 == narr)
- {
- s = (char *) realloc(s, sizeof(char) * (narr + 1) * 2 );
- if( s == NULL)
- exit(0);
- narr = (narr + 1) * 2;
- narr--;
- }
- }
- territories[zone].relationship[i4] = atoi(s);
- i4++;
- i++;
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- }
- i++;
- i2 = 0;
- while( isdigit(message[i]) )
- {
- s[i2] = message[i];
- i2++;
- i++;
- }
- territories[zone].ntreaties = atoi(s);
- i++;
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while( isdigit(message[i]) )
- {
- s[i2] = message[i];
- i2++;
- i++;
- }
- territories[zone].residents = atoi(s);
- i++;
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while( isdigit(message[i]) )
- {
- s[i2] = message[i];
- i2++;
- i++;
- }
- territories[zone].pos.x = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- i++;
- while( isdigit(message[i]) )
- {
- s[i2] = message[i];
- i2++;
- i++;
- }
- territories[zone].pos.y = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- i3 = 0;
- i++;
- while( message[i] != '-')
- {
- while( isdigit(message[i]) )
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- i3++;
- recl_a = (struct recruitment *) malloc( sizeof(struct recruitment) );
- if(recl_a == NULL)
- exit(0);
- if(i3 == 1 )
- territories[zone].recl = recl_a;
- recl_a->duration = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while( isdigit(message[i]) )
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- recl_a->type = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- if(i3 == 1)
- {
- recl_a->next = NULL;
- recl_a->prev = NULL;
- territories[zone].recl = recl_a;
- }
- else
- {
- recl_a->prev = recl_b;
- recl_b->next = recl_a;
- recl_a->next = NULL;
- recl_b = recl_a;
- }
- recl_b = recl_a;
- i++;
- }
- i3 = 0;
- while(message[i] != '-')
- {
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- i3++;
- dipl = (struct diplomatic *) malloc( sizeof(struct diplomatic) );
- if(dipl == NULL)
- exit(0);
- if(i3 == 1)
- territories[zone].dipl_z = dipl;
- dipl->n = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- dipl->ability = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- dipl->zone = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- dipl->treaties = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- if(i3 == 1)
- {
- dipl->next = NULL;
- dipl->prev = NULL;
- territories[zone].dipl_z = dipl;
- }
- else
- {
- dipl->prev = dipl2;
- dipl->next = dipl;
- dipl->next = NULL;
- dipl2 = dipl;
- }
- i++;
- }
- i3 = 0;
- while(message[i] != '-')
- {
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- i3++;
- ev_att = (struct event *) malloc( sizeof(struct event) );
- if(ev_att == NULL)
- exit(0);
- if(i3 == 1)
- territories[zone].ev = ev_att;
- ev_att->descr.clear();
- ev_att->descr.append(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- ev_att->type = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- ev_att->type2 = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- ev_att->ndiplo = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- ev_att->duration = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- ev_att->destination = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- if(i3 == 1)
- territories[zone].ev = ev_att;
- ev_att->prev = ev_att2;
- ev_att2->next = ev_att;
- ev_att->next = NULL;
- i++;
- }
- i3 = 0;
- while(message[i] != '-')
- {
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- i3++;
- ev_att = (struct event *) malloc( sizeof(struct event) );
- if(ev_att == NULL)
- exit(0);
- if(i3 == 1)
- territories[zone].ev = ev_att;
- ev_att->descr.clear();
- ev_att->descr.append(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- ev_att->type = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- ev_att->type2 = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- ev_att->ndiplo = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- ev_att->duration = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- ev_att->destination = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- if(i3 == 1)
- territories[zone].ev_inprogress = ev_att;
- ev_att->prev = ev_att2;
- ev_att2->next = ev_att;
- ev_att->next = NULL;
- i++;
- }
- while(message[i] != '-')
- {
- i4 = 0;
- while(i4 < 23)
- {
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- territories[zone].t->agreements[i4]->clear();
- string str(s);
- territories[zone].t->agreements[i4]->append(str);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i4++;
- }
- i++;
- }
- i3 = 0;
- i4 = 0;
- while(message[i] != '-' )
- {
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- territories[zone].t->done[i3][i4] = atoi(s);
- if(i4 == 21)
- {
- i4 = 0;
- i3++;
- }
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i++;
- }
- territories[zone].buildings = NULL;
- territories[zone].e.primitives = 0;
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- territories[zone].e.primitives = atoi(s);
- i2 = 0;
- while(i2 < narr)
- {
- s[i2] = '#';
- i2++;
- }
- i2 = 0;
- while(message[i] != ' ')
- {
- s[i2] = message[i];
- i++;
- i2++;
- }
- territories[zone].e.lancers = atoi(s);
- i2 = 0;
- while(i2 <= narr)
- {
- s[i2] = '#';
- i2++;
- }
- memset(message, '#', nmess2 );
- m = (char *) malloc( sizeof(char) * 20 );
- i2 = -1000;
- sprintf(m, "%d", i2);
- n = send(n_socket, m, ( sizeof(char) * 20 ) - 1 , MSG_NOSIGNAL );
- free(m);
- }
- // Parallel calculation
- if(calc != 1)
- {
- calc = 1;
- for(i = 0; i < 1; i++)
- {
- if(territories[i].nzone == player->nzone)
- continue;
- if(territories[i].type == 1)
- {
- // Primitives recriutment
- if(territories[i].recl == NULL)
- {
- territories[i].recl = (struct recruitment *) malloc( sizeof(struct recruitment) );
- if(territories[i].recl == NULL)
- exit(0);
- territories[i].recl->type = 2;
- territories[i].recl->duration = 2;
- territories[i].recl->next = NULL;
- territories[i].recl->prev = NULL;
- continue;
- }
- // Continue recruitment
- if(territories[i].recl != NULL )
- {
- recl_a = territories[i].recl;
- while( recl_a != NULL )
- {
- if(recl_a->duration > 0)
- recl_a->duration = recl_a->duration - 1;
- if(recl_a->duration == 0)
- {
- recl_b = recl_a->prev;
- recl_c = recl_a->next;
- territories[i].e.primitives = territories[i].e.primitives + 1;
- if( recl_b != NULL && recl_c != NULL )
- recl_c->prev = NULL;
- if(recl_c != NULL && recl_b != NULL)
- recl_b->next = NULL;
- if(recl_c != NULL && recl_b != NULL)
- {
- recl_b->next = recl_c;
- recl_c->prev = recl_b;
- free(recl_a);
- recl_a = recl_b;
- }
- }
- recl_a = recl_a->next;
- }
- continue;
- }
- if(territories[i].recl != NULL && territories[i].recl->next == NULL )
- {
- recl_a = territories[i].recl;
- recl_b = (struct recruitment *) malloc( sizeof(struct recruitment) );
- if(recl_b == NULL)
- exit(0);
- recl_b->prev = recl_a;
- recl_a->next = recl_b;
- recl_b->next = NULL;
- recl_b->type = 2;
- recl_b->duration = 2;
- }
- }
- }
- }
- memset(message, '#', nmess2 );
- pid = waitpid(-1, &status, WNOHANG);
- }
- }
- close(fd_start);
- close(n_socket);
- free(message);
- return(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment