-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshoogi.c
executable file
·3728 lines (3137 loc) · 91.3 KB
/
shoogi.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
#include "shoogi.h"
MYINT _uid_=0; /* Player's user id. */
MYINT _gid_=0; /* Game currently being played/shown. */
MYINT _blackplid_=0; /* Black's plid. */
MYINT _whiteplid_=0; /* White's plid. */
MYINT _ownplid_=0; /* Own plid. */
MYINT _movesmade_=0; /* Moves made so far. */
int _movestat_=0; /* Status returned by last call of check_move */
int _captured_piece_=0; /* Last piece captured, if last move was capture. */
int _b_impasse_points_ = DEFAULT_IMPASSE_POINTS;
int _w_impasse_points_ = DEFAULT_IMPASSE_POINTS;
int _owncolor_=NOCOLOR; /* Initially not specified. */
ULI _ownseekptr_=NOTSET_SEEKPTR;
ULI _flags_=DEFAULT_FLAGS;
/* Opponent's flags, fetched by fetch_opponents_fields: */
ULI _opflags_=DEFAULT_FLAGS;
char _blackname_[SMALLBUF];
char _whitename_[SMALLBUF];
char *_ownname_;
char space_for_latest_move[MAXBUF+3];
char *_latest_move_=space_for_latest_move;
char *_names_[2] = { _blackname_, _whitename_ };
char _not_me_pattern_[SMALLBUF+1];
char *_everybody_ = "*";
char _dot_[MAXBUF+3];
/* Opponent's fields (from Playerfile).
Stored here by the function fetch_opponents_fields: */
char *_opfields_[MAXPLFIELDS+1];
char spacefor_opfields[BUFSIZ+3]; /* And space for them. */
/* 0 if not completed, 1 if black (0) won, 2 if white (1) won: */
char _eog_=0;
char _debug_flag_=0;
char _su_flag_=0; /* Whether player is super user or not. */
/* If 1 then player is not allowed to jump to shell, nor write files
(option -r): */
char _restricted_flag_=0;
char _get_or_addplid_=0;
int _sig_immediately_ = 0;
int _sig_received_ = 0;
GIDLIST _gidlist_=NULL;
char *wlist[MAXWLIST+2];
int _wlistsize_;
int _morelines_=22;
char *cmds[MAXCMDS+3];
int cmdcnt;
char escapemsgbuf[CHKMSGBUF+3];
char checkmsgbuf[CHKMSGBUF+3];
char E1[MAXBUF+3];
char E2[CHKMSGBUF+3];
char OB[(MAXBUF*3)+3]; /* Output Buffer for fout. */
/* Signals used for various events: */
int notify_signals[]=
{
0,
SIG_MESSAGE_SENT,
SIG_MOVE_MADE,
SIG_GAME_STARTED,
SIG_GAME_FINISHED
};
char *codenames[] =
{
/* 0 */ "NONE",
/* 1 */ "BLINK",
/* 2 */ "BOLD",
/* 3 */ "REVERSE",
/* 4 */ "UNDERLINE",
/* 5 */ "BLACK",
/* 6 */ "RED",
/* 7 */ "GREEN",
/* 8 */ "YELLOW",
/* 9 */ "BLUE",
/* A */ "MAGENTA",
/* B */ "CYAN",
/* C */ "WHITE",
NULL
};
#ifdef CURSES
chtype curses_codes[] =
{
/* 0 */ A_NORMAL,
/* 1 */ A_BLINK,
/* 2 */ A_BOLD,
/* 3 */ A_REVERSE,
/* 4 */ A_UNDERLINE,
/* 5 */ COLOR_PAIR(COLOR_BLACK+1),
/* 6 */ COLOR_PAIR(COLOR_RED+1),
/* 7 */ COLOR_PAIR(COLOR_GREEN+1),
/* 8 */ COLOR_PAIR(COLOR_YELLOW+1),
/* 9 */ COLOR_PAIR(COLOR_BLUE+1),
/* A */ COLOR_PAIR(COLOR_MAGENTA+1),
/* B */ COLOR_PAIR(COLOR_CYAN+1),
/* C */ COLOR_PAIR(COLOR_WHITE+1),
/* D */ A_NORMAL,
/* E */ A_NORMAL,
/* F */ A_NORMAL
};
#endif
unsigned char ansi_codes[] =
{
0, /* 0 NORMAL */
5, /* 1 BLINK */
1, /* 2 BOLD */
7, /* 3 REVERSE */
4, /* 4 UNDERLINE */
30, /* 5 BLACK */
31, /* 6 RED */
32, /* 7 GREEN */
33, /* 8 YELLOW */
34, /* 9 BLUE */
35, /* A MAGENTA */
36, /* B CYAN */
37, /* C WHITE */
0, /* D NORMAL */
0, /* E NORMAL */
0 /* F NORMAL */
};
FILE *err_fp;
int G_argc;
char **G_argv;
char space_for_cmdbuf[MAXBUF+3],sparebuf[MAXBUF+3];
main(argc,argv)
int argc;
char **argv;
{
int sig;
char *cmdbuf;
char *query_string;
G_argc = argc;
G_argv = argv;
_ownname_ = "NOT YET FETCHED";
handle_options();
init_screen();
signal(SIGINT,sighandler);
signal(SIGTERM,sighandler);
#ifdef UNIX
signal(SIGHUP,sighandler);
signal(SIGQUIT,sighandler);
signal(SIGUSR1,sighandler);
signal(SIGUSR2,sighandler);
#endif
if(!(err_fp = myfopen(ERRLOGFILE,"Sa")))
{
fprintf(stderr,
"\n%s: Fatal internal error, can't open file %s for writing!\n",
progname,ERRLOGFILE);
exit(1);
}
initmovefuns();
initboard();
initwlist();
*escapemsgbuf = '\0';
_ownname_ = getname();
_ownplid_ = get_or_addplid(_ownname_,0);
/* Copy player name preceded with ! to _not_me_pattern_: */
*_not_me_pattern_ = '!';
strcpy((_not_me_pattern_+1),_ownname_);
*_dot_ = '\0'; /* Initialize _dot_ to be empty. */
if(getseclevel(_flags_) == BANISHED)
{
pout(("\n?Sorry, you are banished!\n"));
end_screen();
exit(0);
}
if(NULL != (query_string = getenv("QUERY_STRING"))) /* Started as CGI? */
{
char *b,*o;
MYINT board_selected=0;
int other_flag=0;
b = strchr(query_string,'B');
o = strchr(query_string,'O');
if(b && ('=' == *(b+1))) { board_selected = atol(b+2); }
if(o && ('=' == *(o+1))) { other_flag = atol(o+2); }
_flags_ = setboardstyle(_flags_, (HTML_BOARD+UNIKANJI));
fprintf(stdout, "Content-Type: %s\r\n\r\n", "text/html; charset=UTF-8");
fprintf(stdout,"<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
fprintf(stdout,"<TITLE>Situation of Game %u</TITLE>\n", board_selected);
/*
function loop_over_csv_items(fun_to_call, csv_string, other_args)
{
var expr, sep = ',';
var inx1;
var len;
// csv_string = "" + csv_string; // Force it to be really string.
list = csv_string.split(sep);
len = list.length;
inx1 = 0;
while(inx1 < len)
{
expr = fun_to_call + '("' + list[inx1] + '",' + inx1;
if(other_args.length > 0) { expr += ',"' + other_args + '"'; }
expr += ')';
// alert('expression to evaluate=' + expr);
eval(expr);
inx1 = inx1+1;
}
return(len);
}
*/
fprintf(stdout,
"<SCRIPT LANGUAGE=\"JavaScript\" TYPE=\"text/javascript\">\n"
"<!-- Hide it! \n"
"var _AVAILABLE_SQUARES = \"\";\n"
"var _SOURCE_SQUARE = \"\";\n"
"var _PIECECODE = \"\";\n"
"function member(item,list)\n"
"{\n"
" var len = list.length;\n"
" var i = 0;\n"
" while(i < len)\n"
" {\n"
" if(list[i] == item) { return(true); }\n"
" i = i + 1;\n"
" }\n"
" return(false);\n"
"}\n"
"function domove(piececode,src_square,freedoms,avail_squares)\n"
"{\n"
" if('' == document.BOARDFORM.MOVE.value)\n"
" {\n"
" if(0 == freedoms)\n"
" {\n"
" alert(\"You cannot make a move starting from this square \"\n"
" + src_square);\n"
" _AVAILABLE_SQUARES = '';\n"
" _SOURCE_SQUARE = '';\n"
" _PIECECODE = '';\n"
" document.BOARDFORM.MOVE.value = '';\n"
" return(false);\n"
" }\n"
" else\n"
" {\n"
" alert(\"Piece \" + piececode + \" in square \" + src_square + \" has \" + freedoms + \" freedoms: \"\n"
" + avail_squares);\n"
" _AVAILABLE_SQUARES = avail_squares;\n"
" _SOURCE_SQUARE = src_square;\n"
" _PIECECODE = piececode;\n"
" document.BOARDFORM.MOVE.value = piececode + src_square;\n"
" return(true);\n"
" }\n"
" }\n"
" else\n"
" {\n"
" if(member(src_square,_AVAILABLE_SQUARES.split(',')))\n"
" {\n"
" if(' ' == piececode) // Just an ordinary move\n"
" {\n"
" document.BOARDFORM.MOVE.value\n"
" = document.BOARDFORM.MOVE.value + '-';\n"
" }\n"
" else // It's a capture.\n"
" {\n"
" document.BOARDFORM.MOVE.value\n"
" = document.BOARDFORM.MOVE.value + 'x';\n"
" }\n"
" document.BOARDFORM.MOVE.value\n"
" = document.BOARDFORM.MOVE.value + src_square;\n"
" document.BOARDFORM.submit();\n"
" }\n"
" else\n"
" {\n"
" alert(\"Piece \" + _PIECECODE + \" in square \" + _SOURCE_SQUARE\n"
" + \" cannot move to the square \" + src_square)\n"
" _AVAILABLE_SQUARES = '';\n"
" _SOURCE_SQUARE = '';\n"
" _PIECECODE = '';\n"
" document.BOARDFORM.MOVE.value = '';\n"
" return(false);\n"
" }\n"
" }\n"
" return(true);\n"
"}\n"
"// --End Hiding Here -->\n"
"</SCRIPT>\n");
fprintf(stdout, "</HEAD><BODY BGCOLOR=\"#ffffff\">\n"
"<FORM NAME=\"BOARDFORM\" ACTION=\"shoogi.cgi\">");
fprintf(stdout, "<INPUT TYPE=\"TEXT\" NAME=\"MOVE\">\n");
fflush(stdout);
show_board(0, board_selected, other_flag, NULL);
fprintf(stdout, "</FORM></BODY></HTML>\n");
exit(0);
}
/* Show message of the day, if there's any: */
morefile(MOTDFILE,0);
sprintf(sparebuf,"%s has connected to the %s.",_ownname_,progname);
find_all_online(sparebuf,'M',_not_me_pattern_);
fout((OB,"\nPlayer %s, plid: %lu\n",_ownname_,_ownplid_));
STAT();
while(1)
{ /* Clear space_for_cmdbuf first: */
memset(space_for_cmdbuf,'\0',MAXBUF+1);
bigloop:
cmdbuf = space_for_cmdbuf+1; /* Space for ! kludge. */
sig = _sig_received_;
_sig_received_ = 0;
if(sig)
{
pout(("\n"));
switch(sig)
{
case SIGINT:
{
pout(("?Please use the command QUIT for quitting!\n"));
break;
}
#ifndef UNIX
case SIG_GAME_STARTED:
#endif
case SIG_GAME_FINISHED: { readgidlist(); }
case SIG_MOVE_MADE: { checknewmoves(0); }
}
}
/* Check the new messages currently always: */
checkmessages();
/* Print the prompt. */
if(_gid_)
{
fout((OB,"Game %lu, Moves made %lu, ",_gid_,_movesmade_));
if(_owncolor_ != NOCOLOR)
{ fout((OB,"You are %s, ",getcapcolname(_owncolor_))); }
if(_eog_) { fout((OB,"%s won!>",getcapcolname(_eog_-1))); }
else
{ fout((OB,"%s's turn>",getcapcolname(get_parity(_movesmade_)))); }
}
else { fout((OB,"%s>",progname)); } /* No game selected. */
#ifdef UNIX
pout((cmdbuf)); /* Kludge to make input prettier... */
#endif
_sig_immediately_ = 1;
if(_sig_received_) { _sig_immediately_ = 0; continue; }
#ifdef CURSES
if(getstr(cmdbuf),(*cmdbuf == 4)) /* If CTRL-D encountered. */
#else
if(!fgets(cmdbuf,MAXBUF,input))
#endif
{
_sig_immediately_ = 0;
#ifndef CURSES
if(feof(input))
#endif
{
pout(("\n"));
errlog_only((E1,"\n**EOF encountered at main command loop.\n"));
QUIT();
}
#ifndef CURSES
else { goto bigloop; }
#endif
}
_sig_immediately_ = 0;
cmdbuf = nuke_newline(skip_blankos(cmdbuf));
/* Ugly kludge for !command 's */
if((*cmdbuf == '!') || (*cmdbuf == '.'))
{ /* If command begins with ! or . */
*(cmdbuf-1) = *cmdbuf; /* Move that punctuation char one leftward */
*cmdbuf = ' '; /* Then insert one blank between. */
--cmdbuf;
}
/* Another ugly kludge to modify commands like "SET KANJI ..."
to "SET_KANJI ..." */
else if((toupper(*cmdbuf) == 'S') &&
(toupper(*(cmdbuf+1)) == 'E') &&
(toupper(*(cmdbuf+2)) == 'T') &&
(isspace(*(cmdbuf+3))))
{
strcpy((cmdbuf+4),skip_blankos(cmdbuf+3));
*(cmdbuf+3) = '_';
}
/* If no command given: */
if(!(cmdcnt = hack_to_pieces(cmds,cmdbuf))) { continue; }
conv_upper(*cmds); /* Convert first one (command) to uppercase. */
if(strequ(*cmds,"Q") || strequ(*cmds,"QUIT") ||
strequ(*cmds,"X") || strequ(*cmds,"EXIT")) { QUIT(); }
else if(strequ(*cmds,"B") || strequ(*cmds,"BOARD")
|| strequ(*cmds,"REFRESH"))
{ BOARD(); }
else if(strequ(*cmds,".")) { DOT(); }
else if(strequ(*cmds,"G") || strequ(*cmds,"GAMES")) { GAMES(); }
else if(strequ(*cmds,"?") || strequ(*cmds,"H") || strequ(*cmds,"HELP"))
{ HELP(); }
else if(strequ(*cmds,"HANDICAP")) { HANDICAP(); }
else if(strequ(*cmds,"M") || strequ(*cmds,"MOVES")) { MOVES(); }
else if(strequ(*cmds,"N") || strequ(*cmds,"NEW")) { NEW(); }
else if(strequ(*cmds,"PLAY")) { PLAY(); }
else if(strequ(*cmds,"REMOVE")) { REMOVE(); }
else if(strequ(*cmds,"RESIGN")) { RESIGN(); }
else if(strequ(*cmds,"SAY")) { SAY(); }
else if(strequ(*cmds,"SET") || strequ(*cmds,"SET_")) { SET(); }
else if(strequ(*cmds,"SET_ADDRESS") || strequ(*cmds,"ADDRESS"))
{ SET_ADDRESS(); }
else if(strequ(*cmds,"SET_DRAGON")) { SET_DRAGON(); }
else if(strequ(*cmds,"SET_KANJI")) { SET_KANJI(); }
else if(strequ(*cmds,"SET_MAILNOTIFY")) { SET_MAILNOTIFY(); }
else if(strequ(*cmds,"SET_PSTYLE")) { SET_PSTYLE(); }
else if(strequ(*cmds,"SET_BSTYLE") || strequ(*cmds,"STYLE"))
{ SET_BSTYLE(); }
else if(strequ(*cmds,"SET_BLACK")) { SET_BLACK(); }
else if(strequ(*cmds,"SET_WHITE")) { SET_WHITE(); }
else if(strequ(*cmds,"SET_THREATENED")) { SET_THREATENED(); }
else if(strequ(*cmds,"!") || strequ(*cmds,"SHELL") ||
strequ(*cmds,"PUSH")) { SHELL(); }
else if(strequ(*cmds,"SHOUT")) { SHOUT(); }
else if(strequ(*cmds,"STAT") || strequ(*cmds,"S")) { STAT(); }
else if(strequ(*cmds,"STATS") || strequ(*cmds,"USER")) { STATS(); }
else if(strequ(*cmds,"TELL")) { TELL(); }
else if(strequ(*cmds,"U") || strequ(*cmds,"UNDO")) { UNDO(); }
else if(strequ(*cmds,"W") || strequ(*cmds,"WHO"))
{ STATS(); } /* Previously WHO(); */
else if(strequ(*cmds,"DUMPGL")) { dumpgidlist(); }
else
{
fout((OB,"?Unrecognized command: %s\n",*cmds));
}
} /* While */
}
void sighandler(sig)
int sig;
{
signal(sig,sighandler); /* Reassign signal to this function. */
switch(sig)
{
case SIGTERM:
#ifdef UNIX
case SIGHUP:
case SIGQUIT:
#endif
{
_sig_received_ = sig;
errlog((E1,
"\n%s: Signal (%u) received, quitting.\n",progname,sig));
ertzu_exit(sig);
}
}
if(!_sig_immediately_) { _sig_received_ = sig; return; }
pout(("\n"));
switch(sig)
{
case SIGINT:
{
pout(("?Please use the command Quit for quitting!\n"));
break;
}
/* case SIG_GAME_STARTED: */
case SIG_GAME_FINISHED: { readgidlist(); }
/* case SIG_MESSAGE_SENT: { checkmessages(); } */
case SIG_MOVE_MADE: { checkmessages(); checknewmoves(0); }
}
}
QUIT()
{
char buf[MAXBUF+3];
sprintf(buf,"%s has just left the %s.",_ownname_,progname);
/* Clear the pid from player list: */
get_or_addplid(_ownname_,1); /* Maybe this could be... */
find_all_online(buf,'M',_not_me_pattern_); /* done in the one and same function? */
end_screen();
exit(0);
}
ertzu_exit(stat)
int stat;
{
char buf[MAXBUF+3];
if(stat == 101)
{
sprintf(buf,
"%s has been disconnected from the %s due to an internal error.",
_ownname_,progname);
}
else
{
sprintf(buf,
"%s has been disconnected from the %s with the signal %u.",
_ownname_,progname,stat);
}
/* Clear the pid from player list, unless get_or_addplid
itself has generated this error: */
if(!_get_or_addplid_) /* If not involved with an error in that file. */
{
get_or_addplid(_ownname_,1); /* Maybe this could be... */
find_all_online(buf,'M',_not_me_pattern_); /* done in the one and same function? */
}
else
{
errlog((E1,
"%s: Couldn't clear your pid, it has been left to player file as ghost pid.\n",
progname));
}
end_screen();
exit(stat);
}
PLAY()
{
if(cmdcnt < 2)
{
pout((
"Usage: PLAY <move-notation>.\n"));
pout((
"For example, PLAY P-7f or PLAY Gi4-h5\n"));
}
else { play_move(conv_upper(*(cmds+1))); }
}
RESIGN()
{
play_move("RESIGN");
}
HANDICAP()
{
char movebuf[MAXBUF+3];
if(cmdcnt != 2)
{
pout((
"Usage: HANDICAP piece[,piece,...,piece]\n"));
pout((
"For example, HANDICAP L@1a,R@8b removes white's left lance and rook.\n"));
pout((
"(There should be no blanks between the coordinates, only commas!)\n"));
return(0);
}
strcpy(movebuf,"HANDICAP:");
strcat(movebuf,*(cmds+1));
play_move(movebuf);
}
/* General move maker for PLAY, HANDICAP & RESIGN. */
play_move(newmove)
char *newmove;
{
MYINT m,plid;
int pid,old_movestat,old_captured_piece;
char status,new_status;
char tmpbuf[MAXBUF+3],movebuf[MAXBUF+3];
char old_checkmsgbuf[MAXBUF+3],old_escapemsgbuf[MAXBUF+3];
if(!own_gamep()) { return(0); }
if(end_of_gamep()) { return(0); }
if(!(status = lastmovep(&m,movebuf,tmpbuf))) { return(0); }
if(!my_turnp(status,m)) { return(0); }
strcpy(old_checkmsgbuf,checkmsgbuf);
strcpy(old_escapemsgbuf,escapemsgbuf);
old_captured_piece = _captured_piece_;
old_movestat = _movestat_;
/* strcpy(movebuf,newmove); */
/* Increase this here, so that p and o in check_move show correct count: */
_movesmade_++;
if(_movestat_ = check_move(newmove,_owncolor_,1))
{
pout(("Move done!\n"));
/* Transfer old move (which was just read in from .lm file) to movelog: */
/* But not if there is zero move (like 0 W) in .lm file, or the last move
was undone: */
if(m && (status != 'U'))
{
addtomovelog(_gid_,m,old_movestat,old_captured_piece,movebuf,tmpbuf,
old_checkmsgbuf,old_escapemsgbuf);
}
new_status = 'P';
/* If playing this move second time, after undoing, then put status
letter Q instead of P to .lm file, unless the move undone
was HANDICAP, which is always possible to undo.
Or if opponent's mailnotifyflag is set, then also undoing is
impossible: (but that is commented out now) */
/* fetch_opponents_fields(_names_[color]); */
if(/* getmailnotifyflag(_opflags_) || */
((status == 'U') && strequ(movebuf,newmove)
&& !cutstrequ(movebuf,"HANDICAP:")))
{ new_status++; }
/* And overwrite the .lm file with new move: */
put_next_move(_gid_,_movesmade_,newmove,get_timebuf(),new_status);
/* Show some essential consequences of move, if there's any.
However, if game against self, then don't print them here,
as they are immediately printed in checknewmoves anyway.
*/
if(_blackplid_ != _whiteplid_)
{
show_checks(stdout,_movestat_,!_owncolor_,_captured_piece_,
checkmsgbuf,escapemsgbuf);
}
notify_opponent(!_owncolor_,MOVE_MADE);
}
else
{
/* Restore these variables. */
_movesmade_--;
strcpy(checkmsgbuf,old_checkmsgbuf);
strcpy(escapemsgbuf,old_escapemsgbuf);
_captured_piece_ = old_captured_piece;
_movestat_ = old_movestat;
if(*E2) { pout((E2)); }
pout(("?Move not done!\n"));
}
}
UNDO()
{
MYINT m;
int otherflag;
if(!own_gamep()) { return(0); }
if(end_of_gamep()) { return(0); }
otherflag = ((cmdcnt > 1) && strequ(conv_upper(*(cmds+1)),"-O"));
if((m = undo_move(_gid_,_owncolor_,otherflag)) == ERRMOVES)
{
errlog((E1,"Couldn't open a file %lu.lm for reading!\n",_gid_));
return(0);
}
if(m == _movesmade_) { return(0); } /* Couldn't undo. */
/* If undo succesfull then load the game in again, to get the previous
situation: */
if(loadmoves(_gid_,0,0) == ERRMOVES)
{
errlog((E1,"**Can't find movelog for the game %lu !\n",board));
_movesmade_ = _gid_ = 0;
return(0);
}
return(1);
}
int own_gamep()
{
if((_owncolor_ == NOCOLOR) || !_gid_)
{
pout(("?This is not your game!\n"));
return(0);
}
else { return(1); }
}
int end_of_gamep()
{
if(_eog_ /* || EOG(_movestat_) */ )
{
pout(("?This game has been completed, can't do that anymore!\n"));
return(1);
}
else { return(0); }
}
int lastmovep(ptr_to_m,movebuf,timebuf)
MYINT *ptr_to_m;
char *movebuf,*timebuf;
{
unsigned char status;
if((*ptr_to_m = get_next_move(_gid_,&status,movebuf,timebuf,0,0))
== ERRMOVES)
{
fout((OB,
"?Can't find the latest move for the game %lu!\n",_gid_));
pout((
"?The game is probably non-existent, finished or there is bug somewhere.\n"));
return(0);
}
return(status);
}
int my_turnp(status,movecount)
unsigned char status;
MYINT movecount;
{
if((status == 'P') || (status == ('P'+1)) ||
(get_parity(movecount) != _owncolor_))
{
pout(("?It's not your turn!\n"));
return(0);
}
else if(movecount != _movesmade_)
{
pout((
"?Please load the whole game in with MOVES -N command before you make your move.\n"));
return(0);
}
return(1);
}
STAT()
{
readgidlist();
pout(("Games in which you are involved:\n"));
if(!checknewmoves(1)) { pout(("NONE.\n")); }
}
STATS()
{
MYINT plid,pid,count=0;
ULI seekpos=0L;
char *pattern;
char **fields;
char who_flag;
who_flag = (**cmds == 'W');
if(!*(cmds+1)) { pattern = (who_flag ? _everybody_ : _ownname_); }
else { pattern = conv_upper(*(cmds+1)); }
while(fields = getplfields(pattern,&seekpos,who_flag))
{
count++;
if(who_flag)
{
fout((OB,"%-25s %s\n",fields[PLF_NAME],fields[PLF_TIME]));
}
else
{
pid = atol(fields[PLF_PID]);
fout((OB,"Player %-25s plid: %lu flags: %s\n",
fields[PLF_NAME],atol(fields[PLF_PLID]),fields[PLF_FLAGS]));
fout((OB,"Currently %slogged on the %s.\n",
(pid ? "" : "not "),progname));
fout((OB,"Last logged %s %s\n",(pid ? "in" : "out"),
fields[PLF_TIME]));
fout((OB,
"Has used %s %lu times. %lu wins, %lu losses, %lu ties.\n",
progname,atol(fields[PLF_COUNT]),
atol(fields[PLF_WINS]),
atol(fields[PLF_LOSSES]),
atol(fields[PLF_TIES])));
fout((OB,"Address: %s\n\n",fields[PLF_ADDRESS]));
}
}
if(who_flag && (cmdcnt < 2))
{
fout((OB,"%lu player%s online.\n",count,
((count == 1) ? "" : "s"))); /* Use plural if count != 1. */
}
else if(count != 1)
{
fout((OB,"%lu names matched.\n",count));
}
}
SHELL()
{
int stat;
int euid,egid;
char *p;
/* Let's check that this uid is allowed to go to shell, and restore
the original uid & gid of user, so that (s)he can't do any
practical jokes with the uid this program is installed: */
if(!restore_real_uid(&euid,&egid)) { return(0); }
if(cmdcnt > 1) /* If arguments given, then use them. */
{ p = reconstruct_cmdline(cmds+1); }
else /* Otherwise use the default shell. */
{
#ifdef UNIX
if(!(p = getenv("SHELL"))) { p = "/bin/sh"; }
#else /* MS-DOS? */
if(!(p = getenv("COMSPEC"))) { p = "COMMAND"; }
#endif
}
/* Restore the normal mode when we go to shell. (If curses used.) */
end_screen();
stat = system(p);
_restore_effective_uid(euid,egid);
#ifdef CURSES
/* Wait for user to press something before we clear the screen again: */
fprintf(stderr,"<Press enter to return to %s>\n",progname);
getch();
#endif
init_screen();
if((cmdcnt < 2) && stat)
{
errlog((E1,"Couldn't execute %s, stat=%u, errno=%u\n",
p,stat,errno));
}
}
SAY()
{
if(!own_gamep()) { return(0); }
if(cmdcnt < 2)
{
pout(("Usage: SAY The message text.\n"));
pout(("(Sends a message to your opponent in this game.)\n"));
return(0);
}
/* Send the message to the opponent: */
return(gen_tell(_names_[!_owncolor_],'S'));
}
DOT()
{
if(cmdcnt < 2)
{
pout(("Usage: . The message text.\n"));
return(0);
}
return(gen_tell(".",'D'));
}
TELL()
{
if(cmdcnt < 3)
{
pout(("Usage: TELL player.name The message text.\n"));
return(0);
}
return(gen_tell(conv_upper(*(cmds+1)),'T'));
}
SHOUT()
{
MYINT count;
if(cmdcnt < 2)
{
pout(("Usage: SHOUT The message text.\n"));
pout(("(Sends a message to all players currently online.)\n"));
return(0);
}
count = find_all_online(reconstruct_cmdline(cmds+1),'B',_everybody_);
fout((OB,"Message sent to %lu player%s.\n",count,
((count == 1) ? "" : "s"))); /* Use plural if count != 1. */
}
/* Now W and WHO commands are assigned to STATS() function.
WHO()
{
MYINT count;
count = find_all_online(NULL,0,_everybody_);
fout((OB,"%lu player%s online.\n",count,
((count == 1) ? "" : "s")));
}
*/
SET()
{
pout(("Enter HELP SET to see what set commands are available.\n"));
}
SET_ADDRESS()
{
char *address;
char *p=NULL;
if(cmdcnt < 2)
{
ertzu:
pout((
"Usage: SET ADDRESS your-email-address (Or use NONE if you have no address)\n"));
return(0);
}
address = *(cmds+1);
if(strlen(address) > MAX_ADR_LENGTH)
{
fout((OB,
"?Sorry, your address is too long. Max. length is %u.\n",
MAX_ADR_LENGTH));
return(0);
}
/* At least one @, %, !, / or : is required. */