void remove_middle_player(t_tlist *players, int fd) { t_telem *save; t_telem *tmp; int i; i = 0; save = players->begin; while (players->begin != NULL) { if (((t_player *) players->begin->data)->fd == fd) { tmp = players->begin; if (i == 0) remove_mid_beg(players); else if (i > 0 && i < players->taille - 1) remove_mid_mid(players, save); else remove_mid_end(players, save); players->taille--; free_player(tmp->data); free(tmp); return ; } players->begin = players->begin->next; ++i; } }
G_MODULE_EXPORT void on_button_yesno_yes_clicked (GtkButton *button, gpointer user_data) { #ifdef DEBUG printf("on_button_yesno_yes_clicked\n"); #endif window_destroy(&window.yesno); setsav0; switch(stat1) { default: debug_print_message("on_button_yesno_yes_clicked: unknown status %d\n", stat1); break; case STATUS_PAY_LOAN: on_automatic_loan_repayment_activate(NULL, NULL); break; case STATUS_FIRE_PLAYER: player_remove_from_team(current_user.tm, stat2); current_user.money -= stat3; current_user.money_out[1][MON_OUT_COMPENSATIONS] -= stat3; treeview_show_user_player_list(); game_gui_set_main_window_header(); break; case STATUS_USER_MANAGEMENT: user_remove(stat2, TRUE); treeview_show_users( GTK_TREE_VIEW(lookup_widget(window.user_management, "treeview_user_management_users"))); treeview_show_team_list( GTK_TREE_VIEW(lookup_widget(window.user_management, "treeview_user_management_teams")), FALSE, FALSE); break; case STATUS_QUERY_UNFIT: load_save_autosave(); callback_show_next_live_game(); break; case STATUS_QUERY_QUIT: main_exit_program(EXIT_OK, NULL); break; case STATUS_QUERY_USER_NO_TURN: load_save_autosave(); callback_show_next_live_game(); break; case STATUS_QUERY_KICK_YOUTH: free_player(&g_array_index(current_user.youth_academy.players, Player, selected_row)); g_array_remove_index(current_user.youth_academy.players, selected_row); on_menu_show_youth_academy_activate(NULL, NULL); selected_row = -1; break; } if(stat4 == STATUS_SHOW_EVENT) user_event_show_next(); }
void free_list_players(t_tlist *players) { t_telem *tmp; while (players->begin != NULL) { tmp = players->begin; players->begin = players->begin->next; free_player(tmp->data); free(tmp); } }
/** * Remove a player from the game that has been disconnected by logging * out, the socket connection was interrupted, etc. * @param pl The player to remove. */ void remove_ns_dead_player(player *pl) { if (pl == NULL || pl->ob->type == DEAD_OBJECT) { return; } if (pl->state == ST_PLAYING) { /* Trigger the global LOGOUT event */ trigger_global_event(GEVENT_LOGOUT, pl->ob, pl->socket.host); if (!pl->dm_stealth) { new_draw_info_format(NDI_UNIQUE | NDI_ALL | NDI_DK_ORANGE, NULL, "%s left the game.", query_name(pl->ob, NULL)); } /* If this player is in a party, leave the party */ if (pl->party) { command_party(pl->ob, "leave"); } strncpy(pl->killer, "left", MAX_BUF - 1); hiscore_check(pl->ob, 1); /* Be sure we have closed container when we leave */ container_unlink(pl, NULL); save_player(pl->ob, 0); if (!QUERY_FLAG(pl->ob, FLAG_REMOVED)) { leave_map(pl->ob); } if (pl->ob->map) { if (pl->ob->map->in_memory == MAP_IN_MEMORY) { pl->ob->map->timeout = MAP_TIMEOUT(pl->ob->map); } pl->ob->map = NULL; } } LOG(llevInfo, "LOGOUT: >%s< from IP %s\n", pl->ob->name, pl->socket.host); /* To avoid problems with inventory window */ pl->ob->type = DEAD_OBJECT; free_player(pl); }
int main(int ac, char **av) { t_player *player; ac--; av++; check_input(ac, av); if (!(player = init_player(ft_atoi(av[0])))) ERROR_LEMIPC; wait_ncurse(player); loop(player); free_player(player); return (EXIT_SUCCESS); }
void ret_player(t_tlist *players, int fd) { t_telem *tmp; if (players->taille == 1 && ((t_player *) players->begin->data)->fd == fd) { tmp = players->begin; init_tlist(players); free_player(tmp->data); free(tmp); } if (players->taille > 1) remove_middle_player(players, fd); }
int main(int argc, char *argv[]) { int CONTINUE,WINNER,HUMAN,COMPUTER; if(argc!=4) { fprintf(stderr,"Usage: mnkgame <row> <col> <k>\n"); return 1; } if(!read_parameters(argv)) return 1; if(!setup_game(M,N,K)) { fprintf(stderr,"Error: the board is too big!\n"); return 1; } COMPUTER=1; HUMAN=2; setup_player(M,N,K); printf("Computer Player ready\n"); mypause(); CONTINUE=N*M; WINNER=DRAW; while(CONTINUE>0) { if(read_move(PLAYER1,HUMAN)) { WINNER=PLAYER1; CONTINUE=0; } if(--CONTINUE>0) { if(read_move(PLAYER2,HUMAN)) { WINNER=PLAYER2; CONTINUE=0; } --CONTINUE; } } system("cls"); print_board(); end_game(WINNER,HUMAN); free_player(); free_game(); return 0; }
/** Free an array containing players. */ void free_player_array(GArray **players) { #ifdef DEBUG printf("free_player_array\n"); #endif gint i; if(*players != NULL) { for(i=0; i<(*players)->len; i++) free_player(&g_array_index((*players), Player, i)); free_g_array(players); } }
int player_read(t_server *serv, t_player **list, t_player **player) { int r; char buff[512]; r = read((*player)->fd, buff, sizeof(buff)); if (r <= 0) { remove_player_from_events(serv, *player); remove_player_from_case(serv, *player, (*player)->pos); graphic_instr_all(serv, &(*player)->num, PDI); free_player(list, *player); *player = NULL; return (0); } buff_append((*player)->rdbuff, buff, r); return (1); }
/** * The function called by the parser when a closing tag is read. * The state variable is changed in this function. * @see The GLib manual (Simple XML parser). */ void xml_team_read_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error) { #ifdef DEBUG printf("xml_team_read_end_element\n"); #endif if(strcmp(element_name, TAG_TEAM_NAME) == 0 || strcmp(element_name, TAG_STADIUM_NAME) == 0 || strcmp(element_name, TAG_SYMBOL) == 0 || strcmp(element_name, TAG_AVERAGE_TALENT) == 0 || strcmp(element_name, TAG_FORMATION) == 0 || strcmp(element_name, TAG_NAMES_FILE) == 0 || strcmp(element_name, TAG_PLAYER) == 0) { state = STATE_TEAM; if(strcmp(element_name, TAG_PLAYER) == 0) { if(team->players->len == const_int("int_team_cpu_players")) { free_player(&new_player); main_exit_program(EXIT_LOAD_TEAM_DEF, "xml_team_read_end_element: too many players in team definition '%s' (only %d allowed).", d_file, const_int("int_team_cpu_players")); } else g_array_append_val(team->players, new_player); } } else if(strcmp(element_name, TAG_PLAYER_NAME) == 0 || strcmp(element_name, TAG_PLAYER_BIRTH_YEAR) == 0 || strcmp(element_name, TAG_PLAYER_BIRTH_MONTH) == 0 || strcmp(element_name, TAG_PLAYER_SKILL) == 0 || strcmp(element_name, TAG_PLAYER_TALENT) == 0 || strcmp(element_name, TAG_PLAYER_POSITION) == 0) state = STATE_PLAYER; else if(strcmp(element_name, TAG_TEAM) != 0) debug_print_message("xml_team_read_end_element: unknown tag: %s; I'm in state %d\n", element_name, state); }
void free_game ( struct game * const g ) { if (NULL != g->b) { free_board (g->b); } if (NULL != g->rules) { free_ruleset (g->rules); } if (NULL != g->movehist) { free_listmh (g->movehist); } if (NULL != g->players) { unsigned short i; for (i = (unsigned short)0; i < g->playerc; i++) { if (NULL != g->players[i]) { free_player (g->players[i]); } } free (g->players); /* splint thinks g->rules isn't released "if (NULL != g->players)". */ /*@i2@*/\ } free (g); }
/* * Returns NULL if failing to allocate. * * If successful, then game->playerc is set to HT_PLAYERS_MAX. The * players are allocated and initialized according to player_init. * * You must still initialize the rest of the game after calling this * function (the players are only initialized because their player * indices correspond to the array in struct game, and then we may as * well fully initialize them). * * These variables are fully allocated, but not initialized: * * * game->rules * * * game->b */ struct game * alloc_game ( const unsigned short bwidth, const unsigned short bheight ) { unsigned short i; const unsigned short blen = (unsigned short)(bwidth * bheight); struct game * const g = malloc(sizeof(* g)); if (NULL == g) { return NULL; } g->rules = alloc_ruleset(bwidth, bheight); if (NULL == g->rules) { free (g); return NULL; } /* * This is set later and uses ruleset information. */ g->b = alloc_board(blen); if (NULL == g->b) { free_ruleset (g->rules); free (g); return NULL; } g->movehist = alloc_listmh(HT_LISTMH_CAP_DEF); if (NULL == g->movehist) { free_board (g->b); free_ruleset (g->rules); free (g); return NULL; } g->playerc = HT_PLAYERS_MAX; g->players = malloc(sizeof(* g->players) * (size_t)g->playerc); if (NULL == g->players) { free_listmh (g->movehist); free_board (g->b); free_ruleset (g->rules); free (g); return NULL; } for (i = (unsigned short)0; i < g->playerc; i++) { g->players[i] = alloc_player(); if (NULL == g->players[i] || !player_init(g->players[i], i)) { unsigned short j; for (j = (unsigned short)0; j <= i /* Check added for GCC's loop optimizations: */ && j <= HT_PLAYERS_MAX ; j++) { if (NULL != g->players[j]) { free_player(g->players[j]); } } free (g->players); free_listmh (g->movehist); free_board (g->b); free_ruleset (g->rules); free (g); return NULL; } } return g; }
int main() { // Initial blocks uint8_t blocks[5][2] = { {2,1}, {3,5}, {4,5}, {4,6}, {9,8}}; // Initial start position uint8_t startx = WIDTH / 2; uint8_t starty = HEIGHT / 2; int readc = 0, quit = 0, playerid = PLAYER1; int textpos = 0; // Game area gamearea* game = new_gamearea(WIDTH,HEIGHT,5,blocks); // Player player* pl1 = new_player(playerid,startx,starty,HEALTH); initscr(); // Start ncurses noecho(); // Disable echoing of terminal input cbreak(); // Individual keystrokes intrflush(stdscr, FALSE); // Prevent interrupt flush keypad(stdscr,TRUE); // Allow keypad usage start_color(); // Initialize colors // Color pairs init_pair(colorpair id,foreground color, background color) init_pair(PLAYER1,PLAYER1,COLOR_BLACK); // Player1 = COLOR_RED (1) init_pair(PLAYER2,PLAYER2,COLOR_BLACK); // Player2 = COLOR_GREEN (2) init_pair(PLAYER3,PLAYER3,COLOR_BLACK); // Player3 = COLOR_YELLOW (3) init_pair(PLAYER4,PLAYER4,COLOR_BLACK); // Player4 = COLOR_BLUE (4) init_pair(PLAYER_HIT_COLOR,COLOR_RED,COLOR_YELLOW); init_pair(WALL_COLOR,COLOR_WHITE,COLOR_WHITE); // Prepare everything clear_log(); prepare_horizontal_line(WIDTH); ui_draw_grid(game, pl1); fd_set readfs; int rval = 0; while(1) { FD_ZERO(&readfs); FD_SET(fileno(stdin),&readfs); // Block until we have something if((rval = select(fileno(stdin)+1,&readfs,NULL,NULL,NULL)) > 0) { // From user if(FD_ISSET(fileno(stdin),&readfs)) { readc = getch(); // Get each keypress pl1->hitwall = 0; switch(readc) { case KEY_LEFT: if(is_position_a_wall(game,pl1->posx-1,pl1->posy)) pl1->hitwall = 1; else pl1->posx--; break; case KEY_RIGHT: if(is_position_a_wall(game,pl1->posx+1,pl1->posy)) pl1->hitwall = 1; else pl1->posx++; break; case KEY_UP: if(is_position_a_wall(game,pl1->posx,pl1->posy-1)) pl1->hitwall = 1; else pl1->posy--; break; case KEY_DOWN: if(is_position_a_wall(game,pl1->posx,pl1->posy+1)) pl1->hitwall = 1; else pl1->posy++; break; // Function keys, here F1 is reacted to case KEY_F(1): status = status ^ 1; break; case 27: // Escape key quit = 1; break; case '/': // User wants to write something memset(&textbuffer,0,BUFFER); textinput = 1; textpos = 0; break; // Erase text case KEY_BACKSPACE: case KEY_DC: textpos--; textbuffer[textpos] = '\0'; break; // Push the line to log with enter case KEY_ENTER: case '\n': textinput = 0; if(strlen(textbuffer) > 0) add_log(textbuffer,textpos); textpos = 0; memset(&textbuffer,0,BUFFER); break; // Add the character to textbuffer if we were inputting text default: if(textinput == 1) { textbuffer[textpos] = readc; textpos++; if(textpos == BUFFER-1) { textpos = 0; textinput = 0; } } break; } } } // Hit a wall, change player if(pl1->hitwall) { pl1->health--; if(pl1->id == PLAYER4) pl1->id = PLAYER1; else pl1->id++; } // Update screen ui_draw_grid(game, pl1); // Suicide if(pl1->health == 0) { ui_draw_end(1); break; } // Surrended if(quit) { ui_draw_end(0); break; } } free_gamearea(game); free_player(pl1); free_horizontal_line(); sleep(1); endwin(); // End ncurses return 0; }
void init_mars(void) { int i; Arena *a; Track *track; struct player *j; #ifdef DOGDEBUG ERROR(1,("init_mars\n")); #endif /* want to make sure this happens REAL FAST, want to make sure no one suffers from stat hosing. */ for (i = 0, j = &players[i]; i < MAXPLAYER; i++, j++) if (j->p_pos > 0) j->p_pos = -1; /* Don't save player stats */ for (i = 1, a = &arenas[1]; i < 9; i++, a++) { a->a_no = i; a->a_free = 1; a->a_top = 10000 + ((i-1) % 4 * 20000); a->a_bottom = 20000 + a->a_top; if (i-1 < 4) a->a_x = 20000; else a->a_x = 80000; a->a_y = 20000 + ((i-1) % 4 * 20000); if (i-1 < 4) { a->a_left = LEFT1; a->a_right = RIGHT1; } else { a->a_left = LEFT2; a->a_right = RIGHT2; } } a = &arenas[0]; a->a_no = 0; a->a_free = 1; /* this should never be checked, home always free */ a->a_top = VERT2; a->a_bottom = VERT4; a->a_left = RIGHT1; a->a_right = LEFT2; for (i = 0, track = &tracks[i]; i < MAXPLAYER; i++, track++) { free_player(i); track->t_arena = 0; track->t_no = i; } reset_all_players(); }
void player_maint(void) { int i; struct player *j; struct player *enemy; Track *etrack; Track *track; int eno; /* avoid dead slots, me, other robots (which aren't hostile) */ for (i = 0, j = &players[i], track = &tracks[i]; i < MAXPLAYER; i++, j++, track++) { if (j->p_pos > 0) j->p_pos = -1; /* Don't save player stats */ if ((j == me) || (track->t_flags == PU_FILLER)) /*ignore puck,filler*/ continue; if (j->p_status != PALIVE) { /*for dead people*/ j->p_team = ROM; if ((j->p_status == PFREE) && (track->t_status != j->p_status)) { free_player(i); if (inprogress && (track->t_flags & (PU_CONTEST|PU_DONE))) { messAll(255,roboname,"%s (%c) has left the game!!! Removing his effect on the scores.", j->p_name, shipnos[i]); dont_score(i); } } else if (track->t_flags & (PU_DOG)) { track->t_flags &= ~PU_DOG; /* The player died in a tournament dogfight */ if (track->t_flags & PU_WAIT) { /* Somehow the enemy killed this player after he died. Consider how to divide credit. */ track->t_flags &= ~PU_WAIT; eno = track->t_opponent; checkBadPlayerNo(eno,"in maint\n"); enemy = &players[eno]; etrack = &tracks[eno]; if (j->p_whydead == KSHIP) { if (enemy->p_whydead == KSHIP) { /* this should not happen, but just in case */ ogg_credit(track,j,eno); ogg_credit(etrack,enemy,i); } else { mutual_credit(track,j,eno); ogg_credit(etrack,enemy,i); } } else { mutual_credit(track,j,eno); mutual_credit(etrack,enemy,i); } } else { /* This player was killed first by his enemy. Place enemy in wait mode, credit is decided when fate of enemy is known. */ eno = track->t_opponent; checkBadPlayerNo(eno,"in maint\n"); enemy = &players[eno]; etrack = &tracks[eno]; if (!(etrack->t_flags & PU_DOG)) { /* something weird is going on.. enemy not dogfighting, remove both player and enemy from dogfight mode */ messAll(255,roboname,"Something weird is going on with %s (O%c)!!", j->p_name, shipnos[i]); reset_dogfight(i); continue; } etrack->t_flags |= PU_WAIT; etrack->t_wait = 6*PERSEC; } } track->t_opponent = -1; track->t_status = j->p_status; do_teleport_home(i); continue; /* do nothing else for dead players */ } /*At this point, the player is alive*/ if (track->t_flags & PU_DOG) { /* increment time in the arena */ #ifndef LTD_STATS j->p_stats.st_tticks = ++(track->t_stats.d_ticks); #endif if (track->t_flags & PU_WAIT) { track->t_wait--; if (track->t_wait == 0) { /* This player survived long enough past the enemy's death! He gets full credit!! */ track->t_flags &= ~PU_WAIT; eno = track->t_opponent; checkBadPlayerNo(eno,"in maint\n"); enemy = &players[eno]; etrack = &tracks[eno]; win_credit(track,j,eno); loss_credit(etrack,enemy,i); track->t_opponent = -1; track->t_flags &= ~PU_DOG; do_teleport_home(i); } } else { eno = track->t_opponent; checkBadPlayerNo(eno,"in maint\n"); enemy = &players[eno]; etrack = &tracks[eno]; if (!(etrack->t_flags & PU_DOG)) { /* Somethings up... my enemy isn't in dogfight mode... */ messAll(255,roboname,"%s (%c) can't find his enemy: %s (%c)", j->p_name, shipnos[i], enemy->p_name, shipnos[eno]); reset_dogfight(i); continue; } } } else if ((inprogress == 2) && !(track->t_flags & (PU_SITOUT|PU_DONE|PU_CONTEST))) { printf("FLAGS: 0x%x\n",track->t_flags); /* A new victim has arrived. */ join(i); } /* Save current info */ track->t_status = j->p_status; track->t_x = j->p_x; track->t_y = j->p_y; /* Note: track->t_seconds is updated in do_war */ } }
TypeScreen game_input_name_player_screen() { JHI_Image back; Button input_name; Button char_input; Button char_input2; Button play[1]; JHI_Effect ef1; const int num_of_buttons = 1; int cur_player = 0; JHI_MouseSt mouse; JHI_KeyboardSt key; TypeScreen te = IS_CLOSE; int i, pos; char flag = 'z' + 2; char cprint = 'z' + 2; char c = 'a'; char ch[2]; char name[20]; char pname[10]; JHI_Font f1, f2; Player *player_pointer; jhi_load_font(&f1, REGULAR_FONT,30); jhi_load_font(&f2, MONSTER_FONT, 30); jhi_load_image(&back, BACK_FILE_PATH); jhi_load_effect(&ef1, CLICK_BOTTON_SOUND); jhi_set_fps_timer(32); memset(name, 0, sizeof(name)); memset(pname, 0, sizeof(pname)); strncpy(name, "Seu Nome ", sizeof(name)); init_button(&char_input, &f1, "a", BUTTON_TYPE_1); init_button(&char_input2, &f1, "a", BUTTON_TYPE_2); init_button(&input_name, &f1, name, BUTTON_TYPE_1); init_button(&play[0], &f1, "Jogar", BUTTON_TYPE_1); play[0].pos.x = jhi_get_central_pos(jhi_get_width_window(), jhi_get_height_window(), jhi_get_image_width(&(play[0].img)), jhi_get_image_height(&(play[0].img))).x; while(jhi_get_close_window() != JHI_CLOSE) { jhi_timer_start(); jhi_update(); jhi_clean(); char_input.pos.x = 100; char_input.pos.y = 200; jhi_draw_image(&back, jhi_get_point(0, 0)); draw_button(&play[0], jhi_get_point(play[0].pos.x, 500)); mouse.mouse_event = MOUSE_NOT_EVENT; key.key_event = KEYBOARD_NOT_EVENT; for (i = 0; i < jhi_get_number_of_events(); i++) { mouse = jhi_get_mouse_status(i); key = jhi_get_keyboard_status(i); } if (mouse.mouse_event != MOUSE_NOT_EVENT) { cprint = check_input(&char_input, mouse, &flag, f1.length); pos = check_buttons(mouse, play, num_of_buttons); if (pos == 0 && strlen(pname) > 0) { player_pointer = get_player_by_index(cur_player); init_player(player_pointer, &f2, pname, 0); jhi_play_effect(&ef1, 2000); if (get_number_of_players() == -1) { player_pointer = get_player_by_index(1); init_player(player_pointer, &f2, "CPU", 0); te = IS_PLAY3; break; } if (cur_player + 1 == get_number_of_players() && get_number_of_players() == 1) { te = IS_PLAY; break; } if (cur_player + 1 == get_number_of_players() && get_number_of_players() == 2) { te = IS_PLAY2; break; } if (cur_player + 1 == get_number_of_players()-1 && get_number_of_players() == 3) { te = IS_PLAY4; player_pointer = get_player_by_index(2); init_player(player_pointer, &f2, "CPU", 0); break; } memset(name, 0, sizeof(name)); memset(pname, 0, sizeof(pname)); strncpy(name, "Seu Nome ", sizeof(name)); cur_player++; free_button(&input_name); init_button(&input_name, &f1, name, BUTTON_TYPE_1); } } change_button_text(&input_name, name); set_button_size(&input_name, strlen(name)*jhi_get_image_width(&(char_input.img)), jhi_get_image_height(&(char_input.img))); draw_button(&input_name, jhi_get_point(jhi_get_central_pos(jhi_get_width_window(), jhi_get_height_window(), jhi_get_image_width(&(input_name.img)), jhi_get_image_height(&(input_name.img))).x, 100)); while(c != 'z' + 1) { memset(ch, 0, sizeof(ch)); memcpy(ch, &c, sizeof(c)); if (c == flag) { change_button_text(&char_input2, ch); draw_button(&char_input2, char_input.pos); } else { change_button_text(&char_input, ch); draw_button(&char_input, char_input.pos); } if (c == cprint) { if (strlen(name) <= 19) { strncat(name, &c, sizeof(char)); strncat(pname, &c, sizeof(char)); } cprint = 'z' + 2; } char_input.pos.x += 3 * f1.length; if (c == 'h' || c == 'p' || c =='x') { char_input.pos.x = 100; char_input.pos.y += 3 * f1.length; } c++; } c = 'a'; jhi_wait_time(); } jhi_stop_music(); jhi_free_image(&back); free_button(&input_name); free_button(&char_input); free_button(&char_input2); free_button(&play[0]); jhi_free_effect(&ef1); free_title_music(); jhi_free_font(&f1); jhi_free_font(&f2); if (te == IS_CLOSE) { player_pointer = get_player_by_index(0); free_player(player_pointer); } return te; }