#include #include #include "pie/emu.h" #include "bitmap.h" #include "pill.h" #include "level.h" #include "state.h" #include "position.h" #include /** * Return true if a pill is present * on the normalized coordinate (x, y) */ bool is_pill_present(int x, int y) { //printf("looking at (%d, %d)\n", x, y); /* 0x4020 seems to be the start of the level in graphical memory */ short r = machine->readByte(0x4020 + (y-2) + (0x20*(28-x))); if ( r == 0x10 ) return true; else if ( r == 0x40 ) return false; else { //printf("Unknown value : %d\n", r); //assert(!"There's a problem !"); return false; } } /** * Set table bit for a specified node */ static void set_node_bit(graph_t *g, node_t *n, void *data) { position_t *p = (position_t*)n->data; bmap_and_offset_t *bo = (bmap_and_offset_t*)data; //printf("=>> %f, %f\n", p->x, p->y); uint8_t val = is_pill_present(p->x, p->y); jeff_bitmap_set(bo->bmap, bo->offset, val); if ( n->index == 0 ) n->index = bo->offset; bo->offset++; } /** * Set table bit for a specified edge */ static void set_edge_bits(graph_t *g, edge_t *e, void *data) { edge_data_t *ed = (edge_data_t*)e->data; node_t *n1 = (node_t*)e->n1; node_t *n2 = (node_t*)e->n2; position_t *p_n1 = (position_t*)n1->data; position_t *p_n2 = (position_t*)n2->data; bmap_and_offset_t *bo = (bmap_and_offset_t*)data; /* Don't treat tunnel, there are never pellets on their path */ if ( ed->type == CORR_TUNNEL ) return; /* Vertical edge */ if ( p_n1->x != p_n2->x ) { if ( p_n1->x < p_n2->x ) /* N1 left of N2 */ { int offset = p_n1->x + 1; while ( offset < p_n2->x ) { uint8_t res = is_pill_present(offset, p_n1->y); jeff_bitmap_set(bo->bmap, bo->offset, res); bo->offset++; offset++; } } else /* N2 left of N1 */ { int offset = p_n2->x + 1; while ( offset < p_n1->x ) { uint8_t res = is_pill_present(offset, p_n1->y); jeff_bitmap_set(bo->bmap, bo->offset, res); bo->offset++; offset++; } } } else { if ( p_n1->y < p_n2->y ) /* N1 top of N2 */ { int offset = p_n1->y+1; while ( offset < p_n2->y ) { uint8_t res = is_pill_present(p_n1->x, offset); jeff_bitmap_set(bo->bmap, bo->offset, res); bo->offset++; offset++; } } else /* N1 bottom of N2 */ { int offset = p_n2->y+1; while ( offset < p_n1->y ) { uint8_t res = is_pill_present(p_n1->x, offset); jeff_bitmap_set(bo->bmap, bo->offset, res); bo->offset++; offset++; } } } if ( e->index == 0 ) e->index = bo->offset; } /** * Generate pill table from level */ void extract_pill_table(graph_t *g, struct jeff_bmap *bmap) { /* Clear it */ for(int i=0; ireadByte(0x4063) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 1, machine->readByte(0x407C) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 2, machine->readByte(0x4383) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 3, machine->readByte(0x439C) == 0x40 ? 0 : 1); } /* Map2 */ else if ( levelno <= 4 ) { jeff_bitmap_set(bmap, 0, machine->readByte(0x4065) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 1, machine->readByte(0x407B) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 2, machine->readByte(0x4385) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 3, machine->readByte(0x439B) == 0x40 ? 0 : 1); } /* Map 3 and 4 are a special case since they are repeated until the game ends */ else { unsigned int r = (levelno - 5) % 8; /* Map 3 */ if ( r < 4 ) { jeff_bitmap_set(bmap, 0, machine->readByte(0x4064) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 1, machine->readByte(0x4078) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 2, machine->readByte(0x4384) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 3, machine->readByte(0x4398) == 0x40 ? 0 : 1); } /* Map 4 */ else { jeff_bitmap_set(bmap, 0, machine->readByte(0x4064) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 1, machine->readByte(0x407C) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 2, machine->readByte(0x4384) == 0x40 ? 0 : 1); jeff_bitmap_set(bmap, 3, machine->readByte(0x439C) == 0x40 ? 0 : 1); } } /* Then, fill it for nodes */ graph_node_visit(g, &bo, set_node_bit); /* And, for edges */ graph_edge_visit(g, &bo, set_edge_bits); } /** * Create a new pill table */ struct jeff_bmap* create_pill_table() { struct jeff_bmap *bmap = (struct jeff_bmap*)malloc(sizeof(struct jeff_bmap)); jeff_bitmap_create(bmap, 28*32, 1); return bmap; } /** * Print pill table as ascii on screen (for debug) */ void print_pill_table(struct jeff_bmap *bmap) { int count=0; printf("["); for(int i=0;ireadByte(0x4e0e); }