IT WORKS OMG
Great job @wuuff!
I created a very simple function for creating the 2 tile thick horizontal walls and added it to the bottom of your file.
void mappretty(char map[][MAPSIZE],int width, int height){
int i,j;
for (i=0; i < height-1; i++) {
for (j=0;j<width-1;j++){
if (map[i][j]==1 && map[i+1][j]==0){
map[i+1][j]=2;
}
}
}
}
Seeing how you did this really gives me a new outlook on the project
EDIT: Here’s the modified files to get this running so we can all compare with @VonBednar’s method using @jonne’s translation:
The main file:
#include "Pokitto.h"
#include <vector>
#include "sprites.h"
#include "mapgen.c"
Pokitto::Core game;
char dungeon[MAPSIZE][MAPSIZE];
struct entity{
uint8_t x;
uint8_t y;
int8_t hp;
uint8_t id;
};
#define ID_SKELETON_MAGE 9
#define ID_SKELETON_ARCHER 10
#define ID_SKELETON_WARIOR 11
#define ID_BLOOD_SKELETON 12
#define ID_BLOOD 13
#define ID_RAT 14
#define ID_SCROLL 15
#define ID_CHEST 16
#define ID_CHEST_OPEN 17
#define ID_MIMIC 18
#define ID_COIN 19
#define ID_COINS 20
#define ID_BAG 21
//globals
int entities_size = 2;
std::vector<entity> entities(entities_size);
char printer[40] = "";
int playerGold = 0;
int playerHP = 100;
bool colide(int id, int x, int y, bool c){
if (entities[id].x == x && entities[id].y == y) return false;
else return c;
}
void removeEntity(int i){
using std::swap;
std::swap(entities[i], entities.back());
entities.pop_back();
}
bool entitiesLogic(int playerX, int playerY){
bool nocolide = true;
for(int i=0; i<entities.size(); ++i){
switch(entities[i].id){
case ID_CHEST:
nocolide = colide(i,playerX,playerY,nocolide); //its wierd but works
if(!colide(i,playerX,playerY,true)){
entities[i].id = ID_CHEST_OPEN;
sprintf(printer,"open chest, taken %i gold", entities[i].hp);
playerGold += entities[i].hp;
entities[i].hp = 0;
}
break;
case ID_CHEST_OPEN:
break;
case ID_MIMIC:
nocolide = colide(i,playerX,playerY,nocolide); //its wierd but works
if(entities[i].x > playerX && colide(i,playerX+1,playerY,true)) entities[i].x-=1;
else if(!colide(i,playerX+1,playerY,true)) playerHP -=10;
if(entities[i].x < playerX && colide(i,playerX-1,playerY,true)) entities[i].x+=1;
else if(!colide(i,playerX-1,playerY,true)) playerHP -=10;
if(entities[i].y > playerY && colide(i,playerX,playerY+1,true)) entities[i].y-=1;
else if(!colide(i,playerX,playerY+1,true)) playerHP -=10;
if(entities[i].y < playerY && colide(i,playerX,playerY-1,true)) entities[i].y+=1;
else if(!colide(i,playerX,playerY-1,true)) playerHP -=10;
if(!colide(i,playerX,playerY,true)){
entities[i].hp -= 1;
sprintf(printer,"hit mimic, hp: %i", entities[i].hp);
if(entities[i].hp == 0) removeEntity(i);
}
break;
default:
printf("unknown entety %i\n", entities[i].id);
break;
}
}
return nocolide;
}
void drawHP(int hp){
//gui
game.display.drawBitmap(212,0,UI1);
game.display.drawFastVLine(219,3,158);
game.display.drawFastVLine(211,3,158);
game.display.drawBitmap(212,162,UI2);
int UI_hp = hp * 0.78;
if(hp >= 0){
for(int i = 0; i < UI_hp; i++){
game.display.drawBitmap(213,159-(i*2),UI4);
}
game.display.drawBitmap(213,160-((UI_hp+1)*2),UI3);
game.display.drawBitmap(213,161,UI5);
}
}
int main () {
srand(30);
mapinit(dungeon,MAPSIZE,MAPSIZE);
mapgen(dungeon,MAPSIZE,MAPSIZE,0,0,MAPSIZE-1,MAPSIZE-1);
mappretty(dungeon,MAPSIZE,MAPSIZE);
game.begin();
//mapgen(0,0,0,20,20);
game.display.loadRGBPalette(paletteCGA);
//game.display.setFont(fontAdventurer);
//game.display.persistence = true;
game.display.setInvisibleColor(0);
int playerX = 2;
int playerY = 2;
entities[0].id = ID_CHEST;
entities[0].x = 5;
entities[0].y = 3;
entities[0].hp = 100;
entities[1].id = ID_MIMIC;
entities[1].x = 15;
entities[1].y = 15;
entities[1].hp = 10;
while (game.isRunning()) {
if (game.update()) {
if (game.buttons.repeat(BTN_UP,4)){
if (!dungeon[playerY-1][playerX]){
if(entitiesLogic( playerX, playerY-1)) playerY --;
}
}
if (game.buttons.repeat(BTN_DOWN,4)){
if (!dungeon[playerY+1][playerX]){
if(entitiesLogic( playerX, playerY+1)) playerY ++;
}
}
if (game.buttons.repeat(BTN_LEFT,4)){
if (!dungeon[playerY][playerX-1]){
if(entitiesLogic( playerX-1, playerY)) playerX --;
}
}
if (game.buttons.repeat(BTN_RIGHT,4)){
if (!dungeon[playerY][playerX+1]){
if(entitiesLogic( playerX+1, playerY)) playerX ++;
}
}
for(int x =playerX-7; x<playerX+8; x++){ //7
for(int y =playerY-6; y<playerY+6; y++){
if(x >= 0 && y >= 0 && x <MAPSIZE && y < MAPSIZE){
game.display.drawBitmap(14*(x-playerX+7),14*(y-playerY+6),background[dungeon[y][x]]);
}
}
}
game.display.setCursor(0,168);
game.display.color = 1;
game.display.print(printer);
drawHP(playerHP);
for(int i=0; i<entities.size(); ++i){
game.display.color = 0; //remove before release
game.display.fillRect(14*(entities[i].x-playerX+7),14*(entities[i].y-playerY+6),14,14);//remove and fix before release
game.display.drawBitmap(14*(entities[i].x-playerX+7),14*(entities[i].y-playerY+6),sprites[entities[i].id]);
}
game.display.drawBitmap(14*(7),14*(6),sprites[3]);
}
}
return 1;
}
mapgen.c:
#include <stdio.h>
#include <stdlib.h>
#define MAPSIZE 64
void mapinit(char map[][MAPSIZE], int width, int height);
void mapgen(char map[][MAPSIZE], int mapwidth, int mapheight, int startx, int starty, int endx, int endy);
void mapprint(char map[][MAPSIZE], int width, int height);
void mapinit(char map[][MAPSIZE], int width, int height){
int i,j;
//Generate walls around the edges
for( i = 0; i < width; i++ ){
map[0][i] = 1;
map[height-1][i] = 1;
}
for( j = 0; j < height; j++ ){
map[j][0] = 1;
map[j][width-1] = 1;
}
}
#define HORIZONTAL 0
#define VERTICAL 1
#define MIN_WIDTH 6
#define MIN_HEIGHT 8
void mapgen(char map[][MAPSIZE], int mapwidth, int mapheight, int startx, int starty, int endx, int endy){
int i,j,orientation,position,door,doorcount;
int width = endx-startx;
int height = endy-starty;
if( width < MIN_WIDTH && height < MIN_HEIGHT ){
return;
}
//Determine whether we will split the space
//horizontally or vertically by choosing whichever
//orientation is larger (this avoids extremely long rooms)
if( width >= height ){
//If there is a door (or more than one door!) into a small room,
//we may not be able to generate a wall in any location!
//so abort if room is min + number of horiz door tiles
doorcount = 0;
for( i = startx; i < endx; i++ ){
if( map[starty][i] == 0 ) doorcount++;
if( map[endy][i] == 0 ) doorcount++;
}
if( width < MIN_WIDTH + doorcount ){
return;
}
orientation = VERTICAL;
//puts("Trying vertical");
}else{
//If there is a door (or more than one door!) into a small room,
//we may not be able to generate a wall in any location!
//so abort if room is min + number of vert door tiles
doorcount = 0;
for( i = starty; i < endy; i++ ){
if( map[i][startx] == 0 ) doorcount++;
if( map[i][endx] == 0 ) doorcount++;
}
if( height < MIN_HEIGHT + doorcount ){
return;
}
orientation = HORIZONTAL;
//puts("Trying horizontal");
}
//printf("startx %d, starty %d\n",startx,starty);
//mapprint(map,MAPSIZE,MAPSIZE);
position = -1;
if( orientation == HORIZONTAL ){
//Make sure the position is valid:
//1. It must have generated at least one number
//2. It must not be too close to existing walls
//3. It must not be over a door
while( position == -1 || position < starty + (MIN_HEIGHT/2) || position > endy - (MIN_HEIGHT/2) || map[position][startx] == 0 || map[position][endx] == 0 ){
position = starty + (rand()%height);
}
//Generate a door at a random position
door = startx + 1 + (rand()%(width-1));
//printf("HORIZ %d\n",position);
for( i = startx; i < startx + width; i++ ){
if( i != door )
map[position][i] = 1;
}
//Recursively call to fill the two new spaces we generated
mapgen(map, mapwidth, mapheight, startx, starty, endx,position);
mapgen(map, mapwidth, mapheight, startx, position, endx, endy);
}else if( orientation == VERTICAL ){
//Make sure the position is valid:
//1. It must have generated at least one number
//2. It must not be too close to existing walls
//3. It must not be over a door
while( position == -1 || position < startx + (MIN_WIDTH/2) || position > endx - (MIN_WIDTH/2) || map[starty][position] == 0 || map[endy][position] == 0 ){
position = startx + (rand()%width);
}
//Generate a door at a random position
//(allocating space for it to be 2 high)
door = starty + 1 + (rand()%(height-2));
//printf("VERT %d\n",position);
for( i = starty; i < starty + height; i++ ){
if( i != door && i != door+1 )
map[i][position] = 1;
}
//Recursively call to fill the two new spaces we generated
mapgen(map, mapwidth, mapheight, startx, starty, position,endy);
mapgen(map, mapwidth, mapheight, position, starty, endx, endy);
}
}
void mapprint(char map[][MAPSIZE], int width, int height){
int i,j;
for( i = 0; i < height; i++ ){
for( j = 0; j < width; j++ ){
if( map[i][j] == 0 ){
printf("0");
}else if( map[i][j] == 1 ){
printf("1");
}
}
puts("");
}
}
void mappretty(char map[][MAPSIZE],int width, int height){
int i,j;
for (i=0; i < height-1; i++) {
for (j=0;j<width-1;j++){
if (map[i][j]==1 && map[i+1][j]==0){
map[i+1][j]=2;
}
}
}
}