void launch_new_game(t_box *box) { box->is_launch = 1; create_grid(box); put_food(box); mlx_put_image_to_window(box->mlx, box->win, box->img, 0, 0); return ; }
void do_organic_farm (int x, int y) { /* // int_1 is the tech level of the farm when built // int_2 is a flag so we don't create a farm with nearly ripe crops. // int_3 is the food sold count so far this year. // int_4 is the food made last year. // int_5 is the random crop rotation key. // int_6 is the random month stagger, so they don't all flash at once // int_7 is the tech-level dependent output of a powered farm with a full // workforce. */ int i; if (MP_INFO(x,y).int_5 == 0) /* this should be done when we create */ { /* the area! */ MP_INFO(x,y).int_5 = (rand () % 4) + 1; MP_INFO(x,y).int_6 = rand () % 300; } MP_INFO(x,y).flags &= (0xffffffff - FLAG_POWERED); if (get_jobs (x, y, 1) == 0) put_food (x, y, 30); else if (get_jobs (x, y, FARM_JOBS_USED) != 0) { if (get_power (x, y, ORG_FARM_POWER_REC, 0) != 0) { if (put_food (x, y, (ORGANIC_FARM_FOOD_OUTPUT + MP_INFO(x,y).int_7)) == 0) put_jobs (x, y, FARM_JOBS_USED); else MP_INFO(x,y).int_3++; MP_INFO(x,y).flags |= FLAG_POWERED; } else { if (put_food (x, y, (ORGANIC_FARM_FOOD_OUTPUT / 4)) == 0) put_jobs (x, y, FARM_JOBS_USED); else MP_INFO(x,y).int_3++; } } else if (get_jobs (x, y, FARM_JOBS_USED / 4) != 0) { if (get_power (x, y, ORG_FARM_POWER_REC, 0) != 0) { if (put_food (x, y, (ORGANIC_FARM_FOOD_OUTPUT + (MP_INFO(x,y).int_7 / 4))) == 0) put_jobs (x, y, FARM_JOBS_USED / 4); else MP_INFO(x,y).int_3++; MP_INFO(x,y).flags |= FLAG_POWERED; } else { if (put_food (x, y, (ORGANIC_FARM_FOOD_OUTPUT / (4 * 4))) == 0) put_jobs (x, y, FARM_JOBS_USED / 4); else MP_INFO(x,y).int_3++; } } else { if (get_power (x, y, ORG_FARM_POWER_REC, 0) != 0) { if (put_food (x, y, (ORGANIC_FARM_FOOD_OUTPUT + (MP_INFO(x,y).int_7 / 8))) != 0) MP_INFO(x,y).int_3++; MP_INFO(x,y).flags |= FLAG_POWERED; } else if (put_food (x, y, 30 + (ORGANIC_FARM_FOOD_OUTPUT / (4 * 8))) != 0) MP_INFO(x,y).int_3++; } if ((total_time & 0x7f) == 0) if ((MP_INFO(x,y).flags & FLAG_POWERED) != 0) get_waste (x, y, 0x80 * ORG_FARM_WASTE_GET); if ((total_time % 1200) == 0) { MP_INFO(x,y).int_4 = MP_INFO(x,y).int_3; MP_INFO(x,y).int_3 = 0; } i = ((total_time + (MP_INFO(x,y).int_5 * 1200) + MP_INFO(x,y).int_6) % 4800); if (i % 300 == 0) { i /= 300; if ( /* MP_INFO(x,y).int_2!=0 && */ MP_INFO(x,y).int_4 > MIN_FOOD_SOLD_FOR_ANIM) { if (i % 4 == 0) { MP_INFO(x,y).int_6 = rand () % 100; } switch (i) { case (0): MP_TYPE(x,y) = CST_FARM_O3; break; case (1): MP_TYPE(x,y) = CST_FARM_O3; break; case (2): MP_TYPE(x,y) = CST_FARM_O3; break; case (3): MP_TYPE(x,y) = CST_FARM_O3; break; case (4): MP_TYPE(x,y) = CST_FARM_O7; break; case (5): MP_TYPE(x,y) = CST_FARM_O7; break; case (6): MP_TYPE(x,y) = CST_FARM_O7; break; case (7): MP_TYPE(x,y) = CST_FARM_O7; break; case (8): MP_TYPE(x,y) = CST_FARM_O11; break; case (9): MP_TYPE(x,y) = CST_FARM_O11; break; case (10): MP_TYPE(x,y) = CST_FARM_O11; break; case (11): MP_TYPE(x,y) = CST_FARM_O11; break; case (12): MP_TYPE(x,y) = CST_FARM_O15; break; case (13): MP_TYPE(x,y) = CST_FARM_O15; break; case (14): MP_TYPE(x,y) = CST_FARM_O15; break; case (15): MP_TYPE(x,y) = CST_FARM_O15; break; } } else { MP_TYPE(x,y) = CST_FARM_O0; } } }
void change_dir() { // Can eat? if (food.x == snake[snake_len-1].x && food.y == snake[snake_len-1].y) { snake[snake_len].x = food.x; snake[snake_len].y = food.y; if (++snake_len == MAX_LEN) snake_len = 0; screen[food.x][food.y] = SNAKE; score += 10; put_food(); } switch(direction) { case UP: { // Check for screen edge if (snake[snake_len-1].x == 0) { snake[snake_len].x = 5; snake[snake_len].y = snake[snake_len-1].y; } else { snake[snake_len].x = snake[snake_len-1].x-1; snake[snake_len].y = snake[snake_len-1].y; } break; } case RIGHT: { // Check for screen edge if (snake[snake_len-1].y == WIDTH-1) { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = 0; } else { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = snake[snake_len-1].y+1; } break; } case DOWN: { // Check for screen edge if (snake[snake_len-1].x == HEIGHT-1) { snake[snake_len].x = 0; snake[snake_len].y = snake[snake_len-1].y; } else { snake[snake_len].x = snake[snake_len-1].x+1; snake[snake_len].y = snake[snake_len-1].y; } break; } case LEFT: { // Check for screen edge if (snake[snake_len-1].y == 0) { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = WIDTH-1; } else { snake[snake_len].x = snake[snake_len-1].x; snake[snake_len].y = snake[snake_len-1].y-1; } break; } } // Wrong bite if (screen[snake[snake_len].x][snake[snake_len].y] == SNAKE) { // Score to string and screen char ss[] = "Points: "; char s[10]; dtostrf(score, 3, 0, s); strcat(ss,s); LCD_clear(); LCD_write_string(0,0," GAME OVER "); LCD_write_string(0,2,ss); // End game game = 0; return; } // Mark slice as inactive snake[snake_tail].x = -1; // Keep inside array limit if (++snake_tail == MAX_LEN) snake_tail = 0; if (++snake_len == MAX_LEN) snake_len = 0; update_snake(); }
int main(void) { double axg = 0, ayg = 0, azg = 0; double gxds = 0, gyds = 0, gzds = 0; //init mpu6050 mpu6050_init(); _delay_ms(50); LCD_init(); LCD_clear(); put_food(); // INtro while (1) { // Read current parameters mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds); LCD_write_string(0,0," Little Snake "); LCD_write_string(0,3," >> Move << "); if (axg > LIMIT || axg < -LIMIT ||ayg < -LIMIT || ayg > LIMIT) break; } // Init snake, both on screen and array screen[3][5] = SNAKE; screen[3][6] = SNAKE; screen[3][7] = SNAKE; snake[0].x = 3; snake[0].y = 5; snake[1].x = 3; snake[1].y = 6; snake[2].x = 3; snake[2].y = 7; snake_tail = 0; snake_len = 3; for (int z=snake_len; z<MAX_LEN; z++) snake[z].x = -1; while(game) { // Read current parameters mpu6050_getConvData(&axg, &ayg, &azg, &gxds, &gyds, &gzds); // Take a decision if(axg > LIMIT) {if (direction != UP) direction = DOWN;} else if(axg < -LIMIT) {if (direction != DOWN) direction = UP;} if(ayg > LIMIT) {if (direction != LEFT) direction = RIGHT;} else if(ayg < -LIMIT) {if (direction != RIGHT) direction = LEFT;} // Make it happen change_dir(); // Check end game if (!game) break; LCD_init(); LCD_clear(); // Print entire screen for (int x=0; x<HEIGHT; x++) { for (int y=0; y<WIDTH; y++) { if (x == snake[snake_len-1].x && y == snake[snake_len-1].y) { LCD_write_char(HEAD); } else if (screen[x][y] == SNAKE) { LCD_write_char(SNAKE); } else if (screen[x][y] == FOOD) { LCD_write_char(FOOD); } else { LCD_write_char(SPACE); } } } _delay_ms(250); } }
void do_organic_farm(int x, int y) { /* // MP_INFO(x,y) // int_1 unused // int_2 unused // int_3 is the food sold count so far this year. // int_4 is the food made last year. // int_5 is the random crop rotation key. // int_6 is the random month stagger, so they don't all flash at once // int_7 is the jobs stored at the farm * * MP_INFO(x+1,y) stores additional info * int_1 reserved (=x) * int_2 reserved (=y) * int_3 max possible production (assuming 100% water and power) * int_4 number of 1x1 tiles with underground water inside the farm * int_5 current production * // MP_TECH is the tech level of the farm when built // MP_ANIM FIXME, this is unused */ int i; int has_power = false; int used_jobs = 0; int tech_bonus = (int)(((double)MP_TECH(x, y) * ORGANIC_FARM_FOOD_OUTPUT) / MAX_TECH_LEVEL); MP_INFO(x + 1, y).int_3 = ORGANIC_FARM_FOOD_OUTPUT + tech_bonus; /* Animation */ if (MP_INFO(x, y).int_5 == 0) { /* this should be done when we create the area! */ MP_INFO(x, y).int_5 = (rand() % 4) + 1; MP_INFO(x, y).int_6 = rand() % 300; /* AL1 will be sooner or later redefined as %100. see below */ } /* check jobs */ if (MP_INFO(x, y).int_7 < FARM_JOBS_USED) { if (get_jobs(x, y, FARM_JOBS_USED) != 0) MP_INFO(x, y).int_7 += FARM_JOBS_USED; /* adding if (get_jobs ... /2) would allow to have some jobs stored at farm, * so would smooth the behavior and make farms more resistant to job penury. * Currently keep previous behavior. */ else if (get_jobs(x, y, FARM_JOBS_USED / 4) != 0) MP_INFO(x, y).int_7 += FARM_JOBS_USED / 4; else if (get_jobs(x, y, 1) != 0) MP_INFO(x, y).int_7 += 1; } /* check power */ MP_INFO(x, y).flags &= (0xffffffff - FLAG_POWERED); if (MP_INFO(x, y).int_7 >= 1) { /* There are jobs to do some production, so check for power */ if (get_power(x, y, ORG_FARM_POWER_REC, 0) != 0) { MP_INFO(x, y).flags |= FLAG_POWERED; has_power = true; } } /* Produce some food */ int prod = 0; if (MP_INFO(x, y).int_7 >= FARM_JOBS_USED) { used_jobs = FARM_JOBS_USED; if (has_power) { prod = ORGANIC_FARM_FOOD_OUTPUT + tech_bonus; } else { prod = ORGANIC_FARM_FOOD_OUTPUT / 4; } } else if (MP_INFO(x, y).int_7 >= FARM_JOBS_USED / 4) { used_jobs = FARM_JOBS_USED / 4; if (has_power) { prod = ORGANIC_FARM_FOOD_OUTPUT + tech_bonus / 4; } else { prod = ORGANIC_FARM_FOOD_OUTPUT / (4 * 4); } } else if (MP_INFO(x, y).int_7 >= 1) { /* got 1 job */ used_jobs = 1; if (has_power) { prod = ORGANIC_FARM_FOOD_OUTPUT + tech_bonus / 8; } else { /* AL1 "small ouch": * without power output with 1 job is bigger than output with 3 ! * 3 = FARMS_JOBS_USED / 4 * ORGANIC_FARM_FOOD_OUTPUT = 550 currently (ng_1.1) */ prod = 30 + ORGANIC_FARM_FOOD_OUTPUT / (4 * 8); } } else { /* AL1 : the farm gives very small amount of food without job. * ? Probably needed for start ? * ? Useful to prevent starvation when no jobs ? * The various buildings are "done" in random order, * so it should be ok without this. */ put_food(x, y, 30); /* note that this does not generate revenu int_3) */ } /* Check underground water, and reduce production accordingly */ if (use_waterwell) { // TODO No need to count each time. Should be done at build time, and stored int w = 0; int n = 0; for (int i = 0; i < MP_SIZE(x, y); i++) { for (int j = 0; j < MP_SIZE(x, y); j++) { n++; if (HAS_UGWATER(x + i, y + j)) w++; } } prod = (prod * w) / n; MP_INFO(x + 1, y).int_4 = w; } MP_INFO(x + 1, y).int_5 = prod; if (prod != 0) { if (put_food(x, y, prod) != 0) { MP_INFO(x, y).int_3++; MP_INFO(x, y).int_7 -= used_jobs; } } if ((total_time & 0x7f) == 0) if ((MP_INFO(x, y).flags & FLAG_POWERED) != 0) get_waste(x, y, 0x80 * ORG_FARM_WASTE_GET); if ((total_time % 1200) == 0) { MP_INFO(x, y).int_4 = MP_INFO(x, y).int_3; MP_INFO(x, y).int_3 = 0; } i = (total_time + MP_INFO(x, y).int_5 * 1200 + MP_INFO(x, y).int_6) % 4800; if (i % 300 == 0) { i /= 300; if ( MP_INFO(x, y).int_4 > MIN_FOOD_SOLD_FOR_ANIM) { if (i % 4 == 0) { MP_INFO(x, y).int_6 = rand() % 100; /* AL1: initially defined as %300 */ } switch (i) { case (0): MP_TYPE(x, y) = CST_FARM_O3; break; case (1): MP_TYPE(x, y) = CST_FARM_O3; break; case (2): MP_TYPE(x, y) = CST_FARM_O3; break; case (3): MP_TYPE(x, y) = CST_FARM_O3; break; case (4): MP_TYPE(x, y) = CST_FARM_O7; break; case (5): MP_TYPE(x, y) = CST_FARM_O7; break; case (6): MP_TYPE(x, y) = CST_FARM_O7; break; case (7): MP_TYPE(x, y) = CST_FARM_O7; break; case (8): MP_TYPE(x, y) = CST_FARM_O11; break; case (9): MP_TYPE(x, y) = CST_FARM_O11; break; case (10): MP_TYPE(x, y) = CST_FARM_O11; break; case (11): MP_TYPE(x, y) = CST_FARM_O11; break; case (12): MP_TYPE(x, y) = CST_FARM_O15; break; case (13): MP_TYPE(x, y) = CST_FARM_O15; break; case (14): MP_TYPE(x, y) = CST_FARM_O15; break; case (15): MP_TYPE(x, y) = CST_FARM_O15; break; } } else { MP_TYPE(x, y) = CST_FARM_O0; } } }