/* add the recently connected client to the game as a player */ void join_player(struct socket_node *socket) { struct packet_version packet; struct packet_welcome packet2; int i; struct action_node *action; struct socket_node *p; socket->player = get_free_player(); init_player(socket->player, INIT_PLAYER_HUMAN); /* send version */ packet.version = swap_int(PROGRAM_VERSION); send_socket(socket, PACKET_VERSION, &packet, sizeof(struct packet_version)); /* send welcome */ packet2.player = swap_int(socket->player); send_socket(socket, PACKET_WELCOME, &packet2, sizeof(struct packet_welcome)); /* send all previous players to the new client */ for (i = 0; i < NUM_PLAYERS; i++) { if (IS_ACTIVE(i) && i != socket->player) { send_player_info(socket, i, 0); /* send any already selected action */ action = find_action(i, current_turn); if (action && action->action != ACTION_NOTHING) send_action(socket, i, current_turn); } } /* send the new player to ALL clients */ for (p = socket_list; p != NULL; p = p->next) { if (p->type == SOCKET_CLIENT) send_player_info(p, socket->player, 1); } telegram("\n%s has\nconnected to the server.\n", GET_NAME(socket->player)); }
/************************************************************************** ... **************************************************************************/ void handle_diplomacy_accept_treaty(struct player *pplayer, struct packet_diplomacy_info *packet) { struct Treaty *ptreaty; struct player *plr0, *plr1, *pgiver, *pdest; plr0=&game.players[packet->plrno0]; plr1=&game.players[packet->plrno1]; pgiver=&game.players[packet->plrno_from]; if((ptreaty=find_treaty(plr0, plr1)) && pgiver==pplayer) { if(ptreaty->plr0==pgiver) ptreaty->accept0=!ptreaty->accept0; else ptreaty->accept1=!ptreaty->accept1; send_packet_diplomacy_info(plr0->conn, PACKET_DIPLOMACY_ACCEPT_TREATY, packet); send_packet_diplomacy_info(plr1->conn, PACKET_DIPLOMACY_ACCEPT_TREATY, packet); if(ptreaty->accept0 && ptreaty->accept1) { struct genlist_iterator myiter; send_packet_diplomacy_info(plr0->conn, PACKET_DIPLOMACY_CANCEL_MEETING, packet); send_packet_diplomacy_info(plr1->conn, PACKET_DIPLOMACY_CANCEL_MEETING, packet); notify_player(plr0, "Game: A treaty containing %d clauses was agreed upon", genlist_size(&ptreaty->clauses)); notify_player(plr1, "Game: A treaty containing %d clauses was agreed upon", genlist_size(&ptreaty->clauses)); /* verify gold! the player's gold amount could have changed during * the meeting */ genlist_iterator_init(&myiter, &ptreaty->clauses, 0); for(;ITERATOR_PTR(myiter); ITERATOR_NEXT(myiter)) { struct Clause *pclause=(struct Clause *)ITERATOR_PTR(myiter); pgiver=pclause->from; if(pclause->type==CLAUSE_GOLD && pgiver->economic.gold<pclause->value) { notify_player(plr0, "Game: The %s don't have the promised amount of gold! Treaty canceled!", get_race_name_plural(pgiver->race)); notify_player(plr1, "Game: The %s don't have the promised amount of gold! Treaty canceled!", get_race_name_plural(pgiver->race)); goto cleanup; } } genlist_iterator_init(&myiter, &ptreaty->clauses, 0); for(;ITERATOR_PTR(myiter); ITERATOR_NEXT(myiter)) { struct Clause *pclause=(struct Clause *)ITERATOR_PTR(myiter); pgiver=pclause->from; pdest=(plr0==pgiver) ? plr1 : plr0; switch(pclause->type) { case CLAUSE_ADVANCE: notify_player(pdest, "Game: You are taught the knowledge of %s", advances[pclause->value].name); set_invention(pdest, pclause->value, TECH_KNOWN); pdest->research.researchpoints++; break; case CLAUSE_GOLD: notify_player(pdest, "Game: You get %d gold", pclause->value); pgiver->economic.gold-=pclause->value; pdest->economic.gold+=pclause->value; break; case CLAUSE_MAP: give_map_from_player_to_player(pgiver, pdest); notify_player(pdest, "Game: You receive %s's worldmap", pgiver->name); break; } } cleanup: genlist_unlink(&treaties, ptreaty); free(ptreaty); send_player_info(plr0, 0); send_player_info(plr1, 0); } } }