- Joined
- Aug 1, 2008
- Messages
- 1
- Reaction score
- 0
I got really sick of HUGE GM command if/else blocks in the chat handler, as well as wanting an easier way to add in common maps that I'd want to warp to by name.
This addon will allow things such as:
!henny
!ell
!0
!40000
To warp you to those respective map names or mapIDs.
The parser also checks that the map was loaded and is available to warp to before returning a mapID.
Let's get hacking!
Make a new .h file in your project and call it common_maps.h
in that file, insert the following:
Now, in Players.cpp, add #include "common_maps.h" to the rest of your #include directives.
Still in Player.cpp, find void Players::addPlayer(Player* player) and change what you need to to make it look like this:
Now, still remaining in Player.cpp, find your void Players::chatHandler(Player* player, unsigned char* packet) and update from:
to
Now, any time you want to add a named map warp, you just need to add it to common_maps.h
For example, I forgot to add a warp to Zakum's Door. To do so, I'll add commonMaps["zakumsdoor"] = 211042300; to common_maps.h's initialize_commonMaps declaration, and I'm DONE. Rebuild and start the server, and when you type !zakumsdoor you'll be teleported there automagically
Making !map work with parse_mapName() is an exercise for the reader.
This addon will allow things such as:
!henny
!ell
!0
!40000
To warp you to those respective map names or mapIDs.
The parser also checks that the map was loaded and is available to warp to before returning a mapID.
Let's get hacking!
Make a new .h file in your project and call it common_maps.h
in that file, insert the following:
Code:
#ifndef COMMON_MAPS
#define COMMON MAPS
#include <hash_map>
#include <string>
#include "Maps.h"
#include "Player.h"
using namespace std;
using namespace stdext;
#define COMMON_MAP_FOUND(str) commonMaps.find(str) != commonMaps.end()
static hash_map <string,int>commonMaps;
static void initialize_commonMaps() {
commonMaps["amherst"] = 1010000;
commonMaps["southperry"] = 60000;
commonMaps["lith"] = 104000000;
commonMaps["ellinia"] = 101000000;
commonMaps["ell"] = commonMaps["ellinia"];
commonMaps["perion"] = 102000000;
commonMaps["kerning city"] = 103000000;
commonMaps["kerning"] = commonMaps["kerning city"];
commonMaps["kern"] = commonMaps["kerning city"];
commonMaps["henesys"] = 100000000;
commonMaps["henny"] = commonMaps["henesys"];
commonMaps["sleepywood"] = 105040300;
commonMaps["sleepy"] = commonMaps["sleepywood"];
commonMaps["florina"] = 110000000;
commonMaps["florina beach"] = commonMaps["florina"];
commonMaps["orbis"] = 200000000;
commonMaps["elnath"] = 211000000;
commonMaps["el nath"] = commonMaps["elnath"];
commonMaps["aqua"] = 230000000;
commonMaps["aqua road"] = commonMaps["aqua"];
commonMaps["aquarium"] = commonMaps["aqua"];
commonMaps["ludibrium"] = 220000000;
commonMaps["ludi"] = commonMaps["ludibrium"];
commonMaps["omega"] = 221000000;
commonMaps["omega sector"] = commonMaps["omega"];
commonMaps["korean"] = 222000000;
commonMaps["kft"] = commonMaps["kft"];
commonMaps["mulung"] = 250000000;
commonMaps["mu lung"] = commonMaps["mulung"];
commonMaps["herb"] = 251000000;
commonMaps["herbtown"] = commonMaps["herb"];
commonMaps["leafre"] = 240000000;
commonMaps["newleaf"] = 600000000;
commonMaps["nlc"] = commonMaps["newleaf"];
commonMaps["amoria"] = 680000000;
commonMaps["fm"] = 910000000;
commonMaps["freemarket"] = commonMaps["fm"];
commonMaps["gmmap"] = 180000000;
commonMaps["gmcastle"] = commonMaps["gmmap"];
commonMaps["jail"] = 200090300;
}
// returns mapID if mapName/mapID found and available, -1 if not
static int parse_mapName(char *input) {
if ( !input ) return -1; // bad ptr
int mapID = -1;
// Since a map name can't start with a number, assume a mapID
// was entered and return it if it exists.
if ( isdigit(input[0]) ) {
mapID = atoi(input);
return ( Maps::info.find(mapID) != Maps::info.end() ) ? mapID : -1;
}
int length = 0;
if ( (length=strlen(input)) == 0 ) return -1;
if ( COMMON_MAP_FOUND(input) ) {
return commonMaps[input];
}
return -1;
}
Now, in Players.cpp, add #include "common_maps.h" to the rest of your #include directives.
Still in Player.cpp, find void Players::addPlayer(Player* player) and change what you need to to make it look like this:
Code:
void Players::addPlayer(Player* player){
players[player->getPlayerid()] = player;
// only GMs need a copy of the commonMaps
if( player->isGM() ) {
initialize_commonMaps();
}
}
Now, still remaining in Player.cpp, find your void Players::chatHandler(Player* player, unsigned char* packet) and update from:
Code:
char command[90] = "";
if(chatsize>2)
strcpy_s(command, 90, strtok_s(chat+1, " ", &next_token));
if(strcmp(command, "map") == 0){
if(strlen(next_token) > 0){
Code:
char command[90] = "";
if(chatsize>1) // <------- notice the chatsize change
strcpy_s(command, 90, strtok_s(chat+1, " ", &next_token));
// ========== QUICKWARPS USING commonMaps ==========
int mapID = parse_mapName(command);
if ( mapID >= 0 && mapID != player->getMap() ) {
Maps::changeMap(player ,mapID, 0);
return;
}
//===================================================
if(strcmp(command, "map") == 0){
if(strlen(next_token) > 0){
Now, any time you want to add a named map warp, you just need to add it to common_maps.h
For example, I forgot to add a warp to Zakum's Door. To do so, I'll add commonMaps["zakumsdoor"] = 211042300; to common_maps.h's initialize_commonMaps declaration, and I'm DONE. Rebuild and start the server, and when you type !zakumsdoor you'll be teleported there automagically

Making !map work with parse_mapName() is an exercise for the reader.