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 
50  quest_step_definition *step = static_cast<quest_step_definition *>(calloc(1, sizeof(quest_step_definition)));
51  if (!step)
53  return step;
54 }
55 
57  quest_condition *cond = static_cast<quest_condition *>(calloc(1, sizeof(quest_condition)));
58  if (!cond)
60  return cond;
61 }
62 
64  quest_definition *quest = static_cast<quest_definition *>(calloc(1, sizeof(quest_definition)));
65  quest->quest_code = add_string(name);
66  return quest;
67 }
68 
70  free_string(condition->quest_code);
71  free(condition);
72 }
73 
76  auto condition = step->conditions;
77  while (condition != NULL) {
78  auto next_condition = condition->next;
79  quest_destroy_condition(condition);
80  condition = next_condition;
81  }
82  free(step);
83 }
84 
86  while (step) {
87  auto next_step = step->next;
88  quest_destroy_step(step);
89  step = next_step;
90  }
91 }
92 
94  free_string(quest->quest_code);
95  FREE_AND_CLEAR_STR_IF(quest->quest_description);
96  FREE_AND_CLEAR_STR_IF(quest->quest_title);
97  FREE_AND_CLEAR_STR_IF(quest->quest_comment);
98  quest_destroy_steps(quest->steps);
99 }
100 
103  free(quest);
104 }
105 
106 int quest_condition_from_string(quest_condition *condition, const char *buffer) {
107  int condition_parsed = 0;
108  condition->minstep = 0;
109  condition->maxstep = 0;
110  condition->quest_code = nullptr;
111 
112  char namedquest[500];
113  namedquest[0] = '\0';
114  if (sscanf(buffer, "%s %d-%d", namedquest, &condition->minstep, &condition->maxstep) != 3) {
115  if (sscanf(buffer, "%s <=%d", namedquest, &condition->maxstep) == 2) {
116  condition->minstep = 0;
117  condition_parsed = 1;
118  } else if (sscanf(buffer, "%s %d", namedquest, &condition->minstep) == 2) {
119  condition->maxstep = condition->minstep;
120  condition_parsed = 1;
121  } else if (strstr(buffer, "finished")) {
122  if (sscanf(buffer, "%s finished", namedquest) == 1) {
123  condition->minstep = condition->maxstep = -1;
124  condition_parsed = 1;
125  }
126  }
127  } else
128  condition_parsed = 1;
129 
130  if (condition_parsed)
131  condition->quest_code = add_string(namedquest);
132 
133  return condition_parsed;
134 }
135 
136 void quest_write_condition(char *buf, size_t len, const quest_condition *cond) {
137  if (cond->maxstep == -1 && cond->minstep == -1) {
138  snprintf(buf, len, "%s finished", cond->quest_code);
139  } else {
140  if (cond->minstep == 0) {
141  snprintf(buf, len, "%s <=%d", cond->quest_code, cond->maxstep);
142  } else if (cond->minstep == cond->maxstep) {
143  snprintf(buf, len, "%s %d", cond->quest_code, cond->maxstep);
144  } else {
145  snprintf(buf, len, "%s %d-%d", cond->quest_code, cond->minstep, cond->maxstep);
146  }
147  }
148 }
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:200
quest_condition::quest_code
sstring quest_code
Definition: quest.h:20
quest_create_step
quest_step_definition * quest_create_step(void)
Definition: Quests.cpp:49
quest_condition::maxstep
int maxstep
Definition: quest.h:23
quest_write_condition
void quest_write_condition(char *buf, size_t len, const quest_condition *cond)
Definition: Quests.cpp:136
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:93
quest_definition::quest_is_system
bool quest_is_system
Definition: quest.h:46
buf
StringBuffer * buf
Definition: readable.cpp:1610
quest_step_definition
Definition: quest.h:29
asset_destroy
void asset_destroy(quest_definition *quest)
Definition: Quests.cpp:21
quest
Definition: quest.py:1
quest_step_definition::next
struct quest_step_definition * next
Definition: quest.h:33
AssetsCollection< quest_definition >::m_assets
std::map< std::string, quest_definition * > m_assets
Definition: AssetsCollection.h:179
quest_destroy_steps
void quest_destroy_steps(quest_step_definition *step)
Definition: Quests.cpp:85
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:580
free_string
void free_string(sstring str)
Definition: shstr.cpp:280
quest_condition::minstep
int minstep
Definition: quest.h:21
Quests::visibleQuests
size_t visibleQuests
Definition: Quests.h:31
quest_step_definition::conditions
quest_condition * conditions
Definition: quest.h:34
quest_condition
Definition: quest.h:19
quest_definition
Definition: quest.h:38
quest_destroy
void quest_destroy(quest_definition *quest)
Definition: Quests.cpp:101
Quests::added
virtual void added(quest_definition *quest) override
Definition: Quests.cpp:43
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
quest_condition::next
struct quest_condition * next
Definition: quest.h:25
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:106
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