supercasiobros/src/level.c

401 lines
8.1 KiB
C

#include "level.h"
#include "world.h"
#include "mario.h"
#include "score.h"
#include "ennemi.h"
#include "keyboard.h"
#include <plateforme.h>
#include <tuyau.h>
#include <gint/display.h>
#include <gint/std/stdlib.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/std/string.h>
#include <gint/std/stdio.h>
#include <liblog.h>
#include <base.h>
#define PACKED_EMPTY 0
#define PACKED_STONE 1
#define PACKED_COIN 2
#define PACKED_BRICK 3
#define PACKED_BRICK_COIN 4
#define PACKED_BOX_COIN 5
#define PACKED_BOX_CHAMPI 6
#define PACKED_BETON 7
#define PACKED_TUYAU_BOUT 8
#define PACKED_TUYAU_MIDDLE 9
#define PACKED_FLAG 10
#define PACKED_GOOMBA 11
#define PACKED_KOOPA_VERT 12
#define PACKED_KOOPA_ROUGE 13
#define PACKED_MARIO_START 14
#define PACKED_NUAGE 15
#define PACKED_ARBRE_TRONC 16
#define PACKED_ARBRE_FEUILLES 17
#define PACKED_PLANTE 18
void mallocError()
{
extern image_t img_ram;
timer_stop(0);
dimage(0,0,&img_ram);
dupdate();
while (1) MKB_getkey();
}
static uint8_t packAccess(packed_level_t const * const p, unsigned int x, unsigned int y)
{
if (x<p->width && y<p->height) return p->data[x*p->height+y];
else return PACKED_EMPTY;
}
static void cellSet(cell_t *const array, unsigned int w, unsigned int h, unsigned int x, unsigned int y, cell_t const cell)
{
if (x<w && y<h) array[x*h+y] = cell;
}
static void unpackLevel(packed_level_t * p)
{
unsigned int w = p->width;
unsigned int h = p->height;
ll_sendp(LEVEL_INFO, "\nUnpacking level %dx%d", w,h);
cell_t * c = (cell_t *) mallocProf(sizeof(cell_t) * w * h);
ll_sendp(LEVEL_INFO, "\n[i]malloc %d", sizeof(cell_t) * w * h);
if (c==0)
{ // overriding parameters
w = 232;//p->width;
h = 13;//p->height;
ll_sendp(LEVEL_CRITICAL, "\n[e]malloc %d failed !\n[i]Trying defaults:\n %dx%d",sizeof(cell_t) * w * h, w,h);
ll_sendp(LEVEL_WARNING, "\n[w]writing into pack new coordinates:\n %dx%d", w,h);
p->width=w;
p->height=h;
cell_t * c = (cell_t *) mallocProf(sizeof(cell_t) * w * h);
if (c==0)
mallocError();
}
//ll_setLevel(LEVEL_WARNING);
int sx=0, sy=p->height; // Mario start coords
ennemi_t ennemis[30]={0};
int nombre_ennemis=0;
for (unsigned int x=0; x<w; x++)
{
for (unsigned int y=0; y<h; y++)
{
unsigned int contents = packAccess(p,x,y);
cell_t cell = {0,0};
if (contents==PACKED_EMPTY)
{
cell_t t={0,0};
cell = t;
}
else if (contents==PACKED_STONE)
{
int props=0;
{ // determiner x
int px=1;
if (packAccess(p,x-1,y)!=PACKED_STONE)
px=0;
if (packAccess(p,x+1,y)!=PACKED_STONE)
px=2;
props+=16*px;
}
{ // determiner y
int py=1;
if (packAccess(p,x,y+1)!=PACKED_STONE)
py=0;
props+=py;
}
cell_t t={EARTH,props};
cell = t;
}
else if (contents==PACKED_COIN)
{
cell_t t={COIN,0};
cell = t;
}
else if (contents==PACKED_BRICK)
{
cell_t t={BRICK,0};
cell = t;
}
else if (contents==PACKED_BRICK_COIN)
{
cell_t t={BRICK,0x15};
cell = t;
}
else if (contents==PACKED_BOX_COIN)
{
cell_t t={GIFT,0x11};
cell = t;
}
else if (contents==PACKED_BOX_CHAMPI)
{
cell_t t={GIFT,0x21};
cell = t;
}
else if (contents==PACKED_BETON)
{
cell_t t={BLOC,0};
cell = t;
}
else if (contents==PACKED_TUYAU_BOUT)
{
int px=0, py=0;
int props=0;
{ // haut du tuyau horizontal
if (packAccess(p,x-1,y)==PACKED_TUYAU_BOUT)
{
px=1;
if (packAccess(p,x,y-1)==PACKED_TUYAU_MIDDLE)
py=2;
if (packAccess(p,x,y+1)==PACKED_TUYAU_MIDDLE)
py=4;
}
if (packAccess(p,x+1,y)==PACKED_TUYAU_BOUT)
{
px=0;
if (packAccess(p,x,y-1)==PACKED_TUYAU_MIDDLE)
py=2;
if (packAccess(p,x,y+1)==PACKED_TUYAU_MIDDLE)
py=4;
}
}
{ // bout de tuyau vertical
if (packAccess(p,x,y-1)==PACKED_TUYAU_BOUT)
{
py=0;
if (packAccess(p,x+1,y)==PACKED_TUYAU_MIDDLE)
px=0;
if (packAccess(p,x-1,y)==PACKED_TUYAU_MIDDLE)
px=2;
}
if (packAccess(p,x,y+1)==PACKED_TUYAU_BOUT)
{
py=1;
if (packAccess(p,x+1,y)==PACKED_TUYAU_MIDDLE)
px=0;
if (packAccess(p,x-1,y)==PACKED_TUYAU_MIDDLE)
px=2;
}
}
props=16*px+py;
cell_t t={TUYAU,props};
cell = t;
}
else if (contents==PACKED_TUYAU_MIDDLE)
{
int px=0, py=0;
int props=0;
if (/*packAccess(p,x-1,y)==PACKED_TUYAU_MIDDLE && */packAccess(p,x+1,y)!=PACKED_TUYAU_MIDDLE)
{
if (packAccess(p,x+1,y)==PACKED_TUYAU_BOUT)
{
px=1;
if (packAccess(p,x,y+1)!=PACKED_TUYAU_MIDDLE)
py=0;
else
py=1;
}
else
{
px=1;
py=3;
}
}
if (/*packAccess(p,x+1,y)==PACKED_TUYAU_MIDDLE && */packAccess(p,x-1,y)!=PACKED_TUYAU_MIDDLE)
{
if (packAccess(p,x-1,y)==PACKED_TUYAU_BOUT)
{
px=1;
if (packAccess(p,x,y+1)!=PACKED_TUYAU_MIDDLE)
py=0;
else
py=1;
}
else
{
px=0;
py=3;
}
}
props=16*px+py;
cell_t t={TUYAU,props};
cell = t;
}
else if (contents==PACKED_FLAG)
{
int props=0;
if (packAccess(p,x,y+1)!=PACKED_FLAG)
props=0x1400;
else if (packAccess(p,x,y+2)!=PACKED_FLAG)
props=0x1401;
else if (packAccess(p,x,y+3)!=PACKED_FLAG)
props=0x1302;
else if (packAccess(p,x,y+4)!=PACKED_FLAG)
props=0x1302;
else if (packAccess(p,x,y+5)!=PACKED_FLAG)
props=0x1202;
else if (packAccess(p,x,y+6)!=PACKED_FLAG)
props=0x1202;
else
props=0x1102;
cell_t t={END_LEVEL,props};
cell = t;
}
else if (contents==PACKED_NUAGE)
{
int px=1, py=0;
if (packAccess(p,x,y+1)==PACKED_NUAGE)
py=1;
if (packAccess(p,x-1,y)!=PACKED_NUAGE)
px=0;
if (packAccess(p,x+1,y)!=PACKED_NUAGE)
px=2;
cell_t t={NUAGE,16*px+py};
cell = t;
}
else if (contents==PACKED_ARBRE_TRONC)
{
cell_t t={ARBRE,0x11};
cell = t;
}
else if (contents==PACKED_ARBRE_FEUILLES)
{
int px=0;
if (packAccess(p,x+1,y)==PACKED_ARBRE_FEUILLES)
{
if (packAccess(p,x-1,y)==PACKED_ARBRE_FEUILLES)
px=1;
else
px=0;
}
else
px=2;
cell_t t={ARBRE,16*px};
cell = t;
}
cellSet(c,w,h,x,y,cell);
if (contents==PACKED_GOOMBA)
{
ennemi_t e=GOOMBA(8*x,8*y,-1);
ennemis[nombre_ennemis]=e;
nombre_ennemis++;
}
else if (contents==PACKED_KOOPA_VERT)
{
ennemi_t e=KOOPA_V(8*x,8*y,-1);
ennemis[nombre_ennemis]=e;
nombre_ennemis++;
}
else if (contents==PACKED_KOOPA_ROUGE)
{
ennemi_t e=KOOPA_R(8*x,8*y,-1);
ennemis[nombre_ennemis]=e;
nombre_ennemis++;
}
else if (contents==PACKED_PLANTE)
{
ennemi_t e=PLANTE(8*x+4,8*y/*-8*/);
ennemis[nombre_ennemis]=e;
nombre_ennemis++;
}
if (contents==PACKED_MARIO_START)
{
sx=8*x;
sy=8*y;
}
}
}
ll_sendp(LEVEL_INFO, "\n[i]Converted!\n[i]Sending to level zone...", w,h);
worldSet(w, h, sx, sy, c);
ll_sendp(LEVEL_INFO, "\n[i]Achieved unpacking", w,h);
// Don't free c, it will be automatically managed by the worldSet function
//freeProf(c);
ennemiesInit(ennemis, nombre_ennemis);
}
void setLevel(int w, int l)
{
levelNew();
teleportersSet(0,0);
worldSet(0,0,0,0,0);
ennemiesInit(0, 0);
platformsInit(0, 0);
if (w+1==1 && l+1==1) // 1-2
{
extern packed_level_t bin_lvl_1_1;
unpackLevel(&bin_lvl_1_1);
teleport_t t[]=
{
{57,6, 215,12, MK_DOWN},
{227,2, 179,3, MK_RIGHT}
};
teleportersSet(t, sizeof(t)/sizeof(teleport_t));
}
if (w+1==1 && l+1==2) // 1-2
{
extern packed_level_t bin_lvl_1_2;
unpackLevel(&bin_lvl_1_2);
teleport_t t[]=
{
{6,4, 21,12, MK_DOWN},
{106,5, 188,12, MK_DOWN},
{197,2, 118,4, MK_RIGHT},
{180,5, 206,2, MK_RIGHT},
};
teleportersSet(t, sizeof(t)/sizeof(teleport_t));
plateform_t plateforme0[]=
{
PLATEFORME_MOVING_H(8*144,6*8+5,32,1,8*142,8*150)
};
platformsInit(plateforme0,sizeof(plateforme0)/sizeof(plateform_t));
}
if (w+1==1 && l+1==3) // 1-2
{
extern packed_level_t bin_lvl_1_3;
unpackLevel(&bin_lvl_1_3);
}
}
void getLevelID(int w, int l, char * str)
{
str[3]='\0';
sprintf(str, "%d-%d", w+1, l+1);
}