lib/Game/Xomb.xs

filter: Statementstmt 100% 110 / 110 Branchbran 77.2% 68 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.9% 178 / 198 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« bin/xomb ↑ Summary src/jsf.c »
1 /* Xomb.xs - line drawing and random number generation utility functions */
2  
3 #define PERL_NO_GET_CONTEXT
4  
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8  
9 #include "ppport.h"
10  
11 #include "jsf.h"
12  
13 /* NOTE these MUST be kept in sync with similar in Xomb.pm */
14 #define MAP_COLS 78
15 #define MAP_ROWS 22
16  
17 MODULE = Game::Xomb PACKAGE = Game::Xomb
18 PROTOTYPES: DISABLE
19  
20 void
21 bypair (callback, ...)
22 SV *callback;
23 PREINIT:
24 int i;
25 SV *x, *y;
26 PPCODE:
27 3 ▶ if (!(items & 1)) croak("uneven number of arguments");
Branch: 1 branch
BranchTrueFalseTotal
if (!(items & 1)) croak("uneven number of arguments"); 1 2 3
28 2 dSP;
29 5 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 3 2 5
30 3 x = ST(i);
31 3 y = ST(i + 1);
32 3 ENTER;
33 3 SAVETMPS;
34 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
35 3 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 3 3
36 3 PUSHs(x);
37 3 PUSHs(y);
38 3 PUTBACK;
39 3 call_sv(callback, G_DISCARD);
40 3 SPAGAIN;
41 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 3 3
42 3 LEAVE;
43 }
44  
45 UV
46 coinflip ()
47 CODE:
48 10000 ▶ RETVAL = ranval() & 1;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ranval() & 1; 9999 1 10000
49 OUTPUT:
50 RETVAL
51  
52 # NOTE this distance differs from the iters count in linecb() as that
53 # function can do [0,0] .. [3,3] in 3 steps while this will calculate
54 # 4.24, lround'd to 4
55 UV
56 distance (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
57 PREINIT:
58 int dx, dy;
59 CODE:
60 46 dx = x1 - x0;
61 46 dy = y1 - y0;
62 46 ▶ RETVAL = lround(sqrt(dx*dx + dy*dy));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = lround(sqrt(dx*dx + dy*dy)); 43 3 46
63 OUTPUT:
64 RETVAL
65  
66 # splice a random element out of an array reference
67 # NOTE this does not preserve the order of the array as that requires a
68 # series of copies anytime an item is extracted from not the end which
69 # for large lists will be most of the time (previous versions of this
70 # code did such a waterfall copy)
71 SV *
72 extract (avref)
73 AV *avref;
74 PREINIT:
75 SSize_t i, len, rnd;
76 SV *dunno, **swap;
77 CODE:
78 187 len = av_len(avref) + 1;
79 187 ▶ if (len == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) XSRETURN_UNDEF; 1 186 187
80 186 rnd = ranval() % len;
81 186 dunno = av_delete(avref, rnd, 0);
82 186 ▶ if (rnd != len - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rnd != len - 1) { 182 4 186
83 182 swap = av_fetch(avref, len - 1, FALSE);
84 182 av_store(avref, rnd, *swap);
85 182 AvFILLp(avref) -= 1;
86 182 AvMAX(avref) -= 1;
87 }
88 186 SvREFCNT_inc(dunno);
89 186 RETVAL = dunno;
90 OUTPUT:
91 RETVAL
92  
93 # init_jsf - setup the RNG (see src/jsf.*)
94 void
95 init_jsf (seed)
96 UV seed
97 PPCODE:
98 106 raninit(seed);
99  
100 UV
101 irand (uint32_t max)
102 CODE:
103 11851 ▶ RETVAL = ranval() % max;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ranval() % max; 11838 13 11851
104 OUTPUT:
105 RETVAL
106  
107 # linecb - Bresenham with some features to keep it from going off of
108 # the map and to skip the first point and abort should the callback
109 # return -1
110 void
111 linecb (callback, int x0, int y0, int x1, int y1)
112 SV *callback;
113 PREINIT:
114 int answer, dx, dy, err, e2, sx, sy, online, iters;
115 PPCODE:
116 40 dSP;
117 40 dx = abs(x1 - x0);
118 40 dy = abs(y1 - y0);
119 40 ▶ sx = x0 < x1 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
sx = x0 < x1 ? 1 : -1; 15 25 40
120 40 ▶ sy = y0 < y1 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
sy = y0 < y1 ? 1 : -1; 22 18 40
121 40 ▶ err = (dx > dy ? dx : -dy) / 2;
Branch: 1 branch
BranchTrueFalseTotal
err = (dx > dy ? dx : -dy) / 2; 11 29 40
122 40 iters = 0;
123 40 online = 0;
124 while (1) {
125 884 ▶ if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break;
Branch: 4 branches
BranchTrueFalseTotal
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 883 1 884
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 883 0 883
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 883 0 883
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 883 0 883
126 883 ▶ if (online) {
Branch: 1 branch
BranchTrueFalseTotal
if (online) { 843 40 883
127 843 ENTER;
128 843 SAVETMPS;
129 843 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 843 843
130 843 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 843 843
131 843 mPUSHs(newSViv(x0));
132 843 mPUSHs(newSViv(y0));
133 843 mPUSHs(newSViv(iters));
134 843 PUTBACK;
135 843 call_sv(callback, G_SCALAR);
136 843 SPAGAIN;
137 843 answer = POPi;
138 843 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 843 0 843
139 843 LEAVE;
140 843 ▶ if (answer == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if (answer == -1) break; 1 842 843
141 }
142 882 ▶ if (x0 == x1 && y0 == y1) break;
Branch: 2 branches
BranchTrueFalseTotal
if (x0 == x1 && y0 == y1) break; 65 817 882
if (x0 == x1 && y0 == y1) break; 38 27 65
143 844 e2 = err;
144 844 ▶ if (e2 > -dx) {
Branch: 1 branch
BranchTrueFalseTotal
if (e2 > -dx) { 802 42 844
145 802 err -= dy;
146 802 x0 += sx;
147 }
148 844 ▶ if (e2 < dy) {
Branch: 1 branch
BranchTrueFalseTotal
if (e2 < dy) { 209 635 844
149 209 err += dx;
150 209 y0 += sy;
151 }
152 844 online = 1;
153 844 iters++;
154 }
155  
156 UV
157 onein (uint32_t N)
158 CODE:
159 466 ▶ RETVAL = 0 == ranval() % N;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = 0 == ranval() % N; 400 66 466
160 OUTPUT:
161 RETVAL
162  
163 UV
164 roll (uint32_t count, uint32_t sides)
165 PREINIT:
166 uint32_t sum;
167 CODE:
168 33 sum = count;
169 105 ▶ while (count--) sum += ranval() % sides;
Branch: 1 branch
BranchTrueFalseTotal
while (count--) sum += ranval() % sides; 72 33 105
170 33 ▶ RETVAL = sum;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = sum; 27 6 33
171 OUTPUT:
172 RETVAL
173  
174 # pick a random element of an array ref
175 SV *
176 pick (avref)
177 AV *avref;
178 PREINIT:
179 SSize_t len, rnd;
180 SV **svp;
181 CODE:
182 6703 len = av_len(avref) + 1;
183 6703 ▶ if (len == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) XSRETURN_UNDEF; 1 6702 6703
184 6702 rnd = ranval() % len;
185 6702 svp = av_fetch(avref, rnd, FALSE);
186 6702 SvREFCNT_inc(*svp);
187 6702 RETVAL = *svp;
188 OUTPUT:
189 RETVAL
190  
191 # walkcb - linecb, but does not stop at x1,y1. used by Trolls to find
192 # what gets busted, which may or may not be the player
193 void
194 walkcb (callback, int x0, int y0, int x1, int y1)
195 SV *callback;
196 PREINIT:
197 int answer, dx, dy, err, e2, sx, sy, online, iters;
198 PPCODE:
199 2 dSP;
200 2 dx = abs(x1 - x0);
201 2 dy = abs(y1 - y0);
202 2 ▶ sx = x0 < x1 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
sx = x0 < x1 ? 1 : -1; 2 0 2
203 2 ▶ sy = y0 < y1 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
sy = y0 < y1 ? 1 : -1; 2 0 2
204 2 ▶ err = (dx > dy ? dx : -dy) / 2;
Branch: 1 branch
BranchTrueFalseTotal
err = (dx > dy ? dx : -dy) / 2; 2 0 2
205 2 iters = 0;
206 2 online = 0;
207 while (1) {
208 8 ▶ if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break;
Branch: 4 branches
BranchTrueFalseTotal
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 8 0 8
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 7 1 8
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 7 0 7
if (x0 < 0 || x0 >= MAP_COLS || y0 < 0 || y0 >= MAP_ROWS) break; 7 0 7
209 7 ▶ if (online) {
Branch: 1 branch
BranchTrueFalseTotal
if (online) { 5 2 7
210 5 ENTER;
211 5 SAVETMPS;
212 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
213 5 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 5 5
214 5 mPUSHs(newSViv(x0));
215 5 mPUSHs(newSViv(y0));
216 5 mPUSHs(newSViv(iters));
217 5 PUTBACK;
218 5 call_sv(callback, G_SCALAR);
219 5 SPAGAIN;
220 5 answer = POPi;
221 5 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 5 0 5
222 5 LEAVE;
223 5 ▶ if (answer == -1) break;
Branch: 1 branch
BranchTrueFalseTotal
if (answer == -1) break; 1 4 5
224 }
225 6 e2 = err;
226 6 ▶ if (e2 > -dx) {
Branch: 1 branch
BranchTrueFalseTotal
if (e2 > -dx) { 6 0 6
227 6 err -= dy;
228 6 x0 += sx;
229 }
230 6 ▶ if (e2 < dy) {
Branch: 1 branch
BranchTrueFalseTotal
if (e2 < dy) { 6 0 6
231 6 err += dx;
232 6 y0 += sy;
233 }
234 6 online = 1;
235 6 iters++;
236 }
« bin/xomb ↑ Summary src/jsf.c »