        -:    0:Source:Life.c
        -:    0:Graph:Life.gcno
        -:    0:Data:Life.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include "Life.h"
        -:    2:
 84638847:    3:void KILL_Tribe(uint32_t y, uint32_t x){
        -:    4:	//life[y][x]->alive = 0;
 84638847:    5:	SET_ZERO(Alive, y, x);
 84638847:    6:	SET_ZERO(Fought, y, x);
 84638847:    7:	SET_ZERO(Mated, y, x);
        -:    8:	//fought[y][x] = 0;
        -:    9:	//mated[y][x] = 0;
        -:   10:	//stats_t.total_living--;
 84638847:   11:}
        -:   12:
        -:   13:/*
        -:   14:int Is_Alive(uint32_t src_y, uint32_t src_x){
        -:   15:    if( Alive[src_y][src_x/8] & (1 << (src_x % 8)) )
        -:   16:        return 1;
        -:   17:    return 0;
        -:   18:}
        -:   19:
        -:   20:int Has_Mated(uint32_t src_y, uint32_t src_x){
        -:   21:	if( Mated[src_y][src_x/8] & (1 << (src_x % 8)) )
        -:   22:		return 1;
        -:   23:	return 0;
        -:   24:}
        -:   25:
        -:   26:int Has_Fought(uint32_t src_y, uint32_t src_x){
        -:   27:	if( Fought[src_y][src_x/8] & (1 << (src_x % 8)) )
        -:   28:		return 1;
        -:   29:	return 0;
        -:   30:}
        -:   31:*/
        -:   32:
        1:   33:void Allocate_Life_Array(void){
        -:   34:	uint32_t n1, n2;
        1:   35:	life = (struct Species ***)malloc(sizeof(struct Species **) * HEIGHT);
     1801:   36:	for(n1=0; n1<HEIGHT; n1++){
     1800:   37:		life[n1] = (struct Species **)malloc(sizeof(struct Species *) * WIDTH);
  6481800:   38:		for(n2=0; n2<WIDTH; n2++){
  6480000:   39:			life[n1][n2] = (struct Species *)malloc(sizeof(struct Species));
  6480000:   40:			(void)memset( (void *)life[n1][n2], '\0', sizeof(struct Species) );
        -:   41:		}
        -:   42:	}
        1:   43:}
        -:   44:
 17877508:   45:void Gen_Tribe_Color(struct Species *one, unsigned char *rgb){
        -:   46:	// 6751269 possibilities based on species/traits
 17877508:   47:	rgb[0] = one->intelligence;
 17877508:   48:	rgb[1] = (unsigned char)floor((double)one->strength/2) + floor((double)one->speed/2);
 17877508:   49:	rgb[2] = one->temp_l + one->temp_h + one->max_age;
 17877508:   50:}
        -:   51:
960382236:   52:int Is_Same_Species(struct Species *one, struct Species *two){
        -:   53:	// "dna" determined by all variables, no one variable can deviate more than 4
        -:   54:	//better to implement steps instead ie: 0-3 4-7 8-11 etc.  4*(0->63)+(0->4)
        -:   55:	// y=mx+b   y range 0-255,  if x = x same species, b == subspecies deviation
        -:   56:	//must allow for subspecies deviation
        -:   57:	//unsigned char x = floor(num/4);
        -:   58:	//b = 255%4 * 4;
        -:   59:	//if(one == NULL || two == NULL)
        -:   60:	//	return 0;
        -:   61:
960382236:   62:	if( one->intelligence/4 != two->intelligence/4 )
    16310:   63:		return 0;
960365926:   64:	if( one->strength/4 != two->strength/4 )
    12314:   65:		return 0;
960353612:   66:	if( one->speed/4 != two->speed/4 )
    14216:   67:		return 0;
960339396:   68:	if( one->temp_l/4 != two->temp_l/4 )
  3733223:   69:		return 0;
956606173:   70:	if( one->temp_h/4 != two->temp_h/4 )
    #####:   71:		return 0;
956606173:   72:	if( one->max_age/4 != two->max_age/4 )
    10645:   73:		return 0;
        -:   74:	//if( floor((double)one->reproductive_rate/4) != floor((double)two->reproductive_rate/4) )
        -:   75:	//	return 0;
        -:   76:
956595528:   77:	return 1;
        -:   78:}
        -:   79:
    #####:   80:void Add_First_Life(struct Species *one){
        -:   81:	//one->alive = 1;
    #####:   82:	one->intelligence          = INTELLIGENCE;
    #####:   83:	one->strength              = STRENGTH;
    #####:   84:	one->speed                 = SPEED;
    #####:   85:	one->imunity               = IMUNITY;
    #####:   86:	one->temp_l                = TEMP_L;
    #####:   87:	one->temp_h                = TEMP_H;
        -:   88:	//one->reproductive_rate     = REPRODUCTIVE_RATE;
    #####:   89:	one->max_age               = MAX_AGE;
    #####:   90:	one->cur_age               = CUR_AGE;
    #####:   91:	one->fight_flight_balance  = FIGHT_FLIGHT_BALANCE;
    #####:   92:	one->interspecies_violence = INTERSPECIES_VIOLENCE;
    #####:   93:	one->migration_factor      = MIGRATION_FACTOR;
    #####:   94:	one->social_factor         = SOCIAL_FACTOR;
    #####:   95:	one->water_turns           = 0;
    #####:   96:	one->water_direction       = 0;
    #####:   97:}
        -:   98:
341875564:   99:float Location_Temp(uint32_t src_y, uint32_t src_x){
        -:  100:	//return temperature at any given src_y src_x
341875564:  101:	float location_temp = (float)stats_t.cur_temp;
341875564:  102:	float equator_dist = 0;
        -:  103:
        -:  104:	//bordering water, degrees cooler maybe
        -:  105:
        -:  106:	//elevation effect
341875564:  107:	if(ia->dem[src_y][src_x] < 11001){
343486447:  108:		location_temp -= TEMP_PER_1000M*((float)(ia->dem[src_y][src_x]-stats_t.cur_sealevel)/1000);
        -:  109:	}
        -:  110:		//else{
        -:  111:		//location_temp -= TEMP_11K_TO_20K;
        -:  112:	//}
        -:  113:	//dealing with 1/12th scale of 30 arc-second grid
        -:  114:	//1 arc minute = 1/60 of 1deg
        -:  115:	//30 arc-second = 1/30 of 1deg
        -:  116:	//1px = 12x 30 arc-second  = 2/5 of 1deg
        -:  117:	//1800px = 90-0-90  900px is equator
        -:  118:
        -:  119:	//equatorial distance effect
341875564:  120:	equator_dist = abs( (float)src_y - (float)HEIGHT/2 ) * 2 / 5;  //degrees from equator
        -:  121:
        -:  122:	//45c different between eq and poles  .5C per 1deg  hmmmm
341875564:  123:	location_temp -= equator_dist / DEM_SCALE;
        -:  124:
341875564:  125:	return location_temp;
        -:  126:}
        -:  127:
341164967:  128:int Climate_Fatal(struct Species *one, uint32_t src_y, uint32_t src_x){
        -:  129:	//return 1 if climate would kill tribe at this location
        -:  130:	// 6.49C/1000m 
        -:  131:	// -56.5 °C above 11000m
        -:  132:
        -:  133:	//need to account for intelligence and strength to withstand
341164967:  134:	float location_temp = Location_Temp(src_y, src_x);
        -:  135:
342757293:  136:	if(location_temp > (float)one->temp_h){ //if temp is higher than tribe can withstand
        -:  137:		//2 parts intel 1 part strength
        -:  138:		// 127 * 3 = 381
        -:  139:		//
        -:  140:		// initial tribes will have +/-4 added to their tolerance range
        -:  141:		// 
 28024333:  142:		if( ((float)one->temp_h + (float)floor( ((double)one->intelligence*2+(double)one->strength) / 10) ) < location_temp ){ //if it's still higher
   175407:  143:			return 1;
        -:  144:		}
        -:  145:	}
342581886:  146:	if(location_temp < (float)one->temp_l){ //if temp is lower than tribe can withstand
  5823570:  147:		if( ((float)one->temp_l + (float)floor( ((double)one->intelligence*2+(double)one->strength) / 10) ) > location_temp ){ //if it's still lower
  5824064:  148:			return 1;
        -:  149:		}
        -:  150:	}
336757822:  151:	return 0;
        -:  152:}
        -:  153:
 52061838:  154:void Create_Life(struct Species *one, struct Species *two, struct Species *three){
        -:  155:	//mix sub species 1 and 2 to create 3 (average of 1 and 2) [ ~8.3% chance of (+/-1pt) species deviation ]
        -:  156:	//chance of new species:  ~1% 
        -:  157:	uint32_t rand1, rand2;
        -:  158:	//uint32_t n1 = 0;
 52061838:  159:	int use_ceil = 0;
        -:  160:	float tmp;
        -:  161:	//double bigtmp;
        -:  162:	struct timeval cur;
 52061838:  163:	gettimeofday(&cur, NULL);
 52088772:  164:	uint64_t s = (unsigned int)cur.tv_usec + 3;
 52088772:  165:	rand1 = lrand(&s) % 2;
 52092934:  166:	if(rand1)
 24548652:  167:		use_ceil = 1;
        -:  168:
        -:  169:	//Is_Alive(src_y, src_x)
        -:  170:	//three->alive = 1;
 52092934:  171:	three->water_turns = 0;
 52092934:  172:	three->water_direction = 0;
        -:  173:	//genetic
 52092934:  174:	tmp = (float)((float)one->intelligence + (float)two->intelligence) / 2;
 52092934:  175:	three->intelligence = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  176:	tmp = (float)((float)one->strength + (float)two->strength) / 2;
 52092934:  177:	three->strength = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  178:	tmp = (float)((float)one->speed + (float)two->speed) / 2;
 52092934:  179:	three->speed = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
        -:  180:	//keep within ranges
 52092934:  181:	tmp = (float)((float)one->temp_l + (float)two->temp_l) / 2;
 52092934:  182:	three->temp_l = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  183:	tmp = (float)((float)one->temp_h + (float)two->temp_h) / 2;
 52092934:  184:	three->temp_h = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  185:	tmp = (float)((float)one->max_age + (float)two->max_age) / 2;
 52092934:  186:	three->max_age = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
        -:  187:
        -:  188:	//non-genetic
 52092934:  189:	tmp = (float)((float)one->social_factor + (float)two->social_factor) / 2;
 52092934:  190:	three->social_factor = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  191:	tmp = (float)((float)one->interspecies_violence + (float)two->interspecies_violence) / 2;
 52092934:  192:	three->interspecies_violence = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  193:	tmp = (float)((float)one->fight_flight_balance + (float)two->fight_flight_balance) / 2;
 52092934:  194:	three->fight_flight_balance = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
 52092934:  195:	tmp = (float)((float)one->migration_factor + (float)two->migration_factor) / 2;
 52092934:  196:	three->migration_factor = (unsigned char)( use_ceil ? ceil(tmp) : floor(tmp) );
        -:  197:
        -:  198:	//fixed to 1 for now
        -:  199:	//three->reproductive_rate = 1;
        -:  200:	//three->reproductive_rate = (one->reproductive_rate+two->reproductive_rate)/2
        -:  201:	//starts at 0yrs old
 52092934:  202:	three->cur_age = 0;
 52092934:  203:	three->imunity = (unsigned char)IMUNITY;
        -:  204:	//suseconds_t signed integer type capable of storing values at least in the range [-1, 1000000].
        -:  205:
        -:  206:	//rand1 = (int)ceil((double)1/( CHANCE_OF_EVOLUTION ));
        -:  207:	//rand1 = (int)ceil((double)1/( CHANCE_OF_EVOLUTION )*80/3*100);
        -:  208:	//if(lrand(&s) % 100 > rand1)
        -:  209:	//	return;
        -:  210:
        -:  211:	//bigtmp = 1/CHANCE_OF_DEVIATION;
        -:  212:
        -:  213:	//rand1 = (uint32_t)ceil(bigtmp*2147483648);
        -:  214:	//rand2 = (uint32_t)ceil(lrand(&s)/2);
        -:  215:
        -:  216:	//rand2 = lrand(&s) % 2;
 52092934:  217:	rand1 = (int32_t)ceil(CHANCE_OF_EVOLUTION*3/10);
 52092934:  218:	rand2 = lrand(&s) % rand1;
 52076732:  219:	rand1 = lrand(&s) % rand1;
 52097799:  220:	if(rand2 != rand1)
 52087145:  221:		return;
        -:  222:	//for(n1=0; n1<CHANCE_OF_DEVIATION; n1++){
        -:  223:	//	rand1 = lrand(&s) % 2;
        -:  224:	//	rand2 = lrand(&s) % 2;
        -:  225:	//	if(rand2 == rand1)
        -:  226:	//		return;
        -:  227:	//}
        -:  228:
        -:  229:	//3/80 total chance of new species after here !!!!!
        -:  230:
        -:  231:	//1/2 chance we do a deviation
    10654:  232:	unsigned char deviation = (unsigned char)lrand(&s) % 2;
        -:  233:	//+1 deviation, otherwise -1
        -:  234:	//if(deviation)
        -:  235:	//	deviation = 1;
        -:  236:	//else
        -:  237:	//	deviation = 0;
    10654:  238:	rand1 = lrand(&s) % 9; //which feild will be modified
        -:  239:	// 7/16 chance the selected feild is at an upper/lower limit
        -:  240:	// 7/32 chance deviation is the +1/-1 needed to create new species
        -:  241:	// 5/9 genetic deviation selected
        -:  242:	// ~ 1/1000 chance ultimately of new species (1/20 * 1/50)
    10651:  243:	if(rand1 == 0){
     1173:  244:		if(deviation){
      403:  245:			if(three->intelligence < 255)
      403:  246:				three->intelligence++;
        -:  247:		}else{
      770:  248:			if(three->intelligence > 4)
      770:  249:				three->intelligence--;
        -:  250:		}
     1173:  251:		return;
        -:  252:	}
        -:  253:
     9478:  254:	if(rand1 == 1){
     1011:  255:		if(deviation){
      462:  256:			if(three->strength < 255)
      462:  257:				three->strength++;
        -:  258:		}else{
      549:  259:			if(three->strength > 4)
      549:  260:				three->strength--;
        -:  261:		}
     1011:  262:		return;
        -:  263:	}
     8467:  264:	if(rand1 == 2){
     1357:  265:		if(deviation){
      762:  266:			if(three->speed < 255)
      762:  267:				three->speed++;
        -:  268:		}else{
      595:  269:			if(three->speed > 4)
      595:  270:				three->speed--;
        -:  271:		}
     1357:  272:		return;
        -:  273:	}
     7110:  274:	if(rand1 == 3){
        -:  275:		//stay within ranges
     1519:  276:		if(deviation){
      772:  277:			if(three->temp_h < 39){
      772:  278:				three->temp_h++;
      772:  279:				three->temp_l++;
        -:  280:			}
        -:  281:		}else{
      747:  282:			if(three->temp_l > 4){
      306:  283:				three->temp_h--;
      306:  284:				three->temp_l--;
        -:  285:			}
        -:  286:		}
     1519:  287:		return;
        -:  288:	}
     5591:  289:	if(rand1 == 4){
        -:  290:		//stays within range 30 - 128yrs
      983:  291:		if(deviation){
      495:  292:			 if(three->max_age < 100)
      495:  293:				three->max_age++;
        -:  294:		}else{
      488:  295:			if(three->max_age > 26)
      488:  296:				three->max_age--;
        -:  297:		}
      983:  298:		return;
        -:  299:	}
     4608:  300:	if(rand1 == 5){
     1033:  301:		if(deviation){
      661:  302:			if(three->social_factor < 255)
      661:  303:				three->social_factor++;
        -:  304:		}else{
      372:  305:			if(three->social_factor > SOCIAL_FACTOR)
    #####:  306:				three->social_factor--;
        -:  307:		}
     1033:  308:		return;
        -:  309:	}
     3575:  310:	if(rand1 == 6){
     1168:  311:		if(deviation){
      398:  312:			if(three->interspecies_violence < 255)
      398:  313:				three->interspecies_violence++;
        -:  314:		}else{
      770:  315:			if(three->interspecies_violence > INTERSPECIES_VIOLENCE)
    #####:  316:				three->interspecies_violence--;
        -:  317:		}
     1168:  318:		return;
        -:  319:	}
     2407:  320:	if(rand1 == 7){
     1402:  321:		if(deviation){
      670:  322:			if(three->fight_flight_balance < 255)
      669:  323:				three->fight_flight_balance++;
        -:  324:		}else{
      732:  325:			if(three->fight_flight_balance > FIGHT_FLIGHT_BALANCE)
        1:  326:				three->fight_flight_balance--;
        -:  327:		}
     1402:  328:		return;
        -:  329:	}
        -:  330:	//if(rand1 == 8){  //else
     1005:  331:	if(deviation){
      496:  332:		if(three->migration_factor != 255)
      495:  333:			three->migration_factor++;
        -:  334:	}else{
      509:  335:		if(three->migration_factor > MIGRATION_FACTOR)
    #####:  336:			three->migration_factor--;
        -:  337:	}
        -:  338:}
        -:  339:
385418528:  340:void Moves_Populate(uint32_t src_y, uint32_t src_x, struct Moves *moves){
        -:  341:	//populate struct moves with 4 surrounding coordinates
385418528:  342:	const uint32_t width = WIDTH - 1;
385418528:  343:	const uint32_t height = HEIGHT - 1;
385418528:  344:	if(src_x != 0)
385393897:  345:		moves[0].x = src_x - 1;
        -:  346:	else
    24631:  347:		moves[0].x = width;
385418528:  348:	moves[0].y = src_y;
385418528:  349:	moves[0].valid  = 1;
385418528:  350:	moves[0].rating = 4;
        -:  351:
385418528:  352:	if(src_x != width)
385392847:  353:		moves[1].x = src_x + 1;
        -:  354:	else
    25681:  355:		moves[1].x = 0;
        -:  356:
385418528:  357:	moves[1].y = src_y;
385418528:  358:	moves[1].valid  = 1;
385418528:  359:	moves[1].rating = 4;
385418528:  360:	moves[2].x = src_x;
        -:  361:
385418528:  362:	if(src_y != 0)
385418528:  363:		moves[2].y = src_y-1;
        -:  364:	else
    #####:  365:		moves[2].y = height;
        -:  366:
385418528:  367:	moves[2].valid  = 1;
385418528:  368:	moves[2].rating = 4;
385418528:  369:	moves[3].x = src_x;
        -:  370:
385418528:  371:	if(src_y != height)
385418528:  372:		moves[3].y = src_y+1;
        -:  373:	else
    #####:  374:		moves[3].y = 0;
        -:  375:
385418528:  376:	moves[3].valid  = 1;
385418528:  377:	moves[3].rating = 4;
        -:  378:
385418528:  379:}
        -:  380:
305861285:  381:int Tribe_Borders_Water(uint32_t src_y, uint32_t src_x){
        -:  382:	//return 1 if a tribe is bordering open water
305861285:  383:	uint32_t n1 = 0;
        -:  384:	//uint32_t n2 = 0;
        -:  385:	struct Moves moves[4];
305861285:  386:	Moves_Populate(src_y, src_x, moves);
1522647150:  387:	for(n1=0; n1<4; n1++){
1220598852:  388:		if(ia->dem[moves[n1].y][moves[n1].x] <= stats_t.cur_sealevel){
        -:  389:			//if(!life[moves[n1].y][moves[n1].x]->alive){
  6820389:  390:			if( !IS_SET(Alive, moves[n1].y, moves[n1].x) ){
  8183266:  391:				return 1;
        -:  392:			}
        -:  393:		}
        -:  394:	}
        -:  395:
302048298:  396:	return 0;
        -:  397:	//return n2==0 ? 0 : 1;
        -:  398:}
        -:  399:
  2056213:  400:int Tribe_Water_Migrate(uint32_t src_y, uint32_t src_x, uint32_t *dest){
        -:  401:	//migrate while at sea
        -:  402:	//return 1 if migrated onto land, 0 if still at sea
  2056213:  403:	uint32_t n1 = 0;
  2056213:  404:	uint32_t n2 = 0;
        -:  405:	//uint32_t width = WIDTH - 1;
        -:  406:	//uint32_t height = HEIGHT - 1;
        -:  407:	struct timeval cur;
        -:  408:	uint32_t rand1;
  2056213:  409:	gettimeofday(&cur, NULL);
  2056221:  410:	uint64_t s = (unsigned int)cur.tv_usec + 3;
        -:  411:	//uint32_t dest[2];
        -:  412:	struct Moves moves[4];
  2056221:  413:	Moves_Populate(src_y, src_x, moves);
  2056320:  414:	int ret_val = 0;
        -:  415:
 10280936:  416:	for(n1=0; n1<4; n1++){
        -:  417:		//if(!life[moves[n1].y][moves[n1].x]->alive){
  8224616:  418:		if( !IS_SET(Alive, moves[n1].y, moves[n1].x) ){
  6198011:  419:			moves[n1].valid = 1;
        -:  420:		}
        -:  421:		
        -:  422:	}
        -:  423:/*
        -:  424:            rand1 = (int)ceil((double)life[src_y][src_x]->intelligence/256*100);
        -:  425:            if(lrand(&s) % 100 < rand1){
        -:  426:                //search for spot continuing direction
        -:  427:                n1 = life[src_y][src_x]->water_direction;
        -:  428:                if(moves[n1].valid){
        -:  429:                    dest[0] = moves[n1].y;
        -:  430:                    dest[1] = moves[n1].x;
        -:  431:                    (void)memcpy((void *)life[dest[0]][dest[1]], (const void *)life[src_y][src_x], sizeof(struct Species));
        -:  432:                    if(life[dest[0]][dest[1]]->imunity < 255)
        -:  433:                        life[dest[0]][dest[1]]->imunity++;
        -:  434:                    life[dest[0]][dest[1]]->water_turns++;
        -:  435:
        -:  436:                    return 0;
        -:  437:                }
        -:  438:                dest[0] = src_y;  //Don't move
        -:  439:                dest[1] = src_x;  //Don't move
        -:  440:                life[src_y][src_x]->water_turns++;
        -:  441:                return ret_val;
        -:  442:            }
        -:  443:
        -:  444:*/	
        -:  445:
  2056320:  446:	n2 = 0;
        -:  447:	//see if a neightbor is unoccupied land
 10280664:  448:	for(n1=0; n1<4; n1++){
        -:  449:		//moves[n1].valid = 1;
        -:  450:		//if(life[moves[n1].y][moves[n1].x]->alive){
  8224344:  451:		if( IS_SET(Alive, moves[n1].y, moves[n1].x) ){
  2026355:  452:			moves[n1].valid = 0;
  2026355:  453:			n2++;
  2026355:  454:			continue;
        -:  455:		}
  6197989:  456:		if(ia->dem[moves[n1].y][moves[n1].x] <= stats_t.cur_sealevel){
        -:  457:			//if(!life[moves[n1].y][moves[n1].x]->alive){
        -:  458:			
  5441700:  459:				moves[n1].valid = 0;
  5441700:  460:				n2++;
  5441700:  461:				continue;
        -:  462:			//}
        -:  463:		}
   756289:  464:		moves[n1].valid = 1;
        -:  465:		//if(life[moves[n1].y][moves[n1].x]->alive){
        -:  466:		//	moves[n1].valid = 0;
        -:  467:		//	n2++;
        -:  468:		//}
        -:  469:	}
  2056320:  470:	if(n2 < 4){
        -:  471:		//if 1 or more land (unoccupied) choice choose one randomly
   667811:  472:		rand1 = lrand(&s) %  (4 - n2);
   667828:  473:		n2 = 0;
  3339024:  474:		for(n1=0; n1<4; n1++){
  2671196:  475:			if(moves[n1].valid){
   756284:  476:				if(n2 == rand1){
        -:  477:					//this is your choice
   716071:  478:					dest[0] = moves[n1].y;
   716071:  479:					dest[1] = moves[n1].x;
        -:  480:					//life[moves[n1].y][moves[n1].x]->water_turns = 0;
   716071:  481:					ret_val = 1;
   716071:  482:					continue;
        -:  483:				}
    40213:  484:				n2++;
        -:  485:			}
        -:  486:		}
        -:  487:	}else{
  1388509:  488:		n2 = 0;
        -:  489:		//choose a water spot randomly
  6942250:  490:		for(n1=0; n1<4; n1++){
  5553741:  491:			if(ia->dem[moves[n1].y][moves[n1].x] <= stats_t.cur_sealevel){
        -:  492:				//if(!life[moves[n1].y][moves[n1].x]->alive){
  4336409:  493:				if( !IS_SET(Alive, moves[n1].y, moves[n1].x) ){
  4143393:  494:					moves[n1].valid = 1;
  4143393:  495:					n2++;
        -:  496:				}
        -:  497:			}
        -:  498:		}
        -:  499:		//use intelligence to determine if we take straight line path
  1388509:  500:		if(n2 != 0){ //one or more open water spots exists
        -:  501:
  1357335:  502:			rand1 = (uint32_t)ceil((double)life[src_y][src_x]->intelligence/256*100);
  1357335:  503:			if(lrand(&s) % 100 < rand1){
        -:  504:				//search for water spot continuing direction
   337039:  505:				n1 = life[src_y][src_x]->water_direction;
   337039:  506:				if(moves[n1].valid){
   304605:  507:					dest[0] = moves[n1].y;
   304605:  508:					dest[1] = moves[n1].x;
   304605:  509:					SET_ONE(Alive, dest[0], dest[1]);
   304605:  510:					(void)memcpy((void *)life[dest[0]][dest[1]], (const void *)life[src_y][src_x], sizeof(struct Species));
   304605:  511:					if(life[dest[0]][dest[1]]->imunity < 255)
   250613:  512:						life[dest[0]][dest[1]]->imunity++;
   304605:  513:					life[dest[0]][dest[1]]->water_turns++;
   304605:  514:					KILL_Tribe(src_y, src_x);
   304605:  515:					return 0;
        -:  516:				}
    32434:  517:				dest[0] = src_y;  //Don't move
    32434:  518:				dest[1] = src_x;  //Don't move
    32434:  519:				life[src_y][src_x]->water_turns++;
    32434:  520:				return ret_val;
        -:  521:			}
        -:  522:
        -:  523:
  1020297:  524:			rand1 = lrand(&s) %  n2;
  1020293:  525:			n2 = 0;
  5100980:  526:			for(n1=0; n1<4; n1++){
  4080687:  527:				if(moves[n1].valid){
  3031490:  528:					if(n2 == rand1){
  2048454:  529:						dest[0] = moves[n2].y;
  2048454:  530:						dest[1] = moves[n2].x;
        -:  531:						//life[src_y][src_x]->water_direction = n2;
        -:  532:						//life[moves[n2].y][moves[n2].x]->water_turns++;
  2048454:  533:						continue;
        -:  534:					}
   983036:  535:					n2++;
        -:  536:				}
        -:  537:			}
        -:  538:			//continue;
        -:  539:		}else{
    31174:  540:			dest[0] = src_y;  //Don't move
    31174:  541:			dest[1] = src_x;  //Don't move
    31174:  542:			life[dest[0]][dest[1]]->water_turns++;
    31174:  543:			return ret_val;
        -:  544:		}
        -:  545:	}
  1688121:  546:	SET_ONE(Alive, dest[0], dest[1]);
  1688121:  547:	(void)memcpy((void *)life[dest[0]][dest[1]], (const void *)life[src_y][src_x], sizeof(struct Species));
  1688121:  548:	if(life[dest[0]][dest[1]]->imunity < 255)
  1472935:  549:		life[dest[0]][dest[1]]->imunity++;
        -:  550:	//set water_turns
  1688121:  551:	if(ret_val)
   667819:  552:		life[dest[0]][dest[1]]->water_turns = 0;
        -:  553:	else
  1020302:  554:		life[dest[0]][dest[1]]->water_turns++;
        -:  555:	//life[dest[0]][dest[1]ter_turns++;
        -:  556:    //clear old data
  1688121:  557:    KILL_Tribe(src_y, src_x);
  1688105:  558:	return ret_val;
        -:  559:}
        -:  560:
   903218:  561:int Tribe_Move_To_Water(uint32_t src_y, uint32_t src_x, uint32_t *dest){
        -:  562:	//move a tribe onto water, return 1 if no open water available
   903218:  563:	uint32_t n1 = 0;
   903218:  564:	uint32_t n2 = 0;
        -:  565:	//uint32_t width = WIDTH - 1;
        -:  566:	//uint32_t height = HEIGHT - 1;
        -:  567:	struct timeval cur;
        -:  568:	uint32_t rand1;
   903218:  569:	gettimeofday(&cur, NULL);
   903226:  570:	uint64_t s = (unsigned int)cur.tv_usec + 3;
        -:  571:	//uint32_t dest[2] = {0, 0};
        -:  572:	struct Moves moves[4];
   903226:  573:	Moves_Populate(src_y, src_x, moves);
        -:  574:
        -:  575:	//set to invalid moves that aren't water, or are occupied
  4515916:  576:	for(n1=0; n1<4; n1++){
  3612695:  577:		if(ia->dem[moves[n1].y][moves[n1].x] > stats_t.cur_sealevel){
        -:  578:			//if(!life[moves[n1].y][moves[n1].x]->alive){
  2281860:  579:			moves[n1].valid = 0;
        -:  580:			//n2++;
  2281860:  581:			continue;
        -:  582:			//}
        -:  583:		}
        -:  584:		//if(life[moves[n1].y][moves[n1].x]->alive){
  1330835:  585:		if( IS_SET(Alive, moves[n1].y, moves[n1].x) ){
    47867:  586:			moves[n1].valid = 0;
        -:  587:			//n2++;
    47867:  588:			continue;
        -:  589:		}
  1282968:  590:		if( Climate_Fatal(life[src_y][src_x], moves[n1].y, moves[n1].x) ){
    33304:  591:			moves[n1].valid = 0;
        -:  592:			//n2++;
    33304:  593:			continue;
        -:  594:		}
  1249669:  595:		n2++;
        -:  596:	}
        -:  597:
        -:  598:	//error condition
   903221:  599:	if(n2 == 0){
        -:  600:		//printf("This tribe is not surrounded by any open water !\n");
        -:  601:		//kill him
    18677:  602:		dest[0] = src_y;
    18677:  603:		dest[1] = src_x;
    18677:  604:		return 1;
        -:  605:		//KILL_Tribe(src_y, src_x);
        -:  606:		//return;
        -:  607:	}
        -:  608:	//randomly choose one of the valid choices
   884544:  609:	rand1 = lrand(&s) %  n2;
   884537:  610:	n2 = 0;
  2361832:  611:	for(n1=0; n1<4; n1++){
  2361825:  612:		if(moves[n1].valid){
  1057521:  613:			if(n2 == rand1){
        -:  614:				//this is your choice
   884530:  615:				dest[0] = moves[n1].y;
   884530:  616:				dest[1] = moves[n1].x;
   884530:  617:				life[src_y][src_x]->water_direction = n1;
   884530:  618:				break;
        -:  619:			}
   172991:  620:			n2++;
        -:  621:		}
        -:  622:	}
        -:  623:
        -:  624:	//if( Climate_Fatal(life[src_y][src_x], dest[0], dest[1]) ){
        -:  625:	//	KILL_Tribe(src_y, src_x);
        -:  626:	//	stats[t].exposure++;
        -:  627:	//	return;
        -:  628:	//}
        -:  629:	//printf("Moved to water !\n");
        -:  630:	//move to destination
        -:  631:	//life[dest[0]][dest[1]]->alive = 1;
   884537:  632:	SET_ONE(Alive, dest[0], dest[1]);
   884537:  633:	(void)memcpy((void *)life[dest[0]][dest[1]], (const void *)life[src_y][src_x], sizeof(struct Species));
   884537:  634:	if(life[dest[0]][dest[1]]->imunity < 255)
   822125:  635:		life[dest[0]][dest[1]]->imunity++;
        -:  636:	//set water_turns to 1
   884537:  637:	life[dest[0]][dest[1]]->water_turns = 1;
        -:  638:	//clear old data
   884537:  639:	KILL_Tribe(src_y, src_x);
        -:  640:	//life_speed_up_Y[dest[0]] = 1;
        -:  641:	//life_speed_up_X[dest[1]] = 1;
   884549:  642:	return 0;
        -:  643:}
        -:  644:
 75511674:  645:int Tribe_Migrate(uint32_t src_y, uint32_t src_x, uint32_t *dest){
        -:  646:	//return 1 if moved
        -:  647:	//put new location in dest
        -:  648:
        -:  649:	uint32_t n1, n2;
 75511674:  650:	const uint32_t width = WIDTH - 1;
 75511674:  651:	const uint32_t height = HEIGHT - 1;
        -:  652:	struct Moves moves[4];
        -:  653:	//float temp_preferred = 0;
        -:  654:	//float temp_diff = 0;
        -:  655:	//float temp_actual = 0;
        -:  656:
        -:  657:	//if any of these are greater/less than height or width they need to be set to 0
        -:  658:	//same goes for all neighbors calculations
        -:  659:
        -:  660:	//4 potential destination initialization
 75511674:  661:	Moves_Populate(src_y, src_x, moves);
        -:  662:
        -:  663:/*
        -:  664:	if(src_x != 0)
        -:  665:		moves[0].x = src_x - 1;
        -:  666:	else
        -:  667:		moves[0].x = width;
        -:  668:
        -:  669:	moves[0].y = src_y;
        -:  670:	moves[0].valid  = 1;
        -:  671:	moves[0].rating = 4;
        -:  672:
        -:  673:	if(src_x != width)
        -:  674:		moves[1].x = src_x + 1;
        -:  675:	else
        -:  676:		moves[1].x = 0;
        -:  677:
        -:  678:	moves[1].y = src_y;
        -:  679:	moves[1].valid  = 1;
        -:  680:	moves[1].rating = 4;
        -:  681:	moves[2].x = src_x;
        -:  682:
        -:  683:	if(src_y != 0)
        -:  684:		moves[2].y = src_y-1;
        -:  685:	else
        -:  686:		moves[2].y = height;
        -:  687:
        -:  688:	moves[2].valid  = 1;
        -:  689:	moves[2].rating = 4;
        -:  690:	moves[3].x = src_x;
        -:  691:
        -:  692:	if(src_y != height)
        -:  693:		moves[3].y = src_y+1;
        -:  694:	else
        -:  695:		moves[3].y = 0;
        -:  696:
        -:  697:	moves[3].valid  = 1;
        -:  698:	moves[3].rating = 4;
        -:  699:*/
        -:  700:	//4 neighors of a move destination
        -:  701:	struct Neighbors{unsigned int x; unsigned int y;}neighbors[4];
        -:  702:
        -:  703:	//to store candidate destination with highest rating
 75640984:  704:	struct Dest_Cand{int h_rating; unsigned int m_index;}dest_cand = {0, 0};
        -:  705:	int cand_ties[4];
        -:  706:
        -:  707:    struct timeval cur;
        -:  708:    uint32_t rand1;
 75640984:  709:    gettimeofday(&cur, NULL);
 75614215:  710:	uint64_t s = (unsigned int)cur.tv_usec + 3;
        -:  711:
377799169:  712:	for(n1=0; n1<4; n1++){
        -:  713:		//rule out moving to water
302044916:  714:		if(stats_t.cur_sealevel >= ia->dem[moves[n1].y][moves[n1].x]){
  1929194:  715:			moves[n1].valid = 0;
  1929194:  716:			continue;
        -:  717:		}
        -:  718:		//rule out moving to occupied coordinates
        -:  719:		//if(life[moves[n1].y][moves[n1].x]->alive){
300115722:  720:		if( IS_SET(Alive, moves[n1].y, moves[n1].x) ){
260573567:  721:			moves[n1].valid = 0;
260573567:  722:			continue;
        -:  723:		}
        -:  724:		//rule out moving to fatal climates
 39542155:  725:		if( Climate_Fatal(life[src_y][src_x], moves[n1].y, moves[n1].x) )
  3675645:  726:			moves[n1].valid = 0;
        -:  727:	}
246548552:  728:	for(n1=0; n1<4; n1++){
        -:  729:		//if all ruled out return 0;
246393347:  730:		if(moves[n1].valid == 1)
 30005164:  731:			break;
216388183:  732:		if(n1 == 3)
 45593884:  733:			return 0;
        -:  734:	}
        -:  735:	//calculate ratings for valid destinations
150450854:  736:	for(n1=0; n1<4; n1++){
120336643:  737:		if(!moves[n1].valid)
 84313939:  738:			continue;
        -:  739:		//Moves_Populate(moves[n1].y, moves[n1].x, neighbors);
        -:  740:		//initialize neighbors for this potential move
 36022704:  741:		if(moves[n1].x != 0)
 36020376:  742:			neighbors[0].x = moves[n1].x-1;
        -:  743:		else
     2328:  744:			neighbors[0].x = width;
 36022704:  745:		neighbors[0].y = moves[n1].y;
 36022704:  746:		if(moves[n1].x != width)
 36020356:  747:			neighbors[1].x = moves[n1].x+1;
        -:  748:		else
     2348:  749:			neighbors[1].x = 0;
 36022704:  750:		neighbors[1].y = moves[n1].y;
 36022704:  751:		neighbors[2].x = moves[n1].x;
 36022704:  752:		if(moves[n1].y != 0)
 36022704:  753:			neighbors[2].y = moves[n1].y-1;
        -:  754:		else
    #####:  755:			neighbors[2].y = height;
 36022704:  756:		neighbors[3].x = moves[n1].x;
 36022704:  757:		if(moves[n1].y != height)
 36022704:  758:			neighbors[3].y = moves[n1].y+1;
        -:  759:		else
    #####:  760:			neighbors[3].y = 0;
        -:  761:		//calculate rating for this move threat/reward
        -:  762:			//if neighbor is empty terrain inc if climate is more favorable
        -:  763:			//ie: more toward the middle of temp_l - temp_h range than current position
        -:  764:
        -:  765:			/*
        -:  766:			if(!life[moves[n1].y][moves[n1].x]->alive){
        -:  767:				temp_actual = Location_Temp(src_y, src_x);
        -:  768:				temp_diff = Location_Temp(moves[n1].y, moves[n1].x) - temp_actual;
        -:  769:				temp_preferred = (float)(life[src_y][src_x]->temp_l + life[src_y][src_x]->temp_h)/2;
        -:  770:
        -:  771:				if(temp_diff < 0){ //am in a higher temp area
        -:  772:					//if lower temp is better than current location
        -:  773:					if( temp_preferred < temp_actual ){
        -:  774:						moves[n1].rating++;
        -:  775:						//continue;
        -:  776:					}
        -:  777:					//if(life[src_y][src_x]->temp_h
        -:  778:					//	moves[n1].rating++;
        -:  779:				}
        -:  780:				if(temp_diff > 0){ //am in a lower temp area
        -:  781:					//if higher temp is better
        -:  782:					if( temp_preferred > temp_actual ){
        -:  783:						moves[n1].rating++;
        -:  784:						//continue;
        -:  785:					}
        -:  786:				}
        -:  787:				//moves[n1].rating++;
        -:  788:				//continue;
        -:  789:			}
        -:  790:		*/
179887974:  791:        for(n2=0; n2<4; n2++){
143911428:  792:            if(neighbors[n2].y == src_y){  //if a neighbor is src_x/src_y don't count self
 56655989:  793:                if(neighbors[n2].x == src_x)
 35941870:  794:                    continue;
        -:  795:            }
        -:  796:			//if(!life[neighbors[n2].y][neighbors[n2].x]->alive){
107969558:  797:			if( !IS_SET(Alive, neighbors[n2].y, neighbors[n2].x) ){
 11431757:  798:				moves[n1].rating++;
 11431757:  799:				continue;
        -:  800:			}
        -:  801:			//if neighbor is different species dec rating
 96537801:  802:			if(!Is_Same_Species(life[src_y][src_x], life[neighbors[n2].y][neighbors[n2].x]) ){
   760516:  803:				moves[n1].rating--;
   760516:  804:				continue;
        -:  805:			}
        -:  806:			//if neighbor is same inc rating, or water
 95731127:  807:			moves[n1].rating++;
        -:  808:
        -:  809:			//unless already on water ...
        -:  810:			
        -:  811:		}
        -:  812:	}
 30114211:  813:	dest_cand.h_rating = -1;
        -:  814:	//find destination with highest rating
150504704:  815:	for(n1=0; n1<4; n1++){
120390493:  816:		if(!moves[n1].valid){
 84392052:  817:			moves[n1].rating = 0;
 84392052:  818:			continue;
        -:  819:		}
 35998441:  820:		if(	moves[n1].rating >= dest_cand.h_rating){
 35950942:  821:			dest_cand.h_rating = moves[n1].rating;
 35950942:  822:			dest_cand.m_index  = n1;
        -:  823:		} 
        -:  824:
        -:  825:	}
 30114211:  826:	n2=0;
        -:  827:	//if rating tie should pick a random choice out of them
        -:  828:	//count rating ties
150548697:  829:	for(n1=0; n1<4; n1++)
120434486:  830:		cand_ties[n1] = 0;
150521242:  831:	for(n1=0; n1<4; n1++){
120407031:  832:		if(!moves[n1].valid)
 84417366:  833:			continue;
 35989665:  834:		if(dest_cand.h_rating == moves[n1].rating){
 35881014:  835:			cand_ties[n1] = 1;
 35881014:  836:			n2++;
        -:  837:		}
        -:  838:	}
 30114211:  839:	if(n2){ //was no tie
 30114404:  840:		dest[0] = moves[dest_cand.m_index].y;
 30114404:  841:		dest[1] = moves[dest_cand.m_index].x;
        -:  842:	}
 30114211:  843:	if(n2 > 1){ //was a tie
        -:  844:		//n2 can be  2  3  or 4
  5275814:  845:		rand1 = lrand(&s) %  n2 + 1; // 1 to 2, 1 to 3, or 1 to 4
 18064687:  846:		for(n1=0; n1<4; n1++){
 15206644:  847:			if(!cand_ties[n1])
  6725898:  848:				continue;
        -:  849:			//n2++;
  8480746:  850:			if(n2 == rand1){
        -:  851:				//moves[n1] is dest choice
  2417490:  852:				dest[0] = moves[n1].y;
  2417490:  853:				dest[1] = moves[n1].x;
  2417490:  854:				break;
        -:  855:			}
  6063256:  856:			n2++;
        -:  857:		}
        -:  858:	}
        -:  859:
 30113930:  860:	if(stats_t.cur_sealevel < ia->dem[dest[0]][dest[1]]){
        -:  861:		//Move to destination
 30113930:  862:		SET_ONE(Alive, dest[0], dest[1]);
        -:  863:		//life[dest[0]][dest[1]]->alive = 1;
 30113930:  864:		(void)memcpy((void *)life[dest[0]][dest[1]], (const void *)life[src_y][src_x], sizeof(struct Species));
 30113930:  865:		if(life[dest[0]][dest[1]]->imunity < 255)
 29129408:  866:			life[dest[0]][dest[1]]->imunity++;
        -:  867:		//clear old data
 30113930:  868:		KILL_Tribe(src_y, src_x);
 30116634:  869:		return 1;
        -:  870:	}
    #####:  871:	return 0;
        -:  872:}
        -:  873:
 38155800:  874:int Fight_Tribe(struct Species *one, struct Species *two){
        -:  875:	//simulate outcome of a potential combat between these two tribes
        -:  876:	// return 1 *one won, 0 *one lost
        -:  877:	uint32_t rand1, rand2;
        -:  878:	struct timeval cur;
 38155800:  879:    gettimeofday(&cur, NULL);
 38195795:  880:    uint64_t s = (unsigned int)cur.tv_usec + 2;
        -:  881:
        -:  882:	//if(!one->alive || !two->alive)
        -:  883:	//	printf("Warning! Fight_Tribe called with non-alive tribe/s\n");
        -:  884:
        -:  885:	//fight determined by  1 part strength, 1 part speed, 2 parts intelligence, and 1 part chance
 38195795:  886:	rand1 = lrand(&s) % 256;
 38182504:  887:	rand2 = lrand(&s) % 256;
 38213199:  888:	uint32_t rating1 = one->intelligence*2 + one->strength + one->speed + rand1;
 38213199:  889:	uint32_t rating2 = two->intelligence*2 + two->strength + two->speed + rand2;
        -:  890:	//cur_age ?
        -:  891:	// 1 in 256 chance they are equal
 38213199:  892:	if(rating1 > rating2)
 11628935:  893:		return 1;
 26584264:  894:	if(rating2 > rating1)
 26424156:  895:		return 0;
        -:  896:	//if(rating1 == rating2)
   160108:  897:	return (int)lrand(&s) % 2;
        -:  898:	//equal
        -:  899:	//while(rand1 == rand2){
        -:  900:	//	rand1 = lrand(&s) % 2;
        -:  901:		//gettimeofday(&cur, NULL);
        -:  902:		//srand ( (unsigned int)cur.tv_usec+rand1 );
        -:  903:	//	rand2 = lrand(&s) % 2;
        -:  904:	//}
        -:  905:	//return rand1>rand2 ? 1 : 0;
        -:  906:}
        -:  907:
297018847:  908:void Tribe_Interactions(uint32_t src_y, uint32_t src_x, uint32_t tid){
        -:  909:	//evaluate all surrounding tribes for threat/reward take proper actions
        -:  910:	uint32_t n1, n2, n3;
        -:  911:	uint32_t rand1, rand2, rand3;
        -:  912:	int out_come;
        -:  913:	struct timeval cur;
        -:  914:	struct Neighbors{uint32_t y; uint32_t x; uint32_t occupied;}neighbors[4];
        -:  915:	struct Neighbors_T{uint32_t y; uint32_t x;}tmp[4];
297018847:  916:	uint32_t width = WIDTH - 1;
297018847:  917:	uint32_t height = HEIGHT - 1;
        -:  918:	//if(!life[src_y][src_x]->alive)
        -:  919:	//	printf("Warning Tribe_Interactions: Called for dead tribe!\n");
297018847:  920:    gettimeofday(&cur, NULL);
297468595:  921:    uint64_t s = (unsigned int)cur.tv_usec * 3 + 2;
        -:  922:
        -:  923:	//need to be randomly assigned, 24 possible permutations of the 4 structures
        -:  924:	//n×(n - 1)×(n - 2)×...×2×1
        -:  925:
        -:  926:	//for map wrap
297468595:  927:	tmp[0].y = src_y;
297468595:  928:	if(src_x != 0)
297450372:  929:		tmp[0].x = src_x - 1;
        -:  930:	else
    18223:  931:		tmp[0].x = width;
        -:  932:
297468595:  933:	tmp[1].y = src_y;
297468595:  934:	if(src_x != width)
297449416:  935:		tmp[1].x = src_x + 1;
        -:  936:	else
    19179:  937:		tmp[1].x = 0;
        -:  938:
297468595:  939:	if(src_y != 0)
297468595:  940:		tmp[2].y = src_y - 1;
        -:  941:	else
    #####:  942:		tmp[2].y = height;
297468595:  943:	tmp[2].x = src_x;
        -:  944:
297468595:  945:	if(src_y != height)
297468595:  946:		tmp[3].y = src_y + 1;
        -:  947:	else
    #####:  948:		tmp[3].y = 0;
297468595:  949:	tmp[3].x = src_x;
        -:  950:
        -:  951:	//mix them randomly
297468595:  952:	rand1 = lrand(&s) % 4;  //pick of any
299137977:  953:	rand2 = lrand(&s) % 3;  //pick of remaining 3
298883508:  954:	rand3 = lrand(&s) % 2;  //pick of remaining 2
        -:  955:
299102190:  956:	neighbors[0].y = tmp[rand1].y;
299102190:  957:	neighbors[0].x = tmp[rand1].x;
299102190:  958:	neighbors[0].occupied = 0;
299102190:  959:	n2 = 0;
750672282:  960:	for(n1=0; n1<4; n1++){ //pick second one randomly out of 3 remaining
749409956:  961:		if(n1 != rand1){ //if not already taken
596889426:  962:			if(n2 == rand2){
297839864:  963:				neighbors[1].y = tmp[n1].y;
297839864:  964:				neighbors[1].x = tmp[n1].x;
297839864:  965:				neighbors[1].occupied = 0;
297839864:  966:				break;
        -:  967:			}
299049562:  968:			n2++;
        -:  969:		}
        -:  970:	}
299102190:  971:	n3 = 0;
751655661:  972:	for(n2=0; n2<4; n2++){ //pick third one
749889289:  973:		if(n2 != rand1){
598816621:  974:			if(n2 != n1){
447262022:  975:				if(n3 == rand3){
297335818:  976:					neighbors[2].y = tmp[n2].y;
297335818:  977:					neighbors[2].x = tmp[n2].x;
297335818:  978:					neighbors[2].occupied = 0;
297335818:  979:					break;
        -:  980:				}
149926204:  981:				n3++;
        -:  982:			}
        -:  983:		}
        -:  984:	}
751229094:  985:	for(n3=0; n3<4; n3++){ //last one is the remaining 
749397624:  986:		if(n3 != rand1){
599092290:  987:			if(n3 != n1){
449521034:  988:				if(n3 != n2){
297270720:  989:					neighbors[3].y = tmp[n3].y;
297270720:  990:					neighbors[3].x = tmp[n3].x;
297270720:  991:					neighbors[3].occupied = 0;
297270720:  992:					break;
        -:  993:				}
        -:  994:			}
        -:  995:		}
        -:  996:	}
        -:  997:
1478767087:  998:	for(n1=0; n1<4; n1++){
        -:  999:		//if(life[neighbors[n1].y][neighbors[n1].x]->alive){
1179664897: 1000:		if( IS_SET(Alive, neighbors[n1].y, neighbors[n1].x) ){
987636839: 1001:			if(life[neighbors[n1].y][neighbors[n1].x]->water_turns == 0) //don't interact with water migraters
986087940: 1002:				neighbors[n1].occupied = 1;
        -: 1003:		}
        -: 1004:	}
        -: 1005:
1180965151: 1006:	for(n1=0; n1<4; n1++){
987880125: 1007:		if(neighbors[n1].occupied){
849311910: 1008:			if( Is_Same_Species(life[src_y][src_x], life[neighbors[n1].y][neighbors[n1].x]) ){
        -: 1009:				//check to see if we'll fight first !
        -: 1010:				//if(!fought[src_y][src_x]){
842169246: 1011:				if( !IS_SET(Fought, src_y, src_x) ){
623794887: 1012:					rand1 = (uint32_t)ceil((double)life[src_y][src_x]->interspecies_violence/256*100);
623794887: 1013:					if(lrand(&s) % 100 < rand1){
        -: 1014:
        -: 1015:					//rand1 = floor(INTERSPECIES_A_RANGE / life[src_y][src_x]->interspecies_violence);
        -: 1016:					//rand2 = lrand(&s) % rand1;
        -: 1017:					//if(lrand(&s) % rand1){
        -: 1018:					//rand2 = lrand(&s) % INTERSPECIES_A_RANGE;  // 1/16 at 127
        -: 1019:					//if(life[src_y][src_x]->interspecies_violence > rand2){//attacking
        -: 1020:						//fought[src_y][src_x] = 1;
        -: 1021:						//SET_ONE(Fought, src_y, src_x);
 37923302: 1022:						out_come = Fight_Tribe( life[src_y][src_x], life[neighbors[n1].y][neighbors[n1].x] );
 37945793: 1023:						if(out_come){//if the fight was won
 11623569: 1024:							SET_ONE(Fought, src_y, src_x);
 11623569: 1025:							KILL_Tribe(neighbors[n1].y, neighbors[n1].x);
 11628724: 1026:							stats[tid].died_defending++;
 11628724: 1027:							if(life[src_y][src_x]->imunity < 255)
 11229311: 1028:								life[src_y][src_x]->imunity++;
 11628724: 1029:							continue;
        -: 1030:						}
 26322224: 1031:						if(!out_come){ //died
 26324981: 1032:							SET_ONE(Fought, neighbors[n1].y, neighbors[n1].x);
 26324981: 1033:							KILL_Tribe(src_y, src_x);
 26331511: 1034:							stats[tid].died_attacking++;
 26331511: 1035:							if(life[neighbors[n1].y][neighbors[n1].x]->imunity < 255)
 25549654: 1036:								life[neighbors[n1].y][neighbors[n1].x]->imunity++;
 26331511: 1037:							return;
        -: 1038:						}
        -: 1039:					}
        -: 1040:				}
        -: 1041:				//if(mated[src_y][src_x])
        -: 1042:				//	continue;
        -: 1043:				//if(mated[neighbors[n1].y][neighbors[n1].x])
        -: 1044:				//	continue;
807342151: 1045:				if(IS_SET(Mated, src_y, src_x))
340123891: 1046:					continue;
467218260: 1047:				if(IS_SET(Mated, neighbors[n1].y, neighbors[n1].x))
111680044: 1048:					continue;
        -: 1049:
355538216: 1050:				rand1 = (uint32_t)ceil((double)life[src_y][src_x]->social_factor/256*100);
355538216: 1051:				if(lrand(&s) % 100 < rand1){
        -: 1052:				//rand2 = lrand(&s) % life[src_y][src_x]->social_factor + 1; // 1/2 at 127
        -: 1053:				//rand1 = lrand(&s) % INTERSPECIES_M_RANGE + 1;  //high chance
        -: 1054:				//if(rand2 > rand1){
        -: 1055:				//if(life[src_y][src_x]->social_factor > rand1){ //are mating
281594335: 1056:					out_come = 0;
        -: 1057:					//choose open space for offspring
1270815691: 1058:					for(n2=0; n2<4; n2++){
1041161677: 1059:						if(neighbors[n2].occupied == 0){
 56821349: 1060:							if(stats_t.cur_sealevel < ia->dem[neighbors[n2].y][neighbors[n2].x] ){
 51940321: 1061:								out_come = 1;
 51940321: 1062:								break;
        -: 1063:							}
        -: 1064:						}
        -: 1065:					}
        -: 1066:					//choose random open space if multiple
        -: 1067:					//if space available create life
281594335: 1068:					if(out_come){
        -: 1069:						//mated[src_y][src_x] = 1;
        -: 1070:						//mated[neighbors[n1].y][neighbors[n1].x] = 1;
        -: 1071:						//mated[neighbors[n2].y][neighbors[n2].x] = 1; //to keep it from mating/fighting during it's first year of life
 52096511: 1072:						SET_ONE(Mated, src_y, src_x);
 52096511: 1073:						SET_ONE(Mated, neighbors[n1].y, neighbors[n1].x);
 52096511: 1074:						SET_ONE(Mated, neighbors[n2].y, neighbors[n2].x);
        -: 1075:						//fought[neighbors[n2].y][neighbors[n2].x] = 1; //
 52096511: 1076:						SET_ONE(Fought, neighbors[n2].y, neighbors[n2].x);
        -: 1077:						//life[neighbors[n2].y][neighbors[n2].x]->alive = 1;
 52096511: 1078:						SET_ONE(Alive, neighbors[n2].y, neighbors[n2].x);
        -: 1079:
 52096511: 1080:						Create_Life(life[src_y][src_x], life[neighbors[n1].y][neighbors[n1].x], life[neighbors[n2].y][neighbors[n2].x]);
 52069839: 1081:						stats[tid].new_life++;
 52069839: 1082:						life_speed_up_Y[neighbors[n2].y] = 1;
 52069839: 1083:						life_speed_up_X[neighbors[n2].x] = 1;
 52069839: 1084:						if( !Is_Same_Species(life[src_y][src_x], life[neighbors[n2].y][neighbors[n2].x]) )
     2632: 1085:							stats[tid].new_species++;
 52052691: 1086:						if(life[src_y][src_x]->imunity < 255)
 50136557: 1087:							life[src_y][src_x]->imunity++;
 52052691: 1088:						if(life[neighbors[n1].y][neighbors[n1].x]->imunity < 255)
 50374902: 1089:							life[neighbors[n1].y][neighbors[n1].x]->imunity++;
        -: 1090:					}
        -: 1091:				}
        -: 1092:			}else{ 
        -: 1093:				//if(!fought[src_y][src_x]){ //havn't already fought
  3023646: 1094:				if( !IS_SET(Fought, src_y, src_x) ){
  2162754: 1095:					rand1 = (uint32_t)ceil((double)life[src_y][src_x]->fight_flight_balance/256*100);
  2162754: 1096:					if(lrand(&s) % 100 < rand1){
        -: 1097:					//rand2 = lrand(&s) % ENEMY_A_RANGE;
        -: 1098:					//if(life[src_y][src_x]->fight_flight_balance > rand2){//attacking
        -: 1099:						//fought[src_y][src_x] = 1;
        -: 1100:						//SET_TOGGLE(Fought, src_y, src_x);
   273424: 1101:						out_come = Fight_Tribe( life[src_y][src_x], life[neighbors[n1].y][neighbors[n1].x] );
   273425: 1102:						if(out_come){//fight was won
    84333: 1103:							SET_ONE(Fought, src_y, src_x);
    84333: 1104:							KILL_Tribe(neighbors[n1].y, neighbors[n1].x);
    84333: 1105:							stats[tid].died_defending++;
    84333: 1106:							if(life[src_y][src_x]->imunity < 255)
    81575: 1107:								life[src_y][src_x]->imunity++;
        -: 1108:						}
   273425: 1109:						if(!out_come){ //died
   189092: 1110:							SET_ONE(Fought, neighbors[n1].y, neighbors[n1].x);
   189092: 1111:							KILL_Tribe(src_y, src_x);
   189092: 1112:							stats[tid].died_attacking++;
   189092: 1113:							if(life[neighbors[n1].y][neighbors[n1].x]->imunity < 255)
   184207: 1114:								life[neighbors[n1].y][neighbors[n1].x]->imunity++;
   189092: 1115:							return;
        -: 1116:						}
        -: 1117:					}
        -: 1118:				}
        -: 1119:			}
        -: 1120:		}
        -: 1121:		//if(fought[src_y][src_x]){
        -: 1122:		//	if(mated[src_y][src_x]){
        -: 1123:		//		return;
        -: 1124:		//	}
        -: 1125:		//}
497889064: 1126:		if( IS_SET(Fought, src_y, src_x) ){
115730488: 1127:			if( IS_SET(Mated, src_y, src_x) ){
 79458762: 1128:				return;
        -: 1129:			}
        -: 1130:		}
        -: 1131:	}
        -: 1132:}
        -: 1133:
        -: 1134:/*
        -: 1135:void FM_Alloc(void){
        -: 1136:	uint32_t h;
        -: 1137:	fought = (char **)malloc(sizeof(char *)*HEIGHT);
        -: 1138:	for(h=0; h<HEIGHT; h++)
        -: 1139:		fought[h] = (char *)malloc(sizeof(char)*WIDTH);
        -: 1140:	mated = (char **)malloc(sizeof(char *)*HEIGHT);
        -: 1141:	for(h=0; h<HEIGHT; h++)
        -: 1142:		mated[h] = (char *)malloc(sizeof(char)*WIDTH);
        -: 1143:}
        -: 1144:*/
        -: 1145:
      947: 1146:void FM_Reset(uint32_t start, uint32_t end){
        -: 1147:	uint32_t h;
   282422: 1148:	for(h=start; h<end; h++){
   281475: 1149:		(void)memset( (void *)Fought[h], '\0', sizeof(unsigned char)*(size_t)ceil(WIDTH/8));
   281475: 1150:		(void)memset( (void *)Mated[h], '\0', sizeof(unsigned char)*(size_t)ceil(WIDTH/8));
        -: 1151:	}
      947: 1152:}
        -: 1153:
        4: 1154:void *Tribe_Actions(void *tid){
        -: 1155:	//master thread
        -: 1156:	//initiate actions migrate/combat/desease/create_life/migrate/socialize
        4: 1157:	uint32_t t = *(unsigned int *)tid;
        4: 1158:	uint32_t y = 0;
        4: 1159:	uint32_t x = 0;
        4: 1160:	uint32_t x_s = 0;
        -: 1161:	//uint32_t x_h = 0;
        4: 1162:	int right_to_left = 0;
        -: 1163:	//int dec = 0;
        4: 1164:	int stop = 0;
        4: 1165:	int first_member = 0;
        4: 1166:	uint32_t dest[] = {0, 0}; //y x
        4: 1167:	char did_move = 0;
        -: 1168:	//struct timeval starttime,endtime;
        -: 1169:	struct timeval cur;
        4: 1170:    uint32_t rand1 = 0;
        4: 1171:	uint32_t rand2 = 0;
        -: 1172:	//int cur_sealevel = ia->cur_sealevel;
        4: 1173:    gettimeofday(&cur, NULL);
        -: 1174:	//printf("%u\n", Thread_Step);
        4: 1175:    uint64_t s = (unsigned int)cur.tv_usec * 3 + 3;
        -: 1176:	//char *life_speed_up_H = (char *)malloc(Thread_Step);
        -: 1177:	//char life_speed_up_W[WIDTH]; //= (char *)malloc(WIDTH);
        4: 1178:	int row_half = 0;
        4: 1179:	int row_ceiling = 0;
        -: 1180:	//double year_runtime = 0;
        -: 1181:
        -: 1182:	//do first half of rows
        -: 1183:
        -: 1184:	//sychronize threads
        -: 1185:
        -: 1186:	//second half
        -: 1187:
        -: 1188:	//sychronize threads
        -: 1189:
        -: 1190:	//stats / image handled
        -: 1191:
        -: 1192:	//reset arrays as necesary
        -: 1193:
        -: 1194:	//check for exit signals and clean up
        -: 1195:
        -: 1196:	//(void)memset( (void *)life_speed_up_H, '\0', sizeof(char)*HEIGHT );
        -: 1197:	//(void)memset( (void *)&life_speed_up_W, '\0', sizeof(char)*WIDTH );
        -: 1198:	//printf("%u Here !\n", t);
        -: 1199:	//sleep(5);
        4: 1200:	if(t == 0)
        1: 1201:		gettimeofday(&Year_Starttime, NULL);
        -: 1202:
      960: 1203:	while(No_Exit == 1){
        -: 1204:		//(void)memset( (void *)life_speed_up_H, '\0', sizeof(char)*HEIGHT );
        -: 1205:		//(void)memset( (void *)life_speed_up_W, '\0', sizeof(char)*WIDTH );
     2826: 1206:		for(row_half=0; row_half<2; row_half++){
        -: 1207:
        -: 1208:		//row_half = (row_half) ? 0 : 1;
     1884: 1209:			if(row_half){
      941: 1210:				y = Actions_t_info[t].start_h;
      941: 1211:				row_ceiling = y + Thread_Step/2;
        -: 1212:			}else{
      943: 1213:				y = Actions_t_info[t].start_h + Thread_Step/2;
      943: 1214:				row_ceiling = Actions_t_info[t].end_h;
        -: 1215:			}
   430081: 1216:			for(; y < row_ceiling; y++){
   428177: 1217:				if(!life_speed_up_Y[y])
 -1173358: 1218:					continue;
        -: 1219:					//randomly choose to start from right or left of row
  1601535: 1220:					rand1 = lrand(&s) % 2;
        -: 1221:					//rand2 = lrand(&s) % 511;
   321968: 1222:				if( rand1 ){
   161255: 1223:					right_to_left = 0;
   161255: 1224:					x_s = WIDTH - 1;
   161255: 1225:					x = 0;
        -: 1226:				}else{
   160713: 1227:					right_to_left = 1;
   160713: 1228:					x_s = 0;
   160713: 1229:					x = WIDTH - 1;
        -: 1230:				}
   321968: 1231:				stop = 0;
   321968: 1232:				first_member = 1;
1126835957: 1233:				while(!stop){
        -: 1234:					//if(stop)
        -: 1235:					//	break;
1124912434: 1236:					if(right_to_left){
549786626: 1237:						if(!first_member)
549625913: 1238:							x--;
        -: 1239:						else
   160713: 1240:							first_member = 0;
        -: 1241:					}else{
575125808: 1242:						if(!first_member)
574964534: 1243:							x++;
        -: 1244:						else
   161274: 1245:							first_member = 0;
        -: 1246:					}
1124912434: 1247:					if(x == x_s)
   321991: 1248:						stop = 1;
1124912434: 1249:					if(!life_speed_up_X[x])
 50554855: 1250:						continue;
        -: 1251:					//if(life[y][x]->alive){ //if there is life at this location
        -: 1252://printf("here 1\n");
1074357579: 1253:					if( IS_SET(Alive, y, x) ){
        -: 1254://printf("here 2\n");
        -: 1255:						//get one year older
312274541: 1256:						life[y][x]->cur_age++;
        -: 1257:						//die from old age?  // 1/36 chance for initial tribes
312274541: 1258:						if(life[y][x]->cur_age == life[y][x]->max_age ){
   287231: 1259:							KILL_Tribe(y, x);
   287230: 1260:							stats[t].old_age++;
   287230: 1261:							continue;
        -: 1262:						}
        -: 1263://printf("here 3\n");
311987310: 1264:						rand1 = (uint32_t)ceil((double)life[y][x]->imunity/256*100);
311987310: 1265:						rand2 = lrand(&s) % 100;
313170317: 1266:						if(rand2 > rand1){
        -: 1267:						//rand2 = lrand(&s) % IMUNITY_RANGE; // 1/16 at 127   1/32 at 255
        -: 1268:						//if(life[y][x]->imunity > rand2){
        -: 1269:							//printf("year: %u rand1: %d rand2: %d\n", stats_t.year, rand1, rand2);
 11002666: 1270:							KILL_Tribe(y, x);
 11003563: 1271:							stats[t].disease++;
 11003563: 1272:							continue;
        -: 1273:						}
        -: 1274://printf("here 4\n");
302167651: 1275:						if(life[y][x]->water_turns == 0){
        -: 1276://printf("here \n");
300602047: 1277:							if( Tribe_Borders_Water(y, x) ){
        -: 1278://printf("here 4\n");
        -: 1279:								//rand1 = ceil(life[src_y][src_x]->imunity/256*100);
  6132603: 1280:								rand1 = (uint32_t)ceil((double)WATER_MIGRATION_RANGE/256*100);
        -: 1281:								//rand1 = lrand(&s) % WATER_MIGRATION_RANGE;
  6132603: 1282:								if(lrand(&s) % 100 < rand1){
   796183: 1283:									if( !Tribe_Move_To_Water(y, x, dest) ){ //if found Open water
   779897: 1284:										if( Climate_Fatal(life[dest[0]][dest[1]], dest[0], dest[1]) ){
    #####: 1285:											KILL_Tribe(dest[0], dest[1]);
    #####: 1286:											stats[t].exposure++;
    #####: 1287:											continue;
        -: 1288:										}
        -: 1289:										//moved
   779901: 1290:										life_speed_up_Y[dest[0]] = 1;
   779901: 1291:										life_speed_up_X[dest[1]] = 1;
        -: 1292:										//no interactions for water migraters
   779901: 1293:										continue;
        -: 1294:									}
        -: 1295:									//did not move 
    16287: 1296:									if(stats_t.cur_sealevel >= ia->dem[y][x]){ //drown
        2: 1297:										KILL_Tribe(y, x);
        2: 1298:										stats[t].drown++;
        2: 1299:										continue;
        -: 1300:									}
    16285: 1301:									if( Climate_Fatal(life[y][x], y, x) ){
      676: 1302:										KILL_Tribe(y, x);
      676: 1303:										stats[t].exposure++;
      676: 1304:										continue;
        -: 1305:									}
    15609: 1306:									life_speed_up_Y[y] = 1;
    15609: 1307:									life_speed_up_X[x] = 1;
    15609: 1308:									Tribe_Interactions(y, x, t);
    15609: 1309:									continue;
        -: 1310:								}
        -: 1311:							}
        -: 1312:							//continue;
        -: 1313:						}
        -: 1314:
303404414: 1315:						if(life[y][x]->water_turns == 0){ //if tribe is not water migrating
301348212: 1316:							rand1 = (uint32_t)ceil((double)life[y][x]->migration_factor/256*100);
        -: 1317:							//rand2 = lrand(&s) % MIGRATION_RANGE;  // 1/4 at 127 
        -: 1318:							//if(life[y][x]->migration_factor > rand2){  //tribe will attempt to migrate
301348212: 1319:							if(lrand(&s) % 100 < rand1){
 75604619: 1320:								did_move = Tribe_Migrate(y, x, dest);
        -: 1321:
 75528745: 1322:								if(did_move == 0){ //couldn't find a spot, try to water migrate
 45613463: 1323:									rand1 = (uint32_t)ceil((double)WATER_MIGRATION_RANGE/256*100);
 45613463: 1324:									if(lrand(&s) % 100 < rand1){
  5925025: 1325:										if( Tribe_Borders_Water(y, x) ){
        -: 1326:										//rand2 = lrand(&s) % WATER_MIGRATION_RANGE; //1/16 at 256
        -: 1327:										//if(rand2 < 16){
        -: 1328:											//if( Tribe_Move_To_Water(y, x, dest) ){
        -: 1329:												//return 1 if no Open water
        -: 1330:												//that means stayed in same place, check for drowning 
        -: 1331:											//	if(stats_t.cur_sealevel >= ia->dem[dest[0]][dest[1]]){ //drown
        -: 1332:											//		KILL_Tribe(dest[0], [dest[1]);
        -: 1333:											//		stats[t].drown++;
        -: 1334:													//stats[t].drown++;
        -: 1335:											//		continue;
        -: 1336:											//	}
        -: 1337:											//	Tribe_Interactions(dest[0], dest[1], t);
        -: 1338:											//	continue;
        -: 1339:											//}
   107042: 1340:											if( !Tribe_Move_To_Water(y, x, dest) ){ //if found Open water
   104652: 1341:												if( Climate_Fatal(life[dest[0]][dest[1]], dest[0], dest[1]) ){
    #####: 1342:													KILL_Tribe(dest[0], dest[1]);
    #####: 1343:													stats[t].exposure++;
    #####: 1344:													continue;
        -: 1345:												}
   104652: 1346:												life_speed_up_Y[dest[0]] = 1;
   104652: 1347:												life_speed_up_X[dest[1]] = 1;
   104652: 1348:												continue;
        -: 1349:												//no interactions for water migraters
        -: 1350:											}
        -: 1351:
     2390: 1352:											did_move = 0; //didn't move
        -: 1353:										//}
        -: 1354:										}
        -: 1355:									}
        -: 1356:								}
        -: 1357:
        -: 1358:							}
        -: 1359:							//else{
        -: 1360:							
        -: 1361:								//this tribe won't/can't migrate regardless 
300992177: 1362:							if(stats_t.cur_sealevel >= ia->dem[y][x]){ //drown
      906: 1363:								KILL_Tribe(y, x);
      906: 1364:								stats[t].drown++;
      906: 1365:								continue;
        -: 1366:							}
        -: 1367:							//	did_move = 0; //didn't move lived
        -: 1368:							//}
300991271: 1369:							if(did_move){
119838309: 1370:								if( Climate_Fatal(life[dest[0]][dest[1]], dest[0], dest[1]) ){
     3983: 1371:									KILL_Tribe(dest[0], dest[1]);
     3983: 1372:									stats[t].exposure++;
     3983: 1373:									continue;
        -: 1374:								}
120053949: 1375:								Tribe_Interactions(dest[0], dest[1], t);
        -: 1376:							}else{
181152962: 1377:								if( Climate_Fatal(life[y][x], y, x) ){
  2252692: 1378:									KILL_Tribe(y, x);
  2252717: 1379:									stats[t].exposure++;
  2252717: 1380:									continue;
        -: 1381:								}
        -: 1382:								//if(life[y][x]->water_turns == 0)
179355409: 1383:								Tribe_Interactions(y, x, t);
        -: 1384:							}
297048609: 1385:							continue;
        -: 1386:						}
  2056202: 1387:						did_move = Tribe_Water_Migrate(y, x, dest); //1 if found land 
  2056292: 1388:						life_speed_up_Y[dest[0]] = 1;
  2056292: 1389:						life_speed_up_X[dest[1]] = 1;
  2056292: 1390:						if(!did_move){
  1388501: 1391:							if(life[dest[0]][dest[1]]->water_turns == WATER_MIGRATION_T_MAX){
    53924: 1392:								KILL_Tribe(dest[0], dest[1]);
    53924: 1393:								stats[t].drown++;
    53924: 1394:								continue;
        -: 1395:							}
        -: 1396:						}
  2002368: 1397:						if( Climate_Fatal(life[dest[0]][dest[1]], dest[0], dest[1]) ){
    33464: 1398:							KILL_Tribe(dest[0], dest[1]);
    33464: 1399:							stats[t].exposure++;
        -: 1400:						}
        -: 1401:						
        -: 1402:						//if( Climate_Fatal(life[y][x], y, x) ){
        -: 1403:						//	KILL_Tribe(y, x);
        -: 1404:						//	stats[t].exposure++;
        -: 1405:						//}
        -: 1406:
        -: 1407:						//}else{
        -: 1408:						//	rand2 = lrand(&s) % WATER_MIGRATION_RANGE;
        -: 1409:						//	if(rand2 < 16){
        -: 1410:						//did_move = Tribe_Water_Migrate(y, x, dest);
        -: 1411:						
        -: 1412:
        -: 1413:					}//end of if life != NULL
        -: 1414:				}//end of X loop
        -: 1415:			}//end of Y loop
        -: 1416:			//thread_i[t].row_synch = 1;
        -: 1417:			//synchronize threads
        -: 1418:			//pthread_cond_signal( &condition_var );
        -: 1419:			//pthread_cond_wait( &Condition1, &Mutex1 );
        -: 1420:
     1904: 1421:			pthread_barrier_wait(&Barrier1);  //half year barried to stop overlaps
        -: 1422:		}//end of year
        -: 1423:
      942: 1424:		FM_Reset(Actions_t_info[t].start_h, Actions_t_info[t].end_h);
        -: 1425:		
        -: 1426:		//Actions_t_info[t].seed
        -: 1427:
        -: 1428:		//for(y=Actions_t_info[t].start_h; y<Actions_t_info[t].end_h; y++){
        -: 1429:		//	for(x=0; x<WIDTH; x++){
        -: 1430:		//		if(life[y][x]->alive){
        -: 1431:		//			stats[t].total_living++;
        -: 1432:		//		}
        -: 1433:		//	}
        -: 1434:		//}
        -: 1435:		//pthread_barrier_wait(&Barrier2);  //barrier for ^
        -: 1436:
        -: 1437:		//first thread to reach it
        -: 1438:		//if(Stats_Handled){
      947: 1439:		pthread_mutex_lock( &Mutex1 );
        -: 1440:		//Stats_Handled++;
      952: 1441:		if(!Stats_Handled){
        -: 1442:			//pthread_mutex_lock( &mutex1 );
      238: 1443:			Stats_Handled = 1;
      238: 1444:			stats_t.year++;
        -: 1445:			//Drawl_Image();
        -: 1446:			//FM_Reset();
        -: 1447:			//Adjust_Sea_Level(0);
        -: 1448:			//Adjust_Climate(0);
      238: 1449:			gettimeofday(&Year_Endtime, NULL);
      238: 1450:			Year_Runtime += ((double)(Year_Endtime.tv_sec*1000000-Year_Starttime.tv_sec*1000000+Year_Endtime.tv_usec-Year_Starttime.tv_usec))/1000000;
        -: 1451:			//stats_t.total_runtime += time1;
      238: 1452:			if(stats_t.year % CLIMATE_YEARS == 0){
        1: 1453:				Adjust_Sea_Level(0);
        1: 1454:				Adjust_Climate(0);
        -: 1455:			}
      238: 1456:			if(stats_t.year % Backup_Freq == 0){
        -: 1457:				//Adjust_Sea_Level(0);
        -: 1458:				//Adjust_Climate(0);
        1: 1459:				Backup_Freq_Runtime += Year_Runtime;
        -: 1460:				//Year_Runtime = 0;
        1: 1461:				Gen_Detailed_Counts();
        1: 1462:				pthread_mutex_unlock( &Mutex3 );
        1: 1463:				No_Exit = !Save_Current_State();  //cause an exit if failed
        1: 1464:				pthread_mutex_unlock( &Mutex3 );
      237: 1465:			}else if(No_Exit == 0){  //save and exit
        1: 1466:				Backup_Freq_Runtime += Year_Runtime;
        1: 1467:				Gen_Detailed_Counts();
        1: 1468:				(void)Save_Current_State();  //will still exit gracefully
        -: 1469:			}
        -: 1470:			//No_Exit = 2 exit no save
      238: 1471:			gettimeofday(&Year_Starttime, NULL);
        -: 1472:			//if(stats_t.sea_rising){
        -: 1473:			//	printf("Current: Sec/Year %fs cur_sealevel %dm rising +1m/1Kyears", (float)time1/1000, ia->cur_sealevel);
        -: 1474:			//}else{
        -: 1475:			//}
        -: 1476:			//Stats_Handled = 1;
        -: 1477:			//pthread_mutex_unlock( &mutex1 );
        -: 1478:		}
        -: 1479:		//Stats_Handled++;
      952: 1480:		pthread_mutex_unlock( &Mutex1 );
        -: 1481:
      952: 1482:		pthread_barrier_wait(&Barrier2); //barrier for full year
        -: 1483:
      945: 1484:		pthread_mutex_lock( &Mutex2 );
      952: 1485:		Stats_Handled = 0;
      952: 1486:		pthread_mutex_unlock( &Mutex2 );
        -: 1487:
        -: 1488:	}//main thread loop
        -: 1489:
        -: 1490:	//free(life_speed_up_H);
        -: 1491:	//free(life_speed_up_W);
        4: 1492:	pthread_exit(NULL);
        -: 1493:}
        -: 1494:
        -: 1495:
        -: 1496:
       14: 1497:void Drawl_Image(void){
        -: 1498:	uint32_t n1, n2;
        -: 1499:	unsigned char rgb[3];
    25214: 1500:	for(n1=0; n1<HEIGHT; n1++){
 90745200: 1501:		for(n2=0; n2<WIDTH; n2++){
        -: 1502:			//if(ia->dem[n1][n2] > stats_t.cur_sealevel){
        -: 1503:			//if(!life[n1][n2]->alive){
 90720000: 1504:			if( !IS_SET(Alive, n1, n2) ){
 72842492: 1505:				if(ia->dem[n1][n2] > stats_t.cur_sealevel){
 16417863: 1506:					rgb[0] = 0;
 16417863: 1507:					rgb[1] = 255;
 16417863: 1508:					rgb[2] = 0;
        -: 1509:				}else{
 56424629: 1510:					rgb[0] = 0;
 56424629: 1511:					rgb[1] = 0;
 56424629: 1512:					rgb[2] = 255;
        -: 1513:				}
        -: 1514:			}else{
 17877508: 1515:				Gen_Tribe_Color(life[n1][n2], rgb);
        -: 1516:			}
        -: 1517:			//}else{
        -: 1518:			//	rgb[0] = 0;
        -: 1519:			//	rgb[1] = 0;
        -: 1520:			//	rgb[2] = 255;
        -: 1521:			//}
 90720000: 1522:			ia->image[n1*WIDTH*3+(n2*3)]   = rgb[0];
 90720000: 1523:			ia->image[n1*WIDTH*3+(n2*3+1)] = rgb[1];
 90720000: 1524:			ia->image[n1*WIDTH*3+(n2*3+2)] = rgb[2];
        -: 1525:		}
        -: 1526:	}
       14: 1527:}/*EOF*/
