OpenClonk
mapgen-handle.cpp File Reference
Include dependency graph for mapgen-handle.cpp:

Go to the source code of this file.

Classes

struct  _C4MapgenHandle
 

Macros

#define HANDLE_TO_MATERIAL_MAP(handle)   (reinterpret_cast<C4MaterialMap*>(handle))
 
#define HANDLE_TO_TEXTURE_MAP(handle)   (reinterpret_cast<C4TextureMap*>(handle))
 
#define HANDLE_TO_GROUP(handle)   (reinterpret_cast<C4Group*>(handle))
 

Functions

void c4_mapgen_handle_init_script_engine ()
 
void c4_mapgen_handle_deinit_script_engine ()
 
void c4_mapgen_handle_set_map_library (C4GroupHandle *group_handle)
 
C4MapgenHandlec4_mapgen_handle_new_script (const char *filename, const char *source, C4MaterialMapHandle *material_map, C4TextureMapHandle *texture_map, unsigned int map_width, unsigned int map_height)
 
C4MapgenHandlec4_mapgen_handle_new (const char *filename, const char *source, const char *script_path, C4MaterialMapHandle *material_map, C4TextureMapHandle *texture_map, unsigned int map_width, unsigned int map_height)
 
void c4_mapgen_handle_free (C4MapgenHandle *mapgen)
 
const unsigned char * c4_mapgen_handle_get_map (C4MapgenHandle *mapgen)
 
unsigned int c4_mapgen_handle_get_width (C4MapgenHandle *mapgen)
 
unsigned int c4_mapgen_handle_get_height (C4MapgenHandle *mapgen)
 
unsigned int c4_mapgen_handle_get_rowstride (C4MapgenHandle *mapgen)
 
const char * c4_mapgen_handle_get_error (C4MapgenHandle *mapgen)
 

Class Documentation

◆ _C4MapgenHandle

struct _C4MapgenHandle

Definition at line 57 of file mapgen-handle.cpp.

Collaboration diagram for _C4MapgenHandle:
[legend]
Class Members
BYTE * data
StdCopyStrBuf error_message
unsigned int height
unsigned int rowstride
unsigned int width

Macro Definition Documentation

◆ HANDLE_TO_GROUP

#define HANDLE_TO_GROUP (   handle)    (reinterpret_cast<C4Group*>(handle))

Definition at line 32 of file mapgen-handle.cpp.

◆ HANDLE_TO_MATERIAL_MAP

#define HANDLE_TO_MATERIAL_MAP (   handle)    (reinterpret_cast<C4MaterialMap*>(handle))

Definition at line 30 of file mapgen-handle.cpp.

◆ HANDLE_TO_TEXTURE_MAP

#define HANDLE_TO_TEXTURE_MAP (   handle)    (reinterpret_cast<C4TextureMap*>(handle))

Definition at line 31 of file mapgen-handle.cpp.

Function Documentation

◆ c4_mapgen_handle_deinit_script_engine()

void c4_mapgen_handle_deinit_script_engine ( )

Definition at line 71 of file mapgen-handle.cpp.

72 {
73  MapScript.Clear();
74  GameScript.Clear();
76 }
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
C4MapScriptHost MapScript
C4GameScriptHost GameScript
void Clear()
Definition: C4Aul.cpp:85

References C4MapScriptHost::Clear(), C4AulScriptEngine::Clear(), C4GameScriptHost::Clear(), GameScript, MapScript, and ScriptEngine.

Referenced by c4_mapgen_handle_new(), c4_mapgen_handle_new_script(), and mape_mapgen_deinit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ c4_mapgen_handle_free()

void c4_mapgen_handle_free ( C4MapgenHandle mapgen)

Definition at line 289 of file mapgen-handle.cpp.

290 {
291  delete[] mapgen->data;
292  delete mapgen;
293 }

◆ c4_mapgen_handle_get_error()

const char* c4_mapgen_handle_get_error ( C4MapgenHandle mapgen)

Definition at line 318 of file mapgen-handle.cpp.

319 {
320  if(mapgen->data != nullptr)
321  return nullptr;
322  return mapgen->error_message.getData();
323 }

Referenced by mape_mapgen_render().

Here is the caller graph for this function:

◆ c4_mapgen_handle_get_height()

unsigned int c4_mapgen_handle_get_height ( C4MapgenHandle mapgen)

Definition at line 306 of file mapgen-handle.cpp.

307 {
308  assert(mapgen->data != nullptr);
309  return mapgen->height;
310 }

◆ c4_mapgen_handle_get_map()

const unsigned char* c4_mapgen_handle_get_map ( C4MapgenHandle mapgen)

Definition at line 295 of file mapgen-handle.cpp.

296 {
297  return reinterpret_cast<unsigned char*>(mapgen->data);
298 }

◆ c4_mapgen_handle_get_rowstride()

unsigned int c4_mapgen_handle_get_rowstride ( C4MapgenHandle mapgen)

Definition at line 312 of file mapgen-handle.cpp.

313 {
314  assert(mapgen->data != nullptr);
315  return mapgen->rowstride;
316 }

◆ c4_mapgen_handle_get_width()

unsigned int c4_mapgen_handle_get_width ( C4MapgenHandle mapgen)

Definition at line 300 of file mapgen-handle.cpp.

301 {
302  assert(mapgen->data != nullptr);
303  return mapgen->width;
304 }

◆ c4_mapgen_handle_init_script_engine()

void c4_mapgen_handle_init_script_engine ( )

Definition at line 65 of file mapgen-handle.cpp.

66 {
69 }
void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
Definition: C4Script.cpp:1128
void InitFunctionMap(C4AulScriptEngine *pEngine)

References InitCoreFunctionMap(), C4MapScriptHost::InitFunctionMap(), MapScript, and ScriptEngine.

Referenced by c4_mapgen_handle_new(), c4_mapgen_handle_new_script(), and mape_mapgen_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ c4_mapgen_handle_new()

C4MapgenHandle* c4_mapgen_handle_new ( const char *  filename,
const char *  source,
const char *  script_path,
C4MaterialMapHandle material_map,
C4TextureMapHandle texture_map,
unsigned int  map_width,
unsigned int  map_height 
)

Definition at line 174 of file mapgen-handle.cpp.

175 {
176  try
177  {
178  C4SLandscape landscape;
179  landscape.Default();
180 
181  landscape.MapWdt.Set(map_width, 0, map_width, map_width);
182  landscape.MapHgt.Set(map_height, 0, map_height, map_height);
183  landscape.MapPlayerExtend = 0;
184 
185  C4MapCreatorS2 mapgen(
186  &landscape,
187  HANDLE_TO_TEXTURE_MAP(texture_map),
188  HANDLE_TO_MATERIAL_MAP(material_map),
189  1
190  );
191 
192  C4MCParser parser(&mapgen);
193  parser.ParseMemFile(source, filename);
194 
195  C4MCMap* map = mapgen.GetMap(nullptr);
196  if(!map) throw std::runtime_error("No map definition in source file");
197 
198  // Setup the script engine if there is an algo=script overlay in the
199  // Landscape.txt file
200  if(HasAlgoScript(mapgen.GetMap(nullptr)))
201  {
202  // Re-initialize script engine. Otherwise, we get a warning when the user
203  // changes the value of a constant, since it is defined already from the
204  // previous map rendering. Note that we do not need to re-load the map library.
207 
208  if(script_path == nullptr)
209  throw std::runtime_error("For algo=script overlays to work, save the file first at the location of the Script.c file");
210 
211  gchar* dirname = g_path_get_dirname(script_path);
212  gchar* basename = g_path_get_basename(script_path);
213 
214  C4Group File;
215  if(!File.Open(dirname))
216  {
217  StdStrBuf error_msg = FormatString("Failed to open directory '%s': %s", dirname, File.GetError());
218  g_free(dirname);
219  g_free(basename);
220  throw std::runtime_error(error_msg.getData());
221  }
222 
223  // get scripts
224  File.ResetSearch();
225  if(!File.FindNextEntry(basename, (char*)nullptr))
226  {
227  g_free(dirname);
228  g_free(basename);
229  StdStrBuf error_msg = FormatString("Failed to load '%s': No such file", script_path);
230  throw std::runtime_error(error_msg.getData());
231  }
232 
234  GameScript.Load(File, basename, nullptr, nullptr);
235  g_free(dirname);
236  g_free(basename);
237 
238  const char* parse_error = c4_log_handle_get_first_log_message();
239  if(parse_error)
240  throw std::runtime_error(parse_error);
241 
242  // Link script engine (resolve includes/appends, generate code)
246  throw std::runtime_error(c4_log_handle_get_first_log_message());
247  }
248 
250  int32_t out_width, out_height;
251  BYTE* array = mapgen.RenderBuf(nullptr, out_width, out_height);
252 
253  // Don't show any map if there was a script runtime error
254  const char* runtime_error = c4_log_handle_get_first_log_message();
255  if(runtime_error)
256  {
257  delete[] array;
258  throw std::runtime_error(runtime_error);
259  }
260 
261  C4MapgenHandle* handle = new C4MapgenHandle;
262  handle->width = map_width;
263  handle->height = map_height;
264  handle->rowstride = out_width;
265  handle->error_message = nullptr;
266  handle->data = array;
267  return handle;
268  }
269  catch(const C4MCParserErr& err)
270  {
271  C4MapgenHandle* handle = new C4MapgenHandle;
272  handle->width = 0;
273  handle->height = 0;
274  handle->error_message.Copy(err.Msg);
275  handle->data = nullptr;
276  return handle;
277  }
278  catch(const std::exception& ex)
279  {
280  C4MapgenHandle* handle = new C4MapgenHandle;
281  handle->width = 0;
282  handle->height = 0;
283  handle->error_message.Copy(ex.what());
284  handle->data = nullptr;
285  return handle;
286  }
287 }
C4DefList Definitions
Definition: C4Globals.cpp:49
uint8_t BYTE
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
void Link(C4DefList *rDefs)
Definition: C4AulLink.cpp:165
bool Load(C4Group &, const char *, const char *, C4LangStringTable *) override
bool FindNextEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr, bool start_at_filename=false)
Definition: C4Group.cpp:2217
const char * GetError()
Definition: C4Group.cpp:650
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1316
bool Open(const char *group_name, bool do_create=false)
Definition: C4Group.cpp:660
char Msg[C4MaxMessage]
C4SVal MapWdt
Definition: C4Scenario.h:178
C4SVal MapHgt
Definition: C4Scenario.h:178
int32_t MapPlayerExtend
Definition: C4Scenario.h:181
void Default()
Definition: C4Scenario.cpp:286
void Set(int32_t std=0, int32_t rnd=0, int32_t min=0, int32_t max=100)
Definition: C4Scenario.cpp:36
const char * getData() const
Definition: StdBuf.h:442
unsigned int c4_log_handle_get_n_log_messages()
Definition: log-handle.cpp:67
const char * c4_log_handle_get_first_log_message()
Definition: log-handle.cpp:61
void c4_log_handle_clear()
Definition: log-handle.cpp:55
typedefG_BEGIN_DECLS struct _C4MapgenHandle C4MapgenHandle
Definition: log-handle.h:23
unsigned int width
void c4_mapgen_handle_init_script_engine()
#define HANDLE_TO_MATERIAL_MAP(handle)
void c4_mapgen_handle_deinit_script_engine()
#define HANDLE_TO_TEXTURE_MAP(handle)

References c4_log_handle_clear(), c4_log_handle_get_first_log_message(), c4_log_handle_get_n_log_messages(), c4_mapgen_handle_deinit_script_engine(), c4_mapgen_handle_init_script_engine(), C4SLandscape::Default(), Definitions, C4Group::FindNextEntry(), FormatString(), GameScript, StdStrBuf::getData(), C4Group::GetError(), C4MapCreatorS2::GetMap(), HANDLE_TO_MATERIAL_MAP, HANDLE_TO_TEXTURE_MAP, C4AulScriptEngine::Link(), C4GameScriptHost::Load(), C4SLandscape::MapHgt, C4SLandscape::MapPlayerExtend, C4SLandscape::MapWdt, C4MCParserErr::Msg, C4Group::Open(), C4MCParser::ParseMemFile(), C4MapCreatorS2::RenderBuf(), C4Group::ResetSearch(), ScriptEngine, C4SVal::Set(), and _C4MapgenHandle::width.

Referenced by mape_mapgen_render().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ c4_mapgen_handle_new_script()

C4MapgenHandle* c4_mapgen_handle_new_script ( const char *  filename,
const char *  source,
C4MaterialMapHandle material_map,
C4TextureMapHandle texture_map,
unsigned int  map_width,
unsigned int  map_height 
)

Definition at line 98 of file mapgen-handle.cpp.

99 {
100  // Re-initialize script engine. Otherwise, we get a warning when the user
101  // changes the value of a constant, since it is defined already from the
102  // previous map rendering. Note that we do not need to re-load the map library.
105 
106  try
107  {
108  // TODO: Could also re-use an existing CSurface8,
109  // saving unnecessary malloc/free between map renderings
110  C4SLandscape landscape;
111  landscape.Default();
112 
113  landscape.MapWdt.Set(map_width, 0, map_width, map_width);
114  landscape.MapHgt.Set(map_height, 0, map_height, map_height);
115  landscape.MapPlayerExtend = 0;
116 
118  ::MapScript.LoadData(filename, source, nullptr);
119  // If InitializeMap() returns false, the map creator wants to
120  // call a fallback in the scenario script. This crashes if no
121  // scenario script is loaded, so simply load an empty script
122  // here:
123  ::GameScript.LoadData("Script.c", "", nullptr);
124 
125  const char* parse_error = c4_log_handle_get_first_log_message();
126  if(parse_error)
127  throw std::runtime_error(parse_error);
128 
129  // Link script engine (resolve includes/appends, generate code)
133  throw std::runtime_error(c4_log_handle_get_first_log_message());
134 
135  // Generate map, fail if return error occurs
137  std::unique_ptr<CSurface8> out_ptr_fg, out_ptr_bg;
138  const bool result = ::MapScript.InitializeMap(
139  &landscape,
140  HANDLE_TO_TEXTURE_MAP(texture_map),
141  HANDLE_TO_MATERIAL_MAP(material_map),
142  1,
143  &out_ptr_fg, &out_ptr_bg);
144 
145  // Don't show any map if there was a script runtime error
146  const char* runtime_error = c4_log_handle_get_first_log_message();
147  if(runtime_error)
148  throw std::runtime_error(runtime_error);
149 
150  if(!result)
151  throw std::runtime_error("No InitializeMap() function present in the script, or it returns false");
152 
153  C4MapgenHandle* handle = new C4MapgenHandle;
154  handle->width = out_ptr_fg->Wdt;
155  handle->height = out_ptr_fg->Hgt;
156  handle->rowstride = out_ptr_fg->Wdt;
157  handle->error_message = nullptr;
158  handle->data = out_ptr_fg->Bits;
159  out_ptr_fg->ReleaseBuffer();
160 
161  return handle;
162  }
163  catch(const std::exception& ex)
164  {
165  C4MapgenHandle* handle = new C4MapgenHandle;
166  handle->width = 0;
167  handle->height = 0;
168  handle->error_message.Copy(ex.what());
169  handle->data = nullptr;
170  return handle;
171  }
172 }
bool LoadData(const char *, const char *, C4LangStringTable *) override
bool InitializeMap(C4SLandscape *pLandscape, C4TextureMap *pTexMap, C4MaterialMap *pMatMap, uint32_t iPlayerCount, std::unique_ptr< CSurface8 > *pmap_fg_surface, std::unique_ptr< CSurface8 > *pmap_bg_surface)
bool LoadData(const char *, const char *, C4LangStringTable *) override

References c4_log_handle_clear(), c4_log_handle_get_first_log_message(), c4_log_handle_get_n_log_messages(), c4_mapgen_handle_deinit_script_engine(), c4_mapgen_handle_init_script_engine(), C4SLandscape::Default(), Definitions, GameScript, HANDLE_TO_MATERIAL_MAP, HANDLE_TO_TEXTURE_MAP, C4MapScriptHost::InitializeMap(), C4AulScriptEngine::Link(), C4MapScriptHost::LoadData(), C4GameScriptHost::LoadData(), C4SLandscape::MapHgt, C4SLandscape::MapPlayerExtend, MapScript, C4SLandscape::MapWdt, ScriptEngine, C4SVal::Set(), and _C4MapgenHandle::width.

Referenced by mape_mapgen_render().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ c4_mapgen_handle_set_map_library()

void c4_mapgen_handle_set_map_library ( C4GroupHandle group_handle)

Definition at line 78 of file mapgen-handle.cpp.

79 {
81 
82  C4Def* libmap = new C4Def;
83  libmap->id = C4ID(std::string("Library_Map"));
84  libmap->SetName(libmap->id.ToString());
85  libmap->Category = C4D_StaticBack;
86  FakeSkeletonLoader loader;
87  if(!libmap->Load(*HANDLE_TO_GROUP(group_handle), loader, C4D_Load_Script, nullptr, nullptr))
88  {
89  fprintf(stderr, "Failed to load Library_Map script\n");
90  delete libmap;
91  }
92  else
93  {
94  ::Definitions.Add(libmap, false);
95  }
96 }
const DWORD C4D_Load_Script
Definition: C4Def.h:83
const int32_t C4D_StaticBack
Definition: C4Def.h:40
Definition: C4Def.h:99
bool Load(C4Group &hGroup, StdMeshSkeletonLoader &loader, DWORD dwLoadWhat, const char *szLanguage, class C4SoundSystem *pSoundSystem=nullptr, C4DefGraphicsPtrBackup *gfx_backup=nullptr)
C4ID id
Definition: C4Def.h:101
int32_t Category
Definition: C4Def.h:117
void Clear()
bool Add(C4Def *ndef, bool fOverload)
Definition: C4Id.h:26
const char * ToString() const
Definition: C4Id.h:56
virtual void SetName(const char *NewName=nullptr)
Definition: C4PropList.cpp:625
#define HANDLE_TO_GROUP(handle)

References C4DefList::Add(), C4D_Load_Script, C4D_StaticBack, C4Def::Category, C4DefList::Clear(), Definitions, HANDLE_TO_GROUP, C4Def::id, C4Def::Load(), C4PropList::SetName(), and C4ID::ToString().

Referenced by mape_mapgen_set_root_group().

Here is the call graph for this function:
Here is the caller graph for this function: