2008-12-03 05:40:16 +00:00
|
|
|
#include <stdlib.h>
|
2009-01-02 02:18:20 +00:00
|
|
|
#include "../../config.h"
|
|
|
|
#include "../../joystick/joystick.h"
|
|
|
|
#include "../../random/prng.h"
|
2008-12-03 05:40:16 +00:00
|
|
|
#include "invaders2.h"
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
void procCannon(Cannon * cn, pixel * shot)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
static unsigned char mv = 0;
|
2012-11-17 17:24:01 +00:00
|
|
|
if (mv++ >= CANNON_SPEED)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
mv = 0;
|
|
|
|
if (JOYISLEFT)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (cn->pos < (NUM_COLS - 1))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
cn->pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (JOYISRIGHT)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (cn->pos > 0)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
--cn->pos;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (JOYISFIRE)
|
|
|
|
{
|
|
|
|
if (cn->ready)
|
|
|
|
{
|
|
|
|
shot->x = cn->pos;
|
2012-11-17 17:24:01 +00:00
|
|
|
shot->y = NUM_ROWS - 3;
|
2008-12-03 05:40:16 +00:00
|
|
|
cn->ready = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-25 07:47:42 +00:00
|
|
|
static unsigned char areAtBorder(Invaders * iv)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2011-01-30 22:47:44 +00:00
|
|
|
unsigned char y;
|
2008-12-03 05:40:16 +00:00
|
|
|
for (y = SPACESHIP_LINE + 1; y <= GUARD_LINE; ++y)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (getInvaderPixel(iv, NUM_COLS - 1, y) || getInvaderPixel(iv, 0, y))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
void procInvaders(Invaders * iv, pixel *st)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
static unsigned char mv = 0;
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
if (mv++ >= iv->speed)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
mv = 0;
|
|
|
|
if (areAtBorder(iv) && !(iv->isEdged))
|
|
|
|
{
|
|
|
|
iv->pos.y++;
|
|
|
|
iv->direction = -iv->direction;
|
|
|
|
iv->isEdged = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iv->pos.x += iv->direction;
|
|
|
|
iv->isEdged = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char i, y;
|
2012-11-17 17:24:01 +00:00
|
|
|
unsigned char spos = random8() % UNUM_COLS;
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
if (random8() < SHOOTING_RATE)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
for (i = 0; i < MAX_SHOTS; ++i)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (st[i].y >= NUM_ROWS)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
for (y = GUARD_LINE; y > SPACESHIP_LINE; --y)
|
|
|
|
{
|
|
|
|
if (getInvaderPixel(iv, spos, y) != 0)
|
|
|
|
{
|
|
|
|
st[i].x = spos;
|
|
|
|
st[i].y = y + 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //for SHOTS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void procShots(Invaders * iv, Player * pl, Cannon * cn, Spaceship * sc,
|
2012-11-17 17:24:01 +00:00
|
|
|
unsigned char *guards, pixel *st, pixel * shot)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2011-01-30 22:47:44 +00:00
|
|
|
unsigned char i;
|
2008-12-03 05:40:16 +00:00
|
|
|
static unsigned char cmv = 0, imv = 0;
|
|
|
|
|
|
|
|
if (cmv >= CANNON_SHOOTING_SPEED)
|
|
|
|
{
|
|
|
|
cmv = 0;
|
|
|
|
if (!(cn->ready))
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (shot->y > 0)
|
|
|
|
{
|
|
|
|
shot->y--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cn->ready = 1;
|
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imv >= INVADER_SHOOTING_SPEED)
|
|
|
|
{
|
|
|
|
imv = 0;
|
|
|
|
|
2011-01-30 22:47:44 +00:00
|
|
|
for (i = MAX_SHOTS; i--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (st[i].y < NUM_ROWS)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
st[i].y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmv++;
|
|
|
|
imv++;
|
|
|
|
|
|
|
|
/****************************************************************/
|
|
|
|
/* TESTE OB GETROFFEN */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
// USER CANNON
|
|
|
|
unsigned char tmp;
|
|
|
|
if (!(cn->ready))
|
|
|
|
{
|
2011-01-30 22:47:44 +00:00
|
|
|
for (i = MAX_SHOTS; i--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
if (shot->x == st[i].x && shot->y == st[i].y)
|
|
|
|
{
|
|
|
|
st[i].x = 255;
|
|
|
|
st[i].y = 255;
|
|
|
|
cn->ready = 1;
|
2012-11-17 17:24:01 +00:00
|
|
|
goto invader_shots;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//GUARDS
|
|
|
|
if ((tmp = getGuardPixel(guards, shot->x, shot->y)))
|
|
|
|
{
|
|
|
|
--tmp;
|
|
|
|
setGuardPixel(guards, shot->x, shot->y, tmp);
|
|
|
|
cn->ready = 1;
|
|
|
|
goto invader_shots;
|
|
|
|
}
|
|
|
|
|
|
|
|
//INVADER
|
|
|
|
if ((tmp = getInvaderPixel(iv, shot->x, shot->y)))
|
|
|
|
{
|
|
|
|
--tmp;
|
|
|
|
setInvaderPixel(iv, shot->x, shot->y, tmp);
|
|
|
|
|
|
|
|
if (tmp == 0)
|
|
|
|
{
|
|
|
|
iv->speedinc++;
|
|
|
|
if (iv->speedinc == SPEED_INC_RATE)
|
|
|
|
{
|
|
|
|
iv->speedinc = 0;
|
|
|
|
iv->speed -= SPEED_INC_VALUE;
|
|
|
|
}
|
|
|
|
pl->points += POINTS_FOR_KILL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pl->points += POINTS_FOR_HIT;
|
|
|
|
}
|
|
|
|
cn->ready = 1;
|
|
|
|
goto invader_shots;
|
|
|
|
}
|
|
|
|
|
|
|
|
//SPACESHIP
|
|
|
|
if (shot->y == SPACESHIP_LINE)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if ((shot->x <= sc->pos) && (shot->x >= sc->pos - 1))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
sc->pos = NO_SPACESHIP;
|
2008-12-03 05:40:16 +00:00
|
|
|
pl->points += POINTS_FOR_SPACESHIP;
|
|
|
|
cn->ready = 1;
|
|
|
|
goto invader_shots;
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 17:24:01 +00:00
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
invader_shots:
|
|
|
|
for (i = 0; i < MAX_SHOTS; ++i)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
if ((tmp = getGuardPixel(guards, st[i].x, st[i].y)))
|
|
|
|
{
|
|
|
|
--tmp;
|
|
|
|
setGuardPixel(guards, st[i].x, st[i].y, tmp);
|
|
|
|
st[i].x = 255;
|
|
|
|
st[i].y = 255;
|
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
if (st[i].y == (NUM_ROWS - 2))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
if (st[i].x == cn->pos)
|
|
|
|
{
|
|
|
|
|
|
|
|
pl->lives--;
|
|
|
|
st[i].x = 255;
|
|
|
|
st[i].y = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void procSpaceship(Spaceship * sc)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
unsigned char const rnd1 = random8();
|
|
|
|
unsigned char const rnd2 = random8();
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
static unsigned char sct = 0;
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
if (sc->pos == NO_SPACESHIP)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if ((rnd1 == 73) && (rnd2 >= 200))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
sc->pos = NUM_COLS;
|
|
|
|
sct = 0;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (sct++ == SPACESHIP_SPEED)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
|
|
|
sct = 0;
|
2012-11-17 17:24:01 +00:00
|
|
|
if (sc->pos > 0)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
sc->pos--;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
sc->pos = NO_SPACESHIP;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char getStatus(Invaders * iv)
|
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
unsigned char x, y;
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
// did invaders reached earth?
|
|
|
|
for (x = NUM_COLS; x--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
if (getInvaderPixel(iv, x, GUARD_LINE + 1))
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
return 2;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
// any invaders left?
|
|
|
|
for (x = MAX_INVADER_WIDTH; x--;)
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2012-11-17 17:24:01 +00:00
|
|
|
for (y = MAX_INVADER_HEIGHT; y--;)
|
|
|
|
{
|
|
|
|
if (iv->map[x][y])
|
|
|
|
{
|
|
|
|
return 0; // yes
|
|
|
|
}
|
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-17 17:24:01 +00:00
|
|
|
// if we reach here, level was cleared \o/
|
|
|
|
return 1;
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|