ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/game.pl
(Generate patch)

Comparing kgsueme/kgsueme/game.pl (file contents):
Revision 1.28 by pcg, Sun Jun 1 15:43:58 2003 UTC vs.
Revision 1.88 by pcg, Fri May 21 16:13:41 2004 UTC

1use utf8;
2
3use Scalar::Util ();
4
1package game::goclock; 5package game::goclock;
2 6
7# Lo and Behold! I admit it! The rounding stuff etc.. in goclock
8# is completely borked.
9
3use Time::HiRes (); 10use Time::HiRes ();
4 11
5use KGS::Constants; 12use KGS::Constants;
6 13
7use base gtk::widget; 14use Glib::Object::Subclass
15 Gtk2::Label;
8 16
9sub new { 17sub INIT_INSTANCE {
10 my $class = shift; 18 my $self = shift;
11 my $self = $class->SUPER::new(@_);
12 19
13 $self->{widget} = new Gtk2::Label; 20 $self->signal_connect (destroy => sub { $_[0]->stop });
14 21
15 $self->{set} = sub { }; 22 $self->{set} = sub { };
16 $self->{format} = sub { "ERROR" }; 23 $self->{format} = sub { "???" };
17
18 $self;
19} 24}
20 25
21sub append_text { 26sub configure {
22 my ($self, $text) = @_;
23
24 $self->{buffer}->insert ($self->{buffer}->get_end_iter, $text);
25}
26
27sub format_time {
28 my ($time) = @_;
29
30 $time > 60*60
31 ? sprintf "%d:%02d:%02d", $time / (60 * 60), $time / 60 % 60, $time % 60
32 : sprintf "%d:%02d", $time / 60 % 60, $time % 60;
33}
34
35sub set_rules {
36 my ($self, $timesys, $main, $interval, $count) = @_; 27 my ($self, $timesys, $main, $interval, $count) = @_;
37 28
38 if ($timesys == TIMESYS_ABSOLUTE) { 29 if ($timesys == TIMESYS_ABSOLUTE) {
39 $self->{set} = sub { $self->{time} = $_[0] }; 30 $self->{set} = sub { $self->{time} = $_[0] };
40 $self->{format} = sub { format_time $_[0] }; 31 $self->{format} = sub { util::format_time $_[0] };
41 32
42 } elsif ($timesys == TIMESYS_BYO_YOMI) { 33 } elsif ($timesys == TIMESYS_BYO_YOMI) {
43 my $low = $interval * $count; 34 my $low = $interval * $count;
44 35
45 $self->{set} = sub { $self->{time} = $_[0] }; 36 $self->{set} = sub { $self->{time} = $_[0] };
46 37
47 $self->{format} = sub { 38 $self->{format} = sub {
48 if ($_[0] > $low) { 39 if ($_[0] > $low) {
49 format_time $_[0] - $low; 40 util::format_time $_[0] - $low;
50 } else { 41 } else {
51 sprintf "%s (%d)", (format_time int ($_[0] % $interval) || $interval), $_[0] / $interval; 42 sprintf "%s (%d)",
43 util::format_time int (($_[0] - 1) % $interval + 1),
44 ($_[0] - 1) / $interval;
52 } 45 }
53 }; 46 };
54 47
55 } elsif ($timesys == TIMESYS_CANADIAN) { 48 } elsif ($timesys == TIMESYS_CANADIAN) {
56 my $low = $interval;
57
58 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] }; 49 $self->{set} = sub { $self->{time} = $_[0]; $self->{moves} = $_[1] };
59 50
60 $self->{format} = sub { 51 $self->{format} = sub {
61 if ($_[0] > $low) { 52 if (!$self->{moves}) {
62 format_time $_[0] - $low; 53 util::format_time $_[0] - $low;
63 } else { 54 } else {
64 sprintf "%s / %d", (format_time int($_[0] % $interval) || $interval), $self->{moves}; 55 my $time = int (($_[0] - 1) % $interval + 1);
56
57 sprintf "%s/%d =%d",
58 util::format_time $time,
59 $self->{moves},
60 $self->{moves} > 1
61 ? $time / $self->{moves}
62 : $interval;
65 } 63 }
66 }; 64 };
67 65
68 } else { 66 } else {
69 # none, or unknown 67 # none, or unknown
73} 71}
74 72
75sub refresh { 73sub refresh {
76 my ($self, $timestamp) = @_; 74 my ($self, $timestamp) = @_;
77 my $timer = $self->{time} + $self->{start} - $timestamp; 75 my $timer = $self->{time} + $self->{start} - $timestamp;
76
77 # we round the timer value slightly... the protocol isn't exact anyways,
78 # and this gives smoother timers ;)
79 my @format = $self->{format}->(int ($timer + 0.4));
78 $self->{widget}->set_text ($self->{format}->($timer)); 80 $self->set_text ($self->{format}->(int ($timer + 0.4)));
79 81
80 $timer - int $timer; 82 $timer - int $timer;
81} 83}
82 84
83sub set_time { 85sub set_time {
84 my ($self, $time) = @_; 86 my ($self, $time) = @_;
85 87
86 # we ignore requests to re-set the time of a running clock. 88 # we ignore requests to re-set the time of a running clock.
87 # this is the easiest way to ensure that commentary etc. 89 # this is the easiest way to ensure that commentary etc.
88 # doesn't re-set the clock. yes, this is frickle design, 90 # doesn't re-set the clock. yes, this is frickle design,
89 # but I think the protoocl is to blame here, which gives 91 # but I think the protocol is to blame here, which gives
90 # very little time information. (cgoban2 also has had quite 92 # very little time information. (cgoban2 also has had quite
91 # a lot of small time update problems...) 93 # a lot of small time update problems...)
92 unless ($self->{timeout}) { 94 unless ($self->{timeout}) {
93 $self->{set}->($time->[0], $time->[1]); 95 $self->{set}->($time->[0], $time->[1]);
94 $self->refresh ($self->{start}); 96 $self->refresh ($self->{start});
95 } 97 }
96} 98}
97 99
98sub start { 100sub start {
99 my ($self) = @_; 101 my ($self, $when) = @_;
100 102
101 return if $self->{timeout}; 103 $self->stop;
102 104
103 # this is correct, since we assume the last message triggered a start 105 $self->{start} = $when;
104 $self->{start} = $KGS::Protocol::NOW;
105 106
106 my $timeout; $timeout = sub { 107 my $timeout; $timeout = sub {
107 # -100 means we run the timer a bit earlier to avoid 10.99 => 10s roundings.
108 # we "could" cheat by precalculating the time, but I feel uneasy about both
109 # ways to cheat.
110 my $next = int ($self->refresh (Time::HiRes::time) * 1000) - 100; 108 my $next = $self->refresh (Time::HiRes::time) * 1000;
111 $next += 1000 if $next < 0; 109 $next += 1000 if $next < 0;
112 $self->{timeout} = add Glib::Timeout $next, $timeout; 110 $self->{timeout} = add Glib::Timeout $next, $timeout;
113 0; 111 0;
114 }; 112 };
115 113
120 my ($self) = @_; 118 my ($self) = @_;
121 119
122 remove Glib::Source delete $self->{timeout} if $self->{timeout}; 120 remove Glib::Source delete $self->{timeout} if $self->{timeout};
123} 121}
124 122
125sub destroy {
126 my ($self) = @_;
127 $self->stop;
128 $self->SUPER::destroy;
129}
130
131package game::userpanel; 123package game::userpanel;
132 124
133use base gtk::widget; 125use Glib::Object::Subclass
126 Gtk2::HBox,
127 properties => [
128 Glib::ParamSpec->IV ("colour", "colour", "User Colour", 0, 1, 0, [qw(construct-only writable)]),
129 ];
134 130
135sub new { 131sub INIT_INSTANCE {
136 my $class = shift; 132 my ($self) = @_;
137 my $self = $class->SUPER::new(@_);
138 133
139 $self->{widget} = new Gtk2::HBox;
140
141 $self->{widget}->add (my $vbox = new Gtk2::VBox); 134 $self->add (my $vbox = new Gtk2::VBox);
142 135
143 $vbox->add ($self->{name} = new Gtk2::Label $self->{name}); 136 $vbox->add ($self->{name} = new Gtk2::Label $self->{name});
144 $vbox->add ($self->{info} = new Gtk2::Label ""); 137 $vbox->add ($self->{info} = new Gtk2::Label "");
145 $vbox->add (($self->{clock} = new game::goclock)->widget); 138 $vbox->add ($self->{clock} = new game::goclock); Scalar::Util::weaken $self->{clock};
139
140 $vbox->add ($self->{imagebox} = new Gtk2::VBox);
146 141
147 $self; 142 $self;
148} 143}
149 144
150sub set_rules { 145sub configure {
151 my ($self, $rules) = @_; 146 my ($self, $app, $user, $rules) = @_;
152 147
153 if ($self->{name}->get_text ne $rules->{player}[$self->{colour}]) { 148 if ($self->{name}->get_text ne $user->as_string) {
154 $self->{name}->set_text ($rules->{player}[$self->{colour}]); 149 $self->{name}->set_text ($user->as_string);
155 150
151 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
152 $self->{imagebox}->add (gtk::image_from_data undef);
153 $self->{imagebox}->show_all;
154
155 if ($user->has_pic) {
156 # the big picture... 156 # the big picture...
157 appwin::userpic ($rules->{player}[$self->{colour}], sub { 157 $app->userpic ($user->{name}, sub {
158 return unless $self->{imagebox};
159
160 if ($_[0]) {
161 $self->{imagebox}->remove ($_) for $self->{imagebox}->get_children;
158 $self->{widget}->add (gtk::image_from_data $_[0]) if $_[0]; 162 $self->{imagebox}->add (gtk::image_from_data $_[0]);
159 $self->{widget}->show_all; 163 $self->{imagebox}->show_all;
160 # undef => show sth. funny 164 }
165 });
161 }); 166 }
162 } 167 }
163 168
164 $self->{clock}->set_rules (@{$rules->{rules}}{qw(timesys time interval count)}); 169 $self->{clock}->configure (@{$rules}{qw(timesys time interval count)});
165} 170}
166 171
167sub set_state { 172sub set_state {
168 my ($self, $captures, $timer, $running) = @_; 173 my ($self, $captures, $timer, $when) = @_;
169 174
170 $self->{clock}->stop unless $running; 175 $self->{clock}->stop unless $when;
171 $self->{clock}->set_time ($timer); 176 $self->{clock}->set_time ($timer);
172 $self->{clock}->start if $running; 177 $self->{clock}->start ($when) if $when;
173 178
174 $self->{info}->set_text ("$captures pris."); 179 $self->{info}->set_text ("$captures pris.");
175} 180}
176 181
177package game; 182package game;
183
184use Scalar::Util qw(weaken);
178 185
179use KGS::Constants; 186use KGS::Constants;
180use KGS::Game::Board; 187use KGS::Game::Board;
181 188
189use Gtk2::GoBoard;
190
191use Glib::Object::Subclass
192 Gtk2::Window;
193
182use base KGS::Listener::Game; 194use base KGS::Listener::Game;
183use base KGS::Game; 195use base KGS::Game;
184 196
185use base gtk::widget;
186
187use POSIX qw(ceil); 197use POSIX qw(ceil);
188 198
189sub new { 199sub new {
190 my $self = shift; 200 my ($self, %arg) = @_;
191 $self = $self->SUPER::new(@_); 201 $self = $self->Glib::Object::new;
202 $self->{$_} = delete $arg{$_} for keys %arg;
192 203
193 $self->listen($self->{conn}); 204 $self->listen ($self->{conn});
194 205
195 $self->{window} = new Gtk2::Window 'toplevel';
196 my $title = $self->{channel} ? $self->owner->as_string." ".$self->opponent_string : "Game Window";
197 $self->{window}->set_title("KGS Game $title");
198 gtk::state $self->{window}, "game::window", undef, window_size => [600, 500]; 206 gtk::state $self, "game::window", undef, window_size => [600, 500];
199 207
208 $self->signal_connect (delete_event => sub { $self->part; 1 });
200 $self->{window}->signal_connect(delete_event => sub { 209 $self->signal_connect (destroy => sub {
201 $self->part; 210 $self->unlisten;
202 $self->destroy; 211 delete $self->{app}{game}{$self->{channel}};
203 1; 212 %{$_[0]} = ();
204 }); 213 });#d#
205 214
206 $self->{window}->add($self->{hpane} = new Gtk2::HPaned); 215 $self->add (my $hpane = new Gtk2::HPaned);
207 gtk::state $self->{hpane}, "game::hpane", undef, position => 500; 216 gtk::state $hpane, "game::hpane", undef, position => 500;
208 217
218 # LEFT PANE
219
220 $hpane->pack1 (($self->{left} = new Gtk2::VBox), 1, 0);
221
222 $self->{boardbox} = new Gtk2::VBox;
223
209 $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); 224 $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1);
210 225
226 # board box (aspect/canvas)
227
211 $vbox->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0); 228 $self->{boardbox}->pack_start((my $frame = new Gtk2::Frame), 0, 1, 0);
212 229
213 { 230 {
214 # grrr...
215 $frame->add(my $vbox = new Gtk2::VBox); 231 $frame->add (my $vbox = new Gtk2::VBox);
216 $vbox->add($self->{title} = new Gtk2::Label $title); 232 $vbox->add ($self->{title} = new Gtk2::Label $title);
217 233
234 $vbox->add (my $hbox = new Gtk2::HBox);
235
236 $hbox->pack_start (($self->{board_label} = new Gtk2::Label), 0, 1, 0);
237
218 $self->{moveadj} = new Gtk2::Adjustment 1, 0, 1, 0.001, 0.05, 0; 238 $self->{moveadj} = new Gtk2::Adjustment 1, 1, 1, 1, 5, 0;
219 239
220 $vbox->add(my $scale = new Gtk2::HScale $self->{moveadj}); 240 $hbox->pack_start ((my $scale = new Gtk2::HScale $self->{moveadj}), 1, 1, 0);
221 $scale->set_draw_value (0); 241 $scale->set_draw_value (0);
242 $scale->set_digits (0);
222 243
223 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board }); 244 $self->{moveadj}->signal_connect (value_changed => sub { $self->update_board });
224 } 245 }
246
247 $self->{boardbox}->add ($self->{board} = new Gtk2::GoBoard size => $self->{size});
248
249 # RIGHT PANE
250
251 $hpane->pack2 (($self->{vpane} = new Gtk2::VPaned), 1, 1);
252 $hpane->set (position_set => 1);
253 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
254
255# $self->{vpane}->add (my $sw = new Gtk2::ScrolledWindow);
256# $sw->set_policy ("automatic", "always");
257# $sw->add ($self->{userlist} = new userlist);
258
259 $self->{vpane}->add (my $vbox = new Gtk2::VBox);
260
261 $vbox->pack_start ((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
262
263 $hbox->add ($self->{userpanel}[$_] = new game::userpanel colour => $_)
264 for COLOUR_WHITE, COLOUR_BLACK;
225 265
226 $vbox->pack_start((my $aspect_frame = new Gtk2::AspectFrame "", 0.5, 0.5, 1, 0), 1, 1, 0);
227 $aspect_frame->set (border_width => 0, shadow_type => 'none', label_xalign => 0.5);
228 $self->{board_label} = $aspect_frame->get_label_widget;
229
230 $aspect_frame->add($self->{canvas} = new Gtk2::DrawingArea);
231 $self->{canvas}->double_buffered (0) if $::config->{conserve_memory};
232
233 $self->{canvas}->signal_connect(configure_event => \&configure_event, $self);
234 $self->{canvas}->signal_connect(expose_event => \&expose_event, $self);
235
236 # RIGHT PANE
237
238 $self->{hpane}->pack2(($self->{vpane} = new Gtk2::VPaned), 0, 0);
239 $self->{hpane}->set(position_set => 1);
240 gtk::state $self->{vpane}, "game::vpane", $self->{name}, position => 80;
241
242 $self->{vpane}->add(my $sw = new Gtk2::ScrolledWindow);
243 $sw->set_policy("automatic", "always");
244
245 $sw->add(($self->{userlist} = new userlist)->widget);
246
247 $self->{vpane}->add(my $vbox = new Gtk2::VBox);
248
249 $vbox->pack_start((my $hbox = new Gtk2::HBox 1), 0, 1, 0);
250 $hbox->add (($self->{userpanel}[WHITE] = new game::userpanel colour => WHITE)->widget);
251 $hbox->add (($self->{userpanel}[BLACK] = new game::userpanel colour => BLACK)->widget);
252
253 $vbox->pack_start((my $sw = new Gtk2::ScrolledWindow), 1, 1, 0);
254 $sw->set_policy("never", "always");
255
256 $sw->add(($self->{text} = new gtk::text)->widget);
257
258 $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); 266 $vbox->pack_start (($self->{chat} = new superchat), 1, 1, 0);
267
268 $self->{rules_inlay} = $self->{chat}->new_switchable_inlay ("Game Rules", sub { $self->draw_rules (@_) }, 1);
269 $self->{users_inlay} = $self->{chat}->new_switchable_inlay ("Users:", sub { $self->draw_users (@_) }, 0);
270
259 $self->{entry}->signal_connect(activate => sub { 271 $self->{chat}->signal_connect (command => sub {
260 my $text = $self->{entry}->get_text; 272 my ($chat, $cmd, $arg) = @_;
261 $self->say($text) if $text =~ /\S/; 273 if ($cmd eq "rsave") {
262 $self->{entry}->set_text(""); 274 Storable::nstore { tree => $self->{tree}, curnode => $self->{curnode}, move => $self->{move} }, $arg;#d#
275 } else {
276 $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, game => $self);
277 }
263 }); 278 });
264 279
265 $self; 280 $self;
266} 281}
267 282
268sub event_update_users { 283sub event_update_users {
269 my ($self, $add, $update, $remove) = @_; 284 my ($self, $add, $update, $remove) = @_;
270 285
271 $self->{userlist}->update ($add, $update, $remove); 286# $self->{userlist}->update ($add, $update, $remove);
287
288 $self->{users_inlay}->refresh;
289
290 my %important;
291 $important{$self->{user1}{name}}++;
292 $important{$self->{user2}{name}}++;
293 $important{$self->{user3}{name}}++;
294
295 if (my @users = grep $important{$_->{name}}, @$add) {
296 $self->{chat}->append_text ("\n<header>Joins:</header>");
297 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
298 }
299 if (my @users = grep $important{$_->{name}}, @$remove) {
300 $self->{chat}->append_text ("\n<header>Parts:</header>");
301 $self->{chat}->append_text (" <user>" . $_->as_string . "</user>") for @users;
302 }
303
272} 304}
273 305
274sub join { 306sub join {
275 my ($self) = @_; 307 my ($self) = @_;
276 return if $self->{joined}; 308 return if $self->{joined};
277 309
278 $self->SUPER::join; 310 $self->SUPER::join;
311}
279 312
313sub update_board {
314 my ($self) = @_;
315 return unless $self->{path};
316
317 my $move = int $self->{moveadj}->get_value;
318
319 my $running = $move == @{$self->{path}};
320
321 $self->{board_label}->set_text ("Move " . ($move - 1));
322
323 $self->{cur_board} = new KGS::Game::Board $self->{size};
324 $self->{cur_board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
325
326 for my $colour (COLOUR_WHITE, COLOUR_BLACK) {
327 $self->{userpanel}[$colour]->set_state (
328 $self->{cur_board}{captures}[$colour],
329 $self->{cur_board}{timer}[$colour],
330 ($running && $self->{lastmove_colour} == !$colour)
331 ? $self->{lastmove_time} : 0
332 );
333 }
334
335 $self->{board}->set_board ($self->{cur_board});
336}
337
338sub event_update_tree {
339 my ($self) = @_;
340
341 $self->{path} = $self->get_path;
342
343 if ($self->{moveadj}) {
344 my $upper = $self->{moveadj}->upper;
345 my $pos = $self->{moveadj}->get_value;
346 my $move = scalar @{$self->{path}};
347
348 $self->{moveadj}->upper ($move);
349
350 $self->{moveadj}->changed;
351 if ($pos == $upper) {
352 $self->{moveadj}->value ($move);
353 $self->{moveadj}->value_changed;
354 }
355 }
356}
357
358sub event_update_comments {
359 my ($self, $node, $comment, $newnode) = @_;
360 $self->SUPER::event_update_comments($node, $comment, $newnode);
361
362 my $text;
363
364 $text .= "\n<header>Move <move>$node->{move}</move>, Node <node>$node->{id}</node></header>"
365 if $newnode;
366
367 for (split /\n/, $comment) {
368 $text .= "\n";
369 if (s/^([0-9a-zA-Z]+ \[[0-9dkp\?\-]+\])://) {
370 $text .= "<user>" . (util::toxml $1) . "</user>:";
371 }
372
373 # coords only for 19x19 so far
374 $_ = util::toxml $_;
375 s{
376 (
377 \b
378 (?:[bw])?
379 [, ]{0,2}
380 [a-hj-t] # valid for upto 19x19
381 \s?
382 [1-9]?[0-9]
383 \b
384 )
385 }{
386 "<coord>$1</coord>";
387 }sgexi;
388
389 $text .= $_;
390 }
391
392 $self->{chat}->append_text ($text);
393}
394
395sub event_join {
396 my ($self) = @_;
397
398 $self->SUPER::event_join (@_);
399 $self->event_update_game;
280 $self->{window}->show_all; 400 $self->show_all;
281} 401}
282 402
283sub part { 403sub event_part {
284 my ($self) = @_; 404 my ($self) = @_;
285 405
286 $self->SUPER::part; 406 $self->SUPER::event_part;
287 $self->destroy; 407 $self->destroy;
288}
289
290sub configure_event {
291 my ($widget, $event, $self) = @_;
292 delete $self->{stack};
293 delete $self->{pixbuf};
294 delete $self->{board_shown};
295 delete $self->{background};
296 $self->repaint_board;
297 0;
298}
299
300sub expose_event {
301 my ($widget, $event, $self) = @_;
302
303 $self->{pixbuf} or return;
304
305 my $area = $event->area;
306
307 $self->redraw ($area);
308
309 0;
310}
311
312# something Gtk2 fixed
313sub INTERP_NEAREST (){ 'nearest' }
314sub INTERP_TILES (){ 'tiles' }
315sub INTERP_BILINEAR (){ 'bilinear' }
316sub INTERP_HYPER (){ 'hyper' }
317
318sub new_pixbuf {
319 my ($w, $h, $alpha, $fill) = @_;
320
321 my $pixbuf = new Gtk2::Gdk::Pixbuf 'rgb', $alpha, 8, $w, $h;
322 $pixbuf->fill ($fill) if defined $fill;
323
324 $pixbuf;
325}
326
327sub scale_pixbuf {
328 my ($src, $w, $h, $mode) = @_;
329
330 my $dst = new_pixbuf $w, $h, 1;
331
332 $src->scale(
333 $dst, 0, 0, $w, $h, 0, 0,
334 $w / $src->get_width, $h / $src->get_height,
335 $mode,
336 );
337
338 $dst;
339}
340
341# create a stack of stones
342sub create_stack {
343 my ($self, $mark, $size, $rand) = @_;
344
345 my $shadow = $size * 0.05;
346
347 my $c = \$self->{stack}{$mark};
348 unless ($$c) {
349 for my $stone ($mark & (MARK_W | MARK_GRAY_W) ? @::white_img : @::black_img) {
350 my $base = new_pixbuf $size + $shadow, $size + $shadow, 1, 0x00000000;
351
352 # zeroeth the shadow
353 if ($mark & (MARK_B | MARK_W)) {
354 # the -0.5's are a mystery to me
355 $::shadow_img->composite (
356 $base, $shadow, $shadow, $size, $size, $shadow - 0.5, $shadow - 0.5,
357 $size / $::shadow_img->get_width, $size / $::shadow_img->get_height,
358 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
359 );
360 }
361
362 # first the big stones (handicap stones different for effect)
363 for ([MARK_B, $mark & MARK_MOVE ? 255 : 192],
364 [MARK_W, $mark & MARK_MOVE ? 255 : 192],
365 [MARK_GRAY_B, 128],
366 [MARK_GRAY_W, 128]) {
367 my ($mask, $alpha) = @$_;
368 if ($mark & $mask) {
369 $stone->composite (
370 $base, 0, 0, $size, $size, 0, 0,
371 $size / $stone->get_width, $size / $stone->get_height,
372 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, $alpha
373 );
374 }
375 }
376
377 # then the small stones
378 for ([MARK_SMALL_B, $::black_img[$rand % @::black_img]],
379 [MARK_SMALL_W, $::white_img[$rand % @::white_img]]) {
380 my ($mask, $img) = @$_;
381 if ($mark & $mask) {
382 $img->composite (
383 $base, (int ($size / 4)) x2, (ceil ($size / 2 + 1)) x2, ($size / 4) x2,
384 $size / $img->get_width / 2, $size / $img->get_height / 2,
385 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 224
386 );
387 }
388 }
389
390 # and lastly any markers
391 my $dark_bg = ! ! ($mark & (MARK_B | MARK_GRAY_B));
392
393 for ([MARK_CIRCLE, $::circle_img[$dark_bg]],
394 [MARK_TRIANGLE, $::triangle_img[$dark_bg]],
395 [MARK_SQUARE, $::square_img[$dark_bg]]) {
396 my ($mask, $img) = @$_;
397 if ($mark & $mask) {
398 $img->composite (
399 $base, 0, 0, $size, $size, 0, 0,
400 $size / $img->get_width, $size / $img->get_height,
401 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 192
402 );
403 }
404 }
405
406 push @$$c, $base;
407 }
408 }
409
410 $$c->[$rand % @$$c];
411}
412
413sub pixbuf_text {
414 my ($pixbuf, $colour, $x, $y, $height, $text) = @_;
415
416 my @c = grep $_,
417 map $::font[$colour][$::fontmap{$_}],
418 split //, $text;
419
420 if (@c) {
421 my $spacing = $height * 0.1;
422 my $s = $height / List::Util::max map $_->get_height, @c;
423 my $W = List::Util::sum map $_->get_width, @c;
424
425 $x -= ($W * $s + $spacing * (@c - 1)) * 0.5;
426 $y -= $height * 0.5;
427
428 for (@c) {
429 my $w = $_->get_width * $s;
430 # +2 == don't fight the rounding
431 $_->composite ($pixbuf,
432 $x, $y, $w+2, $height+2, $x, $y, $s, $s,
433 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
434
435 $x += $w + $spacing;
436 }
437 }
438}
439
440sub pixbuf_rect {
441 my ($pb, $colour, $x1, $y1, $x2, $y2, $alpha) = @_;
442 # we fake lines by... a horrible method :/
443 my $colour_pb = new_pixbuf 1, 1, 0, $colour;
444 $colour_pb->composite ($pb, $x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1, $x1, $y1, $x2 + 1, $y2 + 1,
445 INTERP_NEAREST, $alpha);
446}
447
448sub repaint_board {
449 my ($self) = @_;
450 my $canvas = $self->{canvas};
451 my $expose_area = undef;
452
453 return $expose_area unless $self->{board};
454
455 my ($w, $h) = ($canvas->allocation->values)[2,3];
456
457 die "FATAL: board aspect ratio != 1" unless $w == $h;
458
459 my $s = $w;
460
461 return unless $s >= 200;
462
463 my $size = $self->{size};
464
465 # we leave enough space for the shadows.. I like smaller stones, and we
466 # do no need to do the nifty recursive screen updates that goban2 does
467 my $border = int ($s / ($size + 3) * 0.5);
468 my $s2 = $s - $border * 2;
469 my $edge = int ($s2 / ($size + 1) * 0.96) - ($::config->{randomize} ? 3 : 0);
470 my $ofs = int ($edge / 2);
471
472 my @k = map int ($s2 * $_ / ($size+1) + $border + 0.5), 0 .. $size;
473
474 my $pixbuf;
475
476 my $oldboard;
477
478 if ($self->{background}) {
479 if ($oldboard = $self->{board_shown}) {
480 $pixbuf = $self->{pixbuf};
481 } else {
482 $pixbuf = $self->{background}->copy;
483 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
484 }
485 } else {
486 $expose_area = new Gtk2::Gdk::Rectangle 0, 0, $s, $s;
487
488 my ($bw, $bh) = ($::board_img->get_width, $::board_img->get_height);
489
490 if ($s < $bw && $s < $bh) {
491 $pixbuf = new_pixbuf $s, $s, 0;
492 $::board_img->copy_area (0, 0, $s, $s, $pixbuf, 0, 0);
493 } else {
494 $pixbuf = scale_pixbuf $::board_img, $s, $s, $::config->{speed} ? INTERP_NEAREST : INTERP_TILES;
495 }
496
497 my $linew = int ($s / 40 / $size);
498
499 # ornamental border... we have time to waste :/
500 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $s-1, $linew, 255;
501 pixbuf_rect $pixbuf, 0xffcc7700, 0, 0, $linew, $s-1, 255;
502 pixbuf_rect $pixbuf, 0xffcc7700, $s-$linew-1, 0, $s-1, $s-1, 255;
503 pixbuf_rect $pixbuf, 0xffcc7700, 0, $s-$linew-1, $s-1, $s-1, 255;
504
505 for my $i (1 .. $size) {
506 pixbuf_rect $pixbuf, 0x44111100, $k[$i] - $linew, $k[1] - $linew, $k[$i] + $linew, $k[$size] + $linew, 192;
507 pixbuf_rect $pixbuf, 0x44111100, $k[1] - $linew, $k[$i] - $linew, $k[$size] + $linew, $k[$i] + $linew, 192;
508
509 # 38 max, but we allow a bit more
510 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
511 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];
512
513 pixbuf_text $pixbuf, 0, $k[$i], $border, $ofs, $label;
514 pixbuf_text $pixbuf, 0, $k[$i], $s2 + $border, $ofs, $label;
515 pixbuf_text $pixbuf, 0, $border, $k[$i], $ofs, $size - $i + 1;
516 pixbuf_text $pixbuf, 0, $s2 + $border, $k[$i], $ofs, $size - $i + 1;
517
518 $a++;
519 $a++ if $a eq "I"; # not correct, instead of AA AB, we should get HH JJ KK...
520 }
521
522 # hoshi points
523 my $hoshi = sub {
524 my ($x, $y) = @_;
525 my $hs = int ($edge / 4) | 1;
526 $x = $k[$x] - $hs / 2; $y = $k[$y] - $hs / 2;
527
528 # we use the shadow mask... not perfect, but I want to finish this
529 $::shadow_img->composite ($pixbuf,
530 $x, $y, $hs + 1, $hs + 1, $x, $y,
531 $hs / $::shadow_img->get_width, $hs / $::shadow_img->get_height,
532 $::config->{speed} ? INTERP_NEAREST : INTERP_BILINEAR, 255);
533 };
534
535 my $h1 = $size < 10 ? 3 : 4; # corner / edge offset
536 $hoshi->($h1, $h1);
537 $hoshi->($size - $h1 + 1, $h1);
538 $hoshi->($h1, $size - $h1 + 1);
539 $hoshi->($size - $h1 + 1, $size - $h1 + 1);
540
541 if ($size % 2) { # on odd boards, also the remaining 5
542 my $h2 = ($size + 1) / 2;
543 if ($size > 10) {
544 $hoshi->($h1, $h2);
545 $hoshi->($size - $h1 + 1, $h2);
546 $hoshi->($h2, $size - $h1 + 1);
547 $hoshi->($h2, $h1);
548 }
549 # the tengen
550 $hoshi->($h2, $h2);
551 }
552
553 unless ($::config->{conserve_memory} > 1) {
554 $self->{background} = $pixbuf;
555 $pixbuf = $pixbuf->copy;
556 }
557 }
558
559 $self->{pixbuf} = $pixbuf;
560
561 # hoshi-points(!)#d#
562 # caching of empty board gfx(!)#d#
563
564 for my $x (1 .. $size) {
565 for my $y (1 .. $size) {
566 my $rand = ($x ^ $y ^ 0x5555);
567
568 my ($dx, $dy) = ($k[$x] - $ofs, $k[$y] - $ofs);
569
570 if ($::config->{randomize}) {
571 $dx += ($rand % 7) - 3;
572 $dy += ($rand / 3 % 7) - 3;
573 }
574
575 my $shadow = $edge * 0.05;
576 my $area = [$dx, $dy, $edge + $shadow, $edge + $shadow];
577
578 my $mark = $self->{board}{board}[$x-1][$y-1];
579 my $old = $oldboard ? $oldboard->{board}[$x-1][$y-1] : 0;
580
581 if ($oldboard) {
582 next if $old == $mark; # no change
583
584 $self->{background}->copy_area (@$area, $pixbuf, $dx, $dy);
585 $expose_area = $expose_area
586 ? $expose_area->union (new Gtk2::Gdk::Rectangle @$area)
587 : new Gtk2::Gdk::Rectangle @$area;
588 }
589
590 if ($mark) {
591 my $pb = $self->create_stack($mark, $edge, $rand);
592
593 $pb->composite ($pixbuf, @$area,
594 $dx, $dy, 1, 1, $::config->{speed} ? INTERP_NEAREST : INTERP_NEAREST, 255);
595
596 # labels are handled here because they are quite rare
597 if ($mark & MARK_LABEL) {
598 my $white = $mark & (MARK_W | MARK_GRAY_W) ? 0 : 1;
599
600 if ($white) {
601 pixbuf_text $pixbuf, 0,
602 $k[$x] + $ofs * 0.1, $k[$y] + $ofs * 0.1, $ofs * 0.7,
603 $self->{board}{label}[$x-1][$y-1];
604 }
605 pixbuf_text $pixbuf, $white,
606 $k[$x], $k[$y], $ofs * 0.7,
607 $self->{board}{label}[$x-1][$y-1];
608 }
609 }
610 }
611 }
612
613 $self->{board_shown} = Storable::dclone $self->{board};
614 #d# save
615 #Storable::nstore { board => $self->{board}, size => $self->{size}, path => $self->{path}}, "testboard.storable";
616
617 $expose_area;
618}
619
620sub redraw {
621 my ($self, $area) = @_;
622
623 if ($area && $self->{pixbuf}) {
624 my ($x, $y, $w, $h) = $area->values;
625
626 $self->{canvas}->window->draw_pixbuf ($self->{canvas}->style->white_gc, $self->{pixbuf},
627 $x, $y, $x, $y, $w, $h,
628 "normal", 0, 0);
629 $self->{canvas}->window->draw_rectangle ($self->{canvas}->style->black_gc, 0,
630 $x - 1, $y - 1, $w + 2, $h + 2) if $::DEBUG_EXPOSE;
631 }
632}
633
634sub update_board {
635 my ($self) = @_;
636 return unless $self->{path};
637
638 my $move = int (@{$self->{path}} * $self->{moveadj}->get_value);
639
640 my $running = $move == @{$self->{path}};
641
642 $self->{board_label}->set_text ("Move $move");
643
644 $self->{board} = new KGS::Game::Board $self->{size};
645 $self->{board}->interpret_path ([@{$self->{path}}[0 .. $move - 1]]);
646
647 $self->{userpanel}[WHITE]->set_state ($self->{board}{captures}[WHITE],
648 $self->{board}{timer}[WHITE],
649 $running && $self->{board}{last} == BLACK);
650 $self->{userpanel}[BLACK]->set_state ($self->{board}{captures}[BLACK],
651 $self->{board}{timer}[BLACK],
652 $running && $self->{board}{last} == WHITE);
653
654 $self->redraw ($self->repaint_board);
655
656 $self->{text}->set_text ($self->{board}{comment});
657}
658
659sub event_update_tree {
660 my ($self) = @_;
661
662 $self->{path} = $self->get_path;
663 $self->{userpanel}[WHITE]->set_rules ($self->{path}[0]); # should be onload only
664 $self->{userpanel}[BLACK]->set_rules ($self->{path}[0]); # should be onload only
665
666 $self->{moveadj}->value_changed if $self->{moveadj};
667}
668
669sub event_part {
670 my ($self) = @_;
671 $self->SUPER::event_part;
672} 408}
673 409
674sub event_move { 410sub event_move {
675 my ($self, $pass) = @_; 411 my ($self, $pass) = @_;
676 sound::play 1, $pass ? "pass" : "move"; 412 sound::play 1, $pass ? "pass" : "move";
677} 413}
678 414
679sub event_update_game { 415sub event_update_game {
680 my ($self) = @_; 416 my ($self) = @_;
681 $self->SUPER::event_update_game; 417 $self->SUPER::event_update_game;
682 warn "UPDATE GAME";#d#
683}
684 418
685sub destroy { 419 return unless $self->{joined};
420
421 my $title = defined $self->{channel}
422 ? $self->owner->as_string . " " . $self->opponent_string
423 : "Game Window";
424 $self->set_title("KGS Game $title");
425 $self->{title}->set_text ($title);
426
427 $self->{user}[COLOUR_BLACK] = $self->{user1};
428 $self->{user}[COLOUR_WHITE] = $self->{user2};
429
430 # show board
431 if ($self->is_inprogress) {
432 $self->{left}->add ($self->{boardbox}) unless $self->{boardbox}->parent;
433 if (my $ch = delete $self->{challenge}) {
434 (delete $_->{inlay})->clear for values %$ch;
435 }
436 }
437
438 $self->{left}->show_all;
439
440 # view text
441
442 eval { #d#
443 my @ga;
444 $ga[0] = "\nType: " . (util::toxml $gametype{$self->type})
445 . " (" . (util::toxml $gameopt{$self->option}) . ")";
446 $ga[1] = "\nFlags:";
447 $ga[1] .= " started" if $self->is_inprogress;
448 $ga[1] .= " adjourned" if $self->is_adjourned;
449 $ga[1] .= " scored" if $self->is_scored;
450 $ga[1] .= " saved" if $self->is_saved;
451
452 $ga[2] = "\nOwner: <user>" . (util::toxml $self->{user3}->as_string) . "</user>"
453 if $self->{user3}->is_inprogress;
454
455 $ga[3] = "\nPlayers: <user>" . (util::toxml $self->{user2}->as_string) . "</user>"
456 . " vs. <user>" . (util::toxml $self->{user1}->as_string) . "</user>"
457 if $self->is_inprogress;
458
459 if ($self->is_inprogress) {
460 $ga[4] = "\nHandicap: " . $self->{handicap};
461 $ga[5] = "\nKomi: " . $self->{komi};
462 $ga[6] = "\nSize: " . $self->size_string;
463 }
464
465 if ($self->is_scored) {
466 $ga[7] = "\nResult: " . $self->score_string;
467 }
468
469 $text = "\n<infoblock><header>Game Update</header>";
470 for (0..7) {
471 if ($self->{gatext}[$_] ne $ga[$_]) {
472 $text .= $ga[$_];
473 }
474 }
475 $text .= "</infoblock>";
476
477 $self->{gatext} = \@ga;
478 };
479
480 $self->{chat}->append_text ($text);
481}
482
483sub draw_rules {
484 my ($self, $inlay) = @_;
485
486 my $rules = $self->{rules};
487
488 my $text = "";
489
490 $text .= "\nRuleset: " . $ruleset{$rules->{ruleset}};
491
492 $text .= "\nTime: ";
493
494 if ($rules->{timesys} == TIMESYS_NONE) {
495 $text .= "UNLIMITED";
496 } elsif ($rules->{timesys} == TIMESYS_ABSOLUTE) {
497 $text .= util::format_time $rules->{time};
498 $text .= " ABS";
499 } elsif ($rules->{timesys} == TIMESYS_BYO_YOMI) {
500 $text .= util::format_time $rules->{time};
501 $text .= sprintf " + %s (%d) BY", util::format_time $rules->{interval}, $rules->{count};
502 } elsif ($rules->{timesys} == TIMESYS_CANADIAN) {
503 $text .= util::format_time $rules->{time};
504 $text .= sprintf " + %s/%d CAN", util::format_time $rules->{interval}, $rules->{count};
505 }
506
507 $inlay->append_text ("<infoblock>$text</infoblock>");
508}
509
510sub event_update_rules {
511 my ($self, $rules) = @_;
512
513 $self->{rules} = $rules;
514
515 if ($self->{user}) {
516 $self->{userpanel}[$_]->configure ($self->{app}, $self->{user}[$_], $rules)
517 for COLOUR_BLACK, COLOUR_WHITE;
518 }
519
520 sound::play 3, "gamestart";
521
522 $self->draw_rules ($self->{rules_inlay});
523}
524
525sub inject_resign_game {
526 my ($self, $msg) = @_;
527
528 sound::play 3, "resign";
529
530 $self->{chat}->append_text ("\n<infoblock><header>Resign</header>"
531 . "\n<user>"
532 . (util::toxml $self->{user}[$msg->{player}]->as_string)
533 . "</user> resigned.</infoblock>");
534}
535
536sub inject_final_result {
537 my ($self, $msg) = @_;
538
539 $self->{chat}->append_text ("<infoblock>\n<header>Game Over</header>"
540 . "\nWhite Score " . (util::toxml $msg->{whitescore}->as_string)
541 . "\nBlack Score " . (util::toxml $msg->{blackscore}->as_string)
542 . "</infoblock>"
543 );
544}
545
546sub draw_challenge {
686 my ($self) = @_; 547 my ($self, $c) = @_;
687 (delete $self->{userpanel}[WHITE])->destroy if $self->{userpanel}[WHITE]; 548
688 (delete $self->{userpanel}[BLACK])->destroy if $self->{userpanel}[BLACK]; 549 my $inlay = $c->{inlay};
689 $self->SUPER::destroy; 550 my $challenge = $c->{challenge};
690 delete $appwin::gamelist->{game}{$self->{channel}}; 551 my $rules = $challenge->{rules};
552
553 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
554 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
555
556 $inlay->append_text ("\n<challenge>Challenge to <user>" . $opponent->as_string . "</user></challenge>");
557 $inlay->append_text ("\nHandicap: $rules->{handicap}");
558
559#bless( (
560# gametype => 3,
561# user1 => bless( {
562# flags => 2633,
563# name => 'dorkusx'
564# }, 'KGS::User' ),
565# rules => bless( {
566# count => 5,
567# time => 900,
568# timesys => 2,
569# interval => 30,
570# komi => '6.5',
571# size => 19,
572# ruleset => 0,
573# handicap => 0
574# }, 'KGS::Rules' ),
575# user2 => bless( {
576# flags => 436220808,
577# name => 'Nerdamus'
578# }, 'KGS::User' )
579# ), 'KGS::Challenge' )
580}
581
582sub draw_users {
583 my ($self, $inlay) = @_;
584
585 for (sort keys %{$self->{users}}) {
586 $inlay->append_text (" <user>" . $self->{users}{$_}->as_string . "</user>");
587 }
588}
589
590sub event_challenge {
591 my ($self, $challenge) = @_;
592
593 my $as_black = $challenge->{user1}{name} eq $self->{conn}{name};
594 my $opponent = $as_black ? $challenge->{user2} : $challenge->{user1};
595
596 my $c = $self->{challenge}{$opponent->{name}} ||= {};
597
598 $c->{inlay} ||= $self->{chat}->new_inlay;
599 $c->{challenge} = $challenge;
600
601 $self->draw_challenge ($c);
602
603# require KGS::Listener::Debug;
604# $self->{chat}->append_text ("\n".KGS::Listener::Debug::dumpval($challenge));
691} 605}
692 606
6931; 6071;
694 608

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines