Crossfire Server  1.75.0
arch.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
28 #include "global.h"
29 
30 #include <ctype.h>
31 #include <stdlib.h>
32 
33 #include "loader.h"
34 
35 #include <map>
36 #include "assets.h"
37 #include "AssetsManager.h"
38 
52  return getManager()->archetypes()->findByObjectName(name);
53 }
54 
64 archetype *find_archetype_by_object_type_name(int type, const char *name) {
65  return getManager()->archetypes()->findByObjectTypeName(type, name);
66 }
67 
78 archetype *get_archetype_by_skill_name(const char *skill, int type) {
79  return getManager()->archetypes()->findBySkillNameAndType(skill, type);
80 }
81 
97 archetype *get_archetype_by_type_subtype(int type, int subtype) {
98  return getManager()->archetypes()->findByTypeSubtype(type, subtype);
99 }
100 
114 object *create_archetype_by_object_name(const char *name) {
115  archetype *at;
116  char tmpname[MAX_BUF];
117  size_t i;
118 
119  strncpy(tmpname, name, MAX_BUF-1);
120  tmpname[MAX_BUF-1] = 0;
121  for (i = strlen(tmpname); i > 0; i--) {
122  tmpname[i] = 0;
123  at = getManager()->archetypes()->findByObjectName(tmpname);
124  if (at != NULL) {
125  return arch_to_object(at);
126  }
127  }
128  return create_singularity(name);
129 }
130 
140  object_dump(&at->clone, sb);
141 }
142 
149  getManager()->archetypes()->each([] (const auto arch) {
150  StringBuffer *sb;
151  char *diff;
152 
153  sb = stringbuffer_new();
154  dump_arch(arch, sb);
155  diff = stringbuffer_finish(sb);
156  LOG(llevDebug, "%s\n", diff);
157  free(diff);
158  });
159 }
160 
167 void free_arch(archetype *at) {
168  if (at->name)
169  free_string(at->name);
170  if (at->clone.name)
171  free_string(at->clone.name);
172  if (at->clone.name_pl)
174  if (at->clone.title)
175  free_string(at->clone.title);
176  if (at->clone.race)
177  free_string(at->clone.race);
178  if (at->clone.slaying)
180  if (at->clone.msg)
181  free_string(at->clone.msg);
183  free(at);
184 }
185 
194  archetype *arch;
195 
196  arch = (archetype *)CALLOC(1, sizeof(archetype));
197  if (arch == NULL)
199  arch->name = NULL;
200  arch->clone.other_arch = NULL;
201  arch->clone.name = NULL;
202  arch->clone.name_pl = NULL;
203  arch->clone.title = NULL;
204  arch->clone.race = NULL;
205  arch->clone.slaying = NULL;
206  arch->clone.msg = NULL;
207  object_clear(&arch->clone); /* to initial state other also */
208  CLEAR_FLAG((&arch->clone), FLAG_FREED); /* This shouldn't matter, since object_copy() */
209  SET_FLAG((&arch->clone), FLAG_REMOVED); /* doesn't copy these flags... */
210  arch->head = NULL;
211  arch->more = NULL;
212  arch->clone.arch = arch;
213  return arch;
214 }
215 
227 object *arch_to_object(archetype *at) {
228  object *op;
229 
230  if (at == NULL) {
231  LOG(llevError, "Couldn't find archetype.\n");
232  return NULL;
233  }
234  op = object_new();
235  object_copy_with_inv(&at->clone, op, true);
236  op->arch = at;
237  return op;
238 }
239 
253 object *create_singularity(const char *name) {
254  object *op;
255  char buf[MAX_BUF];
256 
257  snprintf(buf, sizeof(buf), "%s (%s)", ARCH_SINGULARITY, name);
258  op = object_new();
259  op->arch = empty_archetype;
260  op->name = add_string(buf);
261  op->name_pl = add_string(buf);
262  SET_FLAG(op, FLAG_NO_PICK);
263  return op;
264 }
265 
276 object *create_archetype(const char *name) {
277  archetype *at;
278 
279  at = try_find_archetype(name);
280  if (at == NULL)
281  return create_singularity(name);
282  return arch_to_object(at);
283 }
284 
297  object *op, *prev = NULL, *head = NULL;
298 
299  while (at) {
300  op = arch_to_object(at);
301  op->x = at->clone.x;
302  op->y = at->clone.y;
303  if (head)
304  op->head = head, prev->more = op;
305  if (!head)
306  head = op;
307  prev = op;
308  at = at->more;
309  }
310  return (head);
311 }
312 
313 /*** end of arch.cpp ***/
Error, serious thing.
Definition: logger.h:11
#define ARCH_SINGULARITY
Archetype for singularity.
Definition: object.h:590
void object_free_key_values(object *op)
Zero the key_values on op, decrementing the shared-string refcounts and freeing the links...
Definition: object.cpp:939
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
struct archetype * arch
Pointer to archetype.
Definition: object.h:424
archetype * findBySkillNameAndType(const char *skill, int type)
Retrieve an archetype by skill name and type.
Definition: Archetypes.cpp:109
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
object * more
Pointer to the rest of a large body of objects.
Definition: object.h:303
sstring slaying
Which race to do double damage to.
Definition: object.h:327
sstring add_string(const char *str)
Share a string.
Definition: shstr.cpp:137
archetype * findByObjectName(const char *name)
Retrieve an archetype given the name that appears during the game (for example, "writing pen" instead...
Definition: Archetypes.cpp:82
int16_t y
Position in the map for this object.
Definition: object.h:335
void dump_all_archetypes(void)
Dumps all archetypes to debug-level output.
Definition: arch.cpp:148
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
int16_t x
Definition: object.h:335
Global type definitions and header inclusions.
archetype * try_find_archetype(const char *name)
Definition: assets.cpp:274
#define FLAG_REMOVED
Object is not in any map or invenory.
Definition: define.h:219
#define CALLOC(x, y)
Definition: compat.h:31
archetype * head
The main part of a linked object.
Definition: object.h:485
archetype * find_archetype_by_object_type_name(int type, const char *name)
This function retrieves an archetype by type and name that appears during the game.
Definition: arch.cpp:64
AssetsManager * getManager()
Definition: assets.cpp:309
void free_string(sstring str)
This will reduce the refcount, and if it has reached 0, str will be freed.
Definition: shstr.cpp:294
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:483
archetype * findByObjectTypeName(int type, const char *name)
Retrieve an archetype by type and name that appears during the game.
Definition: Archetypes.cpp:97
sstring name
More definite name, like "generate_kobold".
Definition: object.h:484
Defines for loader.l / loader.c.
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.cpp:227
object * create_archetype(const char *name)
Finds which archetype matches the given name, and returns a new object containing a copy of the arche...
Definition: arch.cpp:276
#define SET_FLAG(xyz, p)
Definition: define.h:384
sstring msg
If this is a book/sign/magic mouth/etc.
Definition: object.h:330
object * head
Points to the main object of a large body.
Definition: object.h:304
sstring race
Human, goblin, dragon, etc.
Definition: object.h:326
void object_dump(const object *op, StringBuffer *sb)
Dumps an object.
Definition: object.cpp:630
Archetypes * archetypes()
Get archetypes.
Definition: AssetsManager.h:44
object * create_archetype_by_object_name(const char *name)
Creates an object given the name that appears during the game (for example, "writing pen" instead of ...
Definition: arch.cpp:114
void object_copy_with_inv(const object *src_ob, object *dest_ob, bool update_speed)
Copy an object with an inventory, duplicate the inv too.
Definition: object.cpp:1193
object * object_new(void)
Grabs an object from the list of unused objects, makes sure it is initialised, and returns it...
Definition: object.cpp:1258
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.cpp:590
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
archetype * get_archetype_by_skill_name(const char *skill, int type)
Retrieves an archetype by skill name and type.
Definition: arch.cpp:78
object * create_singularity(const char *name)
Creates a dummy object.
Definition: arch.cpp:253
archetype * get_archetype_by_type_subtype(int type, int subtype)
Retrieves an archetype by type and subtype.
Definition: arch.cpp:97
object * object_create_arch(archetype *at)
Create a full object using the given archetype.
Definition: arch.cpp:296
struct archetype * other_arch
Pointer used for various things - mostly used for what this objects turns into or what this object cr...
Definition: object.h:425
sstring name_pl
The plural name of the object.
Definition: object.h:323
sstring name
The name of the object, obviously...
Definition: object.h:319
sstring title
Of foo, etc.
Definition: object.h:325
archetype * get_archetype_struct(void)
Allocates, initialises and returns the pointer to an archetype structure.
Definition: arch.cpp:193
Only for debugging purposes.
Definition: logger.h:13
archetype * empty_archetype
Nice to have fast access to it.
Definition: init.cpp:119
#define CLEAR_FLAG(xyz, p)
Definition: define.h:385
void dump_arch(archetype *at, StringBuffer *sb)
Dumps an archetype to buffer.
Definition: arch.cpp:139
void free_arch(archetype *at)
Frees archetype.
Definition: arch.cpp:167
archetype * find_archetype_by_object_name(const char *name)
This function retrieves an archetype given the name that appears during the game (for example...
Definition: arch.cpp:51
C function wrappers to interact with assets.
archetype * more
Next part of a linked object.
Definition: object.h:486
StringBuffer * buf
Definition: readable.cpp:1563
A buffer that will be expanded as content is added to it.
#define FLAG_NO_PICK
Object can&#39;t be picked up.
Definition: define.h:226
archetype * findByTypeSubtype(int type, int subtype)
Retrieve an archetype by type and subtype.
Definition: Archetypes.cpp:121
void object_clear(object *op)
Frees everything allocated by an object, and also clears all variables and flags to default settings...
Definition: object.cpp:968
object clone
An object from which to do object_copy()
Definition: object.h:487
#define FLAG_FREED
Object is in the list of free objects.
Definition: define.h:220
void each(std::function< void(T *)> op)
Apply a function to each asset.