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 "server.h"
30 #include "assets.h"
31 #include "modules.h"
32 
33 #include <string.h>
34 #include <unordered_map>
35 
36 static int last_hr;
37 
39 struct Region {
40  std::unordered_map<std::string, std::string> bells;
41  std::string fallback;
42 };
43 
45 static std::unordered_map<std::string, Region *> regions;
46 
52 static void load_bells(BufferReader *reader, const char *filename) {
53  Region *current = NULL;
54  char *line;
55  char *split[20];
56 
57  while ((line = bufferreader_next_line(reader))) {
58  if (line[0] == '\0' || line[0] == '#') {
59  continue;
60  }
61  char *space = strchr(line, ' ');
62  if (!space) {
63  LOG(llevError, "Invalid bell line '%s' in %s:%zu\n", line, filename, bufferreader_current_line(reader));
64  continue;
65  }
66  *space = '\0';
67  space++;
68 
69  if (strcmp(line, "region") == 0) {
70  current = new Region();
71  regions[space] = current;
72  continue;
73  }
74  if (!current) {
75  LOG(llevError, "Missing 'region' in bell file %s\n", filename);
76  continue;
77  }
78  size_t count = split_string(line, split, sizeof(split), ',');
79  for (size_t i = 0; i < count; i++) {
80  if (strcmp(split[i], "*") == 0) {
81  current->fallback = space;
82  } else {
83  current->bells[split[i]] = space;
84  }
85  }
86  }
87 }
88 
92 static void ring_bell(void) {
93 
94  object *pl = first_player ? first_player->ob : NULL;
95  while (pl) {
96  // If the player is on a map, then try to ring the bell
97  if (pl->map) {
98  region *reg = get_region_by_map(pl->map);
99  if (reg) {
100  auto found = regions.find(reg->name);
101  if (found != regions.end()) {
102  const char *god_name = determine_god(pl);
103  auto god = found->second->bells.find(god_name);
104  std::string msg = god == found->second->bells.end() ? found->second->fallback : god->second;
105  auto r = msg.find("%god");
106  if (r != std::string::npos) {
107  msg.replace(r, 4, god_name);
108  }
110  }
111  }
112  }
113 
114  pl = pl->contr->next ? pl->contr->next->ob : NULL;
115  }
116 }
117 
125 static int clock_listener(int *type, ...) {
126  va_list args;
127  int code;
128  timeofday_t tod;
129 
130  va_start(args, type);
131  code = va_arg(args, int);
132 
133  switch (code) {
134  case EVENT_CLOCK:
135  get_tod(&tod);
136  if (tod.hour != last_hr) {
137  last_hr = tod.hour;
138  ring_bell();
139  }
140  break;
141  }
142 
143  va_end(args);
144 
145  return 0;
146 }
147 
149 
155  if (stage != STARTUP_STAGE_COLLECT_HOOKS)
156  return;
157 
158  timeofday_t tod;
159  get_tod(&tod);
160  last_hr = tod.hour;
162 
164 
165  /* Disable the plugin in case it's still there */
166  serverSettings->disabled_plugins.push_back("cfcitybell");
167 }
168 
171  for (const auto& reg : regions) {
172  delete reg.second;
173  }
174  all_regions.clear();
175 }
176 
ring_bell
static void ring_bell(void)
Ring the city bells for each player.
Definition: cfcitybell.cpp:92
player::next
player * next
Pointer to next player, NULL if this is last.
Definition: player.h:108
global.h
first_player
player * first_player
First player.
Definition: init.cpp:106
Region::fallback
std::string fallback
Message if the god's name is not in Region::bells.
Definition: cfcitybell.cpp:41
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:148
llevError
@ llevError
Problems requiring server admin to fix.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:82
ServerSettings::disabled_plugins
std::vector< std::string > disabled_plugins
List of disabled plugins, 'All' means all.
Definition: server.h:16
player::ob
object * ob
The object representing the player.
Definition: player.h:179
object::map
struct mapstruct * map
Pointer to the map in which this object is present.
Definition: object.h:307
STARTUP_STAGE_COLLECT_HOOKS
@ STARTUP_STAGE_COLLECT_HOOKS
Called when adding collect hooks, before assets loading.
Definition: modules.h:23
timeofday_t
Represents the ingame time.
Definition: tod.h:38
Region
One region with bells.
Definition: cfcitybell.cpp:39
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:280
Region::bells
std::unordered_map< std::string, std::string > bells
Map between a god's name and the message to display.
Definition: cfcitybell.cpp:40
last_hr
static int last_hr
Definition: cfcitybell.cpp:36
MSG_TYPE_MISC
#define MSG_TYPE_MISC
Messages that don't go elsewhere.
Definition: newclient.h:417
NDI_ORANGE
#define NDI_ORANGE
Definition: newclient.h:250
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
object::contr
struct player * contr
Pointer to the player which control this object.
Definition: object.h:286
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
assets_add_collector_hook
void assets_add_collector_hook(const char *name, collectorHook hook)
Definition: assets.cpp:556
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:479
EVENT_CLOCK
#define EVENT_CLOCK
Global time event.
Definition: events.h:53
code
Server as a resource for server administrators and developers the server recognizes the following distinct directories The *name *is what it s called in the server code
Definition: server-directories.txt:8
sproto.h
MSG_SUBTYPE_NONE
#define MSG_SUBTYPE_NONE
Definition: newclient.h:424
modules.h
StartupStage
StartupStage
Definition: modules.h:21
is_valid_types_gen.found
found
Definition: is_valid_types_gen.py:39
Settings
Server settings.
Definition: global.h:245
region
This is a game region.
Definition: map.h:279
NDI_UNIQUE
#define NDI_UNIQUE
Print immediately, don't buffer.
Definition: newclient.h:266
all_regions
std::vector< region * > all_regions
Definition: init.cpp:108
event_registration
unsigned long event_registration
Registration identifier type.
Definition: events.h:84
serverSettings
ServerSettings serverSettings
Definition: init.cpp:46
ServerSettings
Definition: server.h:15
timeofday_t::hour
int hour
Definition: tod.h:43
assets.h
cfcitybell_close
void cfcitybell_close()
Definition: cfcitybell.cpp:169
regions
static std::unordered_map< std::string, Region * > regions
All defined regions.
Definition: cfcitybell.cpp:45
global_handler
static event_registration global_handler
Definition: cfcitybell.cpp:148
get_region_by_map
region * get_region_by_map(mapstruct *m)
Gets a region from a map.
Definition: region.cpp:71
draw_ext_info
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Sends message to player(s).
Definition: main.cpp:316
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2741
server.h
BufferReader
Definition: bufferreader.cpp:22
clock_listener
static int clock_listener(int *type,...)
Global event handling, only uses EVENT_CLOCK.
Definition: cfcitybell.cpp:125
object.h
load_bells
static void load_bells(BufferReader *reader, const char *filename)
Load a .bells file.
Definition: cfcitybell.cpp:52
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25
cfcitybell_init
void cfcitybell_init(Settings *, ServerSettings *serverSettings, StartupStage stage)
Citybells module initialisation.
Definition: cfcitybell.cpp:154
bufferreader_next_line
char * bufferreader_next_line(BufferReader *br)
Return the next line in the buffer, as separated by a newline.
Definition: bufferreader.cpp:110