Crossfire Server, Trunk  1.75.0
cfcitybell.cpp
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-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 
26 #include "global.h"
27 #include "object.h"
28 #include "sproto.h"
29 #include <string.h>
30 
31 #include <unordered_map>
32 
33 static int last_hr;
34 
36 struct Region {
37  std::unordered_map<std::string, std::string> bells;
38  std::string fallback;
39 };
40 
42 static std::unordered_map<std::string, Region *> regions;
43 
49 static void load_bells(BufferReader *reader, const char *filename) {
50  Region *current = NULL;
51  char *line;
52  char *split[20];
53 
54  while ((line = bufferreader_next_line(reader))) {
55  if (line[0] == '\0' || line[0] == '#') {
56  continue;
57  }
58  char *space = strchr(line, ' ');
59  if (!space) {
60  LOG(llevError, "Invalid bell line '%s' in %s:%zu\n", line, filename, bufferreader_current_line(reader));
61  continue;
62  }
63  *space = '\0';
64  space++;
65 
66  if (strcmp(line, "region") == 0) {
67  current = new Region();
68  regions[space] = current;
69  continue;
70  }
71  if (!current) {
72  LOG(llevError, "Missing 'region' in bell file %s\n", filename);
73  continue;
74  }
75  size_t count = split_string(line, split, sizeof(split), ',');
76  for (size_t i = 0; i < count; i++) {
77  if (strcmp(split[i], "*") == 0) {
78  current->fallback = space;
79  } else {
80  current->bells[split[i]] = space;
81  }
82  }
83  }
84 }
85 
89 static void ring_bell(void) {
90 
91  object *pl = first_player ? first_player->ob : NULL;
92  while (pl) {
93  // If the player is on a map, then try to ring the bell
94  if (pl->map) {
95  region *reg = get_region_by_map(pl->map);
96  if (reg) {
97  auto found = regions.find(reg->name);
98  if (found != regions.end()) {
99  const char *god_name = determine_god(pl);
100  auto god = found->second->bells.find(god_name);
101  std::string msg = god == found->second->bells.end() ? found->second->fallback : god->second;
102  auto r = msg.find("%god");
103  if (r != std::string::npos) {
104  msg.replace(r, 4, god_name);
105  }
107  }
108  }
109  }
110 
111  pl = pl->contr->next ? pl->contr->next->ob : NULL;
112  }
113 }
114 
122 static int clock_listener(int *type, ...) {
123  va_list args;
124  int code;
125  timeofday_t tod;
126 
127  va_start(args, type);
128  code = va_arg(args, int);
129 
130  switch (code) {
131  case EVENT_CLOCK:
132  get_tod(&tod);
133  if (tod.hour != last_hr) {
134  last_hr = tod.hour;
135  ring_bell();
136  }
137  break;
138  }
139 
140  va_end(args);
141 
142  return 0;
143 }
144 
146 
152  timeofday_t tod;
153  get_tod(&tod);
154  last_hr = tod.hour;
156 
157  settings->add_hook(".bells", load_bells);
158 
159  /* Disable the plugin in case it's still there */
160  settings->disabled_plugins.push_back(strdup("cfcitybell"));
161 }
162 
165  for (auto reg : regions) {
166  delete reg.second;
167  }
168  all_regions.clear();
169 }
170 
ring_bell
static void ring_bell(void)
Ring the city bells for each player.
Definition: cfcitybell.cpp:89
player::next
player * next
Pointer to next player, NULL if this is last.
Definition: player.h:106
global.h
first_player
player * first_player
First player.
Definition: init.cpp:106
settings
struct Settings settings
Server settings.
Definition: init.cpp:139
Region::fallback
std::string fallback
Message if the god's name is not in Region::bells.
Definition: cfcitybell.cpp:38
bufferreader_current_line
size_t bufferreader_current_line(BufferReader *br)
Return the index of the last line returned by bufferreader_next_line().
Definition: bufferreader.cpp:140
llevError
@ llevError
Error, serious thing.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
player::ob
object * ob
The object representing the player.
Definition: player.h:177
object::map
struct mapstruct * map
Pointer to the map in which this object is present.
Definition: object.h:305
timeofday_t
Represents the ingame time.
Definition: tod.h:38
Region
One region with bells.
Definition: cfcitybell.cpp:36
get_tod
void get_tod(timeofday_t *tod)
Computes the ingame time of the day.
Definition: time.cpp:219
region::name
char * name
Shortend name of the region as maps refer to it.
Definition: map.h:275
Region::bells
std::unordered_map< std::string, std::string > bells
Map between a god's name and the message to display.
Definition: cfcitybell.cpp:37
last_hr
static int last_hr
Definition: cfcitybell.cpp:33
MSG_TYPE_MISC
#define MSG_TYPE_MISC
Messages that don't go elsewhere.
Definition: newclient.h:416
NDI_ORANGE
#define NDI_ORANGE
Definition: newclient.h:249
draw_ext_info
vs only yadda is in because all tags get reset on the next draw_ext_info In the second since it is all in one draw_ext_info
Definition: media-tags.txt:61
events_register_global_handler
event_registration events_register_global_handler(int eventcode, f_plug_event hook)
Register a global event handler.
Definition: events.cpp:19
Settings::add_hook
void add_hook(const char *name, collectorHook hook)
Definition: global.h:340
object::contr
struct player * contr
Pointer to the player which control this object.
Definition: object.h:284
is_valid_types_gen.line
line
Definition: is_valid_types_gen.py:34
determine_god
const char * determine_god(object *op)
Determines if op worships a god.
Definition: gods.cpp:55
EVENT_CLOCK
#define EVENT_CLOCK
Global time event.
Definition: events.h:40
events_unregister_global_handler
void events_unregister_global_handler(int eventcode, event_registration id)
Remove a global event handler.
Definition: events.cpp:26
split_string
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Splits a string delimited by passed in sep value into characters into an array of strings.
Definition: utils.cpp:473
Settings::disabled_plugins
std::vector< char * > disabled_plugins
List of disabled plugins, 'All' means all.
Definition: global.h:328
sproto.h
MSG_SUBTYPE_NONE
#define MSG_SUBTYPE_NONE
Definition: newclient.h:423
cfcitybell_init
void cfcitybell_init(Settings *settings)
Citybells module initialisation.
Definition: cfcitybell.cpp:151
is_valid_types_gen.found
found
Definition: is_valid_types_gen.py:39
Settings
Server settings.
Definition: global.h:242
region
This is a game region.
Definition: map.h:274
NDI_UNIQUE
#define NDI_UNIQUE
Print immediately, don't buffer.
Definition: newclient.h:265
all_regions
std::vector< region * > all_regions
Definition: init.cpp:108
event_registration
unsigned long event_registration
Registration identifier type.
Definition: events.h:71
timeofday_t::hour
int hour
Definition: tod.h:43
cfcitybell_close
void cfcitybell_close()
Definition: cfcitybell.cpp:163
regions
static std::unordered_map< std::string, Region * > regions
All defined regions.
Definition: cfcitybell.cpp:42
global_handler
static event_registration global_handler
Definition: cfcitybell.cpp:145
get_region_by_map
region * get_region_by_map(mapstruct *m)
Gets a region from a map.
Definition: region.cpp:71
code
Crossfire Architecture the general intention is to enhance the enjoyability and playability of CF In this code
Definition: arch-handbook.txt:14
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2734
BufferReader
Definition: bufferreader.cpp:21
clock_listener
static int clock_listener(int *type,...)
Global event handling, only uses EVENT_CLOCK.
Definition: cfcitybell.cpp:122
object.h
load_bells
static void load_bells(BufferReader *reader, const char *filename)
Load a .bells file.
Definition: cfcitybell.cpp:49
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Return the next line in the buffer, as separated by a newline.
Definition: bufferreader.cpp:102