RigsofRods
Soft-body Physics Simulation
Terrn2FileFormat.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2016-2017 Petr Ohlidal
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods 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 Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
22 
23 #include "Terrn2FileFormat.h"
24 
25 #include "AppContext.h"
26 #include "ConfigFile.h"
27 #include "Console.h"
28 #include "Utils.h"
29 #include "SimConstants.h"
30 
31 #include <OgreException.h>
32 
33 using namespace RoR;
34 using namespace Ogre;
35 
36 const std::string VALUE_NOT_FOUND("@@NotFound!!");
37 
38 bool Terrn2Parser::LoadTerrn2(Terrn2Def& def, Ogre::DataStreamPtr &ds)
39 {
41  file.load(ds, "\t:=", true);
42  file.setLoggingInfo(ds->getName(), Console::CONSOLE_MSGTYPE_TERRN);
43 
44  // read in the settings
45  def.name = file.getString("Name", "General");
46  if (def.name.empty())
47  {
48  Str<500> msg; msg << "Error in file '" << ds->getName() << "': Terrain name is empty";
50  return false;
51  }
52 
53  def.ogre_ter_conf_filename = file.getString("GeometryConfig", "General");
54  // otc = ogre terrain config
55  if (!def.ogre_ter_conf_filename.empty() && def.ogre_ter_conf_filename.find(".otc") == String::npos)
56  {
57  Str<500> msg; msg << "Error in file '" << ds->getName() << "': Invalid geometry config file; only '.otc' is supported";
59  return false;
60  }
61 
62  def.ambient_color = file.getColourValue("AmbientColor", "General", ColourValue::White);
63  def.category_id = file.getInt ("CategoryID", "General", 129);
64  def.guid = file.getString ("GUID", "General");
65  def.version = file.getInt ("Version", "General", 1);
66  def.gravity = file.getFloat ("Gravity", "General", -9.81);
67  def.caelum_config = file.getString ("CaelumConfigFile", "General");
68  def.cubemap_config = file.getString ("SandStormCubeMap", "General");
69  def.caelum_fog_start = file.getInt ("CaelumFogStart", "General", -1);
70  def.caelum_fog_end = file.getInt ("CaelumFogEnd", "General", -1);
71  def.has_water = file.getBool ("Water", "General", false);
72  def.hydrax_conf_file = file.getString ("HydraxConfigFile", "General");
73  def.skyx_config = file.getString ("SkyXConfigFile", "General");
74  def.traction_map_file = file.getString ("TractionMap", "General");
75  def.water_height = file.getFloat ("WaterLine", "General");
76  def.water_bottom_height = file.getFloat ("WaterBottomLine", "General");
77  def.custom_material_name = file.getString ("CustomMaterial", "General");
78  def.start_position = file.getVector3 ("StartPosition", "General", Vector3(512.0f, 0.0f, 512.0f));
79 
80  if (file.HasSection("Authors"))
81  {
82  for (auto& author: file.getSettings("Authors"))
83  {
84  String type = SanitizeUtf8String(author.first); // e.g. terrain
85  String name = SanitizeUtf8String(author.second); // e.g. john doe
86 
87  if (!name.empty())
88  {
89  Terrn2Author author;
90  author.type = type;
91  author.name = name;
92  def.authors.push_back(author);
93  }
94  }
95  }
96 
97  if (file.HasSection("Objects"))
98  {
99  for (auto& tobj: file.getSettings("Objects"))
100  {
101  Ogre::String tobj_filename = SanitizeUtf8String(tobj.first);
102  def.tobj_files.push_back(TrimStr(tobj_filename));
103  }
104  }
105 
106  if (file.HasSection("Scripts"))
107  {
108  for (auto& script: file.getSettings("Scripts"))
109  {
110  Ogre::String as_filename = SanitizeUtf8String(script.first);
111  def.as_files.push_back(TrimStr(as_filename));
112  }
113  }
114 
115  if (file.HasSection("AssetPacks"))
116  {
117  for (auto& assetpack: file.getSettings("AssetPacks"))
118  {
119  Ogre::String assetpack_filename = SanitizeUtf8String(assetpack.first);
120  def.assetpack_files.push_back(TrimStr(assetpack_filename));
121  }
122  }
123 
124  if (file.HasSection("AI Presets"))
125  {
126  for (auto& presets: file.getSettings("AI Presets"))
127  {
128  Ogre::String presets_filename = SanitizeUtf8String(presets.first);
129  def.ai_presets_files.push_back(TrimStr(presets_filename));
130  }
131  }
132 
133  this->ProcessTeleport(def, &file);
134 
135  return true;
136 }
137 
139 {
140  def.teleport_map_image = file->getString("NavigationMapImage", "Teleport");
141 
142  unsigned int telepoint_number = 1;
143  for (;;)
144  {
145  char key_position [50];
146  char key_name [50];
147 
148  snprintf(key_position, 50, "Telepoint%u/Position" , telepoint_number);
149  snprintf(key_name, 50, "Telepoint%u/Name" , telepoint_number);
150 
151  std::string pos_str = file->getString(key_position, "Teleport", VALUE_NOT_FOUND);
152  if (pos_str == VALUE_NOT_FOUND)
153  {
154  break; // No more telepoints
155  }
156  Terrn2Telepoint t_point;
157  if (sscanf(pos_str.c_str(), "%f, %f, %f", &t_point.position.x, &t_point.position.y, &t_point.position.z) != 3)
158  {
159  char msg_buf[500];
160  snprintf(msg_buf, 500,
161  "ERROR: Field '[Teleport]/%s' ('%s') is not valid XYZ position. Skipping telepoint %u.",
162  key_position, pos_str.c_str(), telepoint_number);
165  }
166  else
167  {
168  t_point.name = file->getString(key_name, "Teleport"); // Optional field
169  def.telepoints.push_back(t_point); // Persist the entry
170  }
171 
172  ++telepoint_number;
173  }
174 }
175 
177  ambient_color(Ogre::ColourValue::Black),
178  category_id(-1),
179  start_position(Ogre::Vector3::ZERO),
180  version(1),
181  gravity(DEFAULT_GRAVITY),
182  water_height (0),
183  water_bottom_height(0),
184  caelum_fog_start (0),
185  caelum_fog_end (0),
186  has_water(false)
187 {}
RoR::Terrn2Def::authors
std::list< Terrn2Author > authors
Definition: Terrn2FileFormat.h:62
RoR::Terrn2Def::water_height
float water_height
Definition: Terrn2FileFormat.h:60
RoR::Terrn2Def
Definition: Terrn2FileFormat.h:48
VALUE_NOT_FOUND
const std::string VALUE_NOT_FOUND("@@NotFound!!")
RoR::Terrn2Def::assetpack_files
std::list< std::string > assetpack_files
Definition: Terrn2FileFormat.h:65
RoR::Console::CONSOLE_MSGTYPE_TERRN
@ CONSOLE_MSGTYPE_TERRN
Parsing/spawn/simulation messages for terrain.
Definition: Console.h:64
file
This is a raw Ogre binding for Imgui No project cmake file
Definition: README-OgreImGui.txt:3
RoR::Terrn2Def::custom_material_name
std::string custom_material_name
Definition: Terrn2FileFormat.h:76
RoR::Terrn2Def::caelum_fog_end
int caelum_fog_end
Definition: Terrn2FileFormat.h:70
RoR::Terrn2Def::ogre_ter_conf_filename
std::string ogre_ter_conf_filename
Definition: Terrn2FileFormat.h:53
RoR::Terrn2Def::gravity
float gravity
Definition: Terrn2FileFormat.h:59
Terrn2FileFormat.h
RoR::SanitizeUtf8String
std::string SanitizeUtf8String(std::string const &str_in)
Definition: Utils.cpp:117
RoR::Terrn2Def::caelum_fog_start
int caelum_fog_start
Definition: Terrn2FileFormat.h:69
SimConstants.h
RoR::Terrn2Def::cubemap_config
std::string cubemap_config
Definition: Terrn2FileFormat.h:71
AppContext.h
System integration layer; inspired by OgreBites::ApplicationContext.
Console.h
RoR::Console::putMessage
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition: Console.cpp:97
Utils.h
RoR::TrimStr
std::string & TrimStr(std::string &s)
Definition: Utils.h:67
RoR::Terrn2Author
Definition: Terrn2FileFormat.h:36
RoR::Console::CONSOLE_SYSTEM_ERROR
@ CONSOLE_SYSTEM_ERROR
Definition: Console.h:52
RoR::Terrn2Telepoint
< Teleport drop location
Definition: Terrn2FileFormat.h:42
RoR::Terrn2Author::name
std::string name
Definition: Terrn2FileFormat.h:39
RoR::Terrn2Telepoint::position
Ogre::Vector3 position
Definition: Terrn2FileFormat.h:44
RoR::Terrn2Telepoint::name
std::string name
Definition: Terrn2FileFormat.h:45
RoR::Terrn2Parser::LoadTerrn2
bool LoadTerrn2(Terrn2Def &def, Ogre::DataStreamPtr &ds)
Definition: Terrn2FileFormat.cpp:38
RoR::Terrn2Def::Terrn2Def
Terrn2Def()
Definition: Terrn2FileFormat.cpp:176
RoR::Terrn2Def::hydrax_conf_file
std::string hydrax_conf_file
Definition: Terrn2FileFormat.h:73
RoR::Str< 500 >
RoR::Str::ToCStr
const char * ToCStr() const
Definition: Str.h:46
RoR::Terrn2Def::version
int version
Definition: Terrn2FileFormat.h:58
RoR::App::GetConsole
Console * GetConsole()
Definition: Application.cpp:270
RoR::Terrn2Def::telepoints
std::list< Terrn2Telepoint > telepoints
Definition: Terrn2FileFormat.h:67
RoR::Terrn2Parser::ProcessTeleport
void ProcessTeleport(Terrn2Def &def, RoR::ConfigFile *file)
Definition: Terrn2FileFormat.cpp:138
RoR::Terrn2Def::ambient_color
Ogre::ColourValue ambient_color
Definition: Terrn2FileFormat.h:54
RoR::Terrn2Def::water_bottom_height
float water_bottom_height
Definition: Terrn2FileFormat.h:61
RoR::Terrn2Def::tobj_files
std::list< std::string > tobj_files
Definition: Terrn2FileFormat.h:63
RoR::Terrn2Def::guid
std::string guid
Definition: Terrn2FileFormat.h:57
RoR::Terrn2Def::teleport_map_image
std::string teleport_map_image
Definition: Terrn2FileFormat.h:77
RoR::Terrn2Def::caelum_config
std::string caelum_config
Definition: Terrn2FileFormat.h:68
Ogre
Definition: ExtinguishableFireAffector.cpp:35
DEFAULT_GRAVITY
static const float DEFAULT_GRAVITY
earth gravity
Definition: SimConstants.h:50
RoR::Console::CONSOLE_SYSTEM_WARNING
@ CONSOLE_SYSTEM_WARNING
Definition: Console.h:53
RoR::Terrn2Author::type
std::string type
Definition: Terrn2FileFormat.h:38
RoR::Terrn2Def::skyx_config
std::string skyx_config
Definition: Terrn2FileFormat.h:74
RoR::Terrn2Def::category_id
int category_id
Definition: Terrn2FileFormat.h:55
RoR::ConfigFile
Adds direct parsing of custom types.
Definition: ConfigFile.h:37
RoR::Terrn2Def::ai_presets_files
std::list< std::string > ai_presets_files
Definition: Terrn2FileFormat.h:66
RoR::Terrn2Def::name
std::string name
Definition: Terrn2FileFormat.h:52
ConfigFile.h
RoR
Definition: AppContext.h:36
RoR::Terrn2Def::traction_map_file
std::string traction_map_file
Definition: Terrn2FileFormat.h:75
RoR::Terrn2Def::has_water
bool has_water
Definition: Terrn2FileFormat.h:72
RoR::Terrn2Def::start_position
Ogre::Vector3 start_position
Definition: Terrn2FileFormat.h:56
RoR::Terrn2Def::as_files
std::list< std::string > as_files
Definition: Terrn2FileFormat.h:64