Fimbulwinter Project  Pre-Alpha
An Ragnarok Online Emulator
Common/map_index.hpp
00001 /*==================================================================*
00002 *     ___ _           _           _          _       _                          *
00003 *    / __(_)_ __ ___ | |__  _   _| |_      _(_)_ __ | |_ ___ _ __       *
00004 *   / _\ | | '_ ` _ \| '_ \| | | | \ \ /\ / / | '_ \| __/ _ \ '__|      *
00005 *  / /   | | | | | | | |_) | |_| | |\ V  V /| | | | | ||  __/ |         *
00006 *  \/    |_|_| |_| |_|_.__/ \__,_|_| \_/\_/ |_|_| |_|\__\___|_|         *
00007 *                                                                                                                                       *
00008 * ------------------------------------------------------------------*
00009 *                                                           Emulator                                    *
00010 * ------------------------------------------------------------------*
00011 *                                               Licenced under GNU GPL v3                                       *
00012 * ------------------------------------------------------------------*
00013 *                                                   Database Classes                                            *
00014 * ================================================================= */
00015 
00016 #pragma once
00017 
00018 #include <string>
00019 #include <map>
00020 #include <datastruct.hpp>
00021 #include <fstream>
00022 #include <show_message.hpp>
00023 #include <ctype.h>
00024 #include <ragnarok.hpp>
00025 
00026 using namespace std;
00027 
00036 struct map_index_node
00037 {
00038         int id;
00039         char server_name[MAP_NAME_LENGTH];
00040         char subname[MAP_NAME_LENGTH];
00041 };
00042 
00051 class map_index
00052 {
00053 public:
00054         map_index()
00055         {
00056                 last_map = 1;
00057         }
00058 
00060         bool load(string file)
00061         {
00062                 ifstream ifs(file, ifstream::in);
00063                 int count = 0, line = 0, qcp = 0,j = 0;
00064 
00065                 if (ifs.fail())
00066                 {
00067                         ShowError("Error reading map_index, file not found.\n");
00068                         return false;
00069                 }
00070 
00071                 while (ifs.good())
00072                 {
00073                         char buff[256];
00074                         char *sbuff = buff;
00075                         line++;
00076 
00077                         ifs.getline(buff, sizeof(buff));
00078 
00079                         while (isspace(*sbuff) && *sbuff)
00080                                 sbuff++;
00081 
00082                         if (buff[0] == 0)
00083                                 continue;
00084 
00085                         if (buff[0] == '#')
00086                                 continue;
00087 
00088                         char server_name[MAP_NAME_LENGTH];
00089                         char copymapname[1024];
00090                         int id = -1;
00091                         
00092 
00093                         if (sscanf(sbuff, "%11s %d %s",server_name,&id,copymapname) == 3 ||
00094                                 sscanf(sbuff, "%11s", server_name) == 1 ||
00095                                 sscanf(sbuff, "%11s %d", server_name, &id) == 2 
00096                         )
00097                                 
00098                         {
00099 
00100                                  map_index_node *node = new map_index_node();
00101 
00102                                  memset(node->subname, 0, sizeof(node->subname));
00103                                  
00104                                  if(strcmp(copymapname,"") != 0){
00105                                          char* ptr;
00106                                          int j = 0;
00107                                          ptr = strtok (copymapname," ");
00108                                            while (ptr != NULL)
00109                                            {
00110                                                 strncpy(node->subname, copymapname, sizeof(node->subname));
00111                                                 ptr = strtok (NULL," ");
00112                                                 j++;qcp++;
00113                                            }
00114                                                  memset(copymapname,0,1024);
00115                                 }
00116                                 
00117                                 if (id == -1)
00118                                         id = last_map++;
00119                                 else
00120                                         last_map = id + 1;
00121 
00122                                 
00123                                 strncpy(node->server_name, server_name, sizeof(node->server_name));
00124                                 node->id = id;
00125                                 
00126                                 map_id.Insert(string(server_name), node);
00127                                 id_map[id] = node;
00128 
00129                                 count++;
00130                         }
00131                         else
00132                         {
00133                                 ShowWarning("Error reading map_index at line %d.\n", line);
00134                         }
00135                 }
00136 
00137                 ShowStatus("Finished reading map_index, %d maps found, %d virtual maps found.\n", count,qcp);
00138 
00139                 ifs.close();
00140 
00141                 return true;
00142         }
00143 
00145         int get_map_id(string server_name, bool ext = false)
00146         {
00147                 if (ext)
00148                         server_name.resize(server_name.length() - 4);
00149 
00150                 PatriciaTrieNode<string, map_index_node *> *node = map_id.LookupNode(server_name);
00151 
00152                 if (!node)
00153                         throw "Map not found.";
00154 
00155                 return node->GetData()->id;
00156         }
00157 
00159         char *get_map_name(int id)
00160         {
00161                 if (!id_map.count(id))
00162                         throw "Map not found.";
00163 
00164                 return id_map[id]->server_name;
00165         }
00166 
00168         void copy_map_name_ext(char *dst, int id)
00169         {
00170                 char tmp[MAP_NAME_LENGTH_EXT];
00171 
00172                 if (!id_map.count(id))
00173                         throw "Map not found.";
00174 
00175                 map_index_node *min = id_map[id];
00176 
00177                 if (strcmp(id_map[id]->subname, "") != 0)
00178                         strncpy(tmp, id_map[id]->subname, MAP_NAME_LENGTH);
00179                 else
00180                         strncpy(tmp, id_map[id]->server_name, MAP_NAME_LENGTH);
00181 
00182                 strcat(tmp, ".gat");
00183                 strncpy(dst, tmp, MAP_NAME_LENGTH_EXT);
00184         }
00185 
00186 private:
00187         PatriciaTrie<string, map_index_node *> map_id;
00188         map<int, map_index_node *> id_map;
00189         int last_map;
00190 };
 All Classes Functions