commit d044484fbd4353efb08ffe14f55e7679d80c562f
parent 726a4c67ac679d97189e220679b282a9f197d1d1
Author: Claudio Alessi <smoppy@gmail.com>
Date: Thu, 21 Jun 2018 22:35:58 +0200
Rewrite cannonball() and few other improvements.
Diffstat:
4 files changed, 33 insertions(+), 42 deletions(-)
diff --git a/TODO b/TODO
@@ -1 +1,2 @@
- move terminal handling stuff into a library
+- keep energy between levels
diff --git a/config.def.h b/config.def.h
@@ -12,8 +12,8 @@ Object objects[] = {
{'(', OF_OPENRIGHT|OF_STICK, finish, {0} },
{'t', 0, cannon, {.v = &objects[11]} },
{'j', 0, cannon, {.v = &objects[12]} },
-[11] = {'.', OF_OPEN, cannonball, {.i = +1} },
-[12] = {',', OF_OPEN, cannonball, {.i = -1} },
+[11] = {'.', OF_OPEN|OF_AI, cannonball, {.i = +1} },
+[12] = {',', OF_OPEN|OF_AI, cannonball, {.i = -1} },
{'#', OF_FALL|OF_PUSHABLE, NULL, {0} },
{'@', OF_FALL|OF_PUSHABLE, NULL, {0} },
{'x', OF_PLAYER|OF_AI|OF_OPEN|OF_FALL, zombie, {.i = 8} },
diff --git a/globox.c b/globox.c
@@ -181,44 +181,39 @@ cannon(Object *o) {
int
cannonball(Object *o) {
- Block *cb, *b, *p, *fb = NULL;
- int nx, nblk, offset;
+ Block *cb, *b, *p = NULL, *rm = NULL;
+ int out;
for(cb = scene->blocks; cb; cb = cb->next) {
if(cb->o->ontick != cannonball || DELAY(cb, DelayCannonBall, 2))
continue;
- if(fb) {
- detach(fb);
- free(fb);
- fb = NULL;
- }
- offset = cb->o->arg.i;
- nx = cb->x + offset;
- nblk = 0;
- p = NULL;
+ cb->x += cb->o->arg.i;
+ out = 1;
for(b = scene->blocks; b; b = b->next) {
- if(b->x == nx && b->y == cb->y) {
- ++nblk;
- if(!ISSET(b->o->flags, offset > 0 ? OF_OPENLEFT : OF_OPENRIGHT))
- fb = cb;
+ if(cb != b && b->x == cb->x && b->y == cb->y) {
+ out = 0;
+ if(!p && ISSET(b->o->flags, OF_PLAYER)) {
+ p = b;
+ continue;
+ }
+ if(!ISSET(b->o->flags, cb->o->arg.i > 0 ? OF_OPENLEFT : OF_OPENRIGHT)) {
+ rm = cb;
+ break;
+ }
}
- if(b->x == cb->x && b->y == cb->y && ISSET(b->o->flags, OF_PLAYER))
- p = b;
- }
- if(!nblk) {
- fb = cb;
- continue;
}
+ if(out)
+ rm = cb;
if(p) {
- fb = cb;
p->energy -= 2;
+ p = NULL;
+ }
+ if(rm) {
+ cb = cb->next;
+ detach(rm);
+ free(rm);
+ rm = NULL;
}
- cb->x = nx;
- }
- if(fb) {
- detach(fb);
- free(fb);
- fb = NULL;
}
return 0;
}
@@ -314,16 +309,11 @@ draw(void) {
for(b = scene->blocks; b; b = b->next) {
if(!b->_draw)
continue;
- t = NULL;
+ t = b;
for(b2 = scene->blocks; b2; b2 = b2->next)
- if(ISSET(b2->o->flags, OF_PLAYER) && b->x == b2->x && b->y == b2->y && (t = b2))
- break;
- if(!t)
- for(b2 = scene->blocks; b2; b2 = b2->next)
- if(!ISSET(b2->o->flags, OF_OPEN) && b->x == b2->x && b->y == b2->y && (t = b2))
- break;
- if(!t)
- t = b;
+ if((ISSET(b2->o->flags, OF_PLAYER | OF_AI) || !ISSET(b2->o->flags, OF_OPEN))
+ && b->x == b2->x && b->y == b2->y)
+ t = b2;
for(b2 = scene->blocks; b2; b2 = b2->next)
if(b2 != t && t->x == b2->x && t->y == b2->y)
b2->_draw = 0;
@@ -586,7 +576,7 @@ level(int num) {
b->x = x;
b->y = y;
attach(b);
- if(ISSET(o->flags, OF_PLAYER | OF_FALL)) {
+ if(ISSET(o->flags, OF_PLAYER | OF_FALL | OF_AI)) {
o = objbysym(VACUUM);
if(o) {
b = ecalloc(1, sizeof(Block));
@@ -651,7 +641,7 @@ resize(int x, int y) {
void
restart(const Arg *arg) {
- if(choose("ny", "Restart the level (y/[n]?") == 'y')
+ if(choose("ny", "Restart the level (y/[n])?") == 'y')
level(lev);
}
diff --git a/levels.h b/levels.h
@@ -52,7 +52,7 @@ char lev5[] = {
"| # |____\n"
"| _ |\n"
"t |\n"
- "| j\n"
+ "| . j\n"
"t _ |\n"
"|_____ j __ \n"
"|______ |__| *|\n"