Crossfire Server, Trunk  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 
53 }
54 
66 }
67 
80 }
81 
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 ***/
object::name_pl
sstring name_pl
The plural name of the object.
Definition: object.h:323
global.h
llevError
@ llevError
Error, serious thing.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
archetype::more
archetype * more
Next part of a linked object.
Definition: object.h:486
find_archetype_by_object_type_name
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
create_singularity
object * create_singularity(const char *name)
Creates a dummy object.
Definition: arch.cpp:253
AssetsManager.h
get_archetype_struct
archetype * get_archetype_struct(void)
Allocates, initialises and returns the pointer to an archetype structure.
Definition: arch.cpp:193
object::arch
struct archetype * arch
Pointer to archetype.
Definition: object.h:424
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Create a new string buffer.
Definition: stringbuffer.cpp:57
object::x
int16_t x
Definition: object.h:335
archetype::head
archetype * head
The main part of a linked object.
Definition: object.h:485
CALLOC
#define CALLOC(x, y)
Definition: compat.h:31
object_copy_with_inv
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
free_arch
void free_arch(archetype *at)
Frees archetype.
Definition: arch.cpp:167
object::title
sstring title
Of foo, etc.
Definition: object.h:325
buf
StringBuffer * buf
Definition: readable.cpp:1565
getManager
AssetsManager * getManager()
Definition: assets.cpp:305
FLAG_NO_PICK
#define FLAG_NO_PICK
Object can't be picked up.
Definition: define.h:239
name
Plugin animator file specs[Config] name
Definition: animfiles.txt:4
object_clear
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::y
int16_t y
Position in the map for this object.
Definition: object.h:335
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Deallocate the string buffer instance and return the string.
Definition: stringbuffer.cpp:76
archetype::clone
object clone
An object from which to do object_copy()
Definition: object.h:487
object_dump
void object_dump(const object *op, StringBuffer *sb)
Dumps an object.
Definition: object.cpp:630
add_string
sstring add_string(const char *str)
This will add 'str' to the hash table.
Definition: shstr.cpp:124
FLAG_FREED
#define FLAG_FREED
Object is in the list of free objects.
Definition: define.h:233
object_create_arch
object * object_create_arch(archetype *at)
Create a full object using the given archetype.
Definition: arch.cpp:296
Archetypes::findBySkillNameAndType
archetype * findBySkillNameAndType(const char *skill, int type)
Retrieve an archetype by skill name and type.
Definition: Archetypes.cpp:109
dump_arch
void dump_arch(archetype *at, StringBuffer *sb)
Dumps an archetype to buffer.
Definition: arch.cpp:139
archetype
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:483
AssetsCollection::each
void each(std::function< void(T *)> op)
Apply a function to each asset.
Definition: AssetsCollection.h:158
object::race
sstring race
Human, goblin, dragon, etc.
Definition: object.h:326
object::other_arch
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
fatal
void fatal(enum fatal_error err)
fatal() is meant to be called whenever a fatal signal is intercepted.
Definition: utils.cpp:590
Archetypes::findByTypeSubtype
archetype * findByTypeSubtype(int type, int subtype)
Retrieve an archetype by type and subtype.
Definition: Archetypes.cpp:121
AssetsManager::archetypes
Archetypes * archetypes()
Get archetypes.
Definition: AssetsManager.h:44
MAX_BUF
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
object_new
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
object::head
object * head
Points to the main object of a large body.
Definition: object.h:304
create_archetype
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
free_string
void free_string(sstring str)
This will reduce the refcount, and if it has reached 0, str will be freed.
Definition: shstr.cpp:280
StringBuffer
A buffer that will be expanded as content is added to it.
Definition: stringbuffer.cpp:25
FLAG_REMOVED
#define FLAG_REMOVED
Object is not in any map or invenory.
Definition: define.h:232
object::slaying
sstring slaying
Which race to do double damage to.
Definition: object.h:327
find_archetype_by_object_name
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
object::name
sstring name
The name of the object, obviously...
Definition: object.h:319
create_archetype_by_object_name
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
object::msg
sstring msg
If this is a book/sign/magic mouth/etc.
Definition: object.h:330
object_free_key_values
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
assets.h
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
arch_to_object
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.cpp:227
ARCH_SINGULARITY
#define ARCH_SINGULARITY
Archetype for singularity.
Definition: object.h:588
loader.h
get_archetype_by_type_subtype
archetype * get_archetype_by_type_subtype(int type, int subtype)
Retrieves an archetype by type and subtype.
Definition: arch.cpp:97
skill
skill
Definition: arch-handbook.txt:585
try_find_archetype
archetype * try_find_archetype(const char *name)
Definition: assets.cpp:270
Archetypes::findByObjectName
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
empty_archetype
archetype * empty_archetype
Nice to have fast access to it.
Definition: init.cpp:119
archetype::name
sstring name
More definite name, like "generate_kobold".
Definition: object.h:484
object::more
object * more
Pointer to the rest of a large body of objects.
Definition: object.h:303
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
dump_all_archetypes
void dump_all_archetypes(void)
Dumps all archetypes to debug-level output.
Definition: arch.cpp:148
Archetypes::findByObjectTypeName
archetype * findByObjectTypeName(int type, const char *name)
Retrieve an archetype by type and name that appears during the game.
Definition: Archetypes.cpp:97
llevDebug
@ llevDebug
Only for debugging purposes.
Definition: logger.h:13
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
get_archetype_by_skill_name
archetype * get_archetype_by_skill_name(const char *skill, int type)
Retrieves an archetype by skill name and type.
Definition: arch.cpp:78