Crossfire Server, Trunk
Archetypes.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2020 the Crossfire Development Team
5  *
6  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
7  * welcome to redistribute it under certain conditions. For details, please
8  * see COPYING and LICENSE.
9  *
10  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
11  */
12 
13 #include "Archetypes.h"
14 
15 #include <string.h>
16 
17 archetype *Archetypes::create(const std::string& name) {
18  auto arch = get_archetype_struct();
19  CLEAR_FLAG(&arch->clone, FLAG_REMOVED);
20  arch->name = add_string(name.c_str());
21  return arch;
22 }
23 
25  object_free_inventory(&item->clone);
26  free_arch(item);
27 }
28 
29 void Archetypes::recursive_update(object *item, archetype *updated) {
30  if (!item)
31  return;
32  if (item->arch->name == updated->name) {
34  auto diff = stringbuffer_new();
35  get_ob_diff(diff, item, &item->arch->clone);
36  auto arch = item->arch;
37  object_copy_with_inv(&updated->clone, item);
38  item->arch = arch;
39  auto finished = stringbuffer_finish(diff);
40  set_variable(item, finished);
41  free(finished);
42  }
43  recursive_update(item->below, updated);
44  recursive_update(item->inv, updated);
45 }
46 
51 static void mark_inv_not_removed(object *item) {
52  auto inv = item->inv;
53  while (inv) {
56  inv = inv->below;
57  }
58 }
59 
60 void Archetypes::replace(archetype *existing, archetype *update) {
61  for (auto arch : m_assets) {
62  recursive_update(arch.second->clone.inv, update);
63  }
64  if (m_updateListener) {
65  m_updateListener(existing, update);
66  }
67 
68  // We need to mark items as not removed before calling object_free_inventory().
69  // The flag is set at object initialisation but never cleared since an
70  // archetype's object is never inserted anywhere.
71  mark_inv_not_removed(&existing->clone);
72  object_free_inventory(&existing->clone);
73  object_copy_with_inv(&update->clone, &existing->clone);
74  existing->clone.arch = existing;
75  mark_inv_not_removed(&update->clone);
76  object_free_inventory(&update->clone);
77  free_arch(update);
78 }
79 
81  if (name == NULL)
82  return (archetype *)NULL;
83  auto tmp = find_string(name);
84  if (!tmp)
85  return NULL;
86 
87  for (auto arch : m_assets) {
88  if (arch.second->clone.name == tmp) {
89  return arch.second;
90  }
91  }
92  return NULL;
93 }
94 
96  if (name == NULL)
97  return NULL;
98 
99  for (auto arch : m_assets) {
100  if (arch.second->clone.type == type && strcmp(arch.second->clone.name, name) == 0)
101  return arch.second;
102  }
103 
104  return NULL;
105 }
106 
108  if (skill == NULL)
109  return NULL;
110 
111  for (auto arch : m_assets) {
112  if (((type == -1) || (type == arch.second->clone.type))
113  && (arch.second->clone.skill) && (!strcmp(arch.second->clone.skill, skill)))
114  return arch.second;
115  }
116  return NULL;
117 }
118 
120  for (auto arch : m_assets) {
121  if (((type == -1) || (type == arch.second->clone.type))
122  && (subtype == -1 || subtype == arch.second->clone.subtype))
123  return arch.second;
124  }
125  return NULL;
126 }
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.c:57
Archetypes::create
virtual archetype * create(const std::string &name)
Definition: Archetypes.cpp:17
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
get_archetype_struct
archetype * get_archetype_struct(void)
Definition: arch.cpp:198
get_ob_diff
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.c:4952
commongive.inv
inv
Definition: commongive.py:28
Ice.tmp
int tmp
Definition: Ice.py:207
Archetypes::destroy
virtual void destroy(archetype *item)
Definition: Archetypes.cpp:24
free_arch
void free_arch(archetype *at)
Definition: arch.cpp:172
set_variable
int set_variable(object *op, const char *buf)
Definition: loader.c:5267
archt
Definition: object.h:468
object_free_inventory
void object_free_inventory(object *ob)
Definition: object.c:1541
object_copy_with_inv
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:1183
AssetsCollection< archetype >::m_assets
std::map< std::string, archetype * > m_assets
Definition: AssetsCollection.h:157
Archetypes::recursive_update
static void recursive_update(object *item, archetype *updated)
Definition: Archetypes.cpp:29
find_string
sstring find_string(const char *str)
Definition: shstr.c:236
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.c:76
Archetypes::findBySkillNameAndType
archetype * findBySkillNameAndType(const char *skill, int type)
Definition: Archetypes.cpp:107
mark_inv_not_removed
static void mark_inv_not_removed(object *item)
Definition: Archetypes.cpp:51
Archetypes::findByTypeSubtype
archetype * findByTypeSubtype(int type, int subtype)
Definition: Archetypes.cpp:119
obj::arch
struct archt * arch
Definition: object.h:416
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
archt::clone
object clone
Definition: object.h:472
item
Definition: item.py:1
Archetypes::replace
virtual void replace(archetype *existing, archetype *update)
Definition: Archetypes.cpp:60
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
Archetypes.h
Archetypes::findByObjectName
archetype * findByObjectName(const char *name)
Definition: Archetypes.cpp:80
archt::name
sstring name
Definition: object.h:469
Archetypes::m_updateListener
updateListener m_updateListener
Definition: Archetypes.h:122
Archetypes::findByObjectTypeName
archetype * findByObjectTypeName(int type, const char *name)
Definition: Archetypes.cpp:95
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
give.name
name
Definition: give.py:27