root/trunk/src/shared/vmap/CoordModelMapping.cpp @ 13

Revision 2, 6.7 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18
19#include "CoordModelMapping.h"
20
21#include <string.h>
22
23using namespace G3D;
24
25namespace VMAP
26{
27
28    //============================================================
29    //============================================================
30
31    void CMappingEntry::addFilename(char *pName)
32    {
33        std::string name = std::string(pName);
34        if(!iFilenames.contains(name))
35            iFilenames.append(std::string(pName));
36    }
37
38    //============================================================
39
40    const std::string CMappingEntry::getKeyString() const
41    {
42        return(CMappingEntry::getKeyString(iMapId,xPos, yPos));
43    }
44
45    //============================================================
46    //============================================================
47    //============================================================
48
49    CoordModelMapping::~CoordModelMapping()
50    {
51        Array<std::string> keys = iMapObjectFiles.getKeys();
52        for (int k = 0; k < keys.length(); k++)
53        {
54            CMappingEntry *value = getCMappingEntry(keys[k]);
55            if(value != 0)
56            {
57                iMapObjectFiles.remove(keys[k]);
58                delete value;
59            }
60        }
61    }
62
63    //============================================================
64
65    int findPosChar(const char *namebuffer, char pSearch, int pCount)
66    {
67        int result = -1;
68        int pos=0;
69        while(namebuffer[pos] != 0)
70        {
71            if(namebuffer[pos] == pSearch)
72            {
73                --pCount;
74            }
75            if(pCount == 0)
76            {
77                result = pos;
78                break;
79            }
80            ++pos;
81        }
82        return result;
83    }
84    //============================================================
85    bool CoordModelMapping::readCoordinateMapping(const std::string& pDirectoryFileName)
86    {
87        FILE *f = fopen(pDirectoryFileName.c_str(), "rb");
88        bool result = false;
89        char buffer[500+1];
90
91        if(f)
92        {
93            result = true;
94            CMappingEntry* cMappingEntry;
95            while(fgets(buffer, 500, f))
96            {
97                //char namebuffer[500];
98                char positionbuffer[500];
99                int xpos, ypos, noVec;
100                float scale;
101                xpos = ypos = noVec = 0;
102
103                //sscanf(buffer, "%d %d %s %s %f %d", &xpos, &ypos, namebuffer,positionbuffer, &scale, &noVec);
104
105                // this is ugly, but the format has no read delimiter and a space could be in the first part of the name
106                int nameStart = findPosChar(buffer, ' ', 2);// find the 2. space
107                if(nameStart > -1 && (iFilterMethod == NULL || (*iFilterMethod)(buffer)))
108                {
109                    ++nameStart;
110                                                            // find the 1. / (now a space only can be found at the end of the name)
111                    int nameEnd = nameStart + findPosChar(&buffer[nameStart], '/', 1);
112                                                            // find the 1. space (after the name)
113                    nameEnd += findPosChar(&buffer[nameEnd], ' ', 1);
114                    buffer[nameEnd] = 0;                    // terminate the name
115
116                    sscanf(buffer, "%d %d", &xpos, &ypos);
117                    sscanf(&buffer[nameEnd+1], "%s %f %d", positionbuffer, &scale, &noVec);
118                    unsigned int mapId = getMapIdFromFilename(std::string(&buffer[nameStart]));
119                    if(!iMapIds.contains(mapId))
120                    {
121                        iMapIds.append(mapId);
122                    }
123                    if(!isWorldAreaMap(mapId))
124                    {
125                        xpos = 0;                           // store all files under the groupKey
126                        ypos = 0;
127                    }
128
129                    std::string key = CMappingEntry::getKeyString(mapId, xpos, ypos);
130                    cMappingEntry = getCMappingEntry(key);
131                    if(cMappingEntry == 0)
132                    {
133                        cMappingEntry = new CMappingEntry(mapId, xpos, ypos);
134                        addCMappingEntry(cMappingEntry);
135                    }
136                    char namebuffer2[500];
137                    sprintf(namebuffer2, "%d %s#%s_%f", noVec, &buffer[nameStart], positionbuffer, scale);
138                    cMappingEntry->addFilename(namebuffer2);
139                    //break;
140                }
141            }
142            fclose(f);
143        }
144        return result;
145    }
146
147    //============================================================
148
149    const NameCollection CoordModelMapping::getFilenamesForCoordinate(unsigned int pMapId, int xPos, int yPos)
150    {
151        NameCollection result;
152        Array<std::string> rawNames;
153
154        CMappingEntry *entry = getCMappingEntry(CMappingEntry::getKeyString(pMapId, xPos, yPos));
155        if(entry != 0)
156        {
157            rawNames = entry->getFilenames();
158
159            int pos = 0;
160            while(pos < rawNames.size())
161            {
162                char namebuffer[500];
163                int noVerc;
164                int startName = findPosChar(rawNames[pos].c_str(), ' ', 1) + 1;
165                int endName = (int) rawNames[pos].length();
166                sscanf(rawNames[pos].c_str(), "%d", &noVerc);
167                memcpy(namebuffer, &rawNames[pos].c_str()[startName], endName-startName);
168                namebuffer[endName-startName]  = 0;
169                sscanf(rawNames[pos].c_str(), "%d", &noVerc);
170                std::string modelPosFileName = std::string(namebuffer);
171                if(noVerc > MIN_VERTICES_FOR_OWN_CONTAINER_FILE)
172                {
173                    result.appendToSingle(modelPosFileName);
174                }
175                else
176                {
177                    result.appendToMain(modelPosFileName);
178                }
179                ++pos;
180            }
181        }
182        return result;
183    }
184
185    //=================================================================
186
187}
Note: See TracBrowser for help on using the browser.