/***************************************************************************** Called when the return key is pressed. *****************************************************************************/ static void luaconsole_input_return(GtkEntry *w, gpointer data) { const char *theinput; struct luaconsole_data *pdialog = luaconsole_dialog_get(); fc_assert_ret(pdialog); fc_assert_ret(pdialog->history_list); theinput = gtk_entry_get_text(w); if (*theinput) { luaconsole_printf(ftc_luaconsole_input, "(input)> %s", theinput); script_client_do_string(theinput); if (genlist_size(pdialog->history_list) >= MAX_LUACONSOLE_HISTORY) { void *data; data = genlist_get(pdialog->history_list, -1); genlist_remove(pdialog->history_list, data); free(data); } genlist_prepend(pdialog->history_list, fc_strdup(theinput)); pdialog->history_pos = -1; } gtk_entry_set_text(w, ""); }
/************************************************************************ Randomize the elements of a genlist using the Fisher-Yates shuffle. see: genlist_sort() and shared.c:array_shuffle() ************************************************************************/ void genlist_shuffle(struct genlist *pgenlist) { const int n = genlist_size(pgenlist); void *sortbuf[n]; struct genlist_link *myiter; int i, shuffle[n]; if (n <= 1) { return; } myiter = find_genlist_position(pgenlist, 0); for (i = 0; i < n; i++, myiter = myiter->next) { sortbuf[i] = myiter->dataptr; /* also create the shuffle list */ shuffle[i] = i; } /* randomize it */ array_shuffle(shuffle, n); /* create the shuffled list */ myiter = find_genlist_position(pgenlist, 0); for (i = 0; i < n; i++, myiter = myiter->next) { myiter->dataptr = sortbuf[shuffle[i]]; } }
/************************************************************************** Called when the return key is pressed. **************************************************************************/ static void inputline_return(GtkEntry *w, gpointer data) { const char *theinput; theinput = gtk_entry_get_text(w); if (*theinput) { if (client_state() == C_S_RUNNING && gui_gtk2_allied_chat_only && is_plain_public_message(theinput)) { char buf[MAX_LEN_MSG]; fc_snprintf(buf, sizeof(buf), ". %s", theinput); send_chat(buf); } else { send_chat(theinput); } if (genlist_size(history_list) >= MAX_CHATLINE_HISTORY) { void *data; data = genlist_get(history_list, -1); genlist_remove(history_list, data); free(data); } genlist_prepend(history_list, fc_strdup(theinput)); history_pos=-1; } gtk_entry_set_text(w, ""); }
/***************************************************************************** Called when a key is pressed. *****************************************************************************/ static gboolean luaconsole_input_handler(GtkWidget *w, GdkEventKey *ev) { struct luaconsole_data *pdialog = luaconsole_dialog_get(); fc_assert_ret_val(pdialog, FALSE); fc_assert_ret_val(pdialog->history_list, FALSE); switch (ev->keyval) { case GDK_Up: if (pdialog->history_pos < genlist_size(pdialog->history_list) - 1) { gtk_entry_set_text(GTK_ENTRY(w), genlist_get(pdialog->history_list, ++pdialog->history_pos)); gtk_editable_set_position(GTK_EDITABLE(w), -1); } return TRUE; case GDK_Down: if (pdialog->history_pos >= 0) { pdialog->history_pos--; } if (pdialog->history_pos >= 0) { gtk_entry_set_text(GTK_ENTRY(w), genlist_get(pdialog->history_list, pdialog->history_pos)); } else { gtk_entry_set_text(GTK_ENTRY(w), ""); } gtk_editable_set_position(GTK_EDITABLE(w), -1); return TRUE; default: break; } return FALSE; }
/************************************************************************ Sort the elements of a genlist. The comparison function should be a function usable by qsort; note that the const void * arguments to compar should really be "pointers to void*", where the void* being pointed to are the genlist dataptrs. That is, there are two levels of indirection. To do the sort we first construct an array of pointers corresponding the the genlist dataptrs, then sort those and put them back into the genlist. ************************************************************************/ void genlist_sort(struct genlist *pgenlist, int (*compar)(const void *, const void *)) { const int n = genlist_size(pgenlist); void *sortbuf[n]; struct genlist_link *myiter; int i; if (n <= 1) { return; } myiter = find_genlist_position(pgenlist, 0); for (i = 0; i < n; i++, myiter = myiter->next) { sortbuf[i] = myiter->dataptr; } qsort(sortbuf, n, sizeof(*sortbuf), compar); myiter = find_genlist_position(pgenlist, 0); for (i = 0; i < n; i++, myiter = myiter->next) { myiter->dataptr = sortbuf[i]; } }
/************************************************************************** Called when a key is pressed. **************************************************************************/ static gboolean inputline_handler(GtkWidget *w, GdkEventKey *ev) { if ((ev->state & GDK_CONTROL_MASK)) { /* Chatline featured text support. */ switch (ev->keyval) { case GDK_b: inputline_make_tag(GTK_ENTRY(w), TTT_BOLD); return TRUE; case GDK_c: inputline_make_tag(GTK_ENTRY(w), TTT_COLOR); return TRUE; case GDK_i: inputline_make_tag(GTK_ENTRY(w), TTT_ITALIC); return TRUE; case GDK_s: inputline_make_tag(GTK_ENTRY(w), TTT_STRIKE); return TRUE; case GDK_u: inputline_make_tag(GTK_ENTRY(w), TTT_UNDERLINE); return TRUE; default: break; } } else { /* Chatline history controls. */ switch (ev->keyval) { case GDK_Up: if (history_pos < genlist_size(history_list) - 1) { gtk_entry_set_text(GTK_ENTRY(w), genlist_get(history_list, ++history_pos)); gtk_editable_set_position(GTK_EDITABLE(w), -1); } return TRUE; case GDK_Down: if (history_pos >= 0) { history_pos--; } if (history_pos >= 0) { gtk_entry_set_text(GTK_ENTRY(w), genlist_get(history_list, history_pos)); } else { gtk_entry_set_text(GTK_ENTRY(w), ""); } gtk_editable_set_position(GTK_EDITABLE(w), -1); return TRUE; case GDK_Tab: if (gui_gtk2_chatline_autocompletion) { return chatline_autocomplete(GTK_EDITABLE(w)); } default: break; } } return FALSE; }
/************************************************************************** ... **************************************************************************/ 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); } } }