RoyalUr.xs

filter: Statementstmt 97.6% 167 / 171 Branchbran 71.2% 94 / 132 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.1% 261 / 303 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/RoyalUr/Move.pm ↑ Summary ru_moves.c »
1 /* RoyalUr.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::RoyalUr::Engine, not in Game::RoyalUr, and are named
8 * with a leading underscore: they take a raw pointer as a UV and there is no
9 * typemap. The Perl that owns the lifetime is lib/Game/RoyalUr/Engine.pm.
10 *
11 * `_put`, `_lift`, `_set_hand` and `_set_home` are the structure primitives
12 * from ru_abi.h and CHECK 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 "ru_abi.h"
22  
23 static const struct ru_abi *RU = NULL;
24  
25 #define BOARD(u) INT2PTR(struct ru_board *, (u))
26  
27 /* The key crosses as a 12-character hex string, never as a UV.
28 *
29 * It is 47 bits. On a perl with 32-bit IVs a UV cannot hold it and the top
30 * would vanish silently, which for a key means a test that passes while
31 * comparing part of a number. Hand-rolled rather than via my_snprintf, as
32 * Brandubh.xs, Xiangqi.xs and Go.xs do it.
33 */
34 66820 static SV *ru_hex48(pTHX_ unsigned long long v)
35 {
36 static const char digits[] = "0123456789abcdef";
37 char buf[12];
38 int i;
39 868660 ▶ for (i = 11; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 11; i >= 0; i--) { 801840 66820 868660
40 801840 buf[i] = digits[v & 0xfULL];
41 801840 v >>= 4;
42 }
43 66820 return newSVpvn(buf, 12);
44 }
45  
46 /* A count crosses as a decimal STRING, for the reason a key crosses as hex: the
47 * positions of a rule set outgrow a 32-bit IV. Hand-rolled and integer only;
48 * there is no floating point anywhere in this distribution's C.
49 */
50 6540 static SV *ru_dec64(pTHX_ unsigned long long v)
51 {
52 char buf[24];
53 6540 int i = (int) sizeof(buf);
54 do {
55 16045 buf[--i] = (char) ('0' + (int) (v % 10ULL));
56 16045 v /= 10ULL;
57 16045 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 9505 6540 16045
58 6540 return newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i));
59 }
60  
61 /* The rule set crosses as undef for the standard game, as the NAME of a set,
62 * or as a hash reference of what differs from the standard game.
63 *
64 * AN UNKNOWN KEY IS A CROAK and not a shrug: `safe_rosette => 0` silently
65 * playing the standard game is the kind of mistake that agrees with you. So
66 * is a value a field may not hold.
67 */
68 3082380 static void ru_rules_of_sv(pTHX_ SV *sv, struct ru_rules *r)
69 {
70 HV *hv;
71 HE *he;
72  
73 3082380 (RU->rules_named)(RU_RULES_FINKEL, r);
74 3082380 ▶ if (!sv || !SvOK(sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return; 3082380 0 3082380
if (!sv || !SvOK(sv)) return; 207 3082173 3082380
75  
76 3082173 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 79839 3002334 3082173
77 STRLEN len;
78 79839 const char *name = SvPV(sv, len);
79 79839 ▶ if (len == 6 && memEQ(name, "finkel", 6)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 6 && memEQ(name, "finkel", 6)) return; 42406 37433 79839
if (len == 6 && memEQ(name, "finkel", 6)) return; 42406 0 42406
80 37433 ▶ if (len == 7 && memEQ(name, "masters", 7)) { (RU->rules_named)(RU_RULES_MASTERS, r); return; }
Branch: 2 branches
BranchTrueFalseTotal
if (len == 7 && memEQ(name, "masters", 7)) { (RU->rules_named)(RU_RULES_MASTERS, r); return; } 37431 2 37433
if (len == 7 && memEQ(name, "masters", 7)) { (RU->rules_named)(RU_RULES_MASTERS, r); return; } 37431 0 37431
81 2 croak("Game::RoyalUr::Engine: no rule set is called '%" SVf "'", SVfARG(sv));
82 }
83 3002334 ▶ if (SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(sv)) != SVt_PVHV) 1 3002333 3002334
84 1 croak("Game::RoyalUr::Engine: a rule set is a name or a hash reference");
85 3002333 hv = (HV *) SvRV(sv);
86  
87 3002333 hv_iterinit(hv);
88 18010142 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 15007811 3002331 18010142
89 STRLEN klen;
90 15007811 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 15007811 15007811
91 15007811 SV *val = HeVAL(he);
92  
93 18010117 ▶ if (klen == 5 && memEQ(key, "route", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 5 && memEQ(key, "route", 5)) { 3002307 12005504 15007811
if (klen == 5 && memEQ(key, "route", 5)) { 3002307 0 3002307
94 STRLEN vlen;
95 3002307 const char *s = SvPV(val, vlen);
96 3002307 ▶ if (vlen == 5 && memEQ(s, "short", 5)) r->route = RU_ROUTE_SHORT;
Branch: 2 branches
BranchTrueFalseTotal
if (vlen == 5 && memEQ(s, "short", 5)) r->route = RU_ROUTE_SHORT; 1303689 1698618 3002307
if (vlen == 5 && memEQ(s, "short", 5)) r->route = RU_ROUTE_SHORT; 1303689 0 1303689
97 1698618 ▶ else if (vlen == 4 && memEQ(s, "long", 4)) r->route = RU_ROUTE_LONG;
Branch: 2 branches
BranchTrueFalseTotal
else if (vlen == 4 && memEQ(s, "long", 4)) r->route = RU_ROUTE_LONG; 1698617 1 1698618
else if (vlen == 4 && memEQ(s, "long", 4)) r->route = RU_ROUTE_LONG; 1698617 0 1698617
98 1 else croak("Game::RoyalUr::Engine: route is 'short' or 'long'");
99 }
100 12005504 ▶ else if (klen == 4 && memEQ(key, "dice", 4))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 4 && memEQ(key, "dice", 4)) 3001061 9004443 12005504
else if (klen == 4 && memEQ(key, "dice", 4)) 3001061 0 3001061
101 3001061 r->dice = (int) SvIV(val);
102 9004443 ▶ else if (klen == 10 && memEQ(key, "zero_rolls", 10))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 10 && memEQ(key, "zero_rolls", 10)) 3001060 6003383 9004443
else if (klen == 10 && memEQ(key, "zero_rolls", 10)) 3001060 0 3001060
103 3001060 r->zero_rolls = (int) SvIV(val);
104 6003383 ▶ else if (klen == 13 && memEQ(key, "safe_rosettes", 13))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 13 && memEQ(key, "safe_rosettes", 13)) 3002302 3001081 6003383
else if (klen == 13 && memEQ(key, "safe_rosettes", 13)) 3002302 0 3002302
105 3002302 r->safe_rosettes = SvTRUE(val) ? 1 : 0;
106 3001081 ▶ else if (klen == 6 && memEQ(key, "pieces", 6))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && memEQ(key, "pieces", 6)) 3001080 1 3001081
else if (klen == 6 && memEQ(key, "pieces", 6)) 3001080 0 3001080
107 3001080 r->pieces = (int) SvIV(val);
108 else
109 1 ▶ croak("Game::RoyalUr::Engine: no rule is called '%" SVf "'",
Branch: 2 branches
BranchTrueFalseTotal
croak("Game::RoyalUr::Engine: no rule is called '%" SVf "'", 1 0 1
croak("Game::RoyalUr::Engine: no rule is called '%" SVf "'", 0 1 1
110 SVfARG(HeSVKEY_force(he)));
111 }
112 3002331 ▶ if (!(RU->rules_ok)(r))
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->rules_ok)(r)) 5 3002326 3002331
113 5 croak("Game::RoyalUr::Engine: dice is 3 or 4, zero_rolls is 0 or 4, and pieces is 1 to 7");
114 }
115  
116 /* The weights of the evaluation cross as a hash reference, or as undef for the
117 * plain evaluation, and an unknown key croaks, for the reason a rule's does. */
118 6052 static void ru_weights_of_sv(pTHX_ SV *sv, struct ru_weights *w)
119 {
120 HV *hv;
121 HE *he;
122  
123 6052 (RU->weights_default)(w);
124 6052 ▶ if (!sv || !SvOK(sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return; 6052 0 6052
if (!sv || !SvOK(sv)) return; 2066 3986 6052
125 3986 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 3986 0 3986
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 1 3985 3986
126 1 croak("Game::RoyalUr::Engine: weights are a hash reference");
127 3985 hv = (HV *) SvRV(sv);
128  
129 3985 hv_iterinit(hv);
130 12145 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 8161 3984 12145
131 STRLEN klen;
132 8161 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 8161 8161
133 8161 int val = (int) SvIV(HeVAL(he));
134  
135 8161 ▶ if (klen == 7 && memEQ(key, "exposed", 7)) w->exposed = val;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memEQ(key, "exposed", 7)) w->exposed = val; 6070 2091 8161
if (klen == 7 && memEQ(key, "exposed", 7)) w->exposed = val; 3980 2090 6070
136 4181 ▶ else if (klen == 7 && memEQ(key, "rosette", 7)) w->rosette = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 7 && memEQ(key, "rosette", 7)) w->rosette = val; 2090 2091 4181
else if (klen == 7 && memEQ(key, "rosette", 7)) w->rosette = val; 2090 0 2090
137 2091 ▶ else if (klen == 5 && memEQ(key, "entry", 5)) w->entry = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 5 && memEQ(key, "entry", 5)) w->entry = val; 2090 1 2091
else if (klen == 5 && memEQ(key, "entry", 5)) w->entry = val; 2090 0 2090
138 else
139 1 ▶ croak("Game::RoyalUr::Engine: no weight is called '%" SVf "'",
Branch: 2 branches
BranchTrueFalseTotal
croak("Game::RoyalUr::Engine: no weight is called '%" SVf "'", 1 0 1
croak("Game::RoyalUr::Engine: no weight is called '%" SVf "'", 0 1 1
140 SVfARG(HeSVKEY_force(he)));
141 }
142 }
143  
144 MODULE = Game::RoyalUr PACKAGE = Game::RoyalUr::Engine
145  
146 PROTOTYPES: DISABLE
147  
148 BOOT:
149 73 RU = ru_abi_table();
150  
151 UV
152 _abi_ptr()
153 CODE:
154 0 ▶ RETVAL = PTR2UV(RU);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(RU); 0 0 0
155 OUTPUT:
156 RETVAL
157  
158 UV
159 _abi_version()
160 CODE:
161 1 ▶ RETVAL = (UV)RU->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)RU->abi_version; 0 1 1
162 OUTPUT:
163 RETVAL
164  
165 UV
166 _new_board(pieces)
167 int pieces
168 CODE:
169 12827 ▶ RETVAL = PTR2UV((RU->board_new)(pieces));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((RU->board_new)(pieces)); 12774 53 12827
170 OUTPUT:
171 RETVAL
172  
173 UV
174 _copy_board(u)
175 UV u
176 CODE:
177 58371 ▶ RETVAL = PTR2UV((RU->board_copy)(BOARD(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((RU->board_copy)(BOARD(u))); 58322 49 58371
178 OUTPUT:
179 RETVAL
180  
181 void
182 _drop_board(u)
183 UV u
184 CODE:
185 79117 (RU->board_drop)(BOARD(u));
186  
187 int
188 _live()
189 CODE:
190 12 RETVAL = (RU->live)();
191 OUTPUT:
192 RETVAL
193  
194 # A string that will not parse returns undef and the code, rather than
195 # croaking: the reason is what a caller shows a person, and ten of them are
196 # distinguishable.
197 void
198 _of_string(str)
199 const char *str
200 PREINIT:
201 struct ru_board *b;
202 8013 int err = 0;
203 PPCODE:
204 8013 b = (RU->board_of_string)(str, &err);
205 8013 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 8013 8013
206 8013 ▶ if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b))));
Branch: 1 branch
BranchTrueFalseTotal
if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); 7919 94 8013
207 94 else PUSHs(&PL_sv_undef);
208 8013 PUSHs(sv_2mortal(newSViv(err)));
209  
210 SV *
211 _to_string(u)
212 UV u
213 PREINIT:
214 char buf[RU_POS_MAX];
215 int n;
216 CODE:
217 64889 n = (RU->to_string)(BOARD(u), buf, (int)sizeof(buf));
218 64889 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 64889 64889
219 64889 RETVAL = newSVpvn(buf, (STRLEN)n);
220 OUTPUT:
221 RETVAL
222  
223 int
224 _at(u, cell)
225 UV u
226 int cell
227 CODE:
228 274337 RETVAL = (RU->at)(BOARD(u), cell);
229 OUTPUT:
230 RETVAL
231  
232 void
233 _put(u, cell, piece)
234 UV u
235 int cell
236 int piece
237 CODE:
238 92412 (RU->put)(BOARD(u), cell, piece);
239  
240 void
241 _lift(u, cell)
242 UV u
243 int cell
244 CODE:
245 2 (RU->lift)(BOARD(u), cell);
246  
247 int
248 _hand(u, side)
249 UV u
250 int side
251 CODE:
252 10792 RETVAL = (RU->hand)(BOARD(u), side);
253 OUTPUT:
254 RETVAL
255  
256 void
257 _set_hand(u, side, n)
258 UV u
259 int side
260 int n
261 CODE:
262 22611 (RU->set_hand)(BOARD(u), side, n);
263  
264 int
265 _home(u, side)
266 UV u
267 int side
268 CODE:
269 34883 RETVAL = (RU->home)(BOARD(u), side);
270 OUTPUT:
271 RETVAL
272  
273 void
274 _set_home(u, side, n)
275 UV u
276 int side
277 int n
278 CODE:
279 23209 (RU->set_home)(BOARD(u), side, n);
280  
281 int
282 _side(u)
283 UV u
284 CODE:
285 1361155 RETVAL = (RU->side)(BOARD(u));
286 OUTPUT:
287 RETVAL
288  
289 void
290 _set_side(u, side)
291 UV u
292 int side
293 CODE:
294 12847 (RU->set_side)(BOARD(u), side);
295  
296 int
297 _count(u, side)
298 UV u
299 int side
300 CODE:
301 9 RETVAL = (RU->count)(BOARD(u), side);
302 OUTPUT:
303 RETVAL
304  
305 int
306 _consistent(u, n)
307 UV u
308 int n
309 CODE:
310 21367 RETVAL = (RU->consistent)(BOARD(u), n);
311 OUTPUT:
312 RETVAL
313  
314 SV *
315 _key_hex(u)
316 UV u
317 CODE:
318 66820 RETVAL = ru_hex48(aTHX_ (RU->key)(BOARD(u)));
319 OUTPUT:
320 RETVAL
321  
322 int
323 _cell_of(file, row)
324 int file
325 int row
326 CODE:
327 72037 RETVAL = (RU->cell_of)(file, row);
328 OUTPUT:
329 RETVAL
330  
331 int
332 _file_of(cell)
333 int cell
334 CODE:
335 3292252 RETVAL = (RU->file_of)(cell);
336 OUTPUT:
337 RETVAL
338  
339 int
340 _row_of(cell)
341 int cell
342 CODE:
343 3292252 RETVAL = (RU->row_of)(cell);
344 OUTPUT:
345 RETVAL
346  
347 int
348 _is_rosette(cell)
349 int cell
350 CODE:
351 136 RETVAL = (RU->is_rosette)(cell);
352 OUTPUT:
353 RETVAL
354  
355 int
356 _route_len(route)
357 int route
358 CODE:
359 9941 RETVAL = (RU->route_len)(route);
360 OUTPUT:
361 RETVAL
362  
363 int
364 _route_cell(route, side, step)
365 int route
366 int side
367 int step
368 CODE:
369 9300 RETVAL = (RU->route_cell)(route, side, step);
370 OUTPUT:
371 RETVAL
372  
373 int
374 _route_step(route, side, cell)
375 int route
376 int side
377 int cell
378 CODE:
379 8739 RETVAL = (RU->route_step)(route, side, cell);
380 OUTPUT:
381 RETVAL
382  
383 int
384 _route_shared(route, cell)
385 int route
386 int cell
387 CODE:
388 179 RETVAL = (RU->route_shared)(route, cell);
389 OUTPUT:
390 RETVAL
391  
392 # ---- version 2: the chance of a roll -------------------------------------------
393  
394 int
395 _chance_count(dice, zero_rolls)
396 int dice
397 int zero_rolls
398 CODE:
399 6349 RETVAL = (RU->chance_count)(dice, zero_rolls);
400 OUTPUT:
401 RETVAL
402  
403 # The roll, its weight and the denominator, or nothing when there is no such
404 # entry.
405 void
406 _chance(dice, zero_rolls, i)
407 int dice
408 int zero_rolls
409 int i
410 PREINIT:
411 27965 int roll = 0, weight = 0, denominator = 0;
412 PPCODE:
413 27965 ▶ if ((RU->chance)(dice, zero_rolls, i, &roll, &weight, &denominator)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->chance)(dice, zero_rolls, i, &roll, &weight, &denominator)) { 27965 0 27965
414 27965 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 27965 27965
415 27965 PUSHs(sv_2mortal(newSViv(roll)));
416 27965 PUSHs(sv_2mortal(newSViv(weight)));
417 27965 PUSHs(sv_2mortal(newSViv(denominator)));
418 }
419  
420 # ---- version 3: what a roll allows ---------------------------------------------
421  
422 int
423 _moves_max()
424 CODE:
425 0 ▶ RETVAL = RU_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = RU_MOVES_MAX; 0 0 0
426 OUTPUT:
427 RETVAL
428  
429 # The rule set a name or a hash stands for, as five numbers: route, dice,
430 # zero_rolls, safe_rosettes, pieces.
431 void
432 _rules(rules)
433 SV *rules
434 PREINIT:
435 struct ru_rules r;
436 PPCODE:
437 1472 ru_rules_of_sv(aTHX_ rules, &r);
438 1471 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 1471 1471
439 1471 PUSHs(sv_2mortal(newSViv(r.route)));
440 1471 PUSHs(sv_2mortal(newSViv(r.dice)));
441 1471 PUSHs(sv_2mortal(newSViv(r.zero_rolls)));
442 1471 PUSHs(sv_2mortal(newSViv(r.safe_rosettes)));
443 1471 PUSHs(sv_2mortal(newSViv(r.pieces)));
444  
445 # Each move as seven numbers in an array: from_step, to_step, from_cell,
446 # to_cell, captures, rosette, home. A roll that is not 0 to 4 croaks; it is
447 # not an empty list.
448 void
449 _moves(u, roll, rules)
450 UV u
451 int roll
452 SV *rules
453 PREINIT:
454 struct ru_rules r;
455 struct ru_move m[RU_MOVES_MAX];
456 int n, i;
457 PPCODE:
458 656242 ru_rules_of_sv(aTHX_ rules, &r);
459 656233 n = (RU->moves)(BOARD(u), &r, roll, m, RU_MOVES_MAX);
460 656233 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 0 656233 656233
461 0 croak("Game::RoyalUr::Engine: a roll is a whole number from 0 to %d", RU_ROLL_MAX);
462 656233 ▶ if (n > RU_MOVES_MAX) n = RU_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > RU_MOVES_MAX) n = RU_MOVES_MAX; 0 656233 656233
463 656233 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 656233 0 656233
EXTEND(SP, n); 0 656233 656233
464 2525294 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1869061 656233 2525294
465 1869061 AV *av = newAV();
466 1869061 av_extend(av, 6);
467 1869061 av_push(av, newSViv(m[i].from_step));
468 1869061 av_push(av, newSViv(m[i].to_step));
469 1869061 av_push(av, newSViv(m[i].from_cell));
470 1869061 av_push(av, newSViv(m[i].to_cell));
471 1869061 av_push(av, newSViv(m[i].captures));
472 1869061 av_push(av, newSViv(m[i].rosette));
473 1869061 av_push(av, newSViv(m[i].home));
474 1869061 PUSHs(sv_2mortal(newRV_noinc((SV *) av)));
475 }
476  
477 SV *
478 _count_positions(rules)
479 SV *rules
480 PREINIT:
481 struct ru_rules r;
482 CODE:
483 26 ru_rules_of_sv(aTHX_ rules, &r);
484 26 RETVAL = ru_dec64(aTHX_ (RU->count_positions)(&r));
485 OUTPUT:
486 RETVAL
487  
488 # ---- version 4: a move made ------------------------------------------------------
489  
490 int
491 _ply(u)
492 UV u
493 CODE:
494 269799 RETVAL = (RU->ply)(BOARD(u));
495 OUTPUT:
496 RETVAL
497  
498 void
499 _set_ply(u, ply)
500 UV u
501 int ply
502 CODE:
503 13 (RU->set_ply)(BOARD(u), ply);
504  
505 int
506 _ply_cap()
507 CODE:
508 1 RETVAL = (RU->ply_cap)();
509 OUTPUT:
510 RETVAL
511  
512 # What apply and forfeit hand back is the undo, as the bytes of the struct. It
513 # is opaque: the only thing to do with one is hand it to _unapply. undef when
514 # the move was not made.
515 SV *
516 _apply(u, rules, from_step, to_step, from_cell, to_cell, home)
517 UV u
518 SV *rules
519 int from_step
520 int to_step
521 int from_cell
522 int to_cell
523 int home
524 PREINIT:
525 struct ru_rules r;
526 struct ru_move m;
527 struct ru_undo undo;
528 CODE:
529 228927 ru_rules_of_sv(aTHX_ rules, &r);
530 228927 Zero(&m, 1, struct ru_move);
531 228927 m.from_step = from_step;
532 228927 m.to_step = to_step;
533 228927 m.from_cell = from_cell;
534 228927 m.to_cell = to_cell;
535 228927 m.home = home ? 1 : 0;
536 228927 ▶ if (!(RU->apply)(BOARD(u), &r, &m, &undo)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->apply)(BOARD(u), &r, &m, &undo)) XSRETURN_UNDEF; 2 228925 228927
537 228925 RETVAL = newSVpvn((const char *) &undo, sizeof(undo));
538 OUTPUT:
539 RETVAL
540  
541 SV *
542 _forfeit(u)
543 UV u
544 PREINIT:
545 struct ru_undo undo;
546 CODE:
547 12012 (RU->forfeit)(BOARD(u), &undo);
548 12012 RETVAL = newSVpvn((const char *) &undo, sizeof(undo));
549 OUTPUT:
550 RETVAL
551  
552 void
553 _unapply(u, token)
554 UV u
555 SV *token
556 PREINIT:
557 struct ru_undo undo;
558 STRLEN len;
559 const char *bytes;
560 CODE:
561 12276 ▶ if (!SvOK(token) || SvROK(token))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(token) || SvROK(token)) 12275 1 12276
if (!SvOK(token) || SvROK(token)) 0 12275 12275
562 1 croak("Game::RoyalUr::Engine: unapply takes what apply or forfeit returned");
563 12275 bytes = SvPVbyte(token, len);
564 12275 ▶ if (len != sizeof(undo))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(undo)) 1 12274 12275
565 1 croak("Game::RoyalUr::Engine: unapply takes what apply or forfeit returned");
566 12274 Copy(bytes, &undo, 1, struct ru_undo);
567 12274 (RU->unapply)(BOARD(u), &undo);
568  
569 int
570 _status(u, rules)
571 UV u
572 SV *rules
573 PREINIT:
574 struct ru_rules r;
575 CODE:
576 2182273 ru_rules_of_sv(aTHX_ rules, &r);
577 2182273 RETVAL = (RU->status)(BOARD(u), &r);
578 OUTPUT:
579 RETVAL
580  
581 int
582 _how(u, rules)
583 UV u
584 SV *rules
585 PREINIT:
586 struct ru_rules r;
587 CODE:
588 2566 ru_rules_of_sv(aTHX_ rules, &r);
589 2566 RETVAL = (RU->how)(BOARD(u), &r);
590 OUTPUT:
591 RETVAL
592  
593 int
594 _winner(u, rules)
595 UV u
596 SV *rules
597 PREINIT:
598 struct ru_rules r;
599 CODE:
600 2570 ru_rules_of_sv(aTHX_ rules, &r);
601 2570 RETVAL = (RU->winner)(BOARD(u), &r);
602 OUTPUT:
603 RETVAL
604  
605 SV *
606 _walk(u, depth, rules)
607 UV u
608 int depth
609 SV *rules
610 PREINIT:
611 struct ru_rules r;
612 CODE:
613 484 ru_rules_of_sv(aTHX_ rules, &r);
614 484 RETVAL = ru_dec64(aTHX_ (RU->walk)(BOARD(u), &r, depth));
615 OUTPUT:
616 RETVAL
617  
618 # ---- version 5: choosing a move ------------------------------------------------
619  
620 int
621 _evaluate(u, rules, weights, side)
622 UV u
623 SV *rules
624 SV *weights
625 int side
626 PREINIT:
627 struct ru_rules r;
628 struct ru_weights w;
629 CODE:
630 21 ru_rules_of_sv(aTHX_ rules, &r);
631 21 ru_weights_of_sv(aTHX_ weights, &w);
632 20 RETVAL = (RU->evaluate)(BOARD(u), &r, &w, side);
633 OUTPUT:
634 RETVAL
635  
636 int
637 _greedy(u, roll, rules)
638 UV u
639 int roll
640 SV *rules
641 PREINIT:
642 struct ru_rules r;
643 CODE:
644 1768 ru_rules_of_sv(aTHX_ rules, &r);
645 1768 RETVAL = (RU->greedy)(BOARD(u), &r, roll);
646 OUTPUT:
647 RETVAL
648  
649 # The index chosen, then the nodes spent as a string of digits, the depth
650 # finished, the value, and whether the budget ran out. A budget is a whole
651 # number of nodes from 0 to 2,000,000,000: more than that is refused, and does
652 # not wrap round on a perl whose integers are 32 bits.
653 void
654 _search(u, roll, rules, weights, budget, max_depth)
655 UV u
656 int roll
657 SV *rules
658 SV *weights
659 IV budget
660 int max_depth
661 PREINIT:
662 struct ru_rules r;
663 struct ru_weights w;
664 struct ru_search_info info;
665 int chosen;
666 PPCODE:
667 6031 ru_rules_of_sv(aTHX_ rules, &r);
668 6031 ru_weights_of_sv(aTHX_ weights, &w);
669 6030 ▶ if (budget < 0 || budget > 2000000000)
Branch: 2 branches
BranchTrueFalseTotal
if (budget < 0 || budget > 2000000000) 6030 0 6030
if (budget < 0 || budget > 2000000000) 0 6030 6030
670 0 croak("Game::RoyalUr::Engine: a budget is a number of nodes from 0 to 2000000000");
671 6030 chosen = (RU->search)(BOARD(u), &r, roll, &w, (unsigned long long) budget, max_depth, &info);
672 6030 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 6030 6030
673 6030 PUSHs(sv_2mortal(newSViv(chosen)));
674 6030 PUSHs(sv_2mortal(ru_dec64(aTHX_ info.nodes)));
675 6030 PUSHs(sv_2mortal(newSViv(info.depth)));
676 6030 PUSHs(sv_2mortal(newSViv(info.value)));
677 6030 PUSHs(sv_2mortal(newSViv(info.stopped)));
« blib/lib/Game/RoyalUr/Move.pm ↑ Summary ru_moves.c »