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 Quests::Quests() : visibleQuests(0) {
16 }
17 
19 }
20 
21 quest_definition *Quests::create(const std::string& name) {
22  return quest_create(name.data());
23 }
24 
27 }
28 
30  if (!existing->quest_is_system) {
31  visibleQuests--;
32  }
33  quest_clear(existing);
34  (*existing) = (*update);
35  free(update);
36  if (!existing->quest_is_system) {
37  visibleQuests++;
38  }
39 }
40 
42  quest->client_code = m_assets.size();
43  if (!quest->quest_is_system)
44  visibleQuests++;
45 }
46 
48  quest_step_definition *step = static_cast<quest_step_definition *>(calloc(1, sizeof(quest_step_definition)));
49  if (!step)
51  return step;
52 }
53 
55  quest_condition *cond = static_cast<quest_condition *>(calloc(1, sizeof(quest_condition)));
56  if (!cond)
58  return cond;
59 }
60 
62  quest_definition *quest = static_cast<quest_definition *>(calloc(1, sizeof(quest_definition)));
63  quest->quest_code = add_string(name);
64  return quest;
65 }
66 
68  free_string(condition->quest_code);
69  free(condition);
70 }
71 
74  auto condition = step->conditions;
75  while (condition != NULL) {
76  auto next_condition = condition->next;
77  quest_destroy_condition(condition);
78  condition = next_condition;
79  }
80  free(step);
81 }
82 
84  while (step) {
85  auto next_step = step->next;
86  quest_destroy_step(step);
87  step = next_step;
88  }
89 }
90 
92  free_string(quest->quest_code);
93  FREE_AND_CLEAR_STR_IF(quest->quest_description);
94  FREE_AND_CLEAR_STR_IF(quest->quest_title);
95  FREE_AND_CLEAR_STR_IF(quest->quest_comment);
96  quest_destroy_steps(quest->steps);
97 }
98 
101  free(quest);
102 }
103 
104 int quest_condition_from_string(quest_condition *condition, const char *buffer) {
105  int condition_parsed = 0;
106  condition->minstep = 0;
107  condition->maxstep = 0;
108  condition->quest_code = nullptr;
109 
110  char namedquest[500];
111  namedquest[0] = '\0';
112  if (sscanf(buffer, "%s %d-%d", namedquest, &condition->minstep, &condition->maxstep) != 3) {
113  if (sscanf(buffer, "%s <=%d", namedquest, &condition->maxstep) == 2) {
114  condition->minstep = 0;
115  condition_parsed = 1;
116  } else if (sscanf(buffer, "%s %d", namedquest, &condition->minstep) == 2) {
117  condition->maxstep = condition->minstep;
118  condition_parsed = 1;
119  } else if (strstr(buffer, "finished")) {
120  if (sscanf(buffer, "%s finished", namedquest) == 1) {
121  condition->minstep = condition->maxstep = -1;
122  condition_parsed = 1;
123  }
124  }
125  } else
126  condition_parsed = 1;
127 
128  if (condition_parsed)
129  condition->quest_code = add_string(namedquest);
130 
131  return condition_parsed;
132 }
133 
134 void quest_write_condition(char *buf, size_t len, const quest_condition *cond) {
135  if (cond->maxstep == -1 && cond->minstep == -1) {
136  snprintf(buf, len, "%s finished", cond->quest_code);
137  } else {
138  if (cond->minstep == 0) {
139  snprintf(buf, len, "%s <=%d", cond->quest_code, cond->maxstep);
140  } else if (cond->minstep == cond->maxstep) {
141  snprintf(buf, len, "%s %d", cond->quest_code, cond->maxstep);
142  } else {
143  snprintf(buf, len, "%s %d-%d", cond->quest_code, cond->minstep, cond->maxstep);
144  }
145  }
146 }
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:199
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
quest_condition::quest_code
sstring quest_code
Definition: quest.h:24
quest_create_step
quest_step_definition * quest_create_step(void)
Definition: Quests.cpp:47
quest_condition::maxstep
int maxstep
Definition: quest.h:27
quest_write_condition
void quest_write_condition(char *buf, size_t len, const quest_condition *cond)
Definition: Quests.cpp:134
quest_clear
void quest_clear(quest_definition *quest)
Definition: Quests.cpp:91
quest_definition::quest_is_system
bool quest_is_system
Definition: quest.h:50
quest_step_definition
Definition: quest.h:33
free_string
void free_string(sstring str)
Definition: shstr.c:280
quest
Definition: quest.py:1
quest_step_definition::next
struct quest_step_definition * next
Definition: quest.h:37
AssetsCollection< quest_definition >::m_assets
std::map< std::string, quest_definition * > m_assets
Definition: AssetsCollection.h:157
quest_destroy_steps
void quest_destroy_steps(quest_step_definition *step)
Definition: Quests.cpp:83
Quests::Quests
Quests()
Definition: Quests.cpp:15
fatal
void fatal(enum fatal_error err)
Definition: utils.c:580
Quests::replace
virtual void replace(quest_definition *existing, quest_definition *update)
Definition: Quests.cpp:29
Quests.h
quest_destroy_condition
void quest_destroy_condition(quest_condition *condition)
Definition: Quests.cpp:67
Quests::create
virtual quest_definition * create(const std::string &name)
Definition: Quests.cpp:21
quest_condition::minstep
int minstep
Definition: quest.h:25
Quests::visibleQuests
size_t visibleQuests
Definition: Quests.h:35
Quests::added
virtual void added(quest_definition *quest)
Definition: Quests.cpp:41
Quests::destroy
virtual void destroy(quest_definition *item)
Definition: Quests.cpp:25
quest_step_definition::conditions
quest_condition * conditions
Definition: quest.h:38
quest_condition
Definition: quest.h:23
quest_definition
Definition: quest.h:42
quest_destroy
void quest_destroy(quest_definition *quest)
Definition: Quests.cpp:99
buf
StringBuffer * buf
Definition: readable.c:1610
quest_step_definition::step_description
sstring step_description
Definition: quest.h:35
quest_destroy_step
void quest_destroy_step(quest_step_definition *step)
Definition: Quests.cpp:72
quest_condition::next
struct quest_condition * next
Definition: quest.h:29
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
quest_create
quest_definition * quest_create(const char *name)
Definition: Quests.cpp:61
quest_condition_from_string
int quest_condition_from_string(quest_condition *condition, const char *buffer)
Definition: Quests.cpp:104
quest_create_condition
quest_condition * quest_create_condition(void)
Definition: Quests.cpp:54
give.name
name
Definition: give.py:27
Quests::~Quests
virtual ~Quests()
Definition: Quests.cpp:18