Crossfire Server, Trunk
ResourcesManager.cpp
Go to the documentation of this file.
1 #include <QString>
2 #include <qlist.h>
3 #include <qhash.h>
4 #include <QStringList>
5 #include <QMessageBox>
6 #include "ResourcesManager.h"
7 #include <locale.h>
8 
9 extern "C" {
10 #include "global.h"
11 #include "libproto.h"
12 #include "recipe.h"
13 #include "image.h"
14 #include "sproto.h"
15 }
16 #include "assets.h"
17 #include "AssetsManager.h"
19 #include "CRERandomMap.h"
20 #include "LicenseManager.h"
21 #include "ArchetypeWriter.h"
22 #include "QuestWriter.h"
23 
24 ResourcesManager::ResourcesManager() : myArchetypes(new ArchetypeWriter()), myQuests(new QuestWriter())
25 {
26 }
27 
29 {
30 }
31 
32 static void onFatalInit(enum fatal_error) {
33  QMessageBox::critical(nullptr, "Fatal error", "Error while initializing Crossfire data, make sure you have maps and archetypes correctly installed.");
34 }
35 
37 {
38  setlocale(LC_NUMERIC, "C");
39 
46  init_globals();
47  init_library();
48  settings.fatal_hook = nullptr;
49  init_gods();
50  init_readable();
51 
52  QString key;
53 
54  for (int ing = 1; ; ing++)
55  {
57  if (!list)
58  break;
59 
60  QHash<QString, recipestruct*> recipes;
61  for (recipestruct* rec = list->items; rec; rec = rec->next)
62  {
63  key = QString("%1_%2").arg(rec->arch_name[0], rec->title);
64  recipes[key] = rec;
65  }
66  myRecipes.append(recipes);
67  }
68 }
69 
71 {
72  return myRecipes.size();
73 }
74 
75 QStringList ResourcesManager::recipes(int count) const
76 {
77  if (count < 1 || count > myRecipes.size())
78  return QStringList();
79 
80  QStringList keys = myRecipes[count - 1].keys();
81  qSort(keys);
82  return keys;
83 }
84 
85 const recipe* ResourcesManager::recipe(int ingredients, const QString& name) const
86 {
87  if (ingredients < 1 || ingredients > myRecipes.size())
88  return NULL;
89 
90  return myRecipes[ingredients - 1][name];
91 }
92 
94 {
95  bool goOn = true;
96  getManager()->archetypes()->each([&item, &callback, &goOn] (archt *arch) {
97  if (!goOn)
98  return;
99 
100  if (arch->clone.other_arch == item)
101  {
102  goOn = callback(OTHER_ARCH, arch, nullptr, nullptr, nullptr);
103  }
104 
105  sstring death_anim = NULL;
106  if (goOn && (death_anim = object_get_value(&arch->clone, "death_animation")) && strcmp(death_anim, item->name) == 0)
107  {
108  goOn = callback(DEATH_ANIM, arch, nullptr, nullptr, nullptr);
109  }
110  });
111 
112  getManager()->treasures()->each([&item, callback, &goOn] (treasurelist *list) {
113  if (!goOn)
114  return;
115  for (auto t = list->items; t; t = t->next)
116  {
117  if (t->item == item)
118  {
119  goOn = callback(TREASURE_USE, nullptr, list, nullptr, nullptr);
120  }
121  }
122  });
123 
124  QList<CREMapInformation*> mapuse = store->getArchetypeUse(item);
125  foreach(CREMapInformation* information, mapuse)
126  {
127  if (!goOn)
128  continue;
129  goOn = callback(MAP_USE, nullptr, nullptr, information, nullptr);
130  }
131  auto allMaps = store->allMaps();
132  foreach(CREMapInformation *information, allMaps)
133  {
134  if (!goOn)
135  return;
136  foreach(CRERandomMap* rm, information->randomMaps())
137  {
138  if (strcmp(item->name, rm->parameters()->final_exit_archetype) == 0)
139  {
140  goOn = callback(RANDOM_MAP_FINAL_EXIT, nullptr, nullptr, information, nullptr);
141  }
142  if (!goOn)
143  return;
144  }
145  }
146 
147  int count = 1;
148  recipelist* list;
149  while ((list = get_formulalist(count++)))
150  {
151  if (!goOn)
152  break;
153  recipestruct* rec = list->items;
154  while (goOn && rec)
155  {
156  for (size_t ing = 0; ing < rec->arch_names; ing++)
157  {
158  if (strcmp(rec->arch_name[ing], item->name) == 0)
159  {
160  goOn = callback(ALCHEMY_PRODUCT, nullptr, nullptr, nullptr, rec);
161  break;
162  }
163  }
164  rec = rec->next;
165  }
166  }
167 }
168 
170  myArchetypes.assetModified(arch);
171 }
172 
174  myArchetypes.saveModifiedAssets();
175 }
176 
179 }
180 
183 }
AssetUseCallback
std::function< bool(ArchetypeUse use, const archt *, const treasurelist *, const CREMapInformation *, recipe *)> AssetUseCallback
Definition: ResourcesManager.h:26
recipestruct::arch_names
size_t arch_names
Definition: recipe.h:12
ResourcesManager::myQuests
ModifiedAssetsManager< quest_definition > myQuests
Definition: ResourcesManager.h:64
CREMapInformation::randomMaps
QList< CRERandomMap * > randomMaps() const
Definition: CREMapInformation.cpp:268
global.h
get_formulalist
recipelist * get_formulalist(int i)
Definition: recipe.c:96
Settings::hooks_count
uint8_t hooks_count
Definition: global.h:328
ArchetypeWriter.h
ResourcesManager::ResourcesManager
ResourcesManager()
Definition: ResourcesManager.cpp:24
Settings::archetypes_tracker
void * archetypes_tracker
Definition: global.h:332
ArchetypeWriter
Definition: ArchetypeWriter.h:22
ResourcesManager::archetypeUse
static void archetypeUse(const archt *item, CREMapInformationManager *store, AssetUseCallback callback)
Definition: ResourcesManager.cpp:93
archininventory.arch
arch
DIALOGCHECK MINARGS 1 MAXARGS 1
Definition: archininventory.py:16
ResourcesManager::saveArchetypes
void saveArchetypes()
Definition: ResourcesManager.cpp:173
AssetsManager.h
ResourcesManager::questModified
void questModified(quest_definition *quest)
Definition: ResourcesManager.cpp:177
QuestWriter.h
guildoracle.list
list
Definition: guildoracle.py:87
CRERandomMap::parameters
const RMParms * parameters() const
Definition: CRERandomMap.cpp:27
recipe.h
CRERandomMap.h
hall_of_fame.keys
keys
Definition: hall_of_fame.py:43
ResourcesManager::recipeMaxIngredients
int recipeMaxIngredients() const
Definition: ResourcesManager.cpp:70
ResourcesManager::recipe
const recipestruct * recipe(int ingredients, const QString &name) const
Definition: ResourcesManager.cpp:85
ResourcesManager::saveQuests
void saveQuests()
Definition: ResourcesManager.cpp:181
add_server_collect_hooks
void add_server_collect_hooks()
Definition: init.c:1092
Settings::fatal_hook
fatalHook fatal_hook
Definition: global.h:333
getManager
AssetsManager * getManager()
Definition: assets.cpp:329
LicenseManager.h
archt
Definition: object.h:468
settings
struct Settings settings
Definition: init.c:39
init_readable
void init_readable(void)
Definition: readable.c:899
object_get_value
const char * object_get_value(const object *op, const char *const key)
Definition: object.c:4354
quest
Definition: quest.py:1
CREMapInformation
Definition: CREMapInformation.h:13
recipestruct
Definition: recipe.h:10
AssetsTracker
Definition: AssetsTracker.h:28
ModifiedAssetsManager::saveModifiedAssets
void saveModifiedAssets()
Definition: ModifiedAssetsManager.h:47
AssetsManager::treasures
Treasures * treasures()
Definition: AssetsManager.h:54
ResourcesManager::load
void load()
Definition: ResourcesManager.cpp:36
recipestruct::arch_name
char ** arch_name
Definition: recipe.h:13
init_gods
void init_gods(void)
Definition: holy.c:86
nlohmann::detail::cbor_tag_handler_t::store
@ store
store tags as binary type
ResourcesManager::recipes
QStringList recipes(int count) const
Definition: ResourcesManager.cpp:75
LicenseManager::readLicense
static void readLicense(BufferReader *reader, const char *filename)
Definition: LicenseManager.cpp:36
QuestWriter
Definition: QuestWriter.h:19
sstring
const typedef char * sstring
Definition: global.h:40
disinfect.count
int count
Definition: disinfect.py:7
Settings::hooks_filename
const char * hooks_filename[20]
Definition: global.h:329
sproto.h
recipeliststruct
Definition: recipe.h:37
AssetsCollection::each
void each(std::function< void(T *)> op)
Definition: AssetsCollection.h:135
init_globals
void init_globals(void)
Definition: init.c:268
image.h
MAP_USE
@ MAP_USE
Definition: ResourcesManager.h:21
AssetsManager::archetypes
Archetypes * archetypes()
Definition: AssetsManager.h:44
CREMapInformationManager.h
ResourcesManager.h
ResourcesManager::archetypeModified
void archetypeModified(archetype *arch)
Definition: ResourcesManager.cpp:169
ResourcesManager::myRecipes
QList< QHash< QString, recipestruct * > > myRecipes
Definition: ResourcesManager.h:62
RMParms::final_exit_archetype
char final_exit_archetype[RM_SIZE]
Definition: random_map.h:59
Floor.t
t
Definition: Floor.py:62
ModifiedAssetsManager::assetModified
void assetModified(T *asset)
Definition: ModifiedAssetsManager.h:45
item
Definition: item.py:1
quest_definition
Definition: quest.h:42
onFatalInit
static void onFatalInit(enum fatal_error)
Definition: ResourcesManager.cpp:32
assets.h
recipestruct::next
struct recipestruct * next
Definition: recipe.h:24
Settings::hooks
collectorHook hooks[20]
Definition: global.h:330
CRERandomMap
Definition: CRERandomMap.h:14
ALCHEMY_PRODUCT
@ ALCHEMY_PRODUCT
Definition: ResourcesManager.h:23
castle_read.key
key
Definition: castle_read.py:64
RANDOM_MAP_FINAL_EXIT
@ RANDOM_MAP_FINAL_EXIT
Definition: ResourcesManager.h:22
fatal_error
fatal_error
Definition: define.h:47
ResourcesManager::myArchetypes
ModifiedAssetsManager< archetype > myArchetypes
Definition: ResourcesManager.h:63
init_library
void init_library(void)
Definition: init.c:188
CREMapInformationManager
Definition: CREMapInformationManager.h:16
if
if(!(yy_init))
Definition: loader.c:2589
ResourcesManager::~ResourcesManager
virtual ~ResourcesManager()
Definition: ResourcesManager.cpp:28
libproto.h
treasureliststruct
Definition: treasure.h:82
give.name
name
Definition: give.py:27