RoyalUr.xs

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 132 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 303 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/RoyalUr/Terminal.pm ↑ Summary ru_engine.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 0 static SV *ru_hex48(pTHX_ unsigned long long v)
35 {
36 static const char digits[] = "0123456789abcdef";
37 char buf[12];
38 int i;
39 0 ▶ for (i = 11; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 11; i >= 0; i--) { 0 0 0
40 0 buf[i] = digits[v & 0xfULL];
41 0 v >>= 4;
42 }
43 0 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 0 static SV *ru_dec64(pTHX_ unsigned long long v)
51 {
52 char buf[24];
53 0 int i = (int) sizeof(buf);
54 do {
55 0 buf[--i] = (char) ('0' + (int) (v % 10ULL));
56 0 v /= 10ULL;
57 0 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 0 0 0
58 0 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 0 static void ru_rules_of_sv(pTHX_ SV *sv, struct ru_rules *r)
69 {
70 HV *hv;
71 HE *he;
72  
73 0 (RU->rules_named)(RU_RULES_FINKEL, r);
74 0 ▶ if (!sv || !SvOK(sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return; 0 0 0
if (!sv || !SvOK(sv)) return; 0 0 0
75  
76 0 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 0 0 0
77 STRLEN len;
78 0 const char *name = SvPV(sv, len);
79 0 ▶ if (len == 6 && memEQ(name, "finkel", 6)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (len == 6 && memEQ(name, "finkel", 6)) return; 0 0 0
if (len == 6 && memEQ(name, "finkel", 6)) return; 0 0 0
80 0 ▶ 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; } 0 0 0
if (len == 7 && memEQ(name, "masters", 7)) { (RU->rules_named)(RU_RULES_MASTERS, r); return; } 0 0 0
81 0 croak("Game::RoyalUr::Engine: no rule set is called '%" SVf "'", SVfARG(sv));
82 }
83 0 ▶ if (SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(sv)) != SVt_PVHV) 0 0 0
84 0 croak("Game::RoyalUr::Engine: a rule set is a name or a hash reference");
85 0 hv = (HV *) SvRV(sv);
86  
87 0 hv_iterinit(hv);
88 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
89 STRLEN klen;
90 0 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 0 0
91 0 SV *val = HeVAL(he);
92  
93 0 ▶ if (klen == 5 && memEQ(key, "route", 5)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 5 && memEQ(key, "route", 5)) { 0 0 0
if (klen == 5 && memEQ(key, "route", 5)) { 0 0 0
94 STRLEN vlen;
95 0 const char *s = SvPV(val, vlen);
96 0 ▶ 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; 0 0 0
if (vlen == 5 && memEQ(s, "short", 5)) r->route = RU_ROUTE_SHORT; 0 0 0
97 0 ▶ 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; 0 0 0
else if (vlen == 4 && memEQ(s, "long", 4)) r->route = RU_ROUTE_LONG; 0 0 0
98 0 else croak("Game::RoyalUr::Engine: route is 'short' or 'long'");
99 }
100 0 ▶ else if (klen == 4 && memEQ(key, "dice", 4))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 4 && memEQ(key, "dice", 4)) 0 0 0
else if (klen == 4 && memEQ(key, "dice", 4)) 0 0 0
101 0 r->dice = (int) SvIV(val);
102 0 ▶ else if (klen == 10 && memEQ(key, "zero_rolls", 10))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 10 && memEQ(key, "zero_rolls", 10)) 0 0 0
else if (klen == 10 && memEQ(key, "zero_rolls", 10)) 0 0 0
103 0 r->zero_rolls = (int) SvIV(val);
104 0 ▶ else if (klen == 13 && memEQ(key, "safe_rosettes", 13))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 13 && memEQ(key, "safe_rosettes", 13)) 0 0 0
else if (klen == 13 && memEQ(key, "safe_rosettes", 13)) 0 0 0
105 0 r->safe_rosettes = SvTRUE(val) ? 1 : 0;
106 0 ▶ else if (klen == 6 && memEQ(key, "pieces", 6))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && memEQ(key, "pieces", 6)) 0 0 0
else if (klen == 6 && memEQ(key, "pieces", 6)) 0 0 0
107 0 r->pieces = (int) SvIV(val);
108 else
109 0 ▶ croak("Game::RoyalUr::Engine: no rule is called '%" SVf "'",
Branch: 2 branches
BranchTrueFalseTotal
croak("Game::RoyalUr::Engine: no rule is called '%" SVf "'", 0 0 0
croak("Game::RoyalUr::Engine: no rule is called '%" SVf "'", 0 0 0
110 SVfARG(HeSVKEY_force(he)));
111 }
112 0 ▶ if (!(RU->rules_ok)(r))
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->rules_ok)(r)) 0 0 0
113 0 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 0 static void ru_weights_of_sv(pTHX_ SV *sv, struct ru_weights *w)
119 {
120 HV *hv;
121 HE *he;
122  
123 0 (RU->weights_default)(w);
124 0 ▶ if (!sv || !SvOK(sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return; 0 0 0
if (!sv || !SvOK(sv)) return; 0 0 0
125 0 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 0 0 0
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 0 0 0
126 0 croak("Game::RoyalUr::Engine: weights are a hash reference");
127 0 hv = (HV *) SvRV(sv);
128  
129 0 hv_iterinit(hv);
130 0 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 0 0 0
131 STRLEN klen;
132 0 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 0 0
133 0 int val = (int) SvIV(HeVAL(he));
134  
135 0 ▶ if (klen == 7 && memEQ(key, "exposed", 7)) w->exposed = val;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memEQ(key, "exposed", 7)) w->exposed = val; 0 0 0
if (klen == 7 && memEQ(key, "exposed", 7)) w->exposed = val; 0 0 0
136 0 ▶ 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; 0 0 0
else if (klen == 7 && memEQ(key, "rosette", 7)) w->rosette = val; 0 0 0
137 0 ▶ 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; 0 0 0
else if (klen == 5 && memEQ(key, "entry", 5)) w->entry = val; 0 0 0
138 else
139 0 ▶ croak("Game::RoyalUr::Engine: no weight is called '%" SVf "'",
Branch: 2 branches
BranchTrueFalseTotal
croak("Game::RoyalUr::Engine: no weight is called '%" SVf "'", 0 0 0
croak("Game::RoyalUr::Engine: no weight is called '%" SVf "'", 0 0 0
140 SVfARG(HeSVKEY_force(he)));
141 }
142 }
143  
144 MODULE = Game::RoyalUr PACKAGE = Game::RoyalUr::Engine
145  
146 PROTOTYPES: DISABLE
147  
148 BOOT:
149 0 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 0 ▶ RETVAL = (UV)RU->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)RU->abi_version; 0 0 0
162 OUTPUT:
163 RETVAL
164  
165 UV
166 _new_board(pieces)
167 int pieces
168 CODE:
169 0 ▶ RETVAL = PTR2UV((RU->board_new)(pieces));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((RU->board_new)(pieces)); 0 0 0
170 OUTPUT:
171 RETVAL
172  
173 UV
174 _copy_board(u)
175 UV u
176 CODE:
177 0 ▶ RETVAL = PTR2UV((RU->board_copy)(BOARD(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((RU->board_copy)(BOARD(u))); 0 0 0
178 OUTPUT:
179 RETVAL
180  
181 void
182 _drop_board(u)
183 UV u
184 CODE:
185 0 (RU->board_drop)(BOARD(u));
186  
187 int
188 _live()
189 CODE:
190 0 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 0 int err = 0;
203 PPCODE:
204 0 b = (RU->board_of_string)(str, &err);
205 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
206 0 ▶ if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b))));
Branch: 1 branch
BranchTrueFalseTotal
if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); 0 0 0
207 0 else PUSHs(&PL_sv_undef);
208 0 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 0 n = (RU->to_string)(BOARD(u), buf, (int)sizeof(buf));
218 0 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 0 0
219 0 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 0 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 0 (RU->put)(BOARD(u), cell, piece);
239  
240 void
241 _lift(u, cell)
242 UV u
243 int cell
244 CODE:
245 0 (RU->lift)(BOARD(u), cell);
246  
247 int
248 _hand(u, side)
249 UV u
250 int side
251 CODE:
252 0 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 0 (RU->set_hand)(BOARD(u), side, n);
263  
264 int
265 _home(u, side)
266 UV u
267 int side
268 CODE:
269 0 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 0 (RU->set_home)(BOARD(u), side, n);
280  
281 int
282 _side(u)
283 UV u
284 CODE:
285 0 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 0 (RU->set_side)(BOARD(u), side);
295  
296 int
297 _count(u, side)
298 UV u
299 int side
300 CODE:
301 0 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 0 RETVAL = (RU->consistent)(BOARD(u), n);
311 OUTPUT:
312 RETVAL
313  
314 SV *
315 _key_hex(u)
316 UV u
317 CODE:
318 0 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 0 RETVAL = (RU->cell_of)(file, row);
328 OUTPUT:
329 RETVAL
330  
331 int
332 _file_of(cell)
333 int cell
334 CODE:
335 0 RETVAL = (RU->file_of)(cell);
336 OUTPUT:
337 RETVAL
338  
339 int
340 _row_of(cell)
341 int cell
342 CODE:
343 0 RETVAL = (RU->row_of)(cell);
344 OUTPUT:
345 RETVAL
346  
347 int
348 _is_rosette(cell)
349 int cell
350 CODE:
351 0 RETVAL = (RU->is_rosette)(cell);
352 OUTPUT:
353 RETVAL
354  
355 int
356 _route_len(route)
357 int route
358 CODE:
359 0 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 0 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 0 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 0 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 0 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 0 int roll = 0, weight = 0, denominator = 0;
412 PPCODE:
413 0 ▶ if ((RU->chance)(dice, zero_rolls, i, &roll, &weight, &denominator)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((RU->chance)(dice, zero_rolls, i, &roll, &weight, &denominator)) { 0 0 0
414 0 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 0 0
415 0 PUSHs(sv_2mortal(newSViv(roll)));
416 0 PUSHs(sv_2mortal(newSViv(weight)));
417 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
438 0 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 0 0
439 0 PUSHs(sv_2mortal(newSViv(r.route)));
440 0 PUSHs(sv_2mortal(newSViv(r.dice)));
441 0 PUSHs(sv_2mortal(newSViv(r.zero_rolls)));
442 0 PUSHs(sv_2mortal(newSViv(r.safe_rosettes)));
443 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
459 0 n = (RU->moves)(BOARD(u), &r, roll, m, RU_MOVES_MAX);
460 0 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 0 0 0
461 0 croak("Game::RoyalUr::Engine: a roll is a whole number from 0 to %d", RU_ROLL_MAX);
462 0 ▶ if (n > RU_MOVES_MAX) n = RU_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > RU_MOVES_MAX) n = RU_MOVES_MAX; 0 0 0
463 0 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 0 0 0
EXTEND(SP, n); 0 0 0
464 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
465 0 AV *av = newAV();
466 0 av_extend(av, 6);
467 0 av_push(av, newSViv(m[i].from_step));
468 0 av_push(av, newSViv(m[i].to_step));
469 0 av_push(av, newSViv(m[i].from_cell));
470 0 av_push(av, newSViv(m[i].to_cell));
471 0 av_push(av, newSViv(m[i].captures));
472 0 av_push(av, newSViv(m[i].rosette));
473 0 av_push(av, newSViv(m[i].home));
474 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
484 0 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 0 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 0 (RU->set_ply)(BOARD(u), ply);
504  
505 int
506 _ply_cap()
507 CODE:
508 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
530 0 Zero(&m, 1, struct ru_move);
531 0 m.from_step = from_step;
532 0 m.to_step = to_step;
533 0 m.from_cell = from_cell;
534 0 m.to_cell = to_cell;
535 0 m.home = home ? 1 : 0;
536 0 ▶ if (!(RU->apply)(BOARD(u), &r, &m, &undo)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(RU->apply)(BOARD(u), &r, &m, &undo)) XSRETURN_UNDEF; 0 0 0
537 0 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 0 (RU->forfeit)(BOARD(u), &undo);
548 0 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 0 ▶ if (!SvOK(token) || SvROK(token))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(token) || SvROK(token)) 0 0 0
if (!SvOK(token) || SvROK(token)) 0 0 0
562 0 croak("Game::RoyalUr::Engine: unapply takes what apply or forfeit returned");
563 0 bytes = SvPVbyte(token, len);
564 0 ▶ if (len != sizeof(undo))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(undo)) 0 0 0
565 0 croak("Game::RoyalUr::Engine: unapply takes what apply or forfeit returned");
566 0 Copy(bytes, &undo, 1, struct ru_undo);
567 0 (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 0 ru_rules_of_sv(aTHX_ rules, &r);
577 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
589 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
601 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
614 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
631 0 ru_weights_of_sv(aTHX_ weights, &w);
632 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
645 0 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 0 ru_rules_of_sv(aTHX_ rules, &r);
668 0 ru_weights_of_sv(aTHX_ weights, &w);
669 0 ▶ if (budget < 0 || budget > 2000000000)
Branch: 2 branches
BranchTrueFalseTotal
if (budget < 0 || budget > 2000000000) 0 0 0
if (budget < 0 || budget > 2000000000) 0 0 0
670 0 croak("Game::RoyalUr::Engine: a budget is a number of nodes from 0 to 2000000000");
671 0 chosen = (RU->search)(BOARD(u), &r, roll, &w, (unsigned long long) budget, max_depth, &info);
672 0 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 0 0
673 0 PUSHs(sv_2mortal(newSViv(chosen)));
674 0 PUSHs(sv_2mortal(ru_dec64(aTHX_ info.nodes)));
675 0 PUSHs(sv_2mortal(newSViv(info.depth)));
676 0 PUSHs(sv_2mortal(newSViv(info.value)));
677 0 PUSHs(sv_2mortal(newSViv(info.stopped)));
« blib/lib/Game/RoyalUr/Terminal.pm ↑ Summary ru_engine.c »