Crossfire Server, Trunk
Messages.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 "Messages.h"
14 extern "C" {
15 #include "global.h"
16 }
17 
18 Messages::Messages() : m_totalChance(0) {
19 }
20 
21 GeneralMessage *Messages::create(const std::string& name) {
22  auto msg = static_cast<GeneralMessage *>(calloc(1, sizeof(GeneralMessage)));
23  msg->identifier = add_string(name.c_str());
24  return msg;
25 }
26 
28  FREE_AND_CLEAR_STR_IF(item->identifier);
29  FREE_AND_CLEAR_STR_IF(item->message);
30  FREE_AND_CLEAR_STR_IF(item->quest_code);
32  free(item);
33 }
34 
36  FREE_AND_COPY_IF(existing->identifier, update->identifier);
37  FREE_AND_COPY_IF(existing->message, update->message);
38  FREE_AND_COPY_IF(existing->quest_code, update->quest_code);
39  FREE_AND_COPY_IF(existing->title, existing->title);
40  m_totalChance -= existing->chance;
41  m_totalChance += update->chance;
42  existing->chance = update->chance;
43  destroy(update);
44 }
45 
47  m_totalChance += asset->chance;
48 }
49 
51  if (!m_totalChance) {
52  return nullptr;
53  }
54 
55  auto msg = m_assets.begin();
56  int weight = (RANDOM() % m_totalChance);
57  while (msg != m_assets.end()) {
58  weight -= msg->second->chance;
59  if (weight < 0)
60  break;
61  msg++;
62  }
63  return msg->second;
64 }
FREE_AND_CLEAR_STR_IF
#define FREE_AND_CLEAR_STR_IF(xyz)
Definition: global.h:198
global.h
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
GeneralMessage
Definition: book.h:44
Messages.h
Messages::Messages
Messages()
Definition: Messages.cpp:18
Messages::destroy
virtual void destroy(GeneralMessage *item)
Definition: Messages.cpp:27
GeneralMessage::title
sstring title
Definition: book.h:48
GeneralMessage::identifier
sstring identifier
Definition: book.h:47
FREE_AND_COPY_IF
#define FREE_AND_COPY_IF(sv, nv)
Definition: global.h:204
Messages::added
virtual void added(GeneralMessage *asset)
Definition: Messages.cpp:46
Messages::random
GeneralMessage * random()
Definition: Messages.cpp:50
AssetsCollection< GeneralMessage >::m_assets
std::map< std::string, GeneralMessage * > m_assets
Definition: AssetsCollection.h:157
GeneralMessage::chance
int chance
Definition: book.h:45
navar-midane_pickup.msg
list msg
Definition: navar-midane_pickup.py:13
Messages::replace
virtual void replace(GeneralMessage *existing, GeneralMessage *update)
Definition: Messages.cpp:35
RANDOM
#define RANDOM()
Definition: define.h:642
GeneralMessage::message
sstring message
Definition: book.h:49
Messages::create
virtual GeneralMessage * create(const std::string &name)
Definition: Messages.cpp:21
item
Definition: item.py:1
GeneralMessage::quest_code
sstring quest_code
Definition: book.h:50
give.name
name
Definition: give.py:27
Messages::m_totalChance
int m_totalChance
Definition: Messages.h:32