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 #include "global.h"
29 
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "loader.h"
35 
36 #include <string>
37 #include <map>
38 #include "assets.h"
39 #include "AssetsManager.h"
40 
55 }
56 
68 }
69 
80 archetype *get_archetype_by_skill_name(const char *skill, int type) {
81  return getManager()->archetypes()->findBySkillNameAndType(skill, type);
82 }
83 
100  return getManager()->archetypes()->findByTypeSubtype(type, subtype);
101 }
102 
116 object *create_archetype_by_object_name(const char *name) {
117  archetype *at;
118  char tmpname[MAX_BUF];
119  size_t i;
120 
121  strncpy(tmpname, name, MAX_BUF-1);
122  tmpname[MAX_BUF-1] = 0;
123  for (i = strlen(tmpname); i > 0; i--) {
124  tmpname[i] = 0;
125  at = getManager()->archetypes()->findByObjectName(tmpname);
126  if (at != NULL) {
127  return arch_to_object(at);
128  }
129  }
130  return create_singularity(name);
131 }
132 
142  object_dump(&at->clone, sb);
143 }
144 
151  getManager()->archetypes()->each([] (const auto arch) {
152  StringBuffer *sb;
153  char *diff;
154 
155  sb = stringbuffer_new();
156  dump_arch(arch, sb);
157  diff = stringbuffer_finish(sb);
158  LOG(llevDebug, "%s\n", diff);
159  free(diff);
160  });
161 }
162 
169 void free_arch(archetype *at) {
170  if (at->name)
171  free_string(at->name);
172  if (at->clone.name)
173  free_string(at->clone.name);
174  if (at->clone.name_pl)
176  if (at->clone.title)
177  free_string(at->clone.title);
178  if (at->clone.race)
179  free_string(at->clone.race);
180  if (at->clone.slaying)
182  if (at->clone.msg)
183  free_string(at->clone.msg);
185  free(at);
186 }
187 
196  archetype *arch;
197 
198  arch = (archetype *)CALLOC(1, sizeof(archetype));
199  if (arch == NULL)
201  arch->name = NULL;
202  arch->clone.other_arch = NULL;
203  arch->clone.name = NULL;
204  arch->clone.name_pl = NULL;
205  arch->clone.title = NULL;
206  arch->clone.race = NULL;
207  arch->clone.slaying = NULL;
208  arch->clone.msg = NULL;
209  object_clear(&arch->clone); /* to initial state other also */
210  CLEAR_FLAG((&arch->clone), FLAG_FREED); /* This shouldn't matter, since object_copy() */
211  SET_FLAG((&arch->clone), FLAG_REMOVED); /* doesn't copy these flags... */
212  arch->head = NULL;
213  arch->more = NULL;
214  arch->clone.arch = arch;
215  return arch;
216 }
217 
229 object *arch_to_object(archetype *at) {
230  object *op;
231 
232  if (at == NULL) {
233  LOG(llevError, "Couldn't find archetype.\n");
234  return NULL;
235  }
236  op = object_new();
237  object_copy_with_inv(&at->clone, op, true);
238  op->arch = at;
239  return op;
240 }
241 
255 object *create_singularity(const char *name) {
256  object *op;
257  char buf[MAX_BUF];
258 
259  snprintf(buf, sizeof(buf), "%s (%s)", ARCH_SINGULARITY, name);
260  op = object_new();
261  op->arch = empty_archetype;
262  op->name = add_string(buf);
263  op->name_pl = add_string(buf);
265  return op;
266 }
267 
278 object *create_archetype(const char *name) {
279  archetype *at;
280 
281  at = try_find_archetype(name);
282  if (at == NULL)
283  return create_singularity(name);
284  return arch_to_object(at);
285 }
286 
299  object *op, *prev = NULL, *head = NULL;
300 
301  while (at) {
302  op = arch_to_object(at);
303  op->x = at->clone.x;
304  op->y = at->clone.y;
305  if (head)
306  op->head = head, prev->more = op;
307  if (!head)
308  head = op;
309  prev = op;
310  at = at->more;
311  }
312  return (head);
313 }
314 
315 /*** end of arch.cpp ***/
object::name_pl
sstring name_pl
Definition: object.h:323
global.h
llevError
@ llevError
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.cpp:58
SET_FLAG
#define SET_FLAG(xyz, p)
Definition: define.h:224
archetype::more
archetype * more
Definition: object.h:486
find_archetype_by_object_type_name
archetype * find_archetype_by_object_type_name(int type, const char *name)
Definition: arch.cpp:66
create_singularity
object * create_singularity(const char *name)
Definition: arch.cpp:255
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
AssetsManager.h
get_archetype_struct
archetype * get_archetype_struct(void)
Definition: arch.cpp:195
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
object::x
int16_t x
Definition: object.h:335
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)
Definition: object.cpp:1208
free_arch
void free_arch(archetype *at)
Definition: arch.cpp:169
object::title
sstring title
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
Definition: define.h:239
object_clear
void object_clear(object *op)
Definition: object.cpp:983
object::y
int16_t y
Definition: object.h:335
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
archetype::clone
object clone
Definition: object.h:487
object_dump
void object_dump(const object *op, StringBuffer *sb)
Definition: object.cpp:645
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
FLAG_FREED
#define FLAG_FREED
Definition: define.h:233
object_create_arch
object * object_create_arch(archetype *at)
Definition: arch.cpp:298
Archetypes::findBySkillNameAndType
archetype * findBySkillNameAndType(const char *skill, int type)
Definition: Archetypes.cpp:109
dump_arch
void dump_arch(archetype *at, StringBuffer *sb)
Definition: arch.cpp:141
archetype
Definition: object.h:483
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:158
object::race
sstring race
Definition: object.h:326
fatal
void fatal(enum fatal_error err)
Definition: utils.cpp:590
Archetypes::findByTypeSubtype
archetype * findByTypeSubtype(int type, int subtype)
Definition: Archetypes.cpp:121
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
MAX_BUF
#define MAX_BUF
Definition: define.h:35
object_new
object * object_new(void)
Definition: object.cpp:1273
create_archetype
object * create_archetype(const char *name)
Definition: arch.cpp:278
free_string
void free_string(sstring str)
Definition: shstr.cpp:280
StringBuffer
Definition: stringbuffer.cpp:25
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
object::slaying
sstring slaying
Definition: object.h:327
find_archetype_by_object_name
archetype * find_archetype_by_object_name(const char *name)
Definition: arch.cpp:53
object::name
sstring name
Definition: object.h:319
create_archetype_by_object_name
object * create_archetype_by_object_name(const char *name)
Definition: arch.cpp:116
give.op
op
Definition: give.py:33
object::msg
sstring msg
Definition: object.h:330
object_free_key_values
void object_free_key_values(object *op)
Definition: object.cpp:954
assets.h
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
arch_to_object
object * arch_to_object(archetype *at)
Definition: arch.cpp:229
ARCH_SINGULARITY
#define ARCH_SINGULARITY
Definition: object.h:588
loader.h
get_archetype_by_type_subtype
archetype * get_archetype_by_type_subtype(int type, int subtype)
Definition: arch.cpp:99
try_find_archetype
archetype * try_find_archetype(const char *name)
Definition: assets.cpp:270
Archetypes::findByObjectName
archetype * findByObjectName(const char *name)
Definition: Archetypes.cpp:82
empty_archetype
archetype * empty_archetype
Definition: init.cpp:119
archetype::name
sstring name
Definition: object.h:484
object::more
object * more
Definition: object.h:303
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
dump_all_archetypes
void dump_all_archetypes(void)
Definition: arch.cpp:150
Archetypes::findByObjectTypeName
archetype * findByObjectTypeName(int type, const char *name)
Definition: Archetypes.cpp:97
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:80