forked from accreator/Yixin-Board
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
6813 lines (6423 loc) · 203 KB
/
main.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
#define _CRT_SECURE_NO_WARNINGS /* for vs */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "yixin.h"
#include "resource.h" /* not needed if you are not provided with resource.h, Yixin.rc and icon.ico */
#define MAX_SIZE 22
#define CAUTION_NUM 5 //0..CAUTION_NUM
#define MIN_SPLIT_DEPTH 5
#define MAX_SPLIT_DEPTH 20
#define MAX_TOOLBAR_ITEM 64
#define MAX_TOOLBAR_COMMAND_LEN 2048
#define MAX_HOTKEY_ITEM 64
#define MAX_HOTKEY_COMMAND_LEN 2048
int yixin_strnicmp(const char *string1, const char *string2, size_t count)
{
#ifdef _WIN32
return _strnicmp(string1, string2, count);
#else
return strncasecmp(string1, string2, count);
#endif
}
int respawn = 0;
int showdatabase = 1;
int usedatabase = 1;
int databasereadonly = 0;
int boardsizeh = 15, boardsizew = 15;
int rboardsizeh = 15, rboardsizew = 15;
int inforule = 0;
int specialrule = 0;
int infopondering = 0;
int infovcthread = 0;
int64_t timeoutturn = 10000;
int64_t timeoutmatch = 2000000;
int64_t maxnode = 1000000000;
int maxdepth = 100;
int maxthreadnum = 1; //1..maxthreadnum
int maxhashsize = 30;
int increment = 0;
int computerside = 0; /* 0 none 1 black 2 while 3 black&white */
int cautionfactor = 1;
int threadnum = 1;
int hashsize = 19;
int threadsplitdepth = 7;
int nbestsym = 0;
int board[MAX_SIZE][MAX_SIZE];
int boardnumber[MAX_SIZE][MAX_SIZE];
int movepath[MAX_SIZE*MAX_SIZE];
int forbid[MAX_SIZE][MAX_SIZE];
int boardblock[MAX_SIZE][MAX_SIZE];
int boardbestX = -1, boardbestY = -1;
int boardlose[MAX_SIZE][MAX_SIZE];
int boardpos[MAX_SIZE][MAX_SIZE];
int boardtag[MAX_SIZE][MAX_SIZE];
char boardtext[MAX_SIZE][MAX_SIZE][8];
int blockautoreset = 0;
int blockpathautoreset = 0;
int hashautoclear = 0;
int piecenum = 0;
char isthinking = 0, isgameover = 0, isneedrestart = 0, isneedomit = 0;
char isctrlpressed = 0;
char drawpieceonly = 0;
char bestline[MAX_SIZE*MAX_SIZE*5+1] = "";
int bestval;
int move5N;
int levelchoice = 0;
int commandmodel = 0;
int shownumber = 1;
int showlog = 1;
int showanalysis = 1;
int showclock = 0;
int showforbidden = 1;
int showboardtext = 1;
int showtoolbarboth = 1;
int showwarning = 1;
int showdbdelconfirm = 1;
int checktimeout = 1;
int toolbarpos = 1;
int fontbasesize = 10;
int language = 0; /* 0: English 1: Other languages */
int rlanguage = 0;
char **clanguage = NULL; /* Custom language */
int movx[8] = { 0, 0, 1, -1, 1, 1, -1, -1}; /* note that the order is related to winning checking function(s)*/
int movy[8] = { 1, -1, 0, 0, 1, -1, 1, -1};
/* engine */
GIOChannel *iochannelin, *iochannelout, *iochannelerr;
/* windowmain */
GtkWidget *windowmain;
GtkWidget *tableboard;
GtkWidget *imageboard[MAX_SIZE][MAX_SIZE];
GtkWidget *labelboard[2][MAX_SIZE];
GtkWidget *vboxwindowmain;
GdkPixbuf *pixbufboard[9][14];
GdkPixbuf *pixbufboardnumber[9][14][MAX_SIZE*MAX_SIZE+1][2];
GdkPixbuf *pixbufboardchar[9][14][128+100+100+100][2];
int imgtypeboard[MAX_SIZE][MAX_SIZE];
char piecepicname[80] = "piece.bmp";
char fontname[4][120];
/* log */
GtkWidget *textlog, *textpos, *textdbcomment;
GtkTextBuffer *buffertextlog, *buffertextcommand, *buffertextdbcomment, *buffertextpos;
GtkWidget *scrolledtextlog, *scrolledtextcommand, *scrolledtextdbcomment, *scrolledtextpos;
GtkWidget *toolbar, *posbar;
double hdpiscale = 1.0;
int toolbarnum = 6;
GtkWidget *savingdialog;
GtkWidget *windowclock;
GtkWidget *clocklabel[4];
GtkWidget *playerlabel[2];
int64_t timercomputerturn;
int64_t timercomputermatch;
int64_t timerhumanturn;
int64_t timerhumanmatch;
int timercomputerincrement;
int timerhumanincrement;
int timerstart;
int timerstatus = 0;
int timeoutflag = 0;
int recorddebuglog = 0;
FILE *debuglog;
int toolbarlng[MAX_TOOLBAR_ITEM] =
{
48,
46,
47,
49,
45,
44
};
char *toolbaricon[MAX_TOOLBAR_ITEM] =
{
GTK_STOCK_GOTO_FIRST,
GTK_STOCK_GO_BACK,
GTK_STOCK_GO_FORWARD,
GTK_STOCK_GOTO_LAST,
GTK_STOCK_STOP,
GTK_STOCK_EXECUTE
};
char toolbarcommand[MAX_TOOLBAR_ITEM][MAX_TOOLBAR_COMMAND_LEN] =
{
"undo all\n",
"undo one\n",
"redo one\n",
"redo all\n",
"thinking stop\n",
"thinking start\n"
};
int hotkeynum = 6;
int hotkeykey[MAX_HOTKEY_ITEM] =
{
13,
14,
15,
16,
53,
1
};
char hotkeycommand[MAX_HOTKEY_ITEM][MAX_HOTKEY_COMMAND_LEN] =
{
"undo all\n",
"redo all\n",
"undo one\n",
"redo one\n",
"thinking stop\n",
"thinking toggle\n"
};
char *hotkeynamelist[] = {
"",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"Ctrl + Up", "Ctrl + Down", "Ctrl + Left", "Ctrl + Right",
"Ctrl + 1", "Ctrl + 2", "Ctrl + 3", "Ctrl + 4", "Ctrl + 5", "Ctrl + 6", "Ctrl + 7", "Ctrl + 8", "Ctrl + 9", "Ctrl + 0",
"Ctrl + A", "Ctrl + B", "Ctrl + C", "Ctrl + D", "Ctrl + E", "Ctrl + F", "Ctrl + G", "Ctrl + H", "Ctrl + I", "Ctrl + J",
"Ctrl + K", "Ctrl + L", "Ctrl + M", "Ctrl + N", "Ctrl + O", "Ctrl + P", "Ctrl + Q", "Ctrl + R", "Ctrl + S", "Ctrl + T",
"Ctrl + U", "Ctrl + V", "Ctrl + W", "Ctrl + X", "Ctrl + Y", "Ctrl + Z",
"Escape", NULL
};
int hotkeykeylist[][2] =
{
{ 0, 0 }, //0
{ 0, GDK_F1 }, //1
{ 0, GDK_F2 }, //2
{ 0, GDK_F3 }, //3
{ 0, GDK_F4 }, //4
{ 0, GDK_F5 }, //5
{ 0, GDK_F6 }, //6
{ 0, GDK_F7 }, //7
{ 0, GDK_F8 }, //8
{ 0, GDK_F9 }, //9
{ 0, GDK_F10 }, //10
{ 0, GDK_F11 }, //11
{ 0, GDK_F12 }, //12
{ GDK_CONTROL_MASK, GDK_Up }, //13
{ GDK_CONTROL_MASK, GDK_Down }, //14
{ GDK_CONTROL_MASK, GDK_Left }, //15
{ GDK_CONTROL_MASK, GDK_Right }, //16
{ GDK_CONTROL_MASK, GDK_1 }, //17
{ GDK_CONTROL_MASK, GDK_2 }, //18
{ GDK_CONTROL_MASK, GDK_3 }, //19
{ GDK_CONTROL_MASK, GDK_4 }, //20
{ GDK_CONTROL_MASK, GDK_5 }, //21
{ GDK_CONTROL_MASK, GDK_6 }, //22
{ GDK_CONTROL_MASK, GDK_7 }, //23
{ GDK_CONTROL_MASK, GDK_8 }, //24
{ GDK_CONTROL_MASK, GDK_9 }, //25
{ GDK_CONTROL_MASK, GDK_0 }, //26
{ GDK_CONTROL_MASK, GDK_A }, //27
{ GDK_CONTROL_MASK, GDK_B }, //28
{ GDK_CONTROL_MASK, GDK_C }, //29
{ GDK_CONTROL_MASK, GDK_D }, //30
{ GDK_CONTROL_MASK, GDK_E }, //31
{ GDK_CONTROL_MASK, GDK_F }, //32
{ GDK_CONTROL_MASK, GDK_G }, //33
{ GDK_CONTROL_MASK, GDK_H }, //34
{ GDK_CONTROL_MASK, GDK_I }, //35
{ GDK_CONTROL_MASK, GDK_J }, //36
{ GDK_CONTROL_MASK, GDK_K }, //37
{ GDK_CONTROL_MASK, GDK_L }, //38
{ GDK_CONTROL_MASK, GDK_M }, //39
{ GDK_CONTROL_MASK, GDK_N }, //40
{ GDK_CONTROL_MASK, GDK_O }, //41
{ GDK_CONTROL_MASK, GDK_P }, //42
{ GDK_CONTROL_MASK, GDK_Q }, //43
{ GDK_CONTROL_MASK, GDK_R }, //44
{ GDK_CONTROL_MASK, GDK_S }, //45
{ GDK_CONTROL_MASK, GDK_T }, //46
{ GDK_CONTROL_MASK, GDK_U }, //47
{ GDK_CONTROL_MASK, GDK_V }, //48
{ GDK_CONTROL_MASK, GDK_W }, //49
{ GDK_CONTROL_MASK, GDK_X }, //50
{ GDK_CONTROL_MASK, GDK_Y }, //51
{ GDK_CONTROL_MASK, GDK_Z }, //52
{ 0, GDK_Escape } //53
};
char * _T(char *s)
{
return g_locale_to_utf8(s, -1, 0, 0, 0);
}
char * __invT(char *s)
{
return g_locale_from_utf8(s, -1, 0, 0, 0);
}
void print_log(char *text)
{
GtkTextIter start, end;
GtkTextMark *endmark;
static int init = 0;
static int flag = 0, fspace = 0;
int len;
int i;
if(buffertextlog == NULL)
{
printf("%s", text);
return;
}
if (commandmodel == 0)
{
if (strncmp(text, "OK", 2) == 0) text += 2;
if (strncmp(text, "MESSAGE", 7) == 0) text += 7 + 1;
if (strncmp(text, "DETAIL", 6) == 0) text += 6 + 1;
if (strncmp(text, "DEBUG", 5) == 0) text += 5 + 1;
if (strncmp(text, "ERROR", 5) == 0) text += 5;
if (strncmp(text, "UNKNOWN", 7) == 0) text += 7;
//if(strncmp(text, "FORBID", 6) == 0) text += 6;
if (strncmp(text, "YIXIN", 5) == 0 || strncmp(text, "DEEP YIXIN", 10) == 0)
{
if (flag == 0) flag = 1; else return;
}
for (i = 0; text[i]; i++)
{
if (!isspace(text[i])) break;
}
if (text[i] == 0)
{
fspace++;
}
else
{
fspace = 0;
}
if (fspace >= 2) return;
}
len = gtk_text_buffer_get_line_count(GTK_TEXT_BUFFER(buffertextlog));
if(len > 800)
{
gtk_text_buffer_get_iter_at_line(GTK_TEXT_BUFFER(buffertextlog), &start, 0);
gtk_text_buffer_get_iter_at_line(GTK_TEXT_BUFFER(buffertextlog), &end, len - 800);
gtk_text_buffer_delete(GTK_TEXT_BUFFER(buffertextlog), &start, &end);
}
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffertextlog), &start, &end);
gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffertextlog), &end, text, strlen(text));
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(buffertextlog), &end);
gtk_text_iter_set_line_offset(&end, 0);
if(init == 0)
{
init = 1;
gtk_text_buffer_create_mark(GTK_TEXT_BUFFER(buffertextlog), "scroll", &end, TRUE);
}
endmark = gtk_text_buffer_get_mark(GTK_TEXT_BUFFER(buffertextlog), "scroll");
gtk_text_buffer_move_mark(GTK_TEXT_BUFFER(buffertextlog), endmark, &end);
gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(textlog), endmark);
}
int printf_log(char *fmt, ...)
{
int cnt;
char buffer[8192];
char text[8192], *p = text;
int i;
va_list va;
va_start(va,fmt);
cnt = vsprintf(buffer, fmt, va);
if(language)
{
for(i=0; buffer[i]; )
{
if(strncmp(buffer+i, "BESTLINE", 8) == 0)
{
strcpy(p, clanguage[0]);
p += strlen(clanguage[0]);
i += 8;
continue;
}
if(strncmp(buffer+i, "EVALUATION", 10) == 0)
{
strcpy(p, clanguage[1]);
p += strlen(clanguage[1]);
i += 10;
continue;
}
if(strncmp(buffer+i, "SPEED", 5) == 0)
{
strcpy(p, clanguage[2]);
p += strlen(clanguage[2]);
i += 5;
continue;
}
if(strncmp(buffer+i, "TIME", 4) == 0)
{
strcpy(p, clanguage[3]);
p += strlen(clanguage[3]);
i += 4;
continue;
}
if(strncmp(buffer+i, "DEPTH", 5) == 0)
{
strcpy(p, clanguage[4]);
p += strlen(clanguage[4]);
i += 5;
continue;
}
if(strncmp(buffer+i, "BLOCK", 5) == 0)
{
strcpy(p, clanguage[5]);
p += strlen(clanguage[5]);
i += 5;
continue;
}
if(strncmp(buffer+i, "NODE", 4) == 0)
{
strcpy(p, clanguage[6]);
p += strlen(clanguage[6]);
i += 4;
continue;
}
if(strncmp(buffer+i, "VAL", 3) == 0)
{
strcpy(p, clanguage[7]);
p += strlen(clanguage[7]);
i += 3;
continue;
}
if(strncmp(buffer+i, "MS", 2) == 0)
{
strcpy(p, clanguage[8]);
p += strlen(clanguage[8]);
i += 2;
continue;
}
if(strncmp(buffer+i, "RULE", 4) == 0)
{
strcpy(p, clanguage[9]);
p += strlen(clanguage[9]);
i += 4;
continue;
}
*p = buffer[i];
p ++;
i ++;
}
*p = '\0';
}
else
{
strcpy(text, buffer);
}
print_log(_T(text));
va_end(va);
return cnt;
}
void print_command(char *text)
{
GtkTextIter start, end;
if(buffertextcommand == NULL)
{
printf("%s", text);
return;
}
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffertextcommand), &start, &end);
gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffertextcommand), &end, text, strlen(text));
}
int printf_command(char *fmt, ...)
{
int cnt;
char buffer[1024];
va_list va;
va_start(va,fmt);
cnt = vsprintf(buffer, fmt, va);
print_command(_T(buffer));
va_end(va);
return cnt;
}
void show_welcome()
{
printf_log("Yixin Board "VERSION"\n");
if(language)
{
printf_command(clanguage[10]);
}
else
{
printf_command("To get help, type help and press Enter here");
}
}
void show_thanklist()
{
printf_log(language==0?"Thanks to all who helped development of Yixin:":
clanguage[11]);
printf_log("\n");
printf_log(" 彼方\n");
printf_log(" XR\n");
printf_log(" 舒自均\n");
printf_log(" Tianyi Hao\n");
printf_log(" Hao Wu\n");
printf_log(" 雨中飞燕\n");
printf_log(" Tuyen Do\n");
printf_log(" 肥国乃乃\n");
printf_log(" Saturn|Titan\n");
printf_log(" 元\n");
printf_log(" Alexander Bogatirev\n");
printf_log(" Epifanov Dmitry\n");
printf_log(" TZ\n");
printf_log(" 濤声依旧\n");
printf_log(" 嘿嘿\n");
printf_log(" 张锡森\n");
printf_log(" ax_pokl\n");
printf_log(" Ola Strom");
printf_log("\n");
}
GdkPixbuf *draw_overlay_scaled(GdkPixbuf *pb, int w, int h, gchar *text, char *color, char *weight, float scale)
{
GdkPixmap *pm;
GdkGC *gc;
GtkWidget *scratch;
PangoLayout *layout;
gchar *markup;
GdkPixbuf *ret;
gchar format[100];
PangoRectangle ink_rect, logical_rect;
pm = gdk_pixmap_new(windowmain->window, w, h, -1);
gdk_drawable_set_colormap(pm, gdk_colormap_get_system());
gc = gdk_gc_new(pm);
gdk_draw_pixbuf(pm, gc, pb, 0, 0, 0, 0, w, h, GDK_RGB_DITHER_NONE, 0, 0);
scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_realize(scratch);
layout = gtk_widget_create_pango_layout(scratch, NULL);
gtk_widget_destroy(scratch);
sprintf(format, "<span foreground='%s' weight='%s' size='%d' font_family='%s'>%%s</span>",
color, weight, (int)(fontbasesize * 1000 * scale), fontname[0]);
gchar *escaped_text = g_markup_escape_text(text, -1);
markup = g_strdup_printf(format, escaped_text);
pango_layout_set_markup(layout, markup, -1);
g_free(escaped_text);
g_free(markup);
pango_layout_get_pixel_extents(layout, &ink_rect, &logical_rect);
gdk_draw_layout(pm, gc, w/2 - logical_rect.width/2, h/2 - logical_rect.height/2, layout);
g_object_unref(layout);
ret = gdk_pixbuf_get_from_drawable(NULL, pm, NULL, 0, 0, 0, 0, w, h);
g_object_unref(pm);
g_object_unref(gc);
return ret;
}
GdkPixbuf *draw_overlay(GdkPixbuf *pb, int w, int h, gchar *text, char *color) {
return draw_overlay_scaled(pb, w, h, text, color, "bold", 1.0f);
}
void send_command(char *command)
{
gsize size;
g_io_channel_write_chars(iochannelin, command, -1, &size, NULL);
g_io_channel_flush(iochannelin, NULL);
if (debuglog != NULL)
{
fprintf(debuglog, "SEND_COMMAND [%s,%s,%s,%s]: %s\n",
gtk_label_get_text(clocklabel[0]),
gtk_label_get_text(clocklabel[1]),
gtk_label_get_text(clocklabel[2]),
gtk_label_get_text(clocklabel[3]),
command);
fflush(debuglog);
}
}
int swap2done = 0;
int boardtagclear = 0;
void clear_board_tag() {
if (boardtagclear == 0) {
boardtagclear = 1;
memset(boardtag, 0, sizeof(boardtag));
memset(boardtext, 0, sizeof(boardtext));
}
}
typedef struct {
double r; // a fraction between 0 and 1
double g; // a fraction between 0 and 1
double b; // a fraction between 0 and 1
} rgb;
typedef struct {
double h; // angle in degrees
double s; // a fraction between 0 and 1
double v; // a fraction between 0 and 1
} hsv;
rgb hsv2rgb(hsv in)
{
double hh, p, q, t, ff;
long i;
rgb out;
if(in.s <= 0.0) { // < is bogus, just shuts up warnings
out.r = in.v;
out.g = in.v;
out.b = in.v;
return out;
}
hh = in.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = in.v * (1.0 - in.s);
q = in.v * (1.0 - (in.s * ff));
t = in.v * (1.0 - (in.s * (1.0 - ff)));
switch(i) {
case 0:
out.r = in.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = in.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = in.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = in.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = in.v;
break;
case 5:
default:
out.r = in.v;
out.g = p;
out.b = q;
break;
}
return out;
}
int losssaturation = 0;
int winsaturation = 0;
int minsaturation = 0;
int maxsaturation = 0;
int colorvalue = 100;
const char* winrate2colorstr(int winrate) {
static char color_buf[10] = "#";
hsv hsv_color = {
(100 - winrate) * (180 / 100.0),
(winrate <= 0 ? losssaturation : winrate >= 100 ? winsaturation : minsaturation + (maxsaturation - minsaturation) * (winrate / 100.0)) * 0.01,
colorvalue * 0.01
};
rgb rgb_color = hsv2rgb(hsv_color);
sprintf(color_buf + 1, "%02X", (int)(rgb_color.r * 255));
sprintf(color_buf + 3, "%02X", (int)(rgb_color.g * 255));
sprintf(color_buf + 5, "%02X", (int)(rgb_color.b * 255));
return color_buf;
}
int refreshboardflag = 0; //it is set to 1 when making the 5th move under soosorv rule, otherwise, 0
int refreshboardflag2 = 0; //it is set to 1 when refreshboardflag has been set to 1
void refresh_board_area(int x0, int y0, int x1, int y1)
{
for (int i = y0; i < y1; i++)
{
for (int j = x0; j < x1; j++)
{
if (board[i][j] == 0)
{
if (drawpieceonly)
continue;
int f = 0;
if (inforule == 2 && (computerside & 1) == 0 && piecenum % 2 == 0 && forbid[i][j] && isgameover == 0 && isthinking == 0 && showforbidden) f = 2;
if (f == 0)
{
if (boardblock[i][j]) f = 10;
else if (showanalysis)
{
if (boardlose[i][j]) f = 7;
else if (i == boardbestY && j == boardbestX) f = 8;
else if (boardpos[i][j] == 1) f = 9;
else if (boardpos[i][j] == 2) f = 11;
}
if (f == 0 && usedatabase && showdatabase)
{
if (boardtag[i][j])
f = 12 + piecenum % 2;
}
}
if (f <= 11 || boardtag[i][j] <= 0 && (!showboardtext || boardtext[i][j][0] == 0))
{
if (imgtypeboard[i][j] <= 8)
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[imgtypeboard[i][j]][max(0, f)]);
else
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[0][max(1, f)]);
}
else
{
int x, y;
GdkPixbuf *p = NULL;
char n[10];
if (imgtypeboard[i][j] <= 8)
{
y = imgtypeboard[i][j];
x = 0;
}
else
{
y = 0;
x = 1;
}
int W = gdk_pixbuf_get_width(pixbufboard[y][x]);
int H = gdk_pixbuf_get_height(pixbufboard[y][x]);
const char* color = piecenum % 2 ? "#FFFFFF" : "#000000";
const char* weight = "normal";
if (showboardtext && boardtext[i][j][0])
{
int len = strlen(boardtext[i][j]);
char *text = _T(boardtext[i][j]);
float scale = 1.0f - 0.1f * max(len - 4, 0);
p = draw_overlay_scaled(pixbufboard[y][x], W, H, text, color, "bold", scale);
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), p);
g_object_unref(G_OBJECT(p));
g_free(text);
continue;
}
#define UPPERCASE_TAG(tag) ((tag) == 'w' ? 'W' : ((tag) == 'l' ? 'L' : ((tag) == 'd' ? 'D' : (tag))))
int tag = boardtag[i][j];
if (tag < 128)
{
tag = UPPERCASE_TAG(tag);
if (pixbufboardchar[y][x][tag][piecenum % 2] == NULL)
{
if (tag == 'W')
color = winrate2colorstr(100), weight = "bold";
else if (tag == 'L')
color = winrate2colorstr(0);
sprintf(n, "%c", tag);
pixbufboardchar[y][x][tag][piecenum % 2] = draw_overlay_scaled(pixbufboard[y][x], W, H, n, color, weight, 1.0f);
}
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboardchar[y][x][tag][piecenum % 2]);
}
else
{
int first = 0; // first character of the tag
float scale;
if (tag >> 16 == 0)
sprintf(n, "%c%c", first = UPPERCASE_TAG(tag / 256), tag % 256), scale = 0.97f;
else if (tag >> 24 == 0)
sprintf(n, "%c%c%c", first = UPPERCASE_TAG(tag / 65536), tag / 256 % 256, tag % 256), scale = 0.95f;
else
sprintf(n, "%c%c%c%c", first = UPPERCASE_TAG(tag / 16777216), tag / 65536 % 256, tag / 256 % 256, tag % 256), scale = 0.9f;
if (first == 'W' || first == 'L') {
if (first == 'W')
color = winrate2colorstr(100), weight = "bold", scale *= 0.98f;
else
color = winrate2colorstr(0), scale *= 0.95f;
int step = n[1] == '*' && n[2] == 0 ? 0 : atoi(n+1);
if (step == 0) n[1] = '*', n[2] = 0;
if (step >= 0 && step < 100) {
// use cached Win/Lose pixbuf
int idx = step + 100 * (first == 'L') + 128;
if (pixbufboardchar[y][x][idx][piecenum % 2] == NULL) {
pixbufboardchar[y][x][idx][piecenum % 2] = draw_overlay_scaled(pixbufboard[y][x], W, H, n, color, weight, scale);
}
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboardchar[y][x][idx][piecenum % 2]);
continue;
}
} else if (tag % 256 == '%') {
weight = "bold", scale *= 0.98f;
int winrate = atoi(n);
if (winrate >= 0 && winrate < 100) {
// use cached Winrate pixbuf
int idx = winrate + 100 * 2 + 128;
if (pixbufboardchar[y][x][idx][piecenum % 2] == NULL) {
color = winrate2colorstr(max(winrate, 1)); // use winrate=1 at least
pixbufboardchar[y][x][idx][piecenum % 2] = draw_overlay_scaled(pixbufboard[y][x], W, H, n, color, weight, scale);
}
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboardchar[y][x][idx][piecenum % 2]);
continue;
}
}
p = draw_overlay_scaled(pixbufboard[y][x], W, H, n, color, weight, scale);
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), p);
g_object_unref(G_OBJECT(p));
}
}
}
else
{
int f = 0, _f = 0;
int x, y;
int bn = boardnumber[i][j];
int bz = 3 + board[i][j] - 1;
if(movepath[piecenum-1]/boardsizew == i && movepath[piecenum-1]%boardsizew == j) f = 2;
if (refreshboardflag == 1)
{
if (specialrule == 3)
{
int k;
for (k = 4; k < piecenum - 1; k++)
{
if (movepath[k] / boardsizew == i && movepath[k] % boardsizew == j)
{
_f = 2;
bz -= k % 2;
bn = 5;
break;
}
}
if (f)
{
bz -= (piecenum - 1) % 2;
bn = 5;
}
}
}
if(shownumber)
{
y = imgtypeboard[i][j]%9;
x = bz;
int W = gdk_pixbuf_get_width(pixbufboard[y][x]);
int H = gdk_pixbuf_get_height(pixbufboard[y][x]);
if(pixbufboardnumber[y][x][bn][(f || _f)?1:0] == NULL)
{
char n[10];
sprintf(n, "%d", bn);
if(f || _f)
{
pixbufboardnumber[y][x][bn][1] = draw_overlay(pixbufboard[y][x], W, H, n, "#FF0000");
}
else
{
if(boardnumber[i][j] % 2 == 1)
pixbufboardnumber[y][x][bn][0] = draw_overlay(pixbufboard[y][x], W, H, n, "#FFFFFF");
else
pixbufboardnumber[y][x][bn][0] = draw_overlay(pixbufboard[y][x], W, H, n, "#000000");
}
}
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboardnumber[y][x][bn][(f || _f)?1:0]);
}
else
{
y = imgtypeboard[i][j]%9;
x = f+_f+bz;
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[y][x]);
}
}
}
}
}
void refresh_board()
{
refresh_board_area(0, 0, boardsizew, boardsizeh);
}
void update_textpos()
{
char posstring[1024];
int offset, accumulatedOffset = 0;
sprintf(posstring, "");
for (int i = 0; i < piecenum; i++) {
sprintf(posstring + accumulatedOffset, "%c%d%n", movepath[i] % boardsizew + 'a',
boardsizeh - 1 - movepath[i] / boardsizew + 1, &offset);
accumulatedOffset += offset;
}
gtk_text_buffer_set_text(buffertextpos, posstring, -1);
}
int is_legal_move(int y, int x)
{
return y>=0 && x>=0 && y<boardsizeh && x<boardsizew && board[y][x] == 0;
}
void make_move(int y, int x)
{
int i, j, k;
board[y][x] = piecenum%2+1;
boardnumber[y][x] = piecenum+1;
if(movepath[piecenum] != y*boardsizew+x)
{
movepath[piecenum] = y*boardsizew+x;
for(i=piecenum+1; i<MAX_SIZE*MAX_SIZE; i++) movepath[i] = -1;
}
piecenum ++;
if(piecenum == boardsizeh*boardsizew) isgameover = 1;
memset(bestline, 0, sizeof(bestline));
boardbestX = boardbestY = -1;
memset(boardlose, 0, sizeof(boardlose));
memset(boardpos, 0, sizeof(boardpos));
drawpieceonly = usedatabase;
refresh_board();
drawpieceonly = 0;
update_textpos();
for(i=0; i<8; i+=2)
{
int ny, nx;
k = 1;
ny = y;
nx = x;
for(j=1; j<6; j++)
{
ny += movy[i];
nx += movx[i];
if(nx<0 || ny<0 || nx>=boardsizew || ny>=boardsizeh) break;
if(board[ny][nx] != board[y][x]) break;
k ++;
}
ny = y;
nx = x;
for(j=1; j<6; j++)
{
ny -= movy[i];
nx -= movx[i];
if(nx<0 || ny<0 || nx>=boardsizew || ny>=boardsizeh) break;
if(board[ny][nx] != board[y][x]) break;
k ++;
}
if(k==5 || (k>5 && inforule != 1))
{
isgameover = 1;
break;
}
}
}
void show_database()
{
int i;
char command[80];
if (usedatabase)
{
sprintf(command, "yxquerydatabaseallt\n"); // query all & text
send_command(command);
for (i = 0; i < piecenum; i++)
{
sprintf(command, "%d,%d\n", movepath[i] / boardsizew,
movepath[i] % boardsizew);
send_command(command);
}
sprintf(command, "done\n");
send_command(command);
}
}
void show_forbid()
{
int i;
char command[80];
if((computerside&1) || (piecenum%2))
{
memset(forbid, 0, sizeof(forbid));
return;
}
sprintf(command, "start %d %d\n", boardsizew, boardsizeh);
send_command(command);
sprintf(command, "yxboard\n");
send_command(command);
for(i=0; i<piecenum; i++)
{
sprintf(command, "%d,%d,%d\n", movepath[i]/boardsizew,
movepath[i]%boardsizew, piecenum%2==i%2 ? 1 : 2);
send_command(command);
}
sprintf(command, "done\n");
send_command(command);
sprintf(command, "yxshowforbid\n");
send_command(command);
}
void show_dialog_undo_warning_query(GtkWidget *window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, language==0?"The operation will stop the calculation. Do you want to continue?":_T(clanguage[12]));
gtk_window_set_title(GTK_WINDOW(dialog), "Yixin");
result = gtk_dialog_run(GTK_DIALOG(dialog));
switch(result)
{
case GTK_RESPONSE_YES:
change_piece(window, (gpointer)1);
break;
case GTK_RESPONSE_NO:
break;
}
gtk_widget_destroy(dialog);
}
void show_dialog_swap_query2(GtkWidget *window)