lib/Term/Maze.xs

filter: Statementstmt 77.3% 99 / 128 Branchbran 63.4% 52 / 82 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.9% 151 / 210 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/aze.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <stdlib.h>
5 #include <time.h>
6  
7 #define WALL '#'
8 #define PATH ' '
9 #define START 'S'
10 #define END 'E'
11 #define PLAYER '@'
12  
13 82 static int hv_get_int(HV *hv, const char *key) {
14 82 SV **svp = hv_fetch(hv, key, strlen(key), 0);
15 82 ▶ return svp ? SvIV(*svp) : 0;
Branch: 1 branch
BranchTrueFalseTotal
return svp ? SvIV(*svp) : 0; 82 0 82
16 }
17  
18 81 static char* av_get_str(AV *av, int index) {
19 81 SV **svp = av_fetch(av, index, 0);
20 162 ▶ return svp ? SvPV_nolen(*svp) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return svp ? SvPV_nolen(*svp) : NULL; 81 0 81
21 }
22  
23 20 static void hv_set_int(HV *hv, const char *key, int val) {
24 20 hv_store(hv, key, strlen(key), newSViv(val), 0);
25 20 }
26  
27 30 static AV* hv_get_av(HV *hv, const char *key) {
28 30 SV **svp = hv_fetch(hv, key, strlen(key), 0);
29 30 ▶ return (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) ? (AV*)SvRV(*svp) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) ? (AV*)SvRV(*svp) : NULL; 30 0 30
return (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) ? (AV*)SvRV(*svp) : NULL; 30 0 30
return (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) ? (AV*)SvRV(*svp) : NULL; 30 0 30
30 }
31  
32 5 static void hv_set_av(HV *hv, const char *key, AV *av) {
33 5 hv_store(hv, key, strlen(key), newRV_noinc((SV*)av), 0);
34 5 }
35  
36 19 static void carve(HV *hv, int x, int y) {
37 19 int width = hv_get_int(hv, "width");
38 19 int height = hv_get_int(hv, "height");
39 19 AV *maze = hv_get_av(hv, "maze");
40 19 int dirs[4][2] = { {0,-1}, {1,0}, {0,1}, {-1,0} };
41 95 ▶ for (int i = 0; i < 4; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < 4; ++i) { 76 19 95
42 76 int j = rand() % 4;
43 76 int tmp0 = dirs[i][0], tmp1 = dirs[i][1];
44 76 dirs[i][0] = dirs[j][0];
45 76 dirs[i][1] = dirs[j][1];
46 76 dirs[j][0] = tmp0;
47 76 dirs[j][1] = tmp1;
48 }
49 19 char *row = av_get_str(maze, y);
50 19 row[x] = PATH;
51 95 ▶ for (int i = 0; i < 4; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < 4; ++i) { 76 19 95
52 76 int dx = dirs[i][0], dy = dirs[i][1];
53 76 int nx = x + 2*dx, ny = y + 2*dy;
54 76 ▶ if (nx > 0 && nx < width-1 && ny > 0 && ny < height-1) {
Branch: 4 branches
BranchTrueFalseTotal
if (nx > 0 && nx < width-1 && ny > 0 && ny < height-1) { 67 9 76
if (nx > 0 && nx < width-1 && ny > 0 && ny < height-1) { 58 9 67
if (nx > 0 && nx < width-1 && ny > 0 && ny < height-1) { 48 10 58
if (nx > 0 && nx < width-1 && ny > 0 && ny < height-1) { 38 10 48
55 38 char *nrow = av_get_str(maze, ny);
56 38 ▶ if (nrow[nx] == WALL) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrow[nx] == WALL) { 14 24 38
57 14 char *midrow = av_get_str(maze, y+dy);
58 14 midrow[x+dx] = PATH;
59 14 carve(hv, nx, ny);
60 }
61 }
62 }
63 19 }
64  
65 MODULE = Term::Maze PACKAGE = Term::Maze PREFIX = maze_
66  
67 SV*
68 maze_new(class, width_in, height_in)
69 char *class
70 int width_in
71 int height_in
72 PREINIT:
73 HV *hv;
74 AV *maze;
75 int width, height, i, j;
76 CODE:
77 5 ▶ width = (width_in % 2 == 0) ? width_in+1 : width_in;
Branch: 1 branch
BranchTrueFalseTotal
width = (width_in % 2 == 0) ? width_in+1 : width_in; 1 4 5
78 5 ▶ height = (height_in % 2 == 0) ? height_in+1 : height_in;
Branch: 1 branch
BranchTrueFalseTotal
height = (height_in % 2 == 0) ? height_in+1 : height_in; 1 4 5
79 5 hv = newHV();
80 5 maze = newAV();
81 28 ▶ for (i = 0; i < height; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < height; ++i) { 23 5 28
82 23 SV *row = newSVpv("", width);
83 23 ▶ SvGROW(row, width+1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(row, width+1); 23 0 23
SvGROW(row, width+1); 0 23 23
84 23 char *s = SvPV_nolen(row);
85 142 ▶ for (j = 0; j < width; ++j)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < width; ++j) 119 23 142
86 119 s[j] = WALL;
87 23 s[width] = '\0';
88 23 SvCUR_set(row, width);
89 23 av_push(maze, row);
90 }
91 5 hv_set_int(hv, "width", width);
92 5 hv_set_int(hv, "height", height);
93 5 hv_set_int(hv, "px", 1);
94 5 hv_set_int(hv, "py", 1);
95 5 hv_set_av(hv, "maze", maze);
96 5 carve(hv, 1, 1);
97 5 char *start_row = av_get_str(maze, 1);
98 5 ▶ if (start_row) start_row[1] = START;
Branch: 1 branch
BranchTrueFalseTotal
if (start_row) start_row[1] = START; 5 0 5
99 5 char *end_row = av_get_str(maze, height-2);
100 5 ▶ if (end_row) end_row[width-2] = END;
Branch: 1 branch
BranchTrueFalseTotal
if (end_row) end_row[width-2] = END; 5 0 5
101 5 RETVAL = sv_bless(newRV_noinc((SV*)hv), gv_stashpv(class, GV_ADD));
102 OUTPUT:
103 RETVAL
104  
105 void
106 maze_move_player(self, direction)
107 SV *self
108 char direction
109 PREINIT:
110 HV *hv;
111 0 int dx = 0, dy = 0;
112 int nx, ny, width, height;
113 AV *maze;
114 CODE:
115 0 hv = (HV*)SvRV(self);
116 0 int px = hv_get_int(hv, "px");
117 0 int py = hv_get_int(hv, "py");
118 0 width = hv_get_int(hv, "width");
119 0 height = hv_get_int(hv, "height");
120 0 maze = hv_get_av(hv, "maze");
121 0 switch (direction) {
122 0 case 'w': dy = -1; break;
123 0 case 'a': dx = -1; break;
124 0 case 's': dy = 1; break;
125 0 case 'd': dx = 1; break;
126 0 case 'h': dx = -1; break;
127 0 case 'j': dy = 1; break;
128 0 case 'k': dy = -1; break;
129 0 case 'l': dx = 1; break;
130 0 default: XSRETURN_EMPTY;
131 }
132 0 nx = px + dx;
133 0 ny = py + dy;
134 0 ▶ if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
Branch: 4 branches
BranchTrueFalseTotal
if (nx >= 0 && nx < width && ny >= 0 && ny < height) { 0 0 0
if (nx >= 0 && nx < width && ny >= 0 && ny < height) { 0 0 0
if (nx >= 0 && nx < width && ny >= 0 && ny < height) { 0 0 0
if (nx >= 0 && nx < width && ny >= 0 && ny < height) { 0 0 0
135 0 SV **svp = av_fetch(maze, ny, 0);
136 0 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 0 0 0
137 0 char *row = SvPV_nolen(*svp);
138 0 ▶ if (row[nx] != WALL) {
Branch: 1 branch
BranchTrueFalseTotal
if (row[nx] != WALL) { 0 0 0
139 0 hv_set_int(hv, "px", nx);
140 0 hv_set_int(hv, "py", ny);
141 }
142 }
143 }
144  
145 AV *
146 maze_get_maze_with_player(self)
147 SV *self
148 PREINIT:
149 HV *hv;
150 int i, j, width, height, px, py;
151 AV *maze, *rows;
152 char *row, *src;
153 CODE:
154 5 hv = (HV*)SvRV(self);
155 5 width = hv_get_int(hv, "width");
156 5 height = hv_get_int(hv, "height");
157 5 px = hv_get_int(hv, "px");
158 5 py = hv_get_int(hv, "py");
159 5 maze = hv_get_av(hv, "maze");
160 5 rows = newAV();
161 28 ▶ for (i = 0; i < height; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < height; ++i) { 23 5 28
162 23 SV **svp = av_fetch(maze, i, 0);
163 46 ▶ src = svp ? SvPV_nolen(*svp) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
src = svp ? SvPV_nolen(*svp) : NULL; 23 0 23
164 23 row = (char *)malloc(width * 16 + 1);
165 23 int pos = 0;
166 142 ▶ for (j = 0; j < width; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < width; ++j) { 119 23 142
167 119 ▶ if (i == py && j == px) {
Branch: 2 branches
BranchTrueFalseTotal
if (i == py && j == px) { 25 94 119
if (i == py && j == px) { 5 20 25
168 5 pos += sprintf(row + pos, "\033[34m%c\033[0m", PLAYER);
169 114 ▶ } else if (src && src[j] == WALL) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (src && src[j] == WALL) { 114 0 114
} else if (src && src[j] == WALL) { 86 28 114
170 86 pos += sprintf(row + pos, "\033[31m%c\033[0m", WALL);
171 28 ▶ } else if (src && (src[j] == START || src[j] == END)) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (src && (src[j] == START || src[j] == END)) { 28 0 28
} else if (src && (src[j] == START || src[j] == END)) { 28 0 28
} else if (src && (src[j] == START || src[j] == END)) { 4 24 28
172 4 pos += sprintf(row + pos, "\033[32m%c\033[0m", src[j]);
173 24 ▶ } else if (src) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (src) { 24 0 24
174 24 row[pos++] = src[j];
175 } else {
176 0 row[pos++] = ' ';
177 }
178 }
179 23 row[pos] = '\0';
180 23 av_push(rows, newSVpv(row, 0));
181 23 free(row);
182 }
183 5 RETVAL = rows;
184 OUTPUT:
185 RETVAL
186  
187 int
188 maze_at_exit(self)
189 SV *self
190 PREINIT:
191 HV *hv;
192 int px, py;
193 AV *maze;
194 int width, height;
195 CODE:
196 6 hv = (HV*)SvRV(self);
197 6 px = hv_get_int(hv, "px");
198 6 py = hv_get_int(hv, "py");
199 6 maze = hv_get_av(hv, "maze");
200 6 width = hv_get_int(hv, "width");
201 6 height = hv_get_int(hv, "height");
202 12 ▶ if (maze && py >= 0 && py < height) {
Branch: 3 branches
BranchTrueFalseTotal
if (maze && py >= 0 && py < height) { 6 0 6
if (maze && py >= 0 && py < height) { 6 0 6
if (maze && py >= 0 && py < height) { 6 0 6
203 6 SV **svp = av_fetch(maze, py, 0);
204 6 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 6 0 6
205 6 char *row = SvPV_nolen(*svp);
206 6 RETVAL = (row[px] == END);
207 } else {
208 0 RETVAL = 0;
209 }
210 } else {
211 0 RETVAL = 0;
212 }
213 OUTPUT:
214 RETVAL
« blib/lib/aze.pm ↑ Summary