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 template<>
18 archetype *asset_create(const std::string& name) {
19  auto arch = get_archetype_struct();
20  CLEAR_FLAG(&arch->clone, FLAG_REMOVED);
21  arch->name = add_string(name.c_str());
22  return arch;
23 }
24 
25 template<>
27  object_free_inventory(&item->clone);
28  free_arch(item);
29 }
30 
31 void Archetypes::recursive_update(object *item, archetype *updated) {
32  if (!item)
33  return;
34  if (item->arch->name == updated->name) {
36  auto diff = stringbuffer_new();
37  get_ob_diff(diff, item, &item->arch->clone);
38  auto arch = item->arch;
39  object_copy_with_inv(&updated->clone, item, false);
40  item->arch = arch;
41  auto finished = stringbuffer_finish(diff);
42  set_variable(item, finished);
43  free(finished);
44  }
45  recursive_update(item->below, updated);
46  recursive_update(item->inv, updated);
47 }
48 
53 static void mark_inv_not_removed(object *item) {
54  auto inv = item->inv;
55  while (inv) {
58  inv = inv->below;
59  }
60 }
61 
62 void Archetypes::replace(archetype *existing, archetype *update) {
63  for (auto arch : m_assets) {
64  recursive_update(arch.second->clone.inv, update);
65  }
66  if (m_updateListener) {
67  m_updateListener(existing, update);
68  }
69 
70  // We need to mark items as not removed before calling object_free_inventory().
71  // The flag is set at object initialisation but never cleared since an
72  // archetype's object is never inserted anywhere.
73  mark_inv_not_removed(&existing->clone);
74  object_free_inventory(&existing->clone);
75  object_copy_with_inv(&update->clone, &existing->clone, false);
76  existing->clone.arch = existing;
77  mark_inv_not_removed(&update->clone);
78  object_free_inventory(&update->clone);
79  free_arch(update);
80 }
81 
83  if (name == NULL)
84  return (archetype *)NULL;
85  auto tmp = find_string(name);
86  if (!tmp)
87  return NULL;
88 
89  for (auto arch : m_assets) {
90  if (arch.second->clone.name == tmp) {
91  return arch.second;
92  }
93  }
94  return NULL;
95 }
96 
98  if (name == NULL)
99  return NULL;
100 
101  for (auto arch : m_assets) {
102  if (arch.second->clone.type == type && strcmp(arch.second->clone.name, name) == 0)
103  return arch.second;
104  }
105 
106  return NULL;
107 }
108 
110  if (skill == NULL)
111  return NULL;
112 
113  for (auto arch : m_assets) {
114  if (((type == -1) || (type == arch.second->clone.type))
115  && (arch.second->clone.skill) && (!strcmp(arch.second->clone.skill, skill)))
116  return arch.second;
117  }
118  return NULL;
119 }
120 
122  for (auto arch : m_assets) {
123  if (((type == -1) || (type == arch.second->clone.type))
124  && (subtype == -1 || subtype == arch.second->clone.subtype))
125  return arch.second;
126  }
127  return NULL;
128 }
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
get_archetype_struct
archetype * get_archetype_struct(void)
Definition: arch.cpp:195
object::arch
struct archetype * arch
Definition: object.h:424
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
commongive.inv
inv
Definition: commongive.py:29
Ice.tmp
int tmp
Definition: Ice.py:207
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
asset_destroy
void asset_destroy(archetype *item)
Definition: Archetypes.cpp:26
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
asset_create
archetype * asset_create(const std::string &name)
Definition: Archetypes.cpp:18
archetype::clone
object clone
Definition: object.h:487
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
Archetypes::recursive_update
static void recursive_update(object *item, archetype *updated)
Definition: Archetypes.cpp:31
find_string
sstring find_string(const char *str)
Definition: shstr.cpp:236
Archetypes::findBySkillNameAndType
archetype * findBySkillNameAndType(const char *skill, int type)
Definition: Archetypes.cpp:109
archetype
Definition: object.h:483
mark_inv_not_removed
static void mark_inv_not_removed(object *item)
Definition: Archetypes.cpp:53
Archetypes::findByTypeSubtype
archetype * findByTypeSubtype(int type, int subtype)
Definition: Archetypes.cpp:121
FLAG_REMOVED
#define FLAG_REMOVED
Definition: define.h:232
item
Definition: item.py:1
set_variable
int set_variable(object *op, const char *buf)
Definition: loader.cpp:39047
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
Archetypes.h
get_ob_diff
void get_ob_diff(StringBuffer *sb, const object *op, const object *op2)
Definition: object.cpp:4986
Archetypes::replace
virtual void replace(archetype *existing, archetype *update) override
Definition: Archetypes.cpp:62
Archetypes::findByObjectName
archetype * findByObjectName(const char *name)
Definition: Archetypes.cpp:82
AssetsCollection< archetype >::m_assets
std::unordered_map< std::string, archetype * > m_assets
Definition: AssetsCollection.h:190
archetype::name
sstring name
Definition: object.h:484
Archetypes::m_updateListener
updateListener m_updateListener
Definition: Archetypes.h:116
object_free_inventory
void object_free_inventory(object *ob)
Definition: object.cpp:1568
Archetypes::findByObjectTypeName
archetype * findByObjectTypeName(int type, const char *name)
Definition: Archetypes.cpp:97
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
give.name
name
Definition: give.py:27