//Returns the nearest grid square position to the provided mouse position Vector2D GameWorld::GetNearestGridSquarePositionToMouse(int ix, int iy) { Vector2D mousepos; mousepos.Set(ix, iy); Vector2D gsp; Vector2D retval; retval.Set(-1, -1); for(int y = 0; y < 11; y++) { for(int x = 0; x < 17; x++) { gsp = m_gamegrid[x][y].pos; if(mousepos.x > gsp.x && mousepos.x < gsp.x+19 && mousepos.y > gsp.y && mousepos.y < gsp.y+19) { retval.Set(x, y); return retval; } } } return retval; }
//Loads a Level void GameWorld::LoadLevel() { string file; sprintf((char*)file.c_str(), "maps/%s%i.mrl", m_levelname.c_str(), m_level); //Load Data int id; int e; char p[256]; int bi=0; //butterfly id //Clear the background of any decals SDL_BlitSurface(gConMgr.GetSurface("sprites/grid.fif"), 0, m_surf_background, &renderarea); //Reset Player Anim State m_players[0].ResetAnim(); //Clear level data for(int y = 0; y < 11; y++) { for(int x = 0; x < 17; x++) { m_gamegrid[x][y].ent = NULL; } } //Reset some data memset(&m_description, 0x00, sizeof(m_description)); for(int i = 0; i < MAX_CELLS; i++){m_mushrooms[i].SetVisible(0);} m_totalmushrooms = 0; m_totalboxes = 0; m_totalbutterflys = 0; m_totalhats = 0; m_totalwells = 0; m_well[0].SetVisible(0); m_hat[0].SetVisible(0); //If level doesnt exist: FILE* ec = fopen((char*)file.c_str(), "r"); if(ec != NULL){fclose(ec);}else { //It doesnt exist gGameWorld.gScores.AddScore(m_players[0].GetScore(), gGameWorld.GetLevel(), m_players[0].GetLife()); gGameWorld.gScores.m_enabled = 1; gGameWorld.gMenu.m_selected = 2; gGameWorld.gMenu.m_enabled = 1; return; } //Decrypt XorCrypt((char*)file.c_str(), "temp.dat", 471); //Open Map File char out[32]; FILE* open = fopen("temp.dat", "r"); if(open != NULL) { for(int x = 0; x < 17; x++) { for(int y = 0; y < 11; y++) { fscanf(open, "%s", out); id = atoi(out); if(id == 9) { fscanf(open, "%s", out); e = atoi(out); } if(id == 2) { memset(&p, 0x00, sizeof(p)); fscanf(open, "%s", p); } //Add object to the game if(id == 0){AddBoxX(x, y, 1);} if(id == 1){AddBoxX(x, y, 0);} if(id == 2){AddButterflyX(x, y, p, bi); bi++;} if(id == 3) { m_gamegrid[x][y].ent = &m_players[0]; m_players[0].SetPos(x, y); } if(id == 4){AddWellX(x, y);} // well if(id == 5){AddMushroomX(x, y, MUSHROOM_BLACK);} if(id == 6){AddMushroomX(x, y, MUSHROOM_RED);} if(id == 7){AddMushroomX(x, y, MUSHROOM_PINK);} if(id == 8){AddMushroomX(x, y, MUSHROOM_PURPLE);} if(id == 9){Vector2D tp; tp.Set(x, y); AddBlueBonus(tp, e);} if(id == 10){AddHatX(x, y, 4);} // mexican hat if(id == 11){AddHatX(x, y, 3);} // fez hat if(id == 12){AddHatX(x, y, 2);} // diving hat if(id == 13){AddHatX(x, y, 5);} // nightcap if(id == 14){AddHatX(x, y, 6);} // skullcap if(id == 15){AddHatX(x, y, 1);} // dunce hat } } //Load Description fscanf(open, "%s", m_description); for(int i = 0; i < sizeof(m_description); i++){if(m_description[i] == '^'){m_description[i] = ' ';}} //Break description down into lines memset(&line1, 0x00, sizeof(line1)); memset(&line2, 0x00, sizeof(line2)); memset(&line3, 0x00, sizeof(line3)); memset(&line4, 0x00, sizeof(line4)); memset(&line5, 0x00, sizeof(line5)); int line=0; int lc=0; int lsp=0; //last space pos int lcsp=0; for(int i = 0; i < strlen(m_description); i++) { if(m_description[i] == ' '){lsp = i; lcsp = lc;} if(line == 0){line1[lc] = m_description[i];} if(line == 1){line2[lc] = m_description[i];} if(line == 2){line3[lc] = m_description[i];} if(line == 3){line4[lc] = m_description[i];} if(line == 4){line5[lc] = m_description[i];} lc++; if(lc >= 36) { i = lsp; if(line == 0){line1[lcsp] = 0x00;} if(line == 1){line2[lcsp] = 0x00;} if(line == 2){line3[lcsp] = 0x00;} if(line == 3){line4[lcsp] = 0x00;} if(line == 4){line5[lcsp] = 0x00;} line++; lsp=0; lc=0; } if(line > 5){break;} } fclose(open); } //Delete Temp ClearFile("temp.dat"); //Get Ready!! if(firstgen == 0) { m_readyreg = GetTicks(); m_ready = 0; }else{firstgen=0;} m_beginscore = m_players[0].GetScore(); m_gamemode = 1; }
//This is a horrible Hunt & Kill maze algorithm i (shamefully) wrote (deliberate problem: it doesnt account for cross sections) void GameWorld::GenerateMaze() { //Clear the background of any decals SDL_BlitSurface(gConMgr.GetSurface("sprites/grid.fif"), 0, m_surf_background, &renderarea); //Clear game grid (of all but the players) for(int y = 0; y < 11; y++) { for(int x = 0; x < 17; x++) { if(m_gamegrid[x][y].ent != NULL) { if(m_gamegrid[x][y].ent->m_ident != IDENT_MAN){m_gamegrid[x][y].ent = NULL;} } } } //Clear arrays for(int i = 0; i < 100; i++){m_mushrooms[i].SetVisible(0);} m_totalmushrooms = 0; m_totalboxes = 0; m_totalbutterflys = 0; m_totalhats = 0; m_totalwells = 0; m_well[0].SetVisible(0); m_hat[0].SetVisible(0); //Vars Vector2D start_cell; Vector2D current_cell; Vector2D last_cell; int visited[17][11]; //Set all cell pointers to 1 for unvisited for(int y = 0; y < 11; y++) { for(int x = 0; x < 17; x++) { visited[x][y] = 0; } } //Pick a random starting cell, set it as visited 1 start_cell.Set(qRand(0, 17), qRand(0, 11)); visited[start_cell.x][start_cell.y] = 1; last_cell.x = start_cell.x; last_cell.y = start_cell.y; int dir=0; //direction to check int lastdir=0; //last direction int checked[4]; //checked directions loopsearch: //i'm going to hell checked[0]=0; checked[1]=0; checked[2]=0; checked[3]=0; //Choose a random direction to move to if the cell is unvisited, then place the borders around it do { doloop: //there's no redemption for people like me current_cell.x = last_cell.x; current_cell.y = last_cell.y; dir = qRand(1, 5); if(dir == 1){current_cell.x--; checked[0] = 1;} //left if(dir == 2){current_cell.x++; checked[1] = 1;} //right if(dir == 3){current_cell.y--; checked[2] = 1;} //up if(dir == 4){current_cell.y++; checked[3] = 1;} //down if(current_cell.x < 0){goto doloop;} if(current_cell.y < 0){goto doloop;} if(current_cell.x > 16){goto doloop;} if(current_cell.y > 10){goto doloop;} //if no direction was found search all visited cells untill you find one with an accessable direction if(checked[0] == 1 && checked[1] == 1 && checked[2] == 1 && checked[3] == 1) //No unvisited direction avalible { //Find a new last cell (if there are no unvisited cells its complete) for(int y = 0; y < 11; y++) { for(int x = 0; x < 17; x++) { if(visited[x][y] == 0) { visited[x][y] = 1; last_cell.Set(x, y); goto loopsearch; } } } //Well Maze is pretty much done, add some entities AddMushrooms(10, 50, 1); AddMushrooms(1, 4, 4); for(int i = 0; i < qRand(0, 5); i++){AddButterfly(i);} AddEscapeMushroom(0, 0); //Place a blue bonus Vector2D p; p.Set(qRand(0, 17), qRand(0, 11)); while(m_gamegrid[p.x][p.y].ent != NULL) { p.Set(qRand(0, 17), qRand(0, 11)); } AddBlueBonus(p, qRand(100, 1000)); //Give the player a wall destroy m_players[0].m_walldestroy = 1; return; //No unvisited cell found, done maze! } } while(visited[current_cell.x][current_cell.y] == 1); //Place our borders! if(lastdir == 3 || lastdir == 4) { if(lastdir == 3 && dir == 1) //If you turn 'left' while going 'up' { if(visited[last_cell.x+1][last_cell.y] == 0){if(last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y); visited[last_cell.x+1][last_cell.y] = 1;}} if(visited[last_cell.x+1][last_cell.y-1] == 0){if(last_cell.y-1 >= 0 && last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y-1); visited[last_cell.x+1][last_cell.y-1] = 1;}} if(visited[last_cell.x][last_cell.y-1] == 0){if(last_cell.y-1 >= 0){AddBoxPos(last_cell.x, last_cell.y-1); visited[last_cell.x][last_cell.y-1] = 1;}} goto dp; } if(lastdir == 3 && dir == 2) //If you turn 'right' while going 'up' { if(visited[last_cell.x-1][last_cell.y] == 0){if(last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y); visited[last_cell.x-1][last_cell.y] = 1;}} if(visited[last_cell.x-1][last_cell.y-1] == 0){if(last_cell.y-1 >= 0 && last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y-1); visited[last_cell.x-1][last_cell.y-1] = 1;}} if(visited[last_cell.x][last_cell.y-1] == 0){if(last_cell.y-1 >= 0){AddBoxPos(last_cell.x, last_cell.y-1); visited[last_cell.x][last_cell.y-1] = 1;}} goto dp; } if(lastdir == 4 && dir == 1) //If you turn 'left' while going 'down' { if(visited[last_cell.x+1][last_cell.y] == 0){if(last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y); visited[last_cell.x+1][last_cell.y] = 1;}} if(visited[last_cell.x+1][last_cell.y+1] == 0){if(last_cell.y+1 <= 10 && last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y+1); visited[last_cell.x+1][last_cell.y+1] = 1;}} if(visited[last_cell.x][last_cell.y+1] == 0){if(last_cell.y+1 <= 10){AddBoxPos(last_cell.x, last_cell.y+1); visited[last_cell.x][last_cell.y+1] = 1;}} goto dp; } if(lastdir == 4 && dir == 2) //If you turn 'right' while going 'down' { if(visited[last_cell.x-1][last_cell.y] == 0){if(last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y); visited[last_cell.x-1][last_cell.y] = 1;}} if(visited[last_cell.x-1][last_cell.y+1] == 0){if(last_cell.y+1 <= 10 && last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y+1); visited[last_cell.x-1][last_cell.y+1] = 1;}} if(visited[last_cell.x][last_cell.y+1] == 0){if(last_cell.y+1 <= 10){AddBoxPos(last_cell.x, last_cell.y+1); visited[last_cell.x][last_cell.y+1] = 1;}} goto dp; } if(dir == 3 || dir == 4) //if direction is up or down, place borders left and right around last_cell { if(visited[last_cell.x-1][last_cell.y] == 0){if(last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y); visited[last_cell.x-1][last_cell.y] = 1;}} if(visited[last_cell.x+1][last_cell.y] == 0){if(last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y); visited[last_cell.x+1][last_cell.y] = 1;}} //goto dp; } } if(lastdir == 1 || lastdir == 2) { if(lastdir == 1 && dir == 3) //If you turn 'up' while going 'left' { if(visited[last_cell.x][last_cell.y+1]){if(last_cell.y+1 <= 10){AddBoxPos(last_cell.x, last_cell.y+1); visited[last_cell.x][last_cell.y+1] = 1;}} if(visited[last_cell.x-1][last_cell.y+1] == 0){if(last_cell.y+1 <= 10 && last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y+1); visited[last_cell.x-1][last_cell.y+1] = 1;}} if(visited[last_cell.x-1][last_cell.y] == 0){if(last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y); visited[last_cell.x-1][last_cell.y] = 1;}} goto dp; } if(lastdir == 1 && dir == 4) //If you turn 'down' while going 'left' { if(visited[last_cell.x-1][last_cell.y] == 0){if(last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y); visited[last_cell.x-1][last_cell.y] = 1;}} if(visited[last_cell.x][last_cell.y-1]){if(last_cell.y-1 >= 0){AddBoxPos(last_cell.x, last_cell.y-1); visited[last_cell.x][last_cell.y-1] = 1;}} if(visited[last_cell.x-1][last_cell.y-1] == 0){if(last_cell.y-1 >= 0 && last_cell.x-1 >= 0){AddBoxPos(last_cell.x-1, last_cell.y-1); visited[last_cell.x-1][last_cell.y-1] = 1;}} goto dp; } if(lastdir == 2 && dir == 3) //If you turn 'up' while going 'right' { if(visited[last_cell.x+1][last_cell.y] == 0){if(last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y); visited[last_cell.x+1][last_cell.y] = 1;}} if(visited[last_cell.x+1][last_cell.y+1] == 0){if(last_cell.y+1 <= 10 && last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y+1); visited[last_cell.x+1][last_cell.y+1] = 1;}} if(visited[last_cell.x][last_cell.y+1]){if(last_cell.y+1 <= 10){AddBoxPos(last_cell.x, last_cell.y+1); visited[last_cell.x][last_cell.y+1] = 1;}} goto dp; } if(lastdir == 2 && dir == 4) //If you turn 'down' while going 'right' { if(visited[last_cell.x+1][last_cell.y-1] == 0){if(last_cell.y-1 >= 0 && last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y-1); visited[last_cell.x+1][last_cell.y-1] = 1;}} if(visited[last_cell.x+1][last_cell.y] == 0){if(last_cell.x+1 <= 16){AddBoxPos(last_cell.x+1, last_cell.y); visited[last_cell.x+1][last_cell.y] = 1;}} if(visited[last_cell.x][last_cell.y-1]){if(last_cell.y-1 >= 0){AddBoxPos(last_cell.x, last_cell.y-1); visited[last_cell.x][last_cell.y-1] = 1;}} goto dp; } if(dir == 1 || dir == 2) //if direction is left or right, place borders up and down around last_cell { if(visited[last_cell.x][last_cell.y-1]){if(last_cell.y-1 >= 0){AddBoxPos(last_cell.x, last_cell.y-1); visited[last_cell.x][last_cell.y-1] = 1;}} if(visited[last_cell.x][last_cell.y+1]){if(last_cell.y+1 <= 10){AddBoxPos(last_cell.x, last_cell.y+1); visited[last_cell.x][last_cell.y+1] = 1;}} //goto dp; } } dp: //just feed me to the pigs already //Update last cell and direction last_cell.x = current_cell.x; last_cell.y = current_cell.y; lastdir = dir; //Set current sell as visited visited[current_cell.x][current_cell.y] = 1; //Loop the search goto loopsearch; }