collision y timer

This commit is contained in:
Gabriel Lorenzo 2002-10-24 21:59:17 +00:00
parent c0ea7a1e42
commit 2936a401a1
13 changed files with 444 additions and 29 deletions

View file

@ -1,3 +1,12 @@
24/10/2002
----------
* Ahora los id's de los procesos son siempre impares. (Er_Makina)
* Arreglado collision. He sustituido el algoritmo de choque entre dos cajas,
aunque no hacía falta, pero quizás así queda un poco más limpio. (Er_Makina)
* Añadido timer[], pero sólo en Win32. Hay que adaptarlo a Linux. (Er_Makina)
* tutor0b ya rula.. el robots asesinos ya parece que rula bien pero no puedo
mover al personaje, no se por qué... fallo en la input? (Er_Makina)
21/10/2002
----------
* delete_text() daba un runtime error si no existia el texto a eliminar. en

318
ediv/bin/ROBOTS.PRG Normal file
View file

@ -0,0 +1,318 @@
/*********************
Los Robots Asesinos
*********************
(c) Er_Makina, 2000
www.sferasoft.org
********************
Este programa es SOFTWARE LIBRE y puedes hacer con él
lo de te salga de las narices. Puedes modificarlo y
redistribuirlo libremente sin necesidad de notificar-
me nada.
********************
NOTAS:
- El programa funcionará bajo DIV 1 si se quita la 1ª
línea del programa (compiler_options)
- Para ejecutar el EXE, es necesario el archivo
DIV32RUN.DLL que podrás encontrar en la web oficial
de DIV (www.divgames.com)
********************/
COMPILER_OPTIONS _extended_conditions;
Program Juego_robots;
CONST
max_robots=25;
GLOBAL
prota;
num_robots=5;
robots[max_robots];
muerto[max_robots];
muertos;
cogido;
ya;
LOCAL
i;
PRIVATE
protax;
protay;
mueve;
harf;
j;
espe;
BEGIN
load_fpg("robots.fpg");
load_fnt("system.fnt");
write(1,0,0,0,"Pulsa F1 para obtener ayuda");
write(1,160,100,4,"¨PREPARADO?");
espe=1;
mouse.flags=4;
loop
// fade_on();
prota=protag(rand(0,39)*8,rand(0,24)*8);
for(i=1;i<=num_robots;i++)
loop
x=rand(0,39);
y=rand(0,24);
graph=2;
if(fget_dist(x,y,prota.x/8,prota.y/8)>2)
x*=8;
y*=8;
if(i==1)
robots[i]=robot(x,y);
muerto[i]=0;
break;
else
harf=0;
for(j=1;j<=i-1;j++)
if(robots[j].x<>x && robots[j].y<>y)
harf++;
end
end
if(harf==i-1)
robots[i]=robot(x,y);
muerto[i]=0;
break;
end
end
end
end
end
graph=0;
protax=prota.x;
protay=prota.y;
if(espe)
timer[2]=0;
while(/*fading ||*/ timer[2]<200)
frame;
end
delete_text(all_text);
espe=0;
end
loop
frame;
if(cogido)
repeat
frame;
until(ya)
break;
end
if((key(_up) || key(_c_up) || key(_c_home) || key(_c_pgup)) && prota.y>0)
while(key(_up) || key(_c_up))
frame;
end
protay-=8;
mueve=1;
end
if((key(_down) || key(_c_down) || key(_c_end) || key(_c_pgdn)) && prota.y<191)
while(key(_down) || key(_c_down))
frame;
end
protay+=8;
mueve=1;
end
if((key(_left) || key(_c_left) || key(_c_home) || key(_c_end)) && prota.x>0)
while(key(_left) || key(_c_left) || key(_c_home) || key(_c_end))
frame;
end
protax-=8;
mueve=1;
end
if((key(_right) || key(_c_right) || key(_c_pgup) || key(_c_pgdn)) && prota.x<311)
while(key(_right) || key(_c_right) || key(_c_pgup) || key(_c_pgdn))
frame;
end
protax+=8;
mueve=1;
end
if(key(_c_center))
while(key(_c_center))
frame;
end
mueve=1;
end
if(key(_t))
while(key(_t))
frame;
end
loop
x=rand(0,39);
y=rand(0,24);
j=0;
for(i=1;i<=num_robots;i++)
if(fget_dist(x,y,robots[i].x/8,robots[i].y/8)>2)
j++;
end
end
if(j==num_robots)
break;
end
end
protax=x*8;
protay=y*8;
mueve=1;
end
if(key(_u))
while(key(_u))
frame;
end
protax=rand(0,39)*8;
protay=rand(0,24)*8;
mueve=1;
end
if(key(_f1))
graph=14;
x=160;
y=100;
z=-20;
while(key(_f1))
frame;
end
repeat
frame;
until(key(_space) || key(_enter))
graph=0;
end
if(mueve==1)
prota.x=protax;
prota.y=protay;
mueve=0;
mueve_robots();
end
end
//signal(prota,s_kill);
//signal(type robot,s_kill);
muertos=0;
cogido=0;
ya=0;
end
END
Process Protag(x,y)
PRIVATE
imagen=0;
imagenes[12]=1,9,10,11,12,13,12,13,12,11,10,9,1;
saludando;
esp_frame;
coge_robot;
BEGIN
graph=imagenes[imagen];
timer[1]=0;
loop
frame;
if(coge_robot=collision(type robot))
cogido=1;
if(coge_robot.graph==3)
write(1,160,100,4,"­­ PERO QU<51> HACES !!");
else
write(1,160,100,4,"­­ TE HAN COGIDO !!");
end
write(1,160,199,7,"Pulsa una tecla");
repeat
frame;
until(key(_space) || key(_enter))
/*fade_off();
while(fading)
frame;
end*/
delete_text(all_text);
ya=1;
frame;
end
if(saludando==0 && timer[1]=>500)
saludando=1;
end
if(saludando==1)
if(imagen<12)
esp_frame++;
if(esp_frame==2)
esp_frame=0;
imagen++;
graph=imagenes[imagen];
end
else
saludando=0;
imagen=0;
timer[1]=0;
end
end
if(muertos==num_robots)
cogido=1;
write(1,160,100,4,"­­ MUY BIEN !!");
write(1,160,199,7,"Pulsa una tecla");
num_robots++;
repeat
frame;
until(key(_space) || key(_enter))
/*fade_off();
while(fading)
frame;
end*/
delete_text(all_text);
ya=1;
if(num_robots>max_robots)
//exit("­Te has pasado el juego!",0);
end
frame;
end
end
END
Process Robot(x,y)
PRIVATE
imagen=0;
imagenes[3]=2,6,7,8;
ya_mue;
esp_frame;
BEGIN
graph=imagenes[imagen];
loop
frame;
if(collision(type robot))
graph=3;
if(!ya_mue)
muertos++;
ya_mue=1;
end
end
if(graph<>3)
esp_frame++;
if(esp_frame==2)
esp_frame=0;
imagen=(imagen+1)mod 3;
graph=imagenes[imagen];
end
end
end
END
Process mueve_robots()
BEGIN
for(i=1;i<=num_robots;i++)
if(robots[i].graph==3)
muerto[i]=1;
end
if(!muerto[i])
if(robots[i].x<prota.x)
robots[i].x+=8;
end
if(robots[i].x>prota.x)
robots[i].x-=8;
end
if(robots[i].y<prota.y)
robots[i].y+=8;
end
if(robots[i].y>prota.y)
robots[i].y-=8;
end
end
end
END

View file

@ -24,6 +24,9 @@ int main( int argc, char** argv )
RegCloseKey(subkey);
Main.personal_folder.setLatin1((char*)datos);
}
else {
Main.personal_folder.setLatin1(".\\");
}
#else
Main.personal_folder.setLatin1(getenv("HOME"));
#endif

Binary file not shown.

View file

@ -23,6 +23,17 @@
* DIV, tal como las opciones de compilación, estructura reserved, etc.
*/
#ifdef _WIN32
# include <time.h>
# include <sys/timeb.h>
unsigned int tiempo;
unsigned int ultimo_tiempo;
#else
# error ¡adapta las rutinas de timer a Linux!
#endif
#include <stdio.h>
#include "export.h"
//#include "../../shared/varindex.h"
@ -152,6 +163,7 @@ int ExportaFuncs(EXPORTAFUNCS_PARAMS)
FUNCTION("define_region",5,eDiv_DefineRegion) ;
ENTRYPOINT( first_load ) ;
ENTRYPOINT(frame);
// POR HACER: funciones signal, system, ignore_error...
@ -242,6 +254,12 @@ void first_load(FUNCTION_PARAMS)
{
int i ;
#ifdef _WIN32
struct timeb tiempob;
ftime(&tiempob);
ultimo_tiempo=tiempob.time*100+tiempob.millitm/10;
#endif
for ( i = 0 ; i < 32 ; i++ )
{
regions[i].x = 0 ;
@ -254,7 +272,22 @@ void first_load(FUNCTION_PARAMS)
fp->regions = regions ;
fp->existe.regions = 1 ;
}
void frame(FUNCTION_PARAMS)
{
int i;
int timer;
#ifdef _WIN32
struct timeb tiempob;
ftime(&tiempob);
tiempo=tiempob.time*100+tiempob.millitm/10;
timer=globalptr("timer");
for(i=0;i<10;i++)
fp->mem[timer+i]+=tiempo-ultimo_tiempo;
ultimo_tiempo=tiempo;
#endif
}

View file

@ -271,6 +271,7 @@ struct _fun_params{
TYPEOF_Critical_Error *Critical_Error ;
TYPEOF_GetVarOffset *GetVarOffset ;
TYPEOF_Stub_Quit *Stub_Quit ;
int imem_max;
} ;

View file

@ -105,6 +105,7 @@
SuppressStartupBanner="TRUE"
IgnoreDefaultLibraryNames="LIBCMTD.lib"
ModuleDefinitionFile=".\graphics.def"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile=".\Debug/graphics.pdb"
ImportLibrary=".\Debug/graphics.lib"/>
<Tool

View file

@ -9,11 +9,17 @@
//#include <zlib.h>
//#include <assert.h>
#include "export.h"
#include "errors.h"
//#include "errors.h" // Falta añadir errors.h al CVS
#include <SDL/SDL.h>
#include "graphics.h"
#include "SDL_rotozoom.h"
#define ERR_CANNOTOPENFPG 105
#define ERR_INVALIDFPGHEADER 106
#define ERR_INVALIDMAPCODE 110
#define Miedzy(x,a,b) (((x) >= (a)) && ((x) <= (b)))
//#include "varindex.h"
@ -213,6 +219,22 @@ int ExportaFuncs(EXPORTAFUNCS_PARAMS)
return TRUE;
}
/*
* int IntersectionRR(int rc1left,int rc1top,int rc1right,int rc1bottom,int rc2left,int rc2top,int rc2right,int rc2bottom)
* Comprueba si hay colisión entre dos regiones rectangulares.
*
* Devuelve:
* 0 - No hay colisión
* 1 - Hay colisión
*/
int IntersectionRR(int rc1left,int rc1top,int rc1right,int rc1bottom,int rc2left,int rc2top,int rc2right,int rc2bottom)
{
return ((Miedzy(rc1left,rc2left,rc2right) || Miedzy(rc1right,rc2left,rc2right) ||
Miedzy(rc2left,rc1left,rc1right) || Miedzy(rc2right,rc1left,rc1right)) &&
(Miedzy(rc1top,rc2top,rc2bottom) || Miedzy(rc1bottom,rc2top,rc2bottom) ||
Miedzy(rc2top,rc1top,rc1bottom) || Miedzy(rc2bottom,rc1top,rc1bottom)));
}
/* A continuación las funciones que queremos exportar. Para una mayor
* sencillez las hemos puesto en este mismo archivo, aunque puede ser
* aconsejable ponerlas en archivos aparte.
@ -226,14 +248,19 @@ int eDIV_COLLISION(FUNCTION_PARAMS)
int a, i ;
SDL_Rect r1 , r2 ;
a = getparm() ;
// Si se le pasa un ID
if ( a < 4000000 )
{
id1 = fp->procs_s[ fp->proc_orden[ *fp->proceso_actual ] ].id ;
g1 = local("graph",id1) ;
f1 = local("file",id1) ;
if ( files[f1].existe == 0 || files[f1].mapa[g1].existe == 0 )
return -1 ;
return 0;
// Si se le pasa un ID
//if ( a < 4000000 )
if(a<fp->imem_max)
{
if(a==id1) return 0;
r1.x = local("x",id1) ;
r1.y = local("y",id1) ;
r1.w = files[f1].mapa[g1].Surface->w ;
@ -242,47 +269,66 @@ int eDIV_COLLISION(FUNCTION_PARAMS)
g2 = local("graph",id2) ;
f2 = local("file",id2);
if ( files[f2].existe == 0 || files[f2].mapa[g2].existe == 0 )
return -1 ;
return 0;
r2.x = local("x",id2) ;
r2.y = local("y",id2) ;
r2.w = files[f2].mapa[g2].Surface->w ;
r2.h = files[f2].mapa[g1].Surface->h ;
//Colision barata :P
if (!( (r2.x > r1.x && r2.x > r1.x + r1.w) || (r2.x+r2.w < r1.x && r2.x+r2.w<r1.x+r1.w )))
if(IntersectionRR(r1.x,r1.y,r1.x+r1.w-1,r1.x+r1.h-1,r2.x,r2.y,r2.x+r2.w-1,r2.y+r2.h-1))
return id2;
/*if (!( (r2.x > r1.x && r2.x > r1.x + r1.w) || (r2.x+r2.w < r1.x && r2.x+r2.w<r1.x+r1.w )))
if (!( (r2.y > r1.y && r2.y > r1.y + r1.h) || (r2.y+r2.h < r1.y && r2.y+r2.h<r1.y+r1.h )))
return 1 ;
}else
{
return 1 ;*/
}
else {
int* type_scan=&reserved("type_scan",id1);
int* id_scan=&reserved("id_scan",id1);
//assert(0);
// Si se le pasa un type
for ( i = 0 ; i < *fp->num_procs ; i++ )
if(*type_scan!=a) {
*id_scan=0;
*type_scan=a;
}
for ( i = *id_scan+1 ; i < *fp->num_procs ; i++ )
{
id1 = fp->procs_s[ fp->proc_orden[ i ] ].id ;
id2 = fp->procs_s[ fp->proc_orden[ i ] ].id;
if(id2==id1) continue;
printf("collision: %d\n",id2);
//Si el proceso se corresponde con el type
if ( reserved("process_type",id1) == a )
if ( reserved("process_type",id2) == a )
{
g1 = local("graph",id1) ;
f1 = local("file",id1) ;
if ( files[f1].existe == 0 || files[f1].mapa[g1].existe == 0 )
return -1 ;
printf("collision: encontrado: id %d\n",id2);
r1.x = local("x",id1) ;
r1.y = local("y",id1) ;
r1.w = files[f1].mapa[g1].Surface->w ;
r1.h = files[f1].mapa[g1].Surface->h ;
id2 = a ;
//id2 = a ;
g2 = local("graph",id2) ;
f2 = local("file",id2) ;
if ( files[f2].existe == 0 || files[f2].mapa[g2].existe == 0 )
return -1 ;
continue;
r2.x = local("x",id2) ;
r2.y = local("y",id2) ;
r2.w = files[f2].mapa[g2].Surface->w ;
r2.h = files[f2].mapa[g1].Surface->h ;
//Colision barata :P
if (!( (r2.x > r1.x && r2.x > r1.x + r1.w) || (r2.x+r2.w < r1.x && r2.x+r2.w<r1.x+r1.w )))
if (!( (r2.y > r1.y && r2.y > r1.y + r1.h) || (r2.y+r2.h < r1.y && r2.y+r2.h<r1.y+r1.h )))
return 1 ;
if(IntersectionRR(r1.x,r1.y,r1.x+r1.w-1,r1.x+r1.h-1,r2.x,r2.y,r2.x+r2.w-1,r2.y+r2.h-1)) {
*id_scan=i;
printf("COLISION: %d con %d\n",id1,id2);
return id2;
}
/*if (!( (r2.x > r1.x && r2.x > r1.x + r1.w) || (r2.x+r2.w < r1.x && r2.x+r2.w<r1.x+r1.w )))
if (!( (r2.y > r1.y && r2.y > r1.y + r1.h) || (r2.y+r2.h < r1.y && r2.y+r2.h<r1.y+r1.h ))) {
*id_scan=i;
return id2;
}*/
}
}
*type_scan=0;
}
return 0 ;

View file

@ -381,6 +381,7 @@ void first_load()
seno[i] = ftomil( sin( (i / 1000)*PIOVER180 ) );
}
seno[90000] = 0x7FFFFFFF ;
srand(time(NULL));
}

View file

@ -240,6 +240,7 @@ struct _fun_params{
TYPEOF_Critical_Error *Critical_Error ;
TYPEOF_GetVarOffset *GetVarOffset ;
TYPEOF_Stub_Quit *Stub_Quit ;
int imem_max;
} fp ;

View file

@ -86,6 +86,7 @@ int ini_interprete()
fp.Critical_Error=critical_error;
fp.GetVarOffset=GetVarOffset;
fp.Stub_Quit=stub_quit;
fp.imem_max=imem_max;
#ifdef DBG
last_lin=0;
@ -328,7 +329,7 @@ int proceso( int num, int padre )
case ltyp://32
if ( procs_s[num_proc].tipo != 0 )
critical_error(3); // redefinición del tipo de proceso
procs_s[num_proc].id = mem[2] + ( num_proc * iloc_len ) ;
procs_s[num_proc].id = (mem[2] + ( num_proc * iloc_len ))|1;
if(procs_s[num_proc].id>imem_max-iloc_len) critical_error(8); // demasiados procesos en ejecución
memcpy(&mem[procs_s[num_proc].id],&mem[iloc],iloc_pub_len<<2);
reserved("process_id",procs_s[num_proc].id)=procs_s[num_proc].id;
@ -548,7 +549,8 @@ int proceso( int num, int padre )
if ( devolver > 0 && no_devuelve == 0 )
{
pila[++sp] = 0 ;// AQUI SE DEBERA DEVOLVER EL ID
//pila[++sp] = 0 ;// AQUI SE DEBERA DEVOLVER EL ID
pila[++sp]=procs_s[num_proc].id;
devolver-- ;
}

View file

@ -322,7 +322,7 @@ int main(int argc, char* argv[])
switch( event[0].type )
{
case SDL_QUIT:
assert(0);
//assert(0);
stub_quit(0) ;
break ;
case SDL_NOEVENT:

Binary file not shown.