Ejemplo n.º 1
0
int main(int argc, char* argv[]) {
	if(argc > 1) {
		FILE* fptr = fopen(argv[1], "r");
		assert(fptr != NULL);
		
		int city_c = count_cities(fptr);
		printf("City count: %d\n", city_c);
		
		city* cities[city_c];
		
		read_cities(fptr, cities, city_c);
		print_cities(cities, city_c);
		printf("Distance between Helsinki and Espoo: %f km\n", distance(cities[1], cities[4]));
		printf("Path distance: %f km\n", path_distance(cities, city_c));
		
		free_cities(cities, city_c);
		fclose(fptr);
	}
	return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
int main_game_interface() {
    //int lolx=0, loly=0;
    //int city=-1;
    // sanity checks, do take care to see that they are always used whenever unit count changes
    int unitcount, citycount;
    int oldx, oldy;
    
    bool done = false;
    while (!done) {
        unitcount = count_units();
        citycount = count_cities();
        
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;
                // check for keypresses
                /// BUG: Cursor goes off grid
                /// BUG: Cursor behaves strangely in relation to viewscreen around edges
            case SDL_KEYDOWN:
                {
                    if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_KP4) {
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldx = gs.units[gs.selected_thing].x;
                            gs.units[gs.selected_thing].x--; 
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].x = oldx; 
                            }
                        } else {
                            gs.curx--; 
                            gs.selx--;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_RIGHT || event.key.keysym.sym == SDLK_KP6) {
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldx = gs.units[gs.selected_thing].x;
                            gs.units[gs.selected_thing].x++; 
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].x = oldx; 
                            }
                        } else {
                            gs.curx++; 
                            gs.selx++;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_KP8) {
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldy = gs.units[gs.selected_thing].y;
                            gs.units[gs.selected_thing].y--; 
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].y = oldy; 
                            }
                        } else {
                            gs.cury--; 
                            gs.sely--;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_DOWN || event.key.keysym.sym == SDLK_KP2) {
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldy = gs.units[gs.selected_thing].y;
                            gs.units[gs.selected_thing].y++; 
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].y = oldy; 
                            }
                        } else {
                            gs.cury++; 
                            gs.sely++;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_KP7) { // up-left
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldx = gs.units[gs.selected_thing].x;
                            oldy = gs.units[gs.selected_thing].y;
                            gs.units[gs.selected_thing].x--; 
                            gs.units[gs.selected_thing].y--;
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].x = oldx; 
                                gs.units[gs.selected_thing].y = oldy; 
                            }
                        } else {
                            gs.curx--; 
                            gs.cury--;
                            gs.selx--;
                            gs.sely--;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_KP9) { // up-right
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldx = gs.units[gs.selected_thing].x;
                            oldy = gs.units[gs.selected_thing].y;
                            gs.units[gs.selected_thing].x++; 
                            gs.units[gs.selected_thing].y--;
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].x = oldx; 
                                gs.units[gs.selected_thing].y = oldy; 
                            }
                        } else {
                            gs.curx++; 
                            gs.cury--;
                            gs.selx++;
                            gs.sely--;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_KP1) { // down-left
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldx = gs.units[gs.selected_thing].x;
                            oldy = gs.units[gs.selected_thing].y;
                            gs.units[gs.selected_thing].x--; 
                            gs.units[gs.selected_thing].y++;
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].x = oldx; 
                                gs.units[gs.selected_thing].y = oldy; 
                            }
                        } else {
                            gs.curx--; 
                            gs.cury++;
                            gs.selx--;
                            gs.sely++;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_KP3) { // down-right
                        if (gs.selected_type == SELECTED_UNIT && gs.selected_thing >= 0) {
                            oldx = gs.units[gs.selected_thing].x;
                            oldy = gs.units[gs.selected_thing].y;
                            gs.units[gs.selected_thing].x++; 
                            gs.units[gs.selected_thing].y++;
                            if (!sanitize_unit_coords(gs.selected_thing) && gs.units[gs.selected_thing].curmove > 0 && is_unit_allowed_there(gs.selected_thing)) { 
                                gs.units[gs.selected_thing].curmove--;
                                gs.curx = gs.units[gs.selected_thing].x-5;
                                gs.cury = gs.units[gs.selected_thing].y-3;
                                gs.selx = gs.units[gs.selected_thing].x;
                                gs.sely = gs.units[gs.selected_thing].y;
                                // combat here
                                detect_and_do_combat(gs.selected_thing);
                            } else {
                                gs.units[gs.selected_thing].x = oldx; 
                                gs.units[gs.selected_thing].y = oldy; 
                            }
                        } else {
                            gs.curx++; 
                            gs.cury++;
                            gs.selx++;
                            gs.sely++;
                        }
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_c) { // cycle city
                        // find the next extant city
                        // sanity check for now, redo later
                        if (gs.selected_type == SELECTED_UNIT || gs.selected_type == -1 || gs.selected_thing == -1) { 
                            gs.selected_type = SELECTED_CITY;
                            gs.selected_thing = -1;
                        }
                        if (citycount == 0) break;
                        do {
                            gs.selected_thing++;
                            if (gs.selected_thing >= MAX_CITIES) gs.selected_thing = 0;
                            gs.curx = gs.cities[gs.selected_thing].x;
                            gs.cury = gs.cities[gs.selected_thing].y;
                            gs.selx = gs.cities[gs.selected_thing].x;
                            gs.sely = gs.cities[gs.selected_thing].y;
                        } while (gs.cities[gs.selected_thing].size < 1 || gs.cities[gs.selected_thing].faction_id != gs.player_faction);
                        gs.curx -= 5; gs.cury -= 3;
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_u) { // cycle unit
                        // find the next extant unit
                        // sanity check for now, redo later
                        if (gs.selected_type == SELECTED_CITY || gs.selected_type == -1 || gs.selected_thing == -1) { 
                            gs.selected_type = SELECTED_UNIT;
                            gs.selected_thing = -1;
                        }
                        if (unitcount == 0) break; // no active units, go away
                        // finds the closest active unit and centers on it
                        do {
                            gs.selected_thing++;
                            if (gs.selected_thing >= MAX_UNITS) gs.selected_thing = 0;
                            gs.curx = gs.units[gs.selected_thing].x;
                            gs.cury = gs.units[gs.selected_thing].y;
                            gs.selx = gs.units[gs.selected_thing].x;
                            gs.sely = gs.units[gs.selected_thing].y;
                        } while (gs.units[gs.selected_thing].active == UNIT_INACTIVE);
                        gs.curx -= 5; gs.cury -= 3;
                        sanitize_coords();
                        print_map(gs.curx,gs.cury);
                        break;
                    }
                    // Build city command!
                    if (event.key.keysym.sym == SDLK_b) {
                        // make sure a unit is actually selected and can build
                        if (gs.selected_type != SELECTED_UNIT) break; // no unit selected
                        if (gs.selected_thing == -1) break; // nothing selected
                        if ((gs.units[gs.selected_thing].flags&UNIT_FLAG_SETTLER) == 0) break; // unit can't build
                        /// TODO: Checks for if there is a city too close
                        gs.units[gs.selected_thing].hp = 0; // kill the unit
                        // build the city
                        build_new_city(gs.units[gs.selected_thing].x,gs.units[gs.selected_thing].y,gs.units[gs.selected_thing].faction_id);
                        break;
                    }
                    if (event.key.keysym.sym == SDLK_p) { // production
                        if (gs.selected_type != SELECTED_CITY) break; // no city selected
                        if (gs.selected_thing == -1) break; // nothing selected
                        
                        // placeholder code until I can think of something better
                        // toggles production of random unit
                        if (gs.cities[gs.selected_thing].production_order == CITY_NO_PRODUCTION) {
                            gs.cities[gs.selected_thing].production_order = rand()%65536; // fix this later, should be a parameter or something
                            gs.cities[gs.selected_thing].production_type = rand()%3+1; // random type
                        } else {
                            gs.cities[gs.selected_thing].production_order = CITY_NO_PRODUCTION;
                        }
                        
                        
                    }
                    if (event.key.keysym.sym == SDLK_s) { // save
                        return CODE_SAVE_GAME;
                    } 
                    if (event.key.keysym.sym == SDLK_ESCAPE) { // quit
                        return CODE_MAIN_MENU;
                    }
                    if (event.key.keysym.sym == SDLK_BACKSPACE) { // turn
                        // end turn hack
                        do_turn();
                        break;
                    }
                }
            } // end switch
        } // end of message processing
        
        print_map(gs.curx,gs.cury);
        print_interface();
        
        SDL_Flip(MAIN_SCREEN);
        SDL_Delay(100);
    }
    
    return 0;
}