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

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

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

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