Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
Landusemap.cpp
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5
6 For more information, see http://www.rigsofrods.org/
7
8 Rigs of Rods is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 3, as
10 published by the Free Software Foundation.
11
12 Rigs of Rods is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include "Landusemap.h"
22
23#include "Actor.h"
24#include "Application.h"
25#include "Collisions.h"
26#include "Console.h"
27#include "ErrorUtils.h"
28#include "GameContext.h"
29#include "Language.h"
30#include "Terrain.h"
31
32#include "PropertyMaps.h"
33#include "PagedGeometry.h"
34
35#include <OgreConfigFile.h>
36
37using namespace Ogre;
38using namespace RoR;
39
40Landusemap::Landusemap(String configFilename) :
41 data(0)
42 , default_ground_model(nullptr)
43 , mapsize(App::GetGameContext()->GetTerrain()->getMaxTerrainSize())
44{
45 loadConfig(configFilename);
46}
47
49{
50 if (data != nullptr)
51 delete[] data;
52}
53
55{
56 if (!data)
57 return nullptr;
58
59 // we return the default ground model if we are not anymore in this map
60 if (x < 0 || x >= mapsize.x || z < 0 || z >= mapsize.z)
62
63 return data[x + z * (int)mapsize.x];
64}
65
66int Landusemap::loadConfig(const Ogre::String& filename)
67{
68 std::map<unsigned int, String> usemap;
69 String textureFilename = "";
70
71 LOG("Parsing landuse config: '"+filename+"'");
72
73 String group = "";
74 try
75 {
76 group = ResourceGroupManager::getSingleton().findGroupContainingResource(filename);
77 }
78 catch (...)
79 {
80 // we wont catch anything, since the path could be absolute as well, then the group is not found
81 }
82
83 Ogre::ConfigFile cfg;
84 try
85 {
86 // try to load directly otherwise via resource group
87 if (group == "")
88 cfg.loadDirect(filename);
89 else
90 cfg.loadFromResourceSystem(filename, group, "\x09:=", true);
91 }
92 catch (Ogre::Exception& e) // Already logged by OGRE
93 {
95 fmt::format("{}: {}", _L("Error while loading landuse config"), e.getDescription()));
96 return 1;
97 }
98
99 Ogre::ConfigFile::SectionIterator seci = cfg.getSectionIterator();
100 Ogre::String secName, kname, kvalue;
101 while (seci.hasMoreElements())
102 {
103 secName = seci.peekNextKey();
104 Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
105 Ogre::ConfigFile::SettingsMultiMap::iterator i;
106 for (i = settings->begin(); i != settings->end(); ++i)
107 {
108 kname = i->first;
109 kvalue = i->second;
110 // we got all the data available now, processing now
111 if (secName == "general" || secName == "config")
112 {
113 // set some class properties accoring to the information in this section
114 if (kname == "texture")
115 textureFilename = kvalue;
116 else if (kname == "frictionconfig" || kname == "loadGroundModelsConfig")
118 else if (kname == "defaultuse")
120 }
121 else if (secName == "use-map")
122 {
123 if (kname.size() != 10)
124 {
125 LOG("invalid color in landuse line in " + filename);
126 continue;
127 }
128 char* ptr; //not used
129 unsigned int color = strtoul(kname.c_str(), &ptr, 16);
130 usemap[color] = kvalue;
131 }
132 }
133 }
134
135 // process the config data and load the buffers finally
136 try
137 {
140
141 /*
142 // debug things below
143 printf("found ground use definitions:\n");
144 for (std::map < uint32, String >::iterator it=usemap.begin(); it!=usemap.end(); it++)
145 {
146 printf(" 0x%Lx : %s\n", it->first, it->second.c_str());
147 }
148 */
149
150 bool bgr = colourMap->getPixelBox().format == PF_A8B8G8R8;
151
152 Ogre::TRect<Ogre::Real> bounds = Forests::TBounds(0, 0, mapsize.x, mapsize.z);
153
154 // now allocate the data buffer to hold pointers to ground models
155 data = new ground_model_t*[(int)(mapsize.x * mapsize.z)];
156 ground_model_t** ptr = data;
157 //std::map < String, int > counters;
158 for (int z = 0; z < mapsize.z; z++)
159 {
160 for (int x = 0; x < mapsize.x; x++)
161 {
162 unsigned int col = colourMap->getColorAt(x, z, bounds);
163 if (bgr)
164 {
165 // Swap red and blue values
166 unsigned int cols = col & 0xFF00FF00;
167 cols |= (col & 0xFF) << 16;
168 cols |= (col & 0xFF0000) >> 16;
169 col = cols;
170 }
171 String use = usemap[col];
172 //if (use!="")
173 // counters[use]++;
174
175 // store the pointer to the ground model in the data slot
177 ptr++;
178 }
179 }
180 }
181 catch (Ogre::Exception& oex)
182 {
183 LogFormat("[RoR|Physics] Landuse: failed to load texture '%s', <Ogre::Exception> message: '%s'",
184 textureFilename.c_str(), oex.getFullDescription().c_str());
185 }
186 catch (std::exception& stex)
187 {
188 LogFormat("[RoR|Physics] Landuse: failed to load texture '%s', <std::exception> message: '%s'",
189 textureFilename.c_str(), stex.what());
190 }
191 catch (...)
192 {
193 LogFormat("[RoR|Physics] Landuse: failed to load texture '%s', unknown error", textureFilename.c_str());
194 }
195
196 return 0;
197}
Central state/object manager and communications hub.
void LOG(const char *msg)
Legacy alias - formerly a macro.
#define _L
Game state manager and message-queue provider.
A 2D greyscale image that is assigned to a certain region of your world to represent color levels.
static ColorMap * load(const Ogre::String &fileName, MapChannel channel=CHANNEL_COLOR)
Ogre::PixelBox getPixelBox()
Gets a pointer to the pixel data of the color map.
void setFilter(MapFilter filter)
Sets the filtering mode used for this color map.
Ogre::uint32 getColorAt(Ogre::Real x, Ogre::Real z, const Ogre::TRect< Ogre::Real > &mapBounds)
Gets the color value at the specified position.
int loadGroundModelsConfigFile(Ogre::String filename)
ground_model_t * getGroundModelByString(const Ogre::String name)
@ CONSOLE_MSGTYPE_TERRN
Parsing/spawn/simulation messages for terrain.
Definition Console.h:64
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition Console.cpp:103
@ CONSOLE_SYSTEM_ERROR
Definition Console.h:52
const TerrainPtr & GetTerrain()
ground_model_t ** data
Definition Landusemap.h:45
ground_model_t * default_ground_model
Definition Landusemap.h:46
Landusemap(Ogre::String cfgfilename)
Ogre::Vector3 mapsize
Definition Landusemap.h:48
ground_model_t * getGroundModelAt(int x, int z)
int loadConfig(const Ogre::String &filename)
Collisions * GetCollisions()
Definition Terrain.h:86
@ MAPFILTER_NONE
Use no filtering - fast, but may appear blocky.
Ogre::TRect< Ogre::Real > TBounds
Define TBounds as a TRect using Real numeric units.
@ CHANNEL_COLOR
Use the image's full RGB color information.
GameContext * GetGameContext()
Console * GetConsole()
void LogFormat(const char *format,...)
Improved logging utility. Uses fixed 2Kb buffer.
Surface friction properties.
Definition SimData.h:704