Crossfire Server, Trunk  R22047
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 extern "C" {
29 #include "global.h"
30 }
31 
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "loader.h"
37 
38 #include <string>
39 #include <map>
40 #include "assets.h"
41 #include "AssetsManager.h"
42 
55 archetype *find_archetype_by_object_name(const char *name) {
56  return getManager()->archetypes()->findByObjectName(name);
57 }
58 
68 archetype *find_archetype_by_object_type_name(int type, const char *name) {
69  return getManager()->archetypes()->findByObjectTypeName(type, name);
70 }
71 
82 archetype *get_archetype_by_skill_name(const char *skill, int type) {
83  return getManager()->archetypes()->findBySkillNameAndType(skill, type);
84 }
85 
101 archetype *get_archetype_by_type_subtype(int type, int subtype) {
102  return getManager()->archetypes()->findByTypeSubtype(type, subtype);
103 }
104 
118 object *create_archetype_by_object_name(const char *name) {
119  archetype *at;
120  char tmpname[MAX_BUF];
121  size_t i;
122 
123  strncpy(tmpname, name, MAX_BUF-1);
124  tmpname[MAX_BUF-1] = 0;
125  for (i = strlen(tmpname); i > 0; i--) {
126  tmpname[i] = 0;
127  at = getManager()->archetypes()->findByObjectName(tmpname);
128  if (at != NULL) {
129  return arch_to_object(at);
130  }
131  }
132  return create_singularity(name);
133 }
134 
143 void dump_arch(archetype *at, StringBuffer *sb) {
144  object_dump(&at->clone, sb);
145 }
146 
153  for (auto arch = get_next_archetype(NULL); arch != NULL; arch = get_next_archetype(arch)) {
154 
155  StringBuffer *sb;
156  char *diff;
157 
158  sb = stringbuffer_new();
159  dump_arch(arch, sb);
160  diff = stringbuffer_finish(sb);
161  LOG(llevDebug, "%s\n", diff);
162  free(diff);
163  }
164 }
165 
172 void free_arch(archetype *at) {
173  if (at->name)
174  free_string(at->name);
175  if (at->clone.name)
176  free_string(at->clone.name);
177  if (at->clone.name_pl)
178  free_string(at->clone.name_pl);
179  if (at->clone.title)
180  free_string(at->clone.title);
181  if (at->clone.race)
182  free_string(at->clone.race);
183  if (at->clone.slaying)
184  free_string(at->clone.slaying);
185  if (at->clone.msg)
186  free_string(at->clone.msg);
187  object_free_key_values(&at->clone);
188  free(at);
189 }
190 
198 archetype *get_archetype_struct(void) {
199  archetype *arch;
200 
201  arch = (archetype *)CALLOC(1, sizeof(archetype));
202  if (arch == NULL)
204  arch->name = NULL;
205  arch->clone.other_arch = NULL;
206  arch->clone.name = NULL;
207  arch->clone.name_pl = NULL;
208  arch->clone.title = NULL;
209  arch->clone.race = NULL;
210  arch->clone.slaying = NULL;
211  arch->clone.msg = NULL;
212  object_clear(&arch->clone); /* to initial state other also */
213  CLEAR_FLAG((&arch->clone), FLAG_FREED); /* This shouldn't matter, since object_copy() */
214  SET_FLAG((&arch->clone), FLAG_REMOVED); /* doesn't copy these flags... */
215  arch->head = NULL;
216  arch->more = NULL;
217  arch->clone.arch = arch;
218  return arch;
219 }
220 
232 object *arch_to_object(archetype *at) {
233  object *op;
234 
235  if (at == NULL) {
236  LOG(llevError, "Couldn't find archetype.\n");
237  return NULL;
238  }
239  op = object_new();
240  object_copy_with_inv(&at->clone, op);
241  op->arch = at;
242  return op;
243 }
244 
258 object *create_singularity(const char *name) {
259  object *op;
260  char buf[MAX_BUF];
261 
262  snprintf(buf, sizeof(buf), "%s (%s)", ARCH_SINGULARITY, name);
263  op = object_new();
264  op->arch = empty_archetype;
265  op->name = add_string(buf);
266  op->name_pl = add_string(buf);
267  SET_FLAG(op, FLAG_NO_PICK);
268  return op;
269 }
270 
281 object *create_archetype(const char *name) {
282  archetype *at;
283 
284  at = try_find_archetype(name);
285  if (at == NULL)
286  return create_singularity(name);
287  return arch_to_object(at);
288 }
289 
301 object *object_create_arch(archetype *at) {
302  object *op, *prev = NULL, *head = NULL;
303 
304  while (at) {
305  op = arch_to_object(at);
306  op->x = at->clone.x;
307  op->y = at->clone.y;
308  if (head)
309  op->head = head, prev->more = op;
310  if (!head)
311  head = op;
312  prev = op;
313  at = at->more;
314  }
315  return (head);
316 }
317 
318 /*** end of arch.cpp ***/
archetype * findBySkillNameAndType(const char *skill, int type)
Definition: Archetypes.cpp:107
#define SET_FLAG(xyz, p)
Definition: define.h:223
StringBuffer * buf
Definition: readable.c:1591
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:952
void fatal(enum fatal_error err)
Definition: utils.c:597
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
void free_string(sstring str)
Definition: shstr.c:280
archetype * findByObjectName(const char *name)
Definition: Archetypes.cpp:80
void dump_all_archetypes(void)
Definition: arch.cpp:152
archetype * try_find_archetype(const char *name)
Definition: assets.cpp:257
#define FLAG_REMOVED
Definition: define.h:232
#define CALLOC(x, y)
Definition: compat.h:27
archetype * find_archetype_by_object_type_name(int type, const char *name)
Definition: arch.cpp:68
AssetsManager * getManager()
Definition: assets.cpp:304
void object_dump(const object *op, StringBuffer *sb)
Definition: object.c:415
EXTERN archetype * empty_archetype
Definition: global.h:143
object * object_new(void)
Definition: object.c:1011
archetype * findByObjectTypeName(int type, const char *name)
Definition: Archetypes.cpp:95
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
object * create_archetype(const char *name)
Definition: arch.cpp:281
#define snprintf
Definition: win32.h:46
Archetypes * archetypes()
Definition: AssetsManager.h:43
object * create_archetype_by_object_name(const char *name)
Definition: arch.cpp:118
archetype * get_next_archetype(archetype *current)
Definition: assets.cpp:249
void object_clear(object *op)
Definition: object.c:747
#define CLEAR_FLAG(xyz, p)
Definition: define.h:224
#define MAX_BUF
Definition: define.h:35
archetype * get_archetype_by_skill_name(const char *skill, int type)
Definition: arch.cpp:82
object * create_singularity(const char *name)
Definition: arch.cpp:258
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.cpp:101
object * object_create_arch(archetype *at)
Definition: arch.cpp:301
archetype * get_archetype_struct(void)
Definition: arch.cpp:198
void dump_arch(archetype *at, StringBuffer *sb)
Definition: arch.cpp:143
void free_arch(archetype *at)
Definition: arch.cpp:172
sstring add_string(const char *str)
Definition: shstr.c:124
archetype * find_archetype_by_object_name(const char *name)
Definition: arch.cpp:55
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
#define FLAG_NO_PICK
Definition: define.h:239
void object_free_key_values(object *op)
Definition: object.c:718
archetype * findByTypeSubtype(int type, int subtype)
Definition: Archetypes.cpp:119
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
#define FLAG_FREED
Definition: define.h:233