Room* Room::create(){ Room* room = new Room(); room->name = IO::prompt("Name: "); room->label = IO::prompt("Label: "); room->description = IO::prompt("Description: "); Game::ptr()->rooms.push_back(room); while(IO::prompt_yesno("Add connection? ")){ Room* r = Room::create(); string text = IO::prompt("Connection text: "); room->connect(r, text); } return room; }
inline void modify(){ if(p.args.size() < 2) cout << "Modify what?\n"; else if(p.args.size() < 3) cout << "Modify what element?\n"; else if(p.args[1] == "room"){ Room* room = Game::ptr()->get_room(IO::prompt("Name: ")); if(!room){ cout << "No room by that name.\n"; return; } if(p.args[2] == "name") room->name = IO::prompt("Name: "); else if(p.args[2] == "label") room->name = IO::prompt("Label: "); else if(p.args[2] == "description" || p.args[2] == "desc") room->description = IO::prompt("Description: "); else if(p.args[2] == "connection" || p.args[2] == "conn"){ if(IO::prompt_yesno("Add? ")){ string text = IO::prompt("Connection text: "); if(IO::prompt_yesno("Existing? ")) room->connect(Game::ptr()->get_room(IO::prompt("Name: ")), text); else room->connect(Room::create(), text); } else { for(int i = 0; i < room->connections.size(); i++){ cout << i << ": "; cout << room->connections[i].second->name << endl; } string del_tar = IO::prompt("Delete num: "); int n = atoi(del_tar.c_str()); if(n > 0 && n < room->connections.size()) room->connections.erase(room->connections.begin() + n); } } cout << room->print() << endl; } else if(p.args[1] == "item"){ Item* item = Game::ptr()->get_item(IO::prompt("Name: ")); if(p.args[2] == "name") item->name = IO::prompt("Name: "); else if(p.args[2] == "label") item->label = IO::prompt("Label: "); else if(p.args[2] == "description") item->description = IO::prompt("Description: "); else if(p.args[2] == "roomtext") item->room_desc = IO::prompt("Room description: "); else if(p.args[2] == "pickup") item->pickup = IO::prompt_yesno("Carriable? "); else if(p.args[2] == "results"){ if(IO::prompt_yesno("Add? ")){ Item* key = NULL; if(IO::prompt_yesno("Key? ")){ if(IO::prompt_yesno("Exists? ")){ key = Game::ptr()->get_item(IO::prompt("Name: ")); } else { key = Item::create(); } } Result* res = Result::create(); item->results.insert(pair<Item*, Result*>(key, res)); } else { } } cout << item->print() << endl; } }
//Our generate level function creates an appropriate level string //with connected rooms and monsters and items sprinkled around at random, //and then returns the level string //parameters: width of the level to make, height of the level to make, // whether or not this is the first level of the map //returns: a string representing the map we generate string generateLevel(int mapWidth, int mapHeight, bool start){ srand(time(NULL)); //Initialize our map array vector< vector<char*> > levelMap; levelMap.resize(mapHeight); for(int i = 0; i < mapHeight; i++) levelMap[i].resize(sizeof(char) * mapWidth); for(int i = 0; i < mapHeight; i++) for(int j = 0; j < mapWidth; j++){ levelMap[i][j] = new char; *levelMap[i][j] = ' '; } //initialize our list of rooms that haven't been connected to each other yet stack <Room> unconnectedRooms; //initialize our list of rooms that have been connected to each other vector <Room> connectedRooms; int roomsOnLevel = rand()% 6 + 7; //for each room that we're going to generate for (int i = 0; i < roomsOnLevel; i++){ //initialize the room Room newRoom = Room(mapWidth, mapHeight, levelMap); //add it to our list of unconnected rooms unconnectedRooms.push(newRoom); } //select our first room and label it as connected, //now we'll have a room to build our dungeon corridors off of connectedRooms.push_back(unconnectedRooms.top()); unconnectedRooms.pop(); connectedRooms[connectedRooms.size() - 1].connected = true; //while there are rooms that aren't connected to the rest of the dungeon while (!unconnectedRooms.empty()){ //pop a room off of our list of unconnected rooms Room room = unconnectedRooms.top(); //connect our unconnected room to the rest of the dungeon if(connectedRooms.size() > 0) room.connect(connectedRooms[rand()%connectedRooms.size()], levelMap); else room.connect(connectedRooms[0], levelMap); //push our connected room into our list of connected rooms connectedRooms.push_back(room); unconnectedRooms.pop(); } //we now have a basic shell of the dungeon //composed of "." spaces and empty spaces //we call addWalls() to fill in all of the empty spaces //that are adjacent to "." spaces with "//" addWalls(levelMap); //if this is the first level of the dungeon, we dump the player in the first room //we generated (because that's easy) if (!start) connectedRooms[0].placeInRoom('@', levelMap); //put an up staircase on every level connectedRooms[rand()% (connectedRooms.size() - 1)].placeInRoom('<', levelMap); //put a down staircase on every level connectedRooms[rand()% (connectedRooms.size() - 1)].placeInRoom('>', levelMap); //for every room for (int i = 0; i < connectedRooms.size(); i++){ //25% chance of the room having a goblin in it if (rand()%4 + 1 == 4) connectedRooms[i].placeInRoom('g', levelMap); //one in seven chance of a potion being put in the room. if (rand()%7 + 1 == 7) connectedRooms[i].placeInRoom('%', levelMap); } //return the completed string return levelMapToString(levelMap); }