Crossfire Server, Trunk
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 
57 }
58 
70 }
71 
82 archetype *get_archetype_by_skill_name(const char *skill, int type) {
83  return getManager()->archetypes()->findBySkillNameAndType(skill, type);
84 }
85 
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 
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)
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)
185  if (at->clone.msg)
186  free_string(at->clone.msg);
188  free(at);
189 }
190 
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();
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);
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 
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 ***/
empty_archetype
EXTERN archetype * empty_archetype
Definition: global.h:142
global.h
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
object_free_key_values
void object_free_key_values(object *op)
Definition: object.c:947
llevError
@ llevError
Definition: logger.h:11
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
find_archetype_by_object_type_name
archetype * find_archetype_by_object_type_name(int type, const char *name)
Definition: arch.cpp:68
create_singularity
object * create_singularity(const char *name)
Definition: arch.cpp:258
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
obj::race
sstring race
Definition: object.h:319
get_next_archetype
archetype * get_next_archetype(archetype *current)
Definition: assets.cpp:274
AssetsManager.h
get_archetype_struct
archetype * get_archetype_struct(void)
Definition: arch.cpp:198
object_new
object * object_new(void)
Definition: object.c:1242
CALLOC
#define CALLOC(x, y)
Definition: compat.h:31
obj::msg
sstring msg
Definition: object.h:323
free_arch
void free_arch(archetype *at)
Definition: arch.cpp:172
getManager
AssetsManager * getManager()
Definition: assets.cpp:329
FLAG_NO_PICK
#define FLAG_NO_PICK
Definition: define.h:239
archt
Definition: object.h:468
obj::slaying
sstring slaying
Definition: object.h:320
free_string
void free_string(sstring str)
Definition: shstr.c:280
object_copy_with_inv
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:1183
obj::name
sstring name
Definition: object.h:312
object_dump
void object_dump(const object *op, StringBuffer *sb)
Definition: object.c:638
obj::name_pl
sstring name_pl
Definition: object.h:316
fatal
void fatal(enum fatal_error err)
Definition: utils.c:597
object_clear
void object_clear(object *op)
Definition: object.c:976
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
obj::x
int16_t x
Definition: object.h:328
object_create_arch
object * object_create_arch(archetype *at)
Definition: arch.cpp:301
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
Archetypes::findBySkillNameAndType
archetype * findBySkillNameAndType(const char *skill, int type)
Definition: Archetypes.cpp:107
archt::more
struct archt * more
Definition: object.h:471
dump_arch
void dump_arch(archetype *at, StringBuffer *sb)
Definition: arch.cpp:143
Archetypes::findByTypeSubtype
archetype * findByTypeSubtype(int type, int subtype)
Definition: Archetypes.cpp:119
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
MAX_BUF
#define MAX_BUF
Definition: define.h:35
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:281
StringBuffer
Definition: stringbuffer.c:25
obj::y
int16_t y
Definition: object.h:328
obj::title
sstring title
Definition: object.h:318
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
find_archetype_by_object_name
archetype * find_archetype_by_object_name(const char *name)
Definition: arch.cpp:55
archt::clone
object clone
Definition: object.h:472
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
create_archetype_by_object_name
object * create_archetype_by_object_name(const char *name)
Definition: arch.cpp:118
give.op
op
Definition: give.py:33
assets.h
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
buf
StringBuffer * buf
Definition: readable.c:1606
obj::more
struct obj * more
Definition: object.h:296
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:232
ARCH_SINGULARITY
#define ARCH_SINGULARITY
Definition: object.h:575
loader.h
get_archetype_by_type_subtype
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.cpp:101
try_find_archetype
archetype * try_find_archetype(const char *name)
Definition: assets.cpp:282
Archetypes::findByObjectName
archetype * findByObjectName(const char *name)
Definition: Archetypes.cpp:80
archt::name
sstring name
Definition: object.h:469
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
dump_all_archetypes
void dump_all_archetypes(void)
Definition: arch.cpp:152
Archetypes::findByObjectTypeName
archetype * findByObjectTypeName(int type, const char *name)
Definition: Archetypes.cpp:95
llevDebug
@ llevDebug
Definition: logger.h:13
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
give.name
name
Definition: give.py:27
get_archetype_by_skill_name
archetype * get_archetype_by_skill_name(const char *skill, int type)
Definition: arch.cpp:82