ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
Revision: 1.52
Committed: Thu Jun 12 21:03:03 2003 UTC (20 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.51: +11 -9 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.36 use utf8;
2    
3 pcg 1.23 package game::goclock;
4    
5 pcg 1.37 # Lo and Behold! I admit it! The rounding stuff etc.. in goclock
6     # is completely borked.
7    
8 pcg 1.23 use Time::HiRes ();
9    
10     use KGS::Constants;
11    
12     use base gtk::widget;
13    
14     sub new {
15     my $class = shift;
16     my $self = $class->SUPER::new(@_);
17    
18     $self->{widget} = new Gtk2::Label;
19    
20     $self->{set} = sub { };
21     $self->{format} = sub { "ERROR" };
22    
23     $self;
24     }
25    
26 pcg 1.38 sub configure {
27 pcg 1.23 my ($self, $timesys, $main, $interval, $count) = @_;
28    
29     if ($timesys == TIMESYS_ABSOLUTE) {
30     $self->{set} = sub { $self->{time} = $_[0] };
31 pcg 1.37 $self->{format} = sub { util::format_time $_[0] };
32 pcg 1.23
33     } elsif ($timesys == TIMESYS_BYO_YOMI) {
34     my $low = $interval * $count;
35    
36     $self->{set} = sub { $self->{time} = $_[0] };
37    
38     $self->{format} = sub {
39     if ($_[0] > $low) {
40 pcg 1.37 util::format_time $_[0] - $low;
41 pcg 1.23 } else {
42 pcg 1.34 sprintf "%s (%d)",
43 pcg 1.37 util::format_time int (($_[0] - 1) % $interval + 1),
44 pcg 1.43 ($_[0] - 1) / $interval;
45 pcg 1.23 }
46     };
47    
48     } elsif ($timesys == TIMESYS_CANADIAN) {
49     $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
50    
51     $self->{format} = sub {
52 pcg 1.34 if (!$self->{moves}) {
53 pcg 1.37 util::format_time $_[0] - $low;
54 pcg 1.23 } else {
55 pcg 1.38 my $time = int (($_[0] - 1) % $interval + 1);
56    
57 pcg 1.47 sprintf "%s/%d [%d]",
58 pcg 1.37 util::format_time $time,
59 pcg 1.34 $self->{moves},
60 pcg 1.37 $time / ($self->{moves} || 1);
61 pcg 1.34
62 pcg 1.23 }
63     };
64    
65     } else {
66     # none, or unknown
67     $self->{set} = sub { };
68     $self->{format} = sub { "---" }
69     }
70     }
71    
72     sub refresh {
73     my ($self, $timestamp) = @_;
74 pcg 1.38 my $timer = $self->{time} + $self->{start} - $timestamp;
75 pcg 1.36
76     # we round the timer value slightly... the protocol isn't exact anyways,
77     # and this gives smoother timers ;)
78 pcg 1.38 my @format = $self->{format}->(int ($timer + 0.4));
79 pcg 1.36 $self->{widget}->set_text ($self->{format}->(int ($timer + 0.4)));
80 pcg 1.23
81     $timer - int $timer;
82     }
83    
84     sub set_time {
85     my ($self, $time) = @_;
86    
87 pcg 1.27 # we ignore requests to re-set the time of a running clock.
88     # this is the easiest way to ensure that commentary etc.
89     # doesn't re-set the clock. yes, this is frickle design,
90 pcg 1.36 # but I think the protocol is to blame here, which gives
91 pcg 1.27 # very little time information. (cgoban2 also has had quite
92     # a lot of small time update problems...)
93     unless ($self->{timeout}) {
94     $self->{set}->($time->[0], $time->[1]);
95     $self->refresh ($self->{start});
96     }
97 pcg 1.23 }
98    
99     sub start {
100 pcg 1.36 my ($self, $when) = @_;
101 pcg 1.23
102 pcg 1.36 $self->stop;
103 pcg 1.23
104 pcg 1.36 $self->{start} = $when;
105 pcg 1.27
106 pcg 1.23 my $timeout; $timeout = sub {
107 pcg 1.36 my $next = $self->refresh (Time::HiRes::time) * 1000;
108 pcg 1.27 $next += 1000 if $next < 0;
109 pcg 1.23 $self->{timeout} = add Glib::Timeout $next, $timeout;
110     0;
111     };
112    
113     $timeout->();
114     }
115    
116     sub stop {
117     my ($self) = @_;
118    
119     remove Glib::Source delete $self->{timeout} if $self->{timeout};
120 pcg 1.24 }
121    
122     sub destroy {
123     my ($self) = @_;
124     $self->stop;
125     $self->SUPER::destroy;
126 pcg 1.23 }
127    
128     package game::userpanel;
129    
130     use base gtk::widget;
131    
132     sub new {
133     my $class = shift;
134     my $self = $class->SUPER::new(@_);
135    
136 pcg 1.26 $self->{widget} = new Gtk2::HBox;
137 pcg 1.23
138 pcg 1.26 $self->{widget}->add (my $vbox = new Gtk2::VBox);
139    
140     $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
141     $vbox->add ($self->{info} = new Gtk2::Label "");
142     $vbox->add (($self->{clock} = new game::goclock)->widget);
143 pcg 1.23
144 pcg 1.32 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
145    
146 pcg 1.23 $self;
147     }
148    
149 pcg 1.37 sub configure {
150 pcg 1.38 my ($self, $user, $rules) = @_;
151 pcg 1.23
152 pcg 1.38 if ($self->{name}->get_text ne $user->as_string) {
153     $self->{name}->set_text ($user->as_string);
154 pcg 1.26
155 pcg 1.32 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
156     $self->{imagebox}->add (gtk::image_from_data undef);
157 pcg 1.33 $self->{imagebox}->show_all;
158 pcg 1.32
159 pcg 1.52 if ($user->has_pic) {
160     # the big picture...
161     appwin::userpic ($user->{name}, sub {
162     return unless $self->{imagebox};
163     if ($_[0]) {
164     $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
165     $self->{imagebox}->add (gtk::image_from_data $_[0]);
166     $self->{imagebox}->show_all;
167     }
168     });
169     }
170 pcg 1.26 }
171    
172 pcg 1.38 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
173 pcg 1.23 }
174    
175     sub set_state {
176 pcg 1.36 my ($self, $captures, $timer, $when) = @_;
177 pcg 1.23
178 pcg 1.36 $self->{clock}->stop unless $when;
179 pcg 1.23 $self->{clock}->set_time ($timer);
180 pcg 1.36 $self->{clock}->start ($when) if $when;
181 pcg 1.27
182 pcg 1.23 $self->{info}->set_text ("$captures pris.");
183     }
184    
185 pcg 1.1 package game;
186    
187     use KGS::Constants;
188     use KGS::Game::Board;
189    
190     use base KGS::Listener::Game;
191     use base KGS::Game;
192    
193 pcg 1.4 use base gtk::widget;
194    
195 pcg 1.15 use POSIX qw(ceil);
196    
197 pcg 1.1 sub new {
198     my $self = shift;
199     $self = $self->SUPER::new(@_);
200    
201     $self->listen($self->{conn});
202    
203     $self->{window} = new Gtk2::Window 'toplevel';
204 pcg 1.5 my $title = $self->{channel} ? $self->owner->as_string." ".$self->opponent_string : "Game Window";
205 pcg 1.1 $self->{window}->set_title("KGS Game $title");
206     gtk::state $self->{window}, "game::window", undef, window_size => [600, 500];
207    
208 pcg 1.4 $self->{window}->signal_connect(delete_event => sub {
209 pcg 1.22 $self->part;
210 pcg 1.25 $self->destroy;
211 pcg 1.4 1;
212     });
213 pcg 1.1
214 pcg 1.4 $self->{window}->add($self->{hpane} = new Gtk2::HPaned);
215     gtk::state $self->{hpane}, "game::hpane", undef, position => 500;
216 pcg 1.1
217 pcg 1.36 # LEFT PANE
218    
219     $self->{hpane}->pack1(($self->{left} = new Gtk2::VBox), 1, 0);
220 pcg 1.31
221     $self->{boardbox} = new Gtk2::VBox;
222    
223 pcg 1.4 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1);
224 pcg 1.1
225 pcg 1.31 # challenge
226    
227     $self->{challenge} = new challenge channel => $self->{channel};
228    
229     # board box (aspect/canvas)
230    
231     $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
232 pcg 1.1
233     {
234     $frame->add(my $vbox = new Gtk2::VBox);
235     $vbox->add($self->{title} = new Gtk2::Label $title);
236    
237 pcg 1.47 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 1, 0;
238 pcg 1.2
239 pcg 1.1 $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj});
240 pcg 1.2 $scale->set_draw_value (0);
241 pcg 1.30 $scale->set_digits (0);
242 pcg 1.1
243 pcg 1.23 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
244 pcg 1.1 }
245 pcg 1.31
246     $self->{boardbox}->pack_start((my $aspect_frame = new Gtk2::AspectFrame "", 0.5, 0.5, 1, 0), 1, 1, 0);
247 pcg 1.20 $aspect_frame->set (border_width => 0, shadow_type => 'none', label_xalign => 0.5);
248 pcg 1.19 $self->{board_label} = $aspect_frame->get_label_widget;
249    
250     $aspect_frame->add($self->{canvas} = new Gtk2::DrawingArea);
251 pcg 1.21 $self->{canvas}->double_buffered (0) if $::config->{conserve_memory};
252 pcg 1.1
253     $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
254     $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
255    
256 pcg 1.23 # RIGHT PANE
257    
258 pcg 1.36 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 1, 1);
259 pcg 1.4 $self->{hpane}->set(position_set => 1);
260     gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
261 pcg 1.1
262 pcg 1.4 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
263 pcg 1.1 $sw->set_policy("automatic", "always");
264    
265 pcg 1.18 $sw->add(($self->{userlist} = new userlist)->widget);
266 pcg 1.1
267 pcg 1.4 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
268 pcg 1.23
269     $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
270 pcg 1.26 $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget);
271     $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget);
272 pcg 1.1
273 pcg 1.30 $vbox->pack_start(($self->{text} = new gtk::text)->widget, 1, 1, 0);
274 pcg 1.1
275     $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0);
276     $self->{entry}->signal_connect(activate => sub {
277     my $text = $self->{entry}->get_text;
278 pcg 1.10 $self->say($text) if $text =~ /\S/;
279 pcg 1.1 $self->{entry}->set_text("");
280     });
281    
282 pcg 1.31 $self->event_update_game;
283 pcg 1.1 $self;
284     }
285    
286     sub event_update_users {
287 pcg 1.18 my ($self, $add, $update, $remove) = @_;
288 pcg 1.1
289 pcg 1.18 $self->{userlist}->update ($add, $update, $remove);
290 pcg 1.1 }
291    
292     sub join {
293     my ($self) = @_;
294 pcg 1.21 return if $self->{joined};
295    
296 pcg 1.1 $self->SUPER::join;
297    
298     $self->{window}->show_all;
299     }
300    
301     sub part {
302     my ($self) = @_;
303 pcg 1.22
304 pcg 1.1 $self->SUPER::part;
305 pcg 1.22 $self->destroy;
306 pcg 1.1 }
307    
308     sub configure_event {
309 pcg 1.2 my ($widget, $event, $self) = @_;
310 pcg 1.1 delete $self->{stack};
311     delete $self->{pixbuf};
312     delete $self->{board_shown};
313     delete $self->{background};
314     $self->repaint_board;
315 pcg 1.6 0;
316     }
317    
318     sub expose_event {
319     my ($widget, $event, $self) = @_;
320    
321     $self->{pixbuf} or return;
322    
323     my $area = $event->area;
324    
325 pcg 1.19 $self->redraw ($area);
326 pcg 1.6
327     0;
328 pcg 1.1 }
329    
330 pcg 1.2 # something Gtk2 fixed
331     sub INTERP_NEAREST (){ 'nearest' }
332     sub INTERP_TILES (){ 'tiles' }
333     sub INTERP_BILINEAR (){ 'bilinear' }
334     sub INTERP_HYPER (){ 'hyper' }
335 pcg 1.1
336     sub new_pixbuf {
337 pcg 1.2 my ($w, $h, $alpha, $fill) = @_;
338 pcg 1.1
339     my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
340 pcg 1.2 $pixbuf->fill ($fill) if defined $fill;
341 pcg 1.1
342     $pixbuf;
343     }
344    
345     sub scale_pixbuf {
346     my ($src, $w, $h, $mode) = @_;
347    
348     my $dst = new_pixbuf $w, $h, 1;
349    
350     $src->scale(
351     $dst, 0, 0, $w, $h, 0, 0,
352     $w / $src->get_width, $h / $src->get_height,
353     $mode,
354     );
355    
356     $dst;
357     }
358    
359     # create a stack of stones
360     sub create_stack {
361     my ($self, $mark, $size, $rand) = @_;
362    
363 pcg 1.10 my $shadow = $size * 0.05;
364 pcg 1.1
365     my $c = \$self->{stack}{$mark};
366     unless ($$c) {
367     for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
368 pcg 1.2 my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000;
369 pcg 1.1
370     # zeroeth the shadow
371     if ($mark & (MARK_B | MARK_W)) {
372 pcg 1.27 # the -0.5's are a mystery to me
373 pcg 1.14 $::shadow_img->composite (
374     $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5,
375 pcg 1.27 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
376 pcg 1.12 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
377 pcg 1.1 );
378     }
379    
380 pcg 1.38 # first the big stones (handicap stones could be different)
381     for ([MARK_B, $mark & MARK_MOVE ? 255 : 255],
382     [MARK_W, $mark & MARK_MOVE ? 255 : 255],
383 pcg 1.1 [MARK_GRAY_B, 128],
384     [MARK_GRAY_W, 128]) {
385     my ($mask, $alpha) = @$_;
386     if ($mark & $mask) {
387     $stone->composite (
388 pcg 1.11 $base, 0, 0, $size, $size, 0, 0,
389 pcg 1.1 $size / $stone->get_width, $size / $stone->get_height,
390 pcg 1.2 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha
391 pcg 1.1 );
392     }
393     }
394    
395 pcg 1.12 # then the small stones
396 pcg 1.1 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
397     [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
398     my ($mask, $img) = @$_;
399     if ($mark & $mask) {
400     $img->composite (
401 pcg 1.27 $base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2,
402 pcg 1.1 $size / $img->get_width / 2, $size / $img->get_height / 2,
403 pcg 1.15 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224
404 pcg 1.1 );
405     }
406     }
407    
408     # and lastly any markers
409     my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
410    
411     for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
412     [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
413     [MARK_SQUARE, $::square_img[$dark_bg]]) {
414     my ($mask, $img) = @$_;
415     if ($mark & $mask) {
416     $img->composite (
417 pcg 1.27 $base, 0, 0, $size, $size, 0, 0,
418 pcg 1.1 $size / $img->get_width, $size / $img->get_height,
419 pcg 1.27 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
420 pcg 1.1 );
421     }
422     }
423    
424     push @$$c, $base;
425     }
426     }
427    
428     $$c->[$rand % @$$c];
429     }
430    
431     sub pixbuf_text {
432     my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
433    
434     my @c = grep $_,
435     map $::font[$colour][$::fontmap{$_}],
436     split //, $text;
437    
438     if (@c) {
439     my $spacing = $height * 0.1;
440     my $s = $height / List::Util::max map $_->get_height, @c;
441     my $W = List::Util::sum map $_->get_width, @c;
442    
443     $x -= ($W * $s + $spacing * (@c - 1)) * 0.5;
444     $y -= $height * 0.5;
445    
446     for (@c) {
447     my $w = $_->get_width * $s;
448 pcg 1.17 # +2 == don't fight the rounding
449 pcg 1.1 $_->composite ($pixbuf,
450 pcg 1.17 $x, $y, $w+2, $height+2, $x, $y, $s, $s,
451 pcg 1.1 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
452    
453     $x += $w + $spacing;
454     }
455     }
456     }
457    
458     sub pixbuf_rect {
459     my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
460 pcg 1.2 # we fake lines by... a horrible method :/
461     my $colour_pb = new_pixbuf 1, 1, 0, $colour;
462     $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
463     INTERP_NEAREST, $alpha);
464 pcg 1.1 }
465    
466     sub repaint_board {
467     my ($self) = @_;
468     my $canvas = $self->{canvas};
469     my $expose_area = undef;
470    
471     return $expose_area unless $self->{board};
472    
473 pcg 1.2 my ($w, $h) = ($canvas->allocation->values)[2,3];
474 pcg 1.19
475     die "FATAL: board aspect ratio != 1" unless $w == $h;
476 pcg 1.1
477 pcg 1.19 my $s = $w;
478 pcg 1.6
479 pcg 1.19 return unless $s >= 200;
480 pcg 1.1
481     my $size = $self->{size};
482    
483 pcg 1.16 # we leave enough space for the shadows.. I like smaller stones, and we
484     # do no need to do the nifty recursive screen updates that goban2 does
485 pcg 1.1 my $border = int ($s / ($size + 3) * 0.5);
486     my $s2 = $s - $border * 2;
487 pcg 1.10 my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0);
488 pcg 1.1 my $ofs = int ($edge / 2);
489    
490     my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
491    
492     my $pixbuf;
493    
494     my $oldboard;
495    
496     if ($self->{background}) {
497     if ($oldboard = $self->{board_shown}) {
498     $pixbuf = $self->{pixbuf};
499     } else {
500     $pixbuf = $self->{background}->copy;
501 pcg 1.5 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
502 pcg 1.1 }
503     } else {
504 pcg 1.5 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
505 pcg 1.1
506     my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
507    
508     if ($s < $bw && $s < $bh) {
509 pcg 1.2 $pixbuf = new_pixbuf $s, $s, 0;
510 pcg 1.1 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
511     } else {
512     $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES;
513     }
514    
515 pcg 1.27 my $linew = int ($s / 40 / $size);
516 pcg 1.1
517     # ornamental border... we have time to waste :/
518 pcg 1.2 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255;
519     pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255;
520 pcg 1.27 pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255;
521     pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255;
522 pcg 1.1
523     for my $i (1 .. $size) {
524 pcg 1.2 pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192;
525     pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192;
526 pcg 1.1
527     # 38 max, but we allow a bit more
528     my $label = (qw(- A B C D E F G H J K L M N O P Q R S T U V W X Y Z
529     AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX YY ZZ))[$i];
530    
531     pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label;
532     pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label;
533     pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1;
534     pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1;
535    
536     $a++;
537     $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
538     }
539    
540 pcg 1.27 # hoshi points
541     my $hoshi = sub {
542     my ($x, $y) = @_;
543     my $hs = int ($edge / 4) | 1;
544     $x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2;
545    
546     # we use the shadow mask... not perfect, but I want to finish this
547     $::shadow_img->composite ($pixbuf,
548     $x, $y, $hs + 1, $hs + 1, $x, $y,
549     $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
550     $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
551     };
552    
553     my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
554     $hoshi->($h1, $h1);
555     $hoshi->($size - $h1 + 1, $h1);
556     $hoshi->($h1, $size - $h1 + 1);
557     $hoshi->($size - $h1 + 1, $size - $h1 + 1);
558    
559     if ($size % 2) { # on odd boards, also the remaining 5
560     my $h2 = ($size + 1) / 2;
561     if ($size > 10) {
562     $hoshi->($h1, $h2);
563     $hoshi->($size - $h1 + 1, $h2);
564     $hoshi->($h2, $size - $h1 + 1);
565     $hoshi->($h2, $h1);
566     }
567     # the tengen
568     $hoshi->($h2, $h2);
569     }
570    
571 pcg 1.21 unless ($::config->{conserve_memory} > 1) {
572 pcg 1.1 $self->{background} = $pixbuf;
573     $pixbuf = $pixbuf->copy;
574     }
575     }
576    
577     $self->{pixbuf} = $pixbuf;
578    
579     for my $x (1 .. $size) {
580     for my $y (1 .. $size) {
581     my $rand = ($x ^ $y ^ 0x5555);
582    
583     my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
584    
585     if ($::config->{randomize}) {
586     $dx += ($rand % 7) - 3;
587     $dy += ($rand / 3 % 7) - 3;
588     }
589    
590 pcg 1.10 my $shadow = $edge * 0.05;
591 pcg 1.1 my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow];
592    
593     my $mark = $self->{board}{board}[$x-1][$y-1];
594     my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0;
595    
596     if ($oldboard) {
597     next if $old == $mark; # no change
598    
599     $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
600     $expose_area = $expose_area
601 pcg 1.2 ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
602     : new Gtk2::Gdk::Rectangle @$area;
603 pcg 1.1 }
604    
605     if ($mark) {
606     my $pb = $self->create_stack($mark, $edge, $rand);
607    
608     $pb->composite ($pixbuf, @$area,
609     $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
610    
611     # labels are handled here because they are quite rare
612     if ($mark & MARK_LABEL) {
613     my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1;
614    
615     if ($white) {
616     pixbuf_text $pixbuf, 0,
617     $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7,
618     $self->{board}{label}[$x-1][$y-1];
619     }
620     pixbuf_text $pixbuf, $white,
621     $k[$x], $k[$y], $ofs * 0.7,
622     $self->{board}{label}[$x-1][$y-1];
623     }
624     }
625     }
626     }
627    
628     $self->{board_shown} = Storable::dclone $self->{board};
629     #d# save
630     #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
631    
632     $expose_area;
633     }
634    
635 pcg 1.2 sub redraw {
636 pcg 1.1 my ($self, $area) = @_;
637    
638     if ($area && $self->{pixbuf}) {
639 pcg 1.2 my ($x, $y, $w, $h) = $area->values;
640 pcg 1.1
641 pcg 1.2 $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
642 pcg 1.19 $x, $y, $x, $y, $w, $h,
643 pcg 1.2 "normal", 0, 0);
644 pcg 1.1 $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
645 pcg 1.19 $x - 1, $y - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
646 pcg 1.1 }
647     }
648    
649 pcg 1.23 sub update_board {
650     my ($self) = @_;
651     return unless $self->{path};
652    
653 pcg 1.47 #$self->{update_board_cb} ||= add Glib::Idle sub {
654     my $move = int $self->{moveadj}->get_value;
655 pcg 1.23
656 pcg 1.47 my $running = $move == @{$self->{path}};
657 pcg 1.23
658 pcg 1.47 $self->{board_label}->set_text ("Move " . ($move - 1));
659 pcg 1.23
660 pcg 1.47 $self->{board} = new KGS::Game::Board $self->{size};
661     $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
662 pcg 1.23
663 pcg 1.47 for my $colour (WHITE, BLACK) {
664     $self->{userpanel}[$colour]->set_state (
665     $self->{board}{captures}[$colour],
666     $self->{board}{timer}[$colour],
667     ($running && $self->{lastmove_colour} == !$colour)
668     ? $self->{lastmove_time} : 0
669     );
670     }
671    
672     $self->redraw ($self->repaint_board);
673 pcg 1.23
674 pcg 1.47 # delete $self->{update_board_cb};
675     #}
676 pcg 1.23 }
677    
678 pcg 1.1 sub event_update_tree {
679     my ($self) = @_;
680    
681     $self->{path} = $self->get_path;
682 pcg 1.30
683     if ($self->{moveadj}) {
684     my $upper = $self->{moveadj}->upper;
685     my $pos = $self->{moveadj}->get_value;
686 pcg 1.48 my $move = scalar @{$self->{path}};
687 pcg 1.30
688 pcg 1.48 $self->{moveadj}->upper ($move);
689 pcg 1.30
690     $self->{moveadj}->changed;
691     if ($pos == $upper) {
692 pcg 1.48 $self->{moveadj}->value ($move);
693     $self->{moveadj}->value_changed;
694 pcg 1.30 }
695     }
696 pcg 1.29 }
697    
698     sub event_update_comments {
699     my ($self, $node, $comment, $newnode) = @_;
700     $self->SUPER::event_update_comments($node, $comment, $newnode);
701    
702 pcg 1.30 my $text;
703    
704 pcg 1.34 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
705 pcg 1.29 if $newnode;
706    
707 pcg 1.30 for (split /\n/, $comment) {
708     $text .= "\n";
709 pcg 1.40 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
710 pcg 1.30 $text .= "<user>" . (util::toxml $1) . "</user>:";
711     }
712 pcg 1.40
713     # coords only for 19x19 so far
714     $_ = util::toxml $_;
715 pcg 1.39 s{
716     (
717     \b
718     (?:[bw])?
719     [, ]{0,2}
720 pcg 1.41 [a-hj-t] # valid for upto 19x19
721 pcg 1.40 \s?
722 pcg 1.42 [1-9]?[0-9]
723 pcg 1.40 \b
724 pcg 1.39 )
725     }{
726     "<coord>$1</coord>";
727     }sgexi;
728    
729 pcg 1.30 $text .= $_;
730     }
731    
732     $self->{text}->append_text ($text);
733 pcg 1.1 }
734    
735 pcg 1.31 sub event_join {
736     my ($self) = @_;
737     $self->SUPER::event_join;
738     }
739    
740 pcg 1.1 sub event_part {
741     my ($self) = @_;
742     $self->SUPER::event_part;
743 pcg 1.50 $self->destroy;
744 pcg 1.1 }
745    
746     sub event_move {
747     my ($self, $pass) = @_;
748     sound::play 1, $pass ? "pass" : "move";
749 pcg 1.25 }
750    
751     sub event_update_game {
752     my ($self) = @_;
753     $self->SUPER::event_update_game;
754 pcg 1.36
755 pcg 1.37 $self->{user}[BLACK] = $self->{user1};
756     $self->{user}[WHITE] = $self->{user2};
757    
758 pcg 1.38 # show board
759    
760     $self->{left}->remove ($_) for $self->{left}->get_children;
761     if ($self->is_valid) {
762     $self->{left}->add ($self->{boardbox});
763     (delete $self->{challenge})->destroy if $self->{challenge};
764     } else {
765     $self->{left}->add ($self->{challenge}->widget);
766     }
767     $self->{left}->show_all;
768    
769     # view text
770    
771 pcg 1.36 $text = "\n<header>Game Update</header>";
772    
773     $text .= "\nType: " . (util::toxml $gametype{$self->type})
774     . " (" . (util::toxml $gameopt{$self->option}) . ")";
775     $text .= "\nFlags:";
776     $text .= " valid" if $self->is_valid;
777     $text .= " adjourned" if $self->is_adjourned;
778     $text .= " scored" if $self->is_scored;
779     $text .= " saved" if $self->is_saved;
780    
781 pcg 1.37 $text .= "\nWhite: <user>" . (util::toxml $self->{user2}->as_string) . "</user>";
782     $text .= "\nBlack: <user>" . (util::toxml $self->{user1}->as_string) . "</user>";
783 pcg 1.36 $text .= "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>" if $self->{user3}->is_valid;
784    
785     if ($self->is_valid) {
786     $text .= "\nHandicap: " . $self->{handicap};
787     $text .= "\nKomi: " . $self->{komi};
788     $text .= "\nSize: " . $self->size_string;
789     }
790    
791 pcg 1.49 if ($self->is_scored) {
792     $text .= "\nResult: " . $self->score_string;
793     }
794    
795 pcg 1.38 $self->{text}->append_text ("<infoblock>$text</infoblock>");
796 pcg 1.25 }
797    
798 pcg 1.37 sub event_update_rules {
799     my ($self, $rules) = @_;
800    
801 pcg 1.38 $self->{userpanel}[$_]->configure ($self->{user}[$_], $rules)
802     for BLACK, WHITE;
803 pcg 1.37
804     my $text = "\n<header>Game Rules</header>";
805    
806     $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
807    
808 pcg 1.38 $text .= "\nTime: ";
809 pcg 1.37
810 pcg 1.38 if ($rules->{timesys} == TIMESYS_NONE) {
811 pcg 1.46 $text .= "UNLIMITED";
812 pcg 1.38 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
813     $text .= util::format_time $rules->{time};
814 pcg 1.46 $text .= " ABS";
815 pcg 1.38 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
816     $text .= util::format_time $rules->{time} - $rules->{interval} * $rules->{count};
817 pcg 1.46 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
818 pcg 1.38 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
819     $text .= util::format_time $rules->{time};
820 pcg 1.46 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
821 pcg 1.37 }
822    
823 pcg 1.38 $self->{text}->append_text ("<infoblock>$text</infoblock>");
824     }
825    
826     sub inject_resign_game {
827     my ($self, $msg) = @_;
828    
829 pcg 1.39 $self->{text}->append_text ("\n<infoblock><header>Resign</header>"
830     . "\n<user>"
831 pcg 1.38 . (util::toxml $self->{user}[$msg->{player}]->as_string)
832     . "</user> resigned.</infoblock>");
833     }
834    
835     sub inject_final_result {
836     my ($self, $msg) = @_;
837    
838 pcg 1.42 $self->{text}->append_text ("<infoblock>\n<header>Game Over</header>"
839 pcg 1.38 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
840     . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
841 pcg 1.42 . "</infoblock>"
842 pcg 1.38 );
843 pcg 1.37 }
844    
845 pcg 1.25 sub destroy {
846     my ($self) = @_;
847 pcg 1.37 $self->{userpanel}[$_] && (delete $self->{userpanel}[$_])->destroy
848     for BLACK, WHITE;
849 pcg 1.25 $self->SUPER::destroy;
850     delete $appwin::gamelist->{game}{$self->{channel}};
851 pcg 1.1 }
852    
853     1;
854