Sternhalma.xs

filter: Statementstmt 97.4% 153 / 157 Branchbran 65.5% 80 / 122 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.5% 233 / 279 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Game/Sternhalma/Install/Files.pm ↑ Summary sth_engine.c »
1 /* Sternhalma.xs - the door to the C board.
2 *
3 * At the TOP of the distribution and not under lib/, because an XSMULTI build's
4 * export list and Strawberry's import library rule never agree on a name. The
5 * siblings that learned this the hard way say so in Go.xs and Xiangqi.xs.
6 *
7 * The XSUBs land in Game::Sternhalma::Engine, not in Game::Sternhalma, and are
8 * named with a leading underscore: they take a raw pointer as a UV and there is
9 * no typemap. The Perl that owns the lifetime is lib/Game/Sternhalma/Engine.pm.
10 *
11 * `_put` and `_lift` are the structure primitives from sth_abi.h and CHECK
12 * NOTHING.
13 */
14  
15 #define PERL_NO_GET_CONTEXT
16  
17 #include "EXTERN.h"
18 #include "perl.h"
19 #include "XSUB.h"
20  
21 #include "sth_abi.h"
22  
23 static const struct sth_abi *STH = NULL;
24  
25 #define BOARD(u) INT2PTR(struct sth_board *, (u))
26  
27 /* A 64-bit value crosses as a 16-character hex string, never as a UV.
28 *
29 * On a perl with 32-bit IVs a UV cannot hold it and the top half would vanish
30 * silently, which for a zobrist key means a test that passes while comparing
31 * half a number. Hand-rolled rather than via my_snprintf, as Fanorona.xs has it.
32 */
33 917755 static SV *sth_hex64(pTHX_ unsigned long long v)
34 {
35 static const char digits[] = "0123456789abcdef";
36 char buf[16];
37 int i;
38 15601835 ▶ for (i = 15; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 15; i >= 0; i--) { 14684080 917755 15601835
39 14684080 buf[i] = digits[v & 0xfULL];
40 14684080 v >>= 4;
41 }
42 917755 return newSVpvn(buf, 16);
43 }
44  
45 MODULE = Game::Sternhalma PACKAGE = Game::Sternhalma::Engine
46  
47 PROTOTYPES: DISABLE
48  
49 BOOT:
50 54 STH = sth_abi_table();
51  
52 UV
53 _abi_ptr()
54 CODE:
55 1 ▶ RETVAL = PTR2UV(STH);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(STH); 0 1 1
56 OUTPUT:
57 RETVAL
58  
59 UV
60 _abi_version()
61 CODE:
62 2 ▶ RETVAL = (UV)STH->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)STH->abi_version; 1 1 2
63 OUTPUT:
64 RETVAL
65  
66 int
67 _tables_ok()
68 CODE:
69 1 RETVAL = (STH->tables_ok)();
70 OUTPUT:
71 RETVAL
72  
73 # A layout the engine does not build answers 0, which the Perl turns into a
74 # croak with the seats and pieces in it.
75 UV
76 _new_board(seats, pieces)
77 int seats
78 int pieces
79 CODE:
80 1562 ▶ RETVAL = PTR2UV((STH->board_new)(seats, pieces));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((STH->board_new)(seats, pieces)); 1531 31 1562
81 OUTPUT:
82 RETVAL
83  
84 UV
85 _new_empty(seats, pieces)
86 int seats
87 int pieces
88 CODE:
89 5379 ▶ RETVAL = PTR2UV((STH->board_empty)(seats, pieces));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((STH->board_empty)(seats, pieces)); 5359 20 5379
90 OUTPUT:
91 RETVAL
92  
93 UV
94 _copy_board(u)
95 UV u
96 CODE:
97 731190 ▶ RETVAL = PTR2UV((STH->board_copy)(BOARD(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((STH->board_copy)(BOARD(u))); 731174 16 731190
98 OUTPUT:
99 RETVAL
100  
101 # The board is let go by an object of its own, held by the board's object and
102 # freed with it. Its DESTROY is C and calls no perl.
103 SV *
104 _hold_board(u)
105 UV u
106 CODE:
107 807376 RETVAL = sv_setref_uv(newSV(0), "Game::Sternhalma::Engine::Hold", u);
108 OUTPUT:
109 RETVAL
110  
111 int
112 _live()
113 CODE:
114 7 RETVAL = (STH->live)();
115 OUTPUT:
116 RETVAL
117  
118 # A string that will not parse returns undef and the code, rather than
119 # croaking: the reason is what a caller shows a person, and eight of them are
120 # distinguishable.
121 void
122 _of_string(str)
123 const char *str
124 PREINIT:
125 struct sth_board *b;
126 71212 int err = 0;
127 PPCODE:
128 71212 b = (STH->board_of_string)(str, &err);
129 71212 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 71212 71212
130 71212 ▶ if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b))));
Branch: 1 branch
BranchTrueFalseTotal
if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); 69245 1967 71212
131 1967 else PUSHs(&PL_sv_undef);
132 71212 PUSHs(sv_2mortal(newSViv(err)));
133  
134 SV *
135 _to_string(u)
136 UV u
137 PREINIT:
138 char buf[STH_POS_MAX];
139 int n;
140 CODE:
141 188307 n = (STH->to_string)(BOARD(u), buf, (int)sizeof(buf));
142 188307 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 188307 188307
143 188307 RETVAL = newSVpvn(buf, (STRLEN)n);
144 OUTPUT:
145 RETVAL
146  
147 int
148 _at(u, hole)
149 UV u
150 int hole
151 CODE:
152 10242080 RETVAL = (STH->at)(BOARD(u), hole);
153 OUTPUT:
154 RETVAL
155  
156 void
157 _put(u, hole, seat)
158 UV u
159 int hole
160 int seat
161 CODE:
162 198162 (STH->put)(BOARD(u), hole, seat);
163  
164 void
165 _lift(u, hole)
166 UV u
167 int hole
168 CODE:
169 135 (STH->lift)(BOARD(u), hole);
170  
171 int
172 _to_move(u)
173 UV u
174 CODE:
175 132290 RETVAL = (STH->to_move)(BOARD(u));
176 OUTPUT:
177 RETVAL
178  
179 void
180 _set_to_move(u, seat)
181 UV u
182 int seat
183 CODE:
184 3982 (STH->set_to_move)(BOARD(u), seat);
185  
186 int
187 _board_seats(u)
188 UV u
189 CODE:
190 1982799 RETVAL = (STH->seats)(BOARD(u));
191 OUTPUT:
192 RETVAL
193  
194 int
195 _board_pieces(u)
196 UV u
197 CODE:
198 992 RETVAL = (STH->pieces)(BOARD(u));
199 OUTPUT:
200 RETVAL
201  
202 int
203 _count(u, seat)
204 UV u
205 int seat
206 CODE:
207 24 RETVAL = (STH->count)(BOARD(u), seat);
208 OUTPUT:
209 RETVAL
210  
211 SV *
212 _key_hex(u)
213 UV u
214 CODE:
215 911638 RETVAL = sth_hex64(aTHX_ (STH->key)(BOARD(u)));
216 OUTPUT:
217 RETVAL
218  
219 SV *
220 _zobrist_hex(seat, hole)
221 int seat
222 int hole
223 CODE:
224 0 RETVAL = sth_hex64(aTHX_ (STH->zobrist)(seat, hole));
225 OUTPUT:
226 RETVAL
227  
228 SV *
229 _zobrist_move_hex(seat)
230 int seat
231 CODE:
232 0 RETVAL = sth_hex64(aTHX_ (STH->zobrist_move)(seat));
233 OUTPUT:
234 RETVAL
235  
236 SV *
237 _zobrist_seats_hex(seats)
238 int seats
239 CODE:
240 0 RETVAL = sth_hex64(aTHX_ (STH->zobrist_seats)(seats));
241 OUTPUT:
242 RETVAL
243  
244 int
245 _layout_ok(seats, pieces)
246 int seats
247 int pieces
248 CODE:
249 7001 RETVAL = (STH->layout_ok)(seats, pieces);
250 OUTPUT:
251 RETVAL
252  
253 int
254 _layout_point(seats, seat)
255 int seats
256 int seat
257 CODE:
258 126 RETVAL = (STH->layout_point)(seats, seat);
259 OUTPUT:
260 RETVAL
261  
262 int
263 _hole_of(x, y)
264 int x
265 int y
266 CODE:
267 2777 RETVAL = (STH->hole_of)(x, y);
268 OUTPUT:
269 RETVAL
270  
271 int
272 _hole_x(hole)
273 int hole
274 CODE:
275 13532 RETVAL = (STH->hole_x)(hole);
276 OUTPUT:
277 RETVAL
278  
279 int
280 _hole_y(hole)
281 int hole
282 CODE:
283 13532 RETVAL = (STH->hole_y)(hole);
284 OUTPUT:
285 RETVAL
286  
287 int
288 _row_of(hole)
289 int hole
290 CODE:
291 4612 RETVAL = (STH->row_of)(hole);
292 OUTPUT:
293 RETVAL
294  
295 int
296 _col_of(hole)
297 int hole
298 CODE:
299 242 RETVAL = (STH->col_of)(hole);
300 OUTPUT:
301 RETVAL
302  
303 int
304 _row_len(row)
305 int row
306 CODE:
307 34 RETVAL = (STH->row_len)(row);
308 OUTPUT:
309 RETVAL
310  
311 int
312 _row_start(row)
313 int row
314 CODE:
315 123 RETVAL = (STH->row_start)(row);
316 OUTPUT:
317 RETVAL
318  
319 int
320 _next(hole, dir)
321 int hole
322 int dir
323 CODE:
324 1686298 RETVAL = (STH->next)(hole, dir);
325 OUTPUT:
326 RETVAL
327  
328 int
329 _degree(hole)
330 int hole
331 CODE:
332 763 RETVAL = (STH->degree)(hole);
333 OUTPUT:
334 RETVAL
335  
336 int
337 _dir_opposite(dir)
338 int dir
339 CODE:
340 631 RETVAL = (STH->dir_opposite)(dir);
341 OUTPUT:
342 RETVAL
343  
344 int
345 _point_of(hole)
346 int hole
347 CODE:
348 56357 RETVAL = (STH->point_of)(hole);
349 OUTPUT:
350 RETVAL
351  
352 int
353 _on_ring(hole)
354 int hole
355 CODE:
356 617 RETVAL = (STH->on_ring)(hole);
357 OUTPUT:
358 RETVAL
359  
360 int
361 _class_of(hole)
362 int hole
363 CODE:
364 437987 RETVAL = (STH->class_of)(hole);
365 OUTPUT:
366 RETVAL
367  
368 void
369 _point_holes(point)
370 int point
371 PREINIT:
372 int out[STH_POINT_HOLES];
373 int n, i;
374 PPCODE:
375 143 n = (STH->point_holes)(point, out, STH_POINT_HOLES);
376 143 ▶ if (n > STH_POINT_HOLES) n = STH_POINT_HOLES;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_POINT_HOLES) n = STH_POINT_HOLES; 0 143 143
377 143 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 143 0 143
EXTEND(SP, n); 0 143 143
378 1563 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 1420 143 1563
379  
380 void
381 _front(point)
382 int point
383 PREINIT:
384 int out[STH_FRONT_HOLES];
385 int n, i;
386 PPCODE:
387 31 n = (STH->front)(point, out, STH_FRONT_HOLES);
388 31 ▶ if (n > STH_FRONT_HOLES) n = STH_FRONT_HOLES;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_FRONT_HOLES) n = STH_FRONT_HOLES; 0 31 31
389 31 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 31 0 31
EXTEND(SP, n); 0 31 31
390 186 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 155 31 186
391  
392 void
393 _pairs()
394 PREINIT:
395 int out[2 * STH_PAIRS];
396 int n, i;
397 PPCODE:
398 1 n = (STH->pairs)(out, 2 * STH_PAIRS);
399 1 ▶ if (n > STH_PAIRS) n = STH_PAIRS;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_PAIRS) n = STH_PAIRS; 0 1 1
400 1 ▶ EXTEND(SP, 2 * n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 2 * n); 1 0 1
EXTEND(SP, 2 * n); 0 1 1
401 625 ▶ for (i = 0; i < 2 * n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2 * n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 624 1 625
402  
403 # ---- version 2: one step and one hop ----------------------------------------------
404  
405 void
406 _steps(u, hole)
407 UV u
408 int hole
409 PREINIT:
410 int out[STH_STEPS_MAX];
411 int n, i;
412 PPCODE:
413 69901 n = (STH->steps)(BOARD(u), hole, out, STH_STEPS_MAX);
414 69901 ▶ if (n > STH_STEPS_MAX) n = STH_STEPS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_STEPS_MAX) n = STH_STEPS_MAX; 0 69901 69901
415 69901 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 69901 0 69901
EXTEND(SP, n); 0 69901 69901
416 297303 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 227402 69901 297303
417  
418 void
419 _hops(u, hole, from)
420 UV u
421 int hole
422 int from
423 PREINIT:
424 int out[STH_HOPS_MAX];
425 int n, i;
426 PPCODE:
427 1438203 n = (STH->hops)(BOARD(u), hole, from, out, STH_HOPS_MAX);
428 1438203 ▶ if (n > STH_HOPS_MAX) n = STH_HOPS_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_HOPS_MAX) n = STH_HOPS_MAX; 0 1438203 1438203
429 1438203 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 1438203 0 1438203
EXTEND(SP, n); 0 1438203 1438203
430 4075561 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 2637358 1438203 4075561
431  
432 int
433 _do_move(u, from, to)
434 UV u
435 int from
436 int to
437 CODE:
438 64995 RETVAL = (STH->do_move)(BOARD(u), from, to);
439 OUTPUT:
440 RETVAL
441  
442 int
443 _undo_move(u, from, to)
444 UV u
445 int from
446 int to
447 CODE:
448 59943 RETVAL = (STH->undo_move)(BOARD(u), from, to);
449 OUTPUT:
450 RETVAL
451  
452 # ---- version 3: a move -------------------------------------------------------------
453  
454 void
455 _reach(u, from)
456 UV u
457 int from
458 PREINIT:
459 int out[STH_REACH_MAX];
460 int n, i;
461 PPCODE:
462 117359 n = (STH->reach)(BOARD(u), from, out, STH_REACH_MAX);
463 117359 ▶ if (n > STH_REACH_MAX) n = STH_REACH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_REACH_MAX) n = STH_REACH_MAX; 0 117359 117359
464 117359 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 117359 0 117359
EXTEND(SP, n); 0 117359 117359
465 778898 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 661539 117359 778898
466  
467 void
468 _path(u, from, to)
469 UV u
470 int from
471 int to
472 PREINIT:
473 int out[STH_PATH_MAX];
474 int n, i;
475 PPCODE:
476 521569 n = (STH->path)(BOARD(u), from, to, out, STH_PATH_MAX);
477 521569 ▶ if (n > STH_PATH_MAX) n = STH_PATH_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_PATH_MAX) n = STH_PATH_MAX; 0 521569 521569
478 521569 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 521569 0 521569
EXTEND(SP, n); 0 521569 521569
479 1814706 ▶ for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i])));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) PUSHs(sv_2mortal(newSViv(out[i]))); 1293137 521569 1814706
480  
481 # Flat: from, to, from, to. The Perl pairs them.
482 void
483 _legal(u)
484 UV u
485 PREINIT:
486 int from[STH_LEGAL_MAX], to[STH_LEGAL_MAX];
487 int n, i;
488 PPCODE:
489 113736 n = (STH->legal)(BOARD(u), from, to, STH_LEGAL_MAX);
490 113736 ▶ if (n > STH_LEGAL_MAX) n = STH_LEGAL_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > STH_LEGAL_MAX) n = STH_LEGAL_MAX; 0 113736 113736
491 113736 ▶ EXTEND(SP, 2 * n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, 2 * n); 113736 0 113736
EXTEND(SP, 2 * n); 0 113736 113736
492 5482426 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5368690 113736 5482426
493 5368690 PUSHs(sv_2mortal(newSViv(from[i])));
494 5368690 PUSHs(sv_2mortal(newSViv(to[i])));
495 }
496  
497 # ---- version 4: a game --------------------------------------------------------------
498 #
499 # play, pass and resign answer the code and, when it is STH_PLAY_OK, the undo
500 # as a string of the struct's bytes, which _unplay takes back.
501  
502 int
503 _home_complete(u, seat)
504 UV u
505 int seat
506 CODE:
507 10 RETVAL = (STH->home_complete)(BOARD(u), seat);
508 OUTPUT:
509 RETVAL
510  
511 int
512 _finished(u, seat)
513 UV u
514 int seat
515 CODE:
516 490419 RETVAL = (STH->finished)(BOARD(u), seat);
517 OUTPUT:
518 RETVAL
519  
520 int
521 _place_of(u, seat)
522 UV u
523 int seat
524 CODE:
525 314844 RETVAL = (STH->place_of)(BOARD(u), seat);
526 OUTPUT:
527 RETVAL
528  
529 int
530 _distance_left(u, seat)
531 UV u
532 int seat
533 CODE:
534 661883 RETVAL = (STH->distance_left)(BOARD(u), seat);
535 OUTPUT:
536 RETVAL
537  
538 int
539 _can_pass(u)
540 UV u
541 CODE:
542 96896 RETVAL = (STH->can_pass)(BOARD(u));
543 OUTPUT:
544 RETVAL
545  
546 void
547 _play(u, from, to)
548 UV u
549 int from
550 int to
551 PREINIT:
552 struct sth_undo undo;
553 int why;
554 PPCODE:
555 1053664 why = (STH->play)(BOARD(u), from, to, &undo);
556 1053664 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1053664 1053664
557 1053664 PUSHs(sv_2mortal(newSViv(why)));
558 1053664 ▶ if (why == STH_PLAY_OK) PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
Branch: 1 branch
BranchTrueFalseTotal
if (why == STH_PLAY_OK) PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo)))); 1053658 6 1053664
559  
560 void
561 _pass(u)
562 UV u
563 PREINIT:
564 struct sth_undo undo;
565 int why;
566 PPCODE:
567 66 why = (STH->pass)(BOARD(u), &undo);
568 66 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 66 66
569 66 PUSHs(sv_2mortal(newSViv(why)));
570 66 ▶ if (why == STH_PLAY_OK) PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
Branch: 1 branch
BranchTrueFalseTotal
if (why == STH_PLAY_OK) PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo)))); 64 2 66
571  
572 void
573 _resign(u, seat)
574 UV u
575 int seat
576 PREINIT:
577 struct sth_undo undo;
578 int why;
579 PPCODE:
580 704 why = (STH->resign)(BOARD(u), seat, &undo);
581 704 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 704 704
582 704 PUSHs(sv_2mortal(newSViv(why)));
583 704 ▶ if (why == STH_PLAY_OK) PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
Branch: 1 branch
BranchTrueFalseTotal
if (why == STH_PLAY_OK) PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo)))); 652 52 704
584  
585 int
586 _unplay(u, token)
587 UV u
588 SV *token
589 PREINIT:
590 STRLEN len;
591 const char *bytes;
592 struct sth_undo undo;
593 CODE:
594 193643 bytes = SvPV(token, len);
595 193643 ▶ if (len != sizeof(undo))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(undo)) 0 193643 193643
596 0 croak("Game::Sternhalma::Engine: that is not an undo token");
597 193643 Copy(bytes, &undo, 1, struct sth_undo);
598 193643 RETVAL = (STH->unplay)(BOARD(u), &undo);
599 OUTPUT:
600 RETVAL
601  
602 int
603 _game_over(u)
604 UV u
605 CODE:
606 702569 RETVAL = (STH->game_over)(BOARD(u));
607 OUTPUT:
608 RETVAL
609  
610 int
611 _game_how(u)
612 UV u
613 CODE:
614 107992 RETVAL = (STH->game_how)(BOARD(u));
615 OUTPUT:
616 RETVAL
617  
618 int
619 _ply(u)
620 UV u
621 CODE:
622 86058 RETVAL = (STH->ply)(BOARD(u));
623 OUTPUT:
624 RETVAL
625  
626 int
627 _cap(u)
628 UV u
629 CODE:
630 741 RETVAL = (STH->cap)(BOARD(u));
631 OUTPUT:
632 RETVAL
633  
634 void
635 _set_cap(u, cap)
636 UV u
637 int cap
638 CODE:
639 1189 (STH->set_cap)(BOARD(u), cap);
640  
641 # ---- version 6: choosing a move ------------------------------------------------------
642 #
643 # The weights cross as four ints, distance, straggler, classes, blocking. The
644 # node count crosses as a decimal string, as a key crosses as hex: a budget can
645 # outgrow a 32-bit IV.
646  
647 void
648 _weights_default()
649 PREINIT:
650 struct sth_weights w;
651 PPCODE:
652 364708 (STH->weights_default)(&w);
653 364708 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 364708 364708
654 364708 PUSHs(sv_2mortal(newSViv(w.distance)));
655 364708 PUSHs(sv_2mortal(newSViv(w.straggler)));
656 364708 PUSHs(sv_2mortal(newSViv(w.classes)));
657 364708 PUSHs(sv_2mortal(newSViv(w.blocking)));
658  
659 int
660 _evaluate(u, seat, distance, straggler, classes, blocking)
661 UV u
662 int seat
663 int distance
664 int straggler
665 int classes
666 int blocking
667 PREINIT:
668 struct sth_weights w;
669 CODE:
670 361958 (STH->weights_default)(&w);
671 361958 w.distance = distance; w.straggler = straggler; w.classes = classes; w.blocking = blocking;
672 361958 RETVAL = (STH->evaluate)(BOARD(u), seat, &w);
673 OUTPUT:
674 RETVAL
675  
676 # `avoid` is an array reference of keys as sixteen hex digits, the positions
677 # the game has already had.
678 void
679 _search(u, budget, seed, max_depth, mode, distance, straggler, classes, blocking, avoid)
680 UV u
681 UV budget
682 UV seed
683 int max_depth
684 int mode
685 int distance
686 int straggler
687 int classes
688 int blocking
689 SV *avoid
690 PREINIT:
691 struct sth_weights w;
692 struct sth_search_info info;
693 2750 int from = -1, to = -1, ok, n = 0, i;
694 2750 unsigned long long *keys = NULL;
695 char buf[24];
696 PPCODE:
697 2750 (STH->weights_default)(&w);
698 2750 w.distance = distance; w.straggler = straggler; w.classes = classes; w.blocking = blocking;
699 2750 ▶ if (SvROK(avoid) && SvTYPE(SvRV(avoid)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(avoid) && SvTYPE(SvRV(avoid)) == SVt_PVAV) { 2548 202 2750
if (SvROK(avoid) && SvTYPE(SvRV(avoid)) == SVt_PVAV) { 2548 0 2548
700 2548 AV *av = (AV *) SvRV(avoid);
701 2548 int len = (int) (av_len(av) + 1);
702 2548 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 2548 0 2548
703 2548 Newx(keys, len, unsigned long long);
704 45625 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 43077 2548 45625
705 43077 SV **e = av_fetch(av, i, 0);
706 86154 ▶ if (e && SvOK(*e)) keys[n++] = strtoull(SvPV_nolen(*e), NULL, 16);
Branch: 2 branches
BranchTrueFalseTotal
if (e && SvOK(*e)) keys[n++] = strtoull(SvPV_nolen(*e), NULL, 16); 43077 0 43077
if (e && SvOK(*e)) keys[n++] = strtoull(SvPV_nolen(*e), NULL, 16); 43077 0 43077
707 }
708 }
709 }
710 2750 ok = (STH->search)(BOARD(u), &w, (unsigned long long)budget, (unsigned long long)seed,
711 max_depth, mode, keys, n, &from, &to, &info);
712 2750 Safefree(keys);
713 2750 ▶ if (!ok) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) XSRETURN_EMPTY; 0 2750 2750
714 2750 my_snprintf(buf, sizeof buf, "%llu", info.nodes);
715 2750 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 2750 2750
716 2750 PUSHs(sv_2mortal(newSViv(from)));
717 2750 PUSHs(sv_2mortal(newSViv(to)));
718 2750 PUSHs(sv_2mortal(newSVpv(buf, 0)));
719 2750 PUSHs(sv_2mortal(newSViv(info.depth)));
720 2750 PUSHs(sv_2mortal(newSViv(info.score)));
721 2750 PUSHs(sv_2mortal(newSViv(info.stopped)));
722  
723 void
724 _random_forward(u, seed)
725 UV u
726 UV seed
727 PREINIT:
728 1664 int from = -1, to = -1;
729 PPCODE:
730 1664 ▶ if (!(STH->random_forward)(BOARD(u), (unsigned long long)seed, &from, &to)) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!(STH->random_forward)(BOARD(u), (unsigned long long)seed, &from, &to)) XSRETURN_EMPTY; 0 1664 1664
731 1664 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1664 1664
732 1664 PUSHs(sv_2mortal(newSViv(from)));
733 1664 PUSHs(sv_2mortal(newSViv(to)));
734  
735 SV *
736 _own_key_hex(u, seat)
737 UV u
738 int seat
739 CODE:
740 6117 RETVAL = sth_hex64(aTHX_ (STH->own_key)(BOARD(u), seat));
741 OUTPUT:
742 RETVAL
743  
744 MODULE = Game::Sternhalma PACKAGE = Game::Sternhalma::Engine::Hold
745  
746 PROTOTYPES: DISABLE
747  
748 # ---- letting go -----------------------------------------------------------------
749 #
750 # WHY THIS IS NOT A DEMOLISH IN PERL. Fanorona's was, through eight phases. On
751 # perls 5.14 to 5.26 an eval that runs inside a destructor, while an exception
752 # is unwinding past a TEMPORARY of that class, leaves $@ empty: the caller's die
753 # arrives with no message. Object::Proto calls DEMOLISH under an eval, so any
754 # class with one has this. A DESTROY written in C runs no eval.
755  
756 void
757 DESTROY(self)
758 SV *self
759 PREINIT:
760 SV *inner;
761 UV u;
762 CODE:
763 807376 ▶ if (SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(self)) { 807376 0 807376
764 807376 inner = SvRV(self);
765 807376 u = SvUV(inner);
766 807376 ▶ if (u) {
Branch: 1 branch
BranchTrueFalseTotal
if (u) { 807376 0 807376
767 807376 sv_setuv(inner, 0);
768 807376 (STH->board_drop)(BOARD(u));
769 }
770 }
« blib/arch/Game/Sternhalma/Install/Files.pm ↑ Summary sth_engine.c »