-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.c
2594 lines (2036 loc) · 58.8 KB
/
client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Race for the Galaxy AI
*
* Copyright (C) 2009-2011 Keldon Jones
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <gtk/gtk.h>
#include "rftg.h"
#include "comm.h"
#include "client.h"
/*
* File descriptor of connection to server.
*/
static int server_fd = -1;
/*
* I/O source for connection to server.
*/
static GSource *server_src;
/*
* Our current connection state.
*/
int client_state = CS_DISCONN;
/*
* Our joined session ID.
*/
static int client_sid = -1;
/*
* We are currently playing in a game.
*/
static int playing_game;
/*
* Widgets used in multiple functions.
*/
static GtkWidget *login_status;
/*
* We are currently making a choice.
*/
static int making_choice;
/*
* Prevent displayed card updates until server catches up with us.
*/
static int prevent_update, prevent_phase;
/*
* Various things have updated since last redraw.
*/
static int cards_updated, status_updated;
/*
* Waiting status for each player.
*/
int waiting_player[MAX_PLAYER];
/*
* Forward declaration.
*/
static void disconnect(void);
/*
* Send message to server.
*/
void send_msg(int fd, char *msg)
{
struct timeval timeout;
int size, sent = 0, x;
char *ptr;
/* Go to size area of message */
ptr = msg + 4;
/* Read size */
size = get_integer(&ptr);
/* Send until finished */
while (sent < size)
{
/* Write as much as possible */
x = send(fd, msg + sent, size - sent, 0);
/* Check for errors */
if (x < 0)
{
/* Check for broken pipe */
if (errno == EPIPE) return;
/* Check for try again */
if (errno == EAGAIN)
{
/* Create timeout */
timeout.tv_sec = 0;
timeout.tv_usec = 1000;
/* Wait a bit */
select(0, NULL, NULL, NULL, &timeout);
/* Try again */
continue;
}
/* Error */
perror("send");
disconnect();
return;
}
/* Count bytes sent */
sent += x;
}
}
/*
* Delete row from user list if it matches passed-in data.
*/
static gboolean delete_user(GtkTreeModel *model, GtkTreePath *path,
GtkTreeIter *iter, gpointer data)
{
char *ptr;
/* Get first column */
gtk_tree_model_get(model, iter, 0, &ptr, -1);
/* Check for match */
if (!strcmp(ptr, (char *)data))
{
/* Delete row */
gtk_list_store_remove(GTK_LIST_STORE(model), iter);
/* Free copy of string */
g_free(ptr);
/* Found a match */
return TRUE;
}
/* Free copy of string */
g_free(ptr);
/* No match, keep looking */
return FALSE;
}
/*
* Delete row from game list if it matches passed-in session ID.
*/
static gboolean delete_game(GtkTreeModel *model, GtkTreePath *path,
GtkTreeIter *iter, gpointer data)
{
int x = GPOINTER_TO_INT(data);
int y;
/* Get first column */
gtk_tree_model_get(model, iter, 0, &y, -1);
/* Check for match */
if (x == y)
{
/* Delete row */
gtk_tree_store_remove(GTK_TREE_STORE(model), iter);
/* Found a match */
return TRUE;
}
/* No match, keep looking */
return FALSE;
}
/*
* Destroy all entries in game and user lists.
*/
static void clear_games_users(void)
{
GtkTreeIter iter;
/* Get first entry of user list */
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(user_list), &iter))
{
/* Remove rows */
while (gtk_list_store_remove(user_list, &iter));
}
/* Get first entry of game list */
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(game_list), &iter))
{
/* Remove rows */
while (gtk_tree_store_remove(game_list, &iter));
}
}
/*
* Expansion name abbreviations.
*/
static char *exp_abbr[MAX_EXPANSION] =
{
"Base",
"TGS",
"RvI",
"BoW"
};
/*
* Find the given session ID in the game list.
*
* Return false if the given ID does not exist.
*/
static int find_game_iter(int id, GtkTreeIter *iter)
{
int x;
/* Get first row in tree */
if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(game_list), iter))
{
/* No rows */
return 0;
}
/* Loop until match found or no more rows */
while (1)
{
/* Get first column */
gtk_tree_model_get(GTK_TREE_MODEL(game_list), iter, 0, &x, -1);
/* Check for match */
if (x == id) return 1;
/* Go to next row */
if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(game_list), iter))
{
/* No more rows */
return 0;
}
}
}
/*
* Find a player in the given game.
*/
static int find_game_player(GtkTreeIter *parent, GtkTreeIter *child, int who)
{
int x;
/* Get first child of parent */
if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(game_list), child,
parent))
{
/* No match */
return 0;
}
/* Loop until match found or no more rows */
while (1)
{
/* Get player index column */
gtk_tree_model_get(GTK_TREE_MODEL(game_list), child, 0, &x, -1);
/* Check for match */
if (x == who) return 1;
/* Go to next row */
if (!gtk_tree_model_iter_next(GTK_TREE_MODEL(game_list), child))
{
/* No matches */
return 0;
}
}
}
/*
* The current selection in the games list was changed.
*/
void game_view_changed(GtkTreeView *view, gpointer data)
{
GtkTreePath *game_path;
GtkTreeIter game_iter, parent_iter;
int owned, minp, user = 1, self;
/* Check for ability to join game */
gtk_widget_set_sensitive(join_button, client_sid == -1);
/* Check for ability to create game */
gtk_widget_set_sensitive(create_button, client_sid == -1);
/* Check for ability to leave game */
gtk_widget_set_sensitive(leave_button, client_sid != -1);
/* Assume no ability to start game or kick player */
gtk_widget_set_sensitive(start_button, FALSE);
gtk_widget_set_sensitive(kick_button, FALSE);
gtk_widget_set_sensitive(addai_button, FALSE);
/* Get selected game */
gtk_tree_view_get_cursor(GTK_TREE_VIEW(games_view), &game_path, NULL);
/* Check for no selection */
if (!game_path) return;
/* Get iterator for path */
gtk_tree_model_get_iter(GTK_TREE_MODEL(game_list), &game_iter,
game_path);
/* Free path */
gtk_tree_path_free(game_path);
/* Get parent iterator, if any */
if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(game_list), &parent_iter,
&game_iter))
{
/* Set parent to current cursor location */
parent_iter = game_iter;
/* Cursor is not pointing to a user */
user = 0;
}
/* Get game owned flag and minimum number of players */
gtk_tree_model_get(GTK_TREE_MODEL(game_list), &parent_iter, 10, &owned,
12, &minp, -1);
/* Check for user */
if (user)
{
/* Check whether cursor is on ourself */
gtk_tree_model_get(GTK_TREE_MODEL(game_list), &game_iter,
10, &self, -1);
}
/* Check for ability to start game */
gtk_widget_set_sensitive(start_button, owned &&
gtk_tree_model_iter_n_children(GTK_TREE_MODEL(game_list),
&parent_iter) >= minp);
/* Check for ability to kick player */
gtk_widget_set_sensitive(kick_button, user && !self && owned);
/* Check for ability to add AI player */
gtk_widget_set_sensitive(addai_button, owned);
}
/*
* Handle a new open game message.
*/
static void handle_open_game(char *ptr)
{
int x, y;
char buf[1024];
GtkTreeIter list_iter;
/* Read session ID */
x = get_integer(&ptr);
/* Look for row in game list */
if (!find_game_iter(x, &list_iter))
{
/* Add new row to game tree */
gtk_tree_store_append(game_list, &list_iter, NULL);
/* Set ID in game tree */
gtk_tree_store_set(game_list, &list_iter, 0, x, -1);
}
/* Read description */
get_string(buf, &ptr);
/* Set description */
gtk_tree_store_set(game_list, &list_iter, 1, buf, -1);
/* Read creator username */
get_string(buf, &ptr);
/* Set creator */
gtk_tree_store_set(game_list, &list_iter, 2, buf, -1);
/* Read password required */
x = get_integer(&ptr);
/* Set password required */
gtk_tree_store_set(game_list, &list_iter, 3, x, -1);
/* Read min/max number of players */
x = get_integer(&ptr);
y = get_integer(&ptr);
/* Create string for min/max */
if (x != y)
{
/* Create string with range */
sprintf(buf, "%d-%d", x, y);
}
else
{
/* Create string with one number */
sprintf(buf, "%d", x);
}
/* Set number of players string */
gtk_tree_store_set(game_list, &list_iter, 4, buf, 12, x, -1);
/* Read expansion level */
x = get_integer(&ptr);
/* Set expansion string */
gtk_tree_store_set(game_list, &list_iter, 5, exp_abbr[x], -1);
/* Read two-player advanced option */
x = get_integer(&ptr);
/* Set advanced option */
gtk_tree_store_set(game_list, &list_iter, 6, x, -1);
/* Read disable options */
x = get_integer(&ptr);
y = get_integer(&ptr);
/* Set disable options */
gtk_tree_store_set(game_list, &list_iter, 7, x, 8, y, -1);
/* Read game speed option */
x = get_integer(&ptr);
/* Set speed option */
gtk_tree_store_set(game_list, &list_iter, 9, x, -1);
/* Read owner flag */
x = get_integer(&ptr);
/* Set owner information */
gtk_tree_store_set(game_list, &list_iter, 10, x, -1);
/* Make checkboxes visible */
gtk_tree_store_set(game_list, &list_iter, 11, 1, -1);
/* Sort game list by session ID */
gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(game_list), 0,
GTK_SORT_ASCENDING);
/* Reset button state */
game_view_changed(GTK_TREE_VIEW(games_view), NULL);
}
/*
* Handle a message about a player who has joined a game.
*/
static void handle_game_player(char *ptr)
{
int x, y;
char buf[1024];
GtkTreeIter list_iter, child_iter;
/* Read session ID */
x = get_integer(&ptr);
/* Read player spot */
y = get_integer(&ptr);
/* Read user name */
get_string(buf, &ptr);
/* Find game ID */
find_game_iter(x, &list_iter);
/* Check for player already in game */
if (!find_game_player(&list_iter, &child_iter, y))
{
/* Check for no player to add */
if (!strlen(buf)) return;
/* Add a child row */
gtk_tree_store_append(game_list, &child_iter, &list_iter);
}
/* Check for deleting player */
if (!strlen(buf))
{
/* Delete row */
gtk_tree_store_remove(game_list, &child_iter);
/* Reset button state */
game_view_changed(GTK_TREE_VIEW(games_view), NULL);
/* Done */
return;
}
/* Set player number */
gtk_tree_store_set(game_list, &child_iter, 0, y, -1);
/* Set username */
gtk_tree_store_set(game_list, &child_iter, 1, buf, -1);
/* Get online status */
x = get_integer(&ptr);
/* Create online string */
strcpy(buf, x ? "" : "(offline)");
/* Set online status */
gtk_tree_store_set(game_list, &child_iter, 2, buf, -1);
/* Get self flag */
x = get_integer(&ptr);
/* Store note of self */
gtk_tree_store_set(game_list, &child_iter, 10, x, -1);
/* Make checkboxes invisible on this row */
gtk_tree_store_set(game_list, &child_iter, 11, 0, -1);
/* Reset button state */
game_view_changed(GTK_TREE_VIEW(games_view), NULL);
}
/*
* Handle a message about game parameters.
*/
static void handle_status_meta(char *ptr)
{
char name[1024];
int i;
/* Read basic game parameters */
real_game.num_players = get_integer(&ptr);
real_game.expanded = get_integer(&ptr);
real_game.advanced = get_integer(&ptr);
real_game.goal_disabled = get_integer(&ptr);
real_game.takeover_disabled = get_integer(&ptr);
/* Initialize card designs for this expansion level */
init_game(&real_game);
/* Load AI neural networks for this game */
ai_func.init(&real_game, 0, 0);
/* Loop over goals */
for (i = 0; i < MAX_GOAL; i++)
{
/* Read goal presence */
real_game.goal_active[i] = get_integer(&ptr);
}
/* Loop over players */
for (i = 0; i < real_game.num_players; i++)
{
/* Read player name */
get_string(name, &ptr);
/* Copy name */
real_game.p[i].name = strdup(name);
}
/* Redraw status areas */
redraw_status();
redraw_goal();
/* Modify GUI for expansion and number of players */
modify_gui();
}
/*
* Handle a status update about a player.
*/
static void handle_status_player(char *ptr)
{
player *p_ptr;
int x;
/* Get player index */
x = get_integer(&ptr);
/* Get player pointer */
p_ptr = &real_game.p[x];
/* Read actions */
p_ptr->action[0] = get_integer(&ptr);
p_ptr->action[1] = get_integer(&ptr);
/* Read prestige action used flag */
p_ptr->prestige_action_used = get_integer(&ptr);
/* Loop over goals */
for (x = 0; x < MAX_GOAL; x++)
{
/* Read goal claimed */
p_ptr->goal_claimed[x] = get_integer(&ptr);
/* Real goal progress */
p_ptr->goal_progress[x] = get_integer(&ptr);
}
/* Read player's prestige count */
p_ptr->prestige = get_integer(&ptr);
/* Read player's VP count */
p_ptr->vp = get_integer(&ptr);
/* Read player's phase bonuses */
p_ptr->phase_bonus_used = get_integer(&ptr);
p_ptr->bonus_military = get_integer(&ptr);
p_ptr->bonus_reduce = get_integer(&ptr);
/* Redraw status information later */
status_updated = 1;
}
/*
* Handle a status update about a card.
*/
static void handle_status_card(char *ptr)
{
card *c_ptr;
int x;
int owner, where, start_owner, start_where;
/* Read card index */
x = get_integer(&ptr);
/* Get card pointer */
c_ptr = &real_game.deck[x];
/* Read card owner */
owner = get_integer(&ptr);
start_owner = get_integer(&ptr);
/* Read card location */
where = get_integer(&ptr);
start_where = get_integer(&ptr);
/* Move card to current location */
move_card(&real_game, x, owner, where);
/* Move "start of phase" location */
move_start(&real_game, x, start_owner, start_where);
/* Read unpaid good flag */
c_ptr->unpaid = get_integer(&ptr);
/* Read order played */
c_ptr->order = get_integer(&ptr);
/* Read covered by good flag */
c_ptr->covered = get_integer(&ptr);
/* Card locations have been updated */
cards_updated = 1;
status_updated = 1;
/* Track latest played card */
if (c_ptr->owner >= 0 &&
c_ptr->order > real_game.p[c_ptr->owner].table_order)
{
/* Update order */
real_game.p[c_ptr->owner].table_order = c_ptr->order;
}
}
/*
* Handle a goal status update.
*/
static void handle_status_goal(char *ptr)
{
int i;
/* Loop over goals */
for (i = 0; i < MAX_GOAL; i++)
{
/* Read goal availability and progress */
real_game.goal_avail[i] = get_integer(&ptr);
real_game.goal_most[i] = get_integer(&ptr);
}
/* Redraw goal area */
redraw_goal();
}
/*
* Handle a miscellaneous status update.
*/
static void handle_status_misc(char *ptr)
{
int i;
/* Read round number */
real_game.round = get_integer(&ptr);
/* Read VP pool size */
real_game.vp_pool = get_integer(&ptr);
/* Loop over actions */
for (i = 0; i < MAX_ACTION; i++)
{
/* Read action selected flag */
real_game.action_selected[i] = get_integer(&ptr);
}
/* Read current action */
real_game.cur_action = get_integer(&ptr);
/* Check for server catching up */
if (prevent_update && real_game.cur_action != prevent_phase)
{
/* Allow updates */
prevent_update = 0;
}
/* Redraw phase status */
redraw_phase();
/* Check for locked hand/table areas */
if (!prevent_update && !making_choice && cards_updated)
{
/* Reset cards in hand/table area */
reset_cards(&real_game, TRUE, TRUE);
/* Redraw hand and table areas */
redraw_hand();
redraw_table();
/* Clear cards updated flag */
cards_updated = 0;
}
/* Check for update in status */
if (status_updated)
{
/* Redraw status areas */
redraw_status();
/* Clear update flag */
status_updated = 0;
}
}
/*
* Handle message about waiting players.
*/
static void handle_waiting(char *ptr)
{
int i;
/* Loop over players */
for (i = 0; i < real_game.num_players; i++)
{
/* Get wait status */
waiting_player[i] = get_integer(&ptr);
}
/* Update status areas */
redraw_status();
}
/*
* Server has asked us to make a choice.
*
* Ask player and return result.
*/
static void handle_choose(char *ptr)
{
player *p_ptr;
char msg[1024];
int pos, type, num, num_special;
int list[MAX_DECK], special[MAX_DECK];
int arg1, arg2, arg3;
int i;
/* Allow display updates when asked a direct question */
prevent_update = 0;
/* Check for already making choice */
if (making_choice) return;
/* Get player pointer */
p_ptr = &real_game.p[player_us];
/* Read choice log position expected */
pos = get_integer(&ptr);
/* Check for further along in log than we are */
if (pos > p_ptr->choice_pos)
{
/* Adjust current position */
p_ptr->choice_size = p_ptr->choice_pos = pos;
}
/* Check for request for choice we have already made */
else if (pos < p_ptr->choice_pos)
{
/* XXX Do nothing */
return;
}
/* Read choice type */
type = get_integer(&ptr);
/* Read number of items in list */
num = get_integer(&ptr);
/* Loop over items in list */
for (i = 0; i < num; i++)
{
/* Read list item */
list[i] = get_integer(&ptr);
}
/* Read number of special items in list */
num_special = get_integer(&ptr);
/* Loop over special items */
for (i = 0; i < num_special; i++)
{
/* Read special item */
special[i] = get_integer(&ptr);
}
/* Read extra arguments */
arg1 = get_integer(&ptr);
arg2 = get_integer(&ptr);
arg3 = get_integer(&ptr);
/* Do not update hand/table areas while player is deciding */
making_choice = 1;
/* Ask player for decision */
gui_func.make_choice(&real_game, player_us, type, list, &num,
special, &num_special, arg1, arg2, arg3);
/* Hand/table areas may be redrawn */
making_choice = 0;
/* Reset hand/table areas to default */
reset_cards(&real_game, TRUE, TRUE);
/* Redraw everything */
redraw_everything();
/* Check for disconnected */
if (client_state == CS_DISCONN) return;
/* Check for resigned */
if (client_sid == -1) return;
/* Start reply */
ptr = msg;
/* Begin message */
start_msg(&ptr, MSG_CHOOSE);
/* Put choice log position */
put_integer(p_ptr->choice_pos, &ptr);
/* Copy entries from choice log */
for (i = p_ptr->choice_pos; i < p_ptr->choice_size; i++)
{
/* Copy entry */
put_integer(p_ptr->choice_log[i], &ptr);
}
/* Move current position to end of choice log */
p_ptr->choice_pos = p_ptr->choice_size;
/* Finish message */
finish_msg(msg, ptr);
/* Send reply */
send_msg(server_fd, msg);
}
/*
* Handle a choice request in a simulated game.
*
* This function is called by the local rules engine when preparing answers
* for the current phase in advance of the server asking. We ask the
* player to make the choice and send the answer to the server.
*/
static void prepare_make_choice(game *g, int who, int type, int list[], int *nl,
int special[], int *ns, int arg1, int arg2,
int arg3)
{
player *p_ptr = &g->p[who];
char msg[1024], *ptr = msg;
int i;
/* Check for random number generator used in simulated game */
if (g->random_seed != 0)
{
/* Abort preparation */
g->game_over = 1;
return;
}
/* Ask player using normal GUI function */
gui_func.make_choice(g, who, type, list, nl, special, ns,
arg1, arg2, arg3);
/* Check for disconnected or resigned */
if (client_state == CS_DISCONN || client_sid == -1)
{
/* Abort simulated game */
g->game_over = 1;
return;
}
/* Begin message */
start_msg(&ptr, MSG_CHOOSE);
/* Put choice log position */
put_integer(p_ptr->choice_pos, &ptr);
/* Copy entries from choice log */
for (i = p_ptr->choice_pos; i < p_ptr->choice_size; i++)
{
/* Copy entry */
put_integer(p_ptr->choice_log[i], &ptr);
}
/* Finish message */
finish_msg(msg, ptr);
/* Send to server */
send_msg(server_fd, msg);
}
/*
* Control interface used only when preparing answers to predicted questions.
*/
static decisions prepare_func =
{
NULL,
NULL,
NULL,
prepare_make_choice,
NULL,
NULL,
NULL,
NULL,
NULL,
};
/*
* Prepare choice respones for the given phase.
*/
static void handle_prepare(char *ptr)
{
game sim;
player *p_ptr = &real_game.p[player_us];
int pos, phase, arg;
/* Get position in choice log to fill */
pos = get_integer(&ptr);
/* Check for further along in log than we are */
if (pos > p_ptr->choice_pos)
{
/* Adjust current position */
p_ptr->choice_size = p_ptr->choice_pos = pos;
}
/* Check for request for choice we have already made */
else if (pos < p_ptr->choice_pos)
{
/* XXX Do nothing */
return;
}
/* Get phase and argument from message */
phase = get_integer(&ptr);
arg = get_integer(&ptr);
/* Make simulated game */
sim = real_game;
sim.simulation = 1;
sim.sim_who = player_us;
/* Clear random number generator information to detect use */
sim.random_seed = 0;
/* Set our control interface to send answers to the server */
sim.p[player_us].control = &prepare_func;
/* We are making choices */