Crossfire Server, Trunk
Quests.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 2021 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 "Quests.h"
14 
15 template<>
16 quest_definition *asset_create(const std::string& name) {
17  return quest_create(name.data());
18 }
19 
20 template<>
23 }
24 
25 Quests::Quests() : visibleQuests(0) {
26 }
27 
29 }
30 
32  if (!existing->quest_is_system) {
33  visibleQuests--;
34  }
35  quest_clear(existing);
36  (*existing) = (*update);
37  free(update);
38  if (!existing->quest_is_system) {
39  visibleQuests++;
40  }
41 }
42 
44  quest->client_code = m_assets.size();
45  if (!quest->quest_is_system)
46  visibleQuests++;
47 }
48 
51  if (!step)
53  return step;
54 }
55 
57  quest_condition *cond = new quest_condition();
58  if (!cond)
60  return cond;
61 }
62 
65  quest->quest_code = add_string(name);
66  return quest;
67 }
68 
70  free_string(condition->quest_code);
71  delete condition;
72 }
73 
76  while (!step->conditions.empty()) {
77  quest_destroy_condition(step->conditions.back());
78  step->conditions.pop_back();
79  }
80  delete step;
81 }
82 
84  free_string(quest->quest_code);
85  FREE_AND_CLEAR_STR_IF(quest->quest_description);
86  FREE_AND_CLEAR_STR_IF(quest->quest_title);
87  FREE_AND_CLEAR_STR_IF(quest->quest_comment);
88  while (!quest->steps.empty()) {
89  quest_destroy_step(quest->steps.back());
90  quest->steps.pop_back();
91  }
92  quest->steps.clear();
93 }
94 
97  delete quest;
98 }
99 
100 int quest_condition_from_string(quest_condition *condition, const char *buffer) {
101  int condition_parsed = 0;
102  condition->minstep = 0;
103  condition->maxstep = 0;
104  condition->quest_code = nullptr;
105 
106  char namedquest[500];
107  namedquest[0] = '\0';
108  if (sscanf(buffer, "%s %d-%d", namedquest, &condition->minstep, &condition->maxstep) != 3) {
109  if (sscanf(buffer, "%s <=%d", namedquest, &condition->maxstep) == 2) {
110  condition->minstep = 0;
111  condition_parsed = 1;
112  } else if (sscanf(buffer, "%s %d", namedquest, &condition->minstep) == 2) {
113  condition->maxstep = condition->minstep;
114  condition_parsed = 1;
115  } else if (strstr(buffer, "finished")) {
116  if (sscanf(buffer, "%s finished", namedquest) == 1) {
117  condition->minstep = condition->maxstep = -1;
118  condition_parsed = 1;
119  }
120  }
121  } else
122  condition_parsed = 1;
123 
124  if (condition_parsed)
125  condition->quest_code = add_string(namedquest);
126 
127  return condition_parsed;
128 }
129 
130 void quest_write_condition(char *buf, size_t len, const quest_condition *cond) {
131  if (cond->maxstep == -1 && cond->minstep == -1) {
132  snprintf(buf, len, "%s finished", cond->quest_code);
133  } else {
134  if (cond->minstep == 0) {
135  snprintf(buf, len, "%s <=%d", cond->quest_code, cond->maxstep);
136  } else if (cond->minstep == cond->maxstep) {
137  snprintf(buf, len, "%s %d", cond->quest_code, cond->maxstep);
138  } else {
139  snprintf(buf, len, "%s %d-%d", cond->quest_code, cond->minstep, cond->maxstep);
140  }
141  }
142 }
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:204
quest_condition::quest_code
sstring quest_code
Definition: quest.h:21
quest_create_step
quest_step_definition * quest_create_step(void)
Definition: Quests.cpp:49
quest_condition::maxstep
int maxstep
Definition: quest.h:24
quest_write_condition
void quest_write_condition(char *buf, size_t len, const quest_condition *cond)
Definition: Quests.cpp:130
Quests::replace
virtual void replace(quest_definition *existing, quest_definition *update) override
Definition: Quests.cpp:31
quest_clear
void quest_clear(quest_definition *quest)
Definition: Quests.cpp:83
quest_definition::quest_is_system
bool quest_is_system
Definition: quest.h:45
buf
StringBuffer * buf
Definition: readable.cpp:1551
quest_step_definition
Definition: quest.h:29
asset_destroy
void asset_destroy(quest_definition *quest)
Definition: Quests.cpp:21
quest
Definition: quest.py:1
AssetsCollection< quest_definition >::m_assets
std::map< std::string, quest_definition * > m_assets
Definition: AssetsCollection.h:190
Quests::Quests
Quests()
Definition: Quests.cpp:25
add_string
sstring add_string(const char *str)
Definition: shstr.cpp:124
asset_create
quest_definition * asset_create(const std::string &name)
Definition: Quests.cpp:16
Quests.h
quest_destroy_condition
void quest_destroy_condition(quest_condition *condition)
Definition: Quests.cpp:69
fatal
void fatal(enum fatal_error err)
Definition: utils.cpp:570
free_string
void free_string(sstring str)
Definition: shstr.cpp:280
quest_condition::minstep
int minstep
Definition: quest.h:22
Quests::visibleQuests
size_t visibleQuests
Definition: Quests.h:31
quest_condition
Definition: quest.h:20
quest_definition
Definition: quest.h:37
quest_destroy
void quest_destroy(quest_definition *quest)
Definition: Quests.cpp:95
Quests::added
virtual void added(quest_definition *quest) override
Definition: Quests.cpp:43
quest_step_definition::conditions
std::vector< quest_condition * > conditions
Definition: quest.h:33
quest_step_definition::step_description
sstring step_description
Definition: quest.h:31
quest_destroy_step
void quest_destroy_step(quest_step_definition *step)
Definition: Quests.cpp:74
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
quest_create
quest_definition * quest_create(const char *name)
Definition: Quests.cpp:63
quest_condition_from_string
int quest_condition_from_string(quest_condition *condition, const char *buffer)
Definition: Quests.cpp:100
quest_create_condition
quest_condition * quest_create_condition(void)
Definition: Quests.cpp:56
give.name
name
Definition: give.py:27
Quests::~Quests
virtual ~Quests()
Definition: Quests.cpp:28