789 lines
41 KiB
C++
789 lines
41 KiB
C++
AcctRegSetDftEntry("TempleOS/TimeOut","I64 best_score=0;\n");
|
|
AcctRegExeBranch("TempleOS/TimeOut");
|
|
|
|
#define MAP_HEIGHT 4096
|
|
#define SHOT_LEN 10
|
|
#define SHOT_SPEED 5
|
|
|
|
#define MAX_SHOTS 128
|
|
class Shot
|
|
{
|
|
I64 x,y,dx,dy,dx2,dy2;
|
|
Bool dead,pad[7];
|
|
} s[MAX_SHOTS];
|
|
I64 shots_fired;
|
|
|
|
|
|
I64 x,y,dx,dy,scroll_y,finish_line;
|
|
F64 é;
|
|
F64 t0,tf,snd_shot_timeout,snd_death_timeout;
|
|
|
|
#define PHASES_GROUPS 10
|
|
#define MAX_ENEMY (PHASES_GROUPS*100)
|
|
#define MAX_FRIENDLY (PHASES_GROUPS*20)
|
|
#define MAX_MAN (MAX_FRIENDLY+MAX_ENEMY)
|
|
class Man
|
|
{
|
|
I64 x,y;
|
|
F64 corpse_timeout;
|
|
Bool friendly,dead,pad[6];
|
|
} m[MAX_MAN];
|
|
|
|
I64 total_score,friendly_fire,enemy_by_friendly,friendly_left,enemy_left,
|
|
main_loop_pass;
|
|
Bool game_over;
|
|
$BG,14$
|
|
|
|
|
|
$SP,"<1>",1$
|
|
|
|
|
|
|
|
|
|
|
|
$SP,"<2>",2$
|
|
|
|
|
|
|
|
$SP,"<3>",3$
|
|
|
|
|
|
|
|
$SP,"<4>",4$
|
|
|
|
|
|
$SP,"<5>",5$
|
|
|
|
|
|
#define LANDSCAPE_TYPES 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$SP,"<6>",6$
|
|
|
|
|
|
|
|
|
|
$SP,"<7>",7$
|
|
|
|
|
|
|
|
|
|
|
|
$SP,"<8>",8$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$SP,"<9>",9$
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$BG$
|
|
|
|
CSprite *l_imgs[LANDSCAPE_TYPES]=
|
|
{$IB,"<tree>",6$,$IB,"<rock>",7$,$IB,"<mountain>",8$,$IB,"<castle>",9$};
|
|
|
|
#define MAX_LANDSCAPE 64
|
|
class LandScapeItem
|
|
{
|
|
I64 x,y;
|
|
U8 *img;
|
|
} ls[MAX_LANDSCAPE];
|
|
|
|
|
|
U0 DrawIt(CTask *task,CDC *dc)
|
|
{
|
|
I64 i,yy;
|
|
F64 t1;
|
|
Man *tempm;
|
|
Shot *temps;
|
|
|
|
for (i=0;i<MAX_LANDSCAPE;i++) {
|
|
yy=(ls[i].y-scroll_y)&(MAP_HEIGHT-1);
|
|
if (-32<=yy<=Fs->win_pixel_bottom+32)
|
|
SpritePlot3(dc,ls[i].x,yy,0,ls[i].img);
|
|
}
|
|
|
|
dc->pen_width=3;
|
|
dc->color=BROWN;
|
|
GrLine3(dc,0, (finish_line-scroll_y)&(MAP_HEIGHT-1),0,
|
|
GR_WIDTH,(finish_line-scroll_y)&(MAP_HEIGHT-1),0);
|
|
|
|
dc->color=LTRED;
|
|
for (i=0,temps=s;i<MAX_SHOTS;i++,temps++)
|
|
if (!temps->dead)
|
|
GrLine(dc,temps->x>>32,temps->y>>32,
|
|
(temps->x+temps->dx*SHOT_LEN)>>32,
|
|
(temps->y+temps->dy*SHOT_LEN)>>32);
|
|
for (i=0,tempm=m;i<MAX_MAN;i++,tempm++) {
|
|
yy=(tempm->y-scroll_y)&(MAP_HEIGHT-1);
|
|
if (-32<=yy<=Fs->win_pixel_bottom+32) {
|
|
if (tempm->dead &&
|
|
tT<tempm->corpse_timeout)
|
|
if (!(RandU16&15)) {
|
|
if (tempm->friendly)
|
|
SpritePlot3(dc,tempm->x,yy,0,$IB,"<dead_friendly>",4$);
|
|
else
|
|
SpritePlot3(dc,tempm->x,yy,0,$IB,"<dead_enemy>",5$);
|
|
}
|
|
}
|
|
}
|
|
for (i=0,tempm=m;i<MAX_MAN;i++,tempm++) {
|
|
yy=(tempm->y-scroll_y)&(MAP_HEIGHT-1);
|
|
if (-32<=yy<=Fs->win_pixel_bottom+32) {
|
|
if (!tempm->dead) {
|
|
if (tempm->friendly)
|
|
SpritePlot3(dc,tempm->x,yy,0,$IB,"<friendly>",2$);
|
|
else
|
|
SpritePlot3(dc,tempm->x,yy,0,$IB,"<enemy>",3$);
|
|
}
|
|
}
|
|
}
|
|
SpritePlotRotZ3B(dc,x,y,0,$IB,"<ship>",1$,é);
|
|
|
|
if (tf) {
|
|
t1=tf;
|
|
dc->color=RED;
|
|
if (game_over && Blink)
|
|
GrPrint(dc,task->win_pixel_width/2-9*FONT_WIDTH/2,
|
|
task->win_pixel_height/2,"Game Over");
|
|
} else {
|
|
t1=tT;
|
|
if (!enemy_left || !friendly_left)
|
|
game_over=TRUE;
|
|
}
|
|
dc->color=BROWN;
|
|
GrPrint(dc,0,0,"Enemy:%d Friends:%d Friendly Fire:%d Time:%6.2f Shots:%d",
|
|
enemy_left,friendly_left,friendly_fire,t1-t0,shots_fired);
|
|
GrPrint(dc,0,8,"Total Score:%,d High Score:%,d",
|
|
total_score,best_score);
|
|
}
|
|
|
|
U0 CheckCollisions()
|
|
{
|
|
I64 i,x,y;
|
|
Man *tempm;
|
|
Shot *temps;
|
|
CDC *dc2 =DCNew(DCT_COLOR8,GR_WIDTH,GR_HEIGHT);
|
|
dc2->color=LTRED;
|
|
for (i=0,temps=s;i<MAX_SHOTS;i++,temps++)
|
|
if (!temps->dead)
|
|
GrLine(dc2,temps->x>>32,temps->y>>32,
|
|
(temps->x+temps->dx*SHOT_LEN)>>32,
|
|
(temps->y+temps->dy*SHOT_LEN)>>32);
|
|
|
|
dc2->color =ROP_COLLISION;
|
|
dc2->bkcolor=BLACK;
|
|
for (i=0,tempm=m;i<MAX_MAN;i++,tempm++) {
|
|
if (!tempm->dead) {
|
|
x=tempm->x;
|
|
y=(tempm->y-scroll_y)&(MAP_HEIGHT-1);
|
|
if (0<=x<GR_WIDTH && 0<=y<GR_HEIGHT) {
|
|
dc2->collision_cnt=0;
|
|
if (tempm->friendly)
|
|
SpritePlot3(dc2,x,y,0,$IB,"<friend>",2$);
|
|
else
|
|
SpritePlot3(dc2,x,y,0,$IB,"<enemy>",3$);
|
|
if (dc2->collision_cnt) {
|
|
tempm->dead=TRUE;
|
|
tempm->corpse_timeout=tT+30.0;
|
|
if (tempm->friendly) {
|
|
friendly_fire++;
|
|
friendly_left--;
|
|
Snd(200);
|
|
} else {
|
|
enemy_left--;
|
|
Snd(50);
|
|
}
|
|
snd_death_timeout=tT+0.01;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
DCDel(dc2);
|
|
}
|
|
|
|
U0 Init()
|
|
{
|
|
I64 i,xx,yy;
|
|
Man *tempm;
|
|
|
|
snd_shot_timeout=snd_death_timeout=0;
|
|
Snd(0);
|
|
|
|
total_score=0;
|
|
game_over=FALSE;
|
|
main_loop_pass=0;
|
|
|
|
scroll_y=MAP_HEIGHT-100;
|
|
x=Fs->win_pixel_width>>1;
|
|
y=0.9*Fs->win_pixel_height;
|
|
finish_line=scroll_y+y;
|
|
dx=0;
|
|
dy=0;
|
|
é=0;
|
|
|
|
for (i=0;i<MAX_LANDSCAPE;i++) {
|
|
ls[i].x=(Fs->win_pixel_width-100)*RandU32/MAX_U32+50;
|
|
ls[i].y=(MAP_HEIGHT-100)*RandU32/MAX_U32+50;
|
|
ls[i].img=l_imgs[RandU16%(LANDSCAPE_TYPES-2)];
|
|
}
|
|
ls[0].img=l_imgs[LANDSCAPE_TYPES-2]; //mountain
|
|
ls[1].img=l_imgs[LANDSCAPE_TYPES-2]; //mountain
|
|
ls[2].img=l_imgs[LANDSCAPE_TYPES-1]; //castle
|
|
|
|
for (i=0,tempm=m;i<MAX_MAN;i++,tempm++) {
|
|
if (i<MAX_FRIENDLY) {
|
|
tempm->friendly=TRUE;
|
|
if (!(i&7)) {
|
|
xx=(Fs->win_pixel_width-200)*RandU32/MAX_U32;
|
|
yy=(MAP_HEIGHT-200)*RandU32/MAX_U32;
|
|
}
|
|
} else {
|
|
tempm->friendly=FALSE;
|
|
if (!(i&31)) {
|
|
xx=(Fs->win_pixel_width-200)*RandU32/MAX_U32;
|
|
yy=(MAP_HEIGHT-200)*RandU32/MAX_U32;
|
|
}
|
|
}
|
|
tempm->dead=FALSE;
|
|
tempm->x=xx+64*RandI32/MAX_I32+100;
|
|
tempm->y=yy+64*RandU32/MAX_I32-64+100;
|
|
tempm->corpse_timeout=0;
|
|
}
|
|
for (i=0;i<MAX_SHOTS;i++)
|
|
s[i].dead=TRUE;
|
|
friendly_left=MAX_FRIENDLY;
|
|
enemy_left =MAX_ENEMY;
|
|
enemy_by_friendly=0;
|
|
shots_fired=0;
|
|
friendly_fire=0;
|
|
t0=tT;
|
|
tf=0;
|
|
}
|
|
|
|
U0 FireShot()
|
|
{
|
|
I64 i,j;
|
|
F64 a;
|
|
Shot *temps;
|
|
for (i=0;i<MAX_SHOTS-1;i++)
|
|
if (s[i].dead)
|
|
break;
|
|
temps=&s[i];
|
|
|
|
j=x+24.0*Sin(é);
|
|
temps->x=j<<32;
|
|
|
|
j=y-24.0*Cos(é);
|
|
temps->y=j<<32;
|
|
|
|
temps->dx2=(SHOT_SPEED*Sin(é)+dx)*0x100000000;
|
|
temps->dy2=-SHOT_SPEED*Cos(é)*0x100000000;
|
|
a=Arg(temps->dx2,temps->dy2);
|
|
temps->dx=Cos(a)*0x100000000;
|
|
temps->dy=Sin(a)*0x100000000;
|
|
shots_fired++;
|
|
temps->dead=FALSE;
|
|
if (!snd_death_timeout) {
|
|
Snd(1000);
|
|
snd_shot_timeout=tT+0.0005;
|
|
}
|
|
}
|
|
|
|
U0 MenMove(I64 phase_group)
|
|
{
|
|
I64 i,j,d,closest,closest_d;
|
|
for (i=phase_group;i<MAX_FRIENDLY;i+=PHASES_GROUPS) {
|
|
if (!m[i].dead) {
|
|
closest=MAX_FRIENDLY;
|
|
closest_d=MAX_I64;
|
|
for (j=MAX_FRIENDLY;j<MAX_MAN;j++) {
|
|
if (!m[j].dead) {
|
|
d=AbsI64(m[i].x-m[j].x)+AbsI64(m[i].y-m[j].y);
|
|
if (d<closest_d) {
|
|
closest_d=d;
|
|
closest=j;
|
|
}
|
|
}
|
|
}
|
|
if (closest_d!=MAX_I64) {
|
|
m[i].x+=4*SignI64(m[closest].x-m[i].x);
|
|
m[i].y+=4*SignI64(m[closest].y-m[i].y);
|
|
}
|
|
}
|
|
}
|
|
for (i=MAX_FRIENDLY+phase_group;i<MAX_MAN;i+=PHASES_GROUPS) {
|
|
if (!m[i].dead) {
|
|
closest=0;
|
|
closest_d=MAX_I64;
|
|
for (j=0;j<MAX_FRIENDLY;j++) {
|
|
if (!m[j].dead) {
|
|
d=AbsI64(m[i].x-m[j].x)+AbsI64(m[i].y-m[j].y);
|
|
if (d<closest_d) {
|
|
closest_d=d;
|
|
closest=j;
|
|
}
|
|
}
|
|
}
|
|
if (closest_d!=MAX_I64) {
|
|
m[i].x+=4*SignI64(m[closest].x-m[i].x);
|
|
m[i].y+=4*SignI64(m[closest].y-m[i].y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
U0 MenFight(I64 phase_group)
|
|
{
|
|
I64 i,j,d,closest,closest_d;
|
|
for (i=phase_group;i<MAX_FRIENDLY;i+=PHASES_GROUPS) {
|
|
if (!m[i].dead) {
|
|
closest=MAX_FRIENDLY;
|
|
closest_d=MAX_I64;
|
|
for (j=MAX_FRIENDLY;j<MAX_MAN;j++) {
|
|
if (!m[j].dead && m[i].y-m[j].y<8) {
|
|
d=Sqr(m[i].x-m[j].x)+Sqr(m[i].y-m[j].y);
|
|
if (d<closest_d) {
|
|
closest_d=d;
|
|
closest=j;
|
|
}
|
|
}
|
|
}
|
|
if (closest_d<8*8)
|
|
if (!(RandU16&1)) {
|
|
enemy_left--;
|
|
enemy_by_friendly++;
|
|
m[closest].dead=TRUE;
|
|
m[closest].corpse_timeout=tT+30.0;
|
|
}
|
|
}
|
|
}
|
|
for (i=MAX_FRIENDLY+phase_group;i<MAX_MAN;i+=PHASES_GROUPS) {
|
|
if (!m[i].dead) {
|
|
closest=0;
|
|
closest_d=MAX_I64;
|
|
for (j=0;j<MAX_FRIENDLY;j++) {
|
|
if (!m[j].dead&& m[i].y-m[j].y<8) {
|
|
d=Sqr(m[i].x-m[j].x)+Sqr(m[i].y-m[j].y);
|
|
if (d<closest_d) {
|
|
closest_d=d;
|
|
closest=j;
|
|
}
|
|
}
|
|
}
|
|
if (closest_d<8*8)
|
|
if (!(RandU16&1)) {
|
|
friendly_left--;
|
|
m[closest].dead=TRUE;
|
|
m[closest].corpse_timeout=tT+30.0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
U0 TimeOut()
|
|
{
|
|
I64 i,msg_code,p1,p2;
|
|
Bool gun_on;
|
|
|
|
I64 next_update_jiffy;
|
|
SettingsPush; //See $LK,"SettingsPush","MN:SettingsPush"$
|
|
Preempt(OFF);
|
|
WinBorder;
|
|
WordStat;
|
|
|
|
MenuPush(
|
|
"File {"
|
|
" Abort(,CH_SHIFT_ESC);"
|
|
" Exit(,CH_ESC);"
|
|
"}"
|
|
"Play {"
|
|
" Restart(,'\n');"
|
|
" Fire(,CH_SPACE);"
|
|
" Left(,,SC_CURSOR_LEFT);"
|
|
" Right(,,SC_CURSOR_RIGHT);"
|
|
"}"
|
|
);
|
|
WinMax;
|
|
DocClear;
|
|
"\n\nYou're sent back through time "
|
|
"to help fight some bad guys. ($$FG,LTPURPLE$$Purple$$FG$$)\n\n"
|
|
"Scoring:\n"
|
|
" Enemy Killed\t\t+5\n"
|
|
" Enemy by Friendly\t+10\n"
|
|
" Friendly Fire\t\t-200\n"
|
|
" Shots Fired\t\t-1\n"
|
|
" Game Time\t\t-10 per second\n"
|
|
" Surviving Friendlies\t+500\n"
|
|
"\n";
|
|
|
|
PressAKey;
|
|
Fs->text_attr=YELLOW<<4+CYAN;
|
|
Init;
|
|
DocCursor;
|
|
DocClear;
|
|
Fs->draw_it=&DrawIt;
|
|
|
|
gun_on=FALSE;
|
|
|
|
try {
|
|
while (TRUE) {
|
|
next_update_jiffy=sys_jiffies+JIFFY_FREQ/100;
|
|
while (msg_code=ScanMsg(&p1,&p2,
|
|
1<<MSG_KEY_DOWN+1<<MSG_KEY_UP)) {
|
|
switch (msg_code) {
|
|
case MSG_KEY_DOWN:
|
|
switch (p1) {
|
|
case CH_SHIFT_ESC:
|
|
case CH_ESC:
|
|
goto to_done;
|
|
case '\n':
|
|
Init;
|
|
break;
|
|
case CH_SPACE:
|
|
gun_on=TRUE;
|
|
break;
|
|
}
|
|
break;
|
|
case MSG_KEY_UP:
|
|
if (p1==CH_SPACE)
|
|
gun_on=FALSE;
|
|
else if (p2.u8[0]==SC_CURSOR_RIGHT||p2.u8[0]==SC_CURSOR_LEFT)
|
|
é=0.0;
|
|
break;
|
|
}
|
|
}
|
|
if (snd_death_timeout) {
|
|
if (tT>snd_death_timeout) {
|
|
snd_death_timeout=0;
|
|
Snd(0);
|
|
}
|
|
} else if (snd_shot_timeout && tT>snd_shot_timeout) {
|
|
snd_shot_timeout=0;
|
|
Snd(0);
|
|
}
|
|
|
|
for (i=0;i<MAX_SHOTS;i++) {
|
|
if (!s[i].dead) {
|
|
s[i].x+=s[i].dx2;
|
|
s[i].y+=s[i].dy2;
|
|
if (s[i].y<0||s[i].x<0||
|
|
s[i].x>>32>=Fs->win_pixel_width||
|
|
s[i].y>>32>=Fs->win_pixel_height)
|
|
s[i].dead=TRUE;
|
|
}
|
|
}
|
|
|
|
dx=0;
|
|
if (Bt(key_down_bitmap,SC_CURSOR_LEFT)) {
|
|
é=-15.0*ã/180.0;
|
|
dx=-2;
|
|
} else if (Bt(key_down_bitmap,SC_CURSOR_RIGHT)) {
|
|
é=15.0*ã/180.0;
|
|
dx=2;
|
|
}
|
|
x+=dx;
|
|
while (x>=Fs->win_pixel_width)
|
|
x-=Fs->win_pixel_width;
|
|
while (x<0)
|
|
x+=Fs->win_pixel_width;
|
|
|
|
//It takes too much CPU do do all these all the time.
|
|
nobound_switch (main_loop_pass&7) {
|
|
case 0:
|
|
nobound_switch (main_loop_pass>>3&7) {
|
|
case 0:
|
|
if (--y<20)
|
|
game_over=TRUE;
|
|
case 2:
|
|
case 4:
|
|
case 6:
|
|
MenFight(main_loop_pass>>4%PHASES_GROUPS);
|
|
break;
|
|
case 1:
|
|
case 3:
|
|
case 5:
|
|
case 7:
|
|
MenMove (main_loop_pass>>4%PHASES_GROUPS);
|
|
break;
|
|
}
|
|
case 4:
|
|
break;
|
|
case 2:
|
|
case 6:
|
|
if (gun_on)
|
|
FireShot;
|
|
break;
|
|
case 1:
|
|
case 3:
|
|
case 5:
|
|
case 7:
|
|
CheckCollisions;
|
|
break;
|
|
}
|
|
main_loop_pass++;
|
|
|
|
scroll_y--;
|
|
SleepUntil(next_update_jiffy);
|
|
|
|
total_score=5*(MAX_ENEMY-enemy_left)
|
|
+10*enemy_by_friendly
|
|
-200*friendly_fire
|
|
-10*(tT-t0)
|
|
-shots_fired;
|
|
if (game_over) {
|
|
tf=tT;
|
|
Sleep(500);
|
|
FlushMsgs;
|
|
while (!ScanKey(&p1)&&friendly_left) {
|
|
total_score+=500;
|
|
Snd(2000); Sleep(150);
|
|
Snd(0); Sleep(50);
|
|
friendly_left--;
|
|
}
|
|
total_score+=500*friendly_left;
|
|
if (total_score>best_score)
|
|
best_score=total_score;
|
|
if (!p1) p1=GetChar(,FALSE);
|
|
if (p1==CH_ESC||p1==CH_SHIFT_ESC)
|
|
goto to_done;
|
|
gun_on=FALSE;
|
|
Init;
|
|
}
|
|
}
|
|
to_done:
|
|
GetMsg(,,1<<MSG_KEY_UP);
|
|
}
|
|
catch
|
|
CatchAll;
|
|
SettingsPop;
|
|
MenuPop;
|
|
AcctRegWriteBranch("TempleOS/TimeOut","I64 best_score=%d;\n",best_score);
|
|
}
|
|
|