Crossfire Server, Trunk  1.75.0
random_house_generator.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 
35 #include <stdarg.h>
36 #include <assert.h>
37 #include <string.h>
38 
39 #include "global.h"
40 #include "object.h"
41 #include "sproto.h"
42 #include "server.h"
43 
46  const char *mappath;
47  const char *monsterstyle;
48 };
49 
51 static const house_zone_struct zones[] = {
52  /* Scorn */
53  { "/world/world_104_115", "city" },
54  { "/world/world_105_115", "city" },
55  { "/world/world_104_116", "city" },
56  { "/world/world_105_116", "city" },
57  /* Navar */
58  { "/world/world_122_116", "city" },
59  { "/world/world_121_116", "city" },
60  { "/world/world_122_117", "city" },
61  { "/world/world_121_117", "city" },
62  { NULL, NULL }
63 };
64 
72 static const house_zone_struct *get_map_zone(const mapstruct *map) {
73  int zone;
74 
75  for (zone = 0; zones[zone].mappath != NULL; zone++) {
76  if (strcmp(zones[zone].mappath, map->path) == 0)
77  return &zones[zone];
78  }
79 
80  return NULL;
81 }
82 
90 static int is_suitable_exit(object *exit) {
91  assert(exit);
92 
93  if (exit->type != EXIT)
94  return 0;
95  if (exit->slaying || exit->msg)
96  return 0;
97 
98  return 1;
99 }
100 
110 static int get_exit_seed(const object *exit, const mapstruct *map) {
111  char r[HUGE_BUF + 100];
112  int seed = 0, len, w = 0;
113 
114  snprintf(r, sizeof(r), "%s!%d,%d*%s", exit->arch->name, exit->x, exit->y, map->path);
115 
116  len = strlen(r)-1;
117  while (len >= 0) {
118  seed ^= ((int)r[len])<<w;
119  w += 8;
120  w = w%32;
121  len--;
122  }
123 
124  return seed;
125 }
126 
136 static void add_exit_to_item(object *exit, const house_zone_struct *zone, const mapstruct *map) {
137  char params[MAX_BUF];
138 
139  assert(exit);
140  assert(zone);
141 
142  snprintf(params, sizeof(params), "layoutstyle onion\n"
143  "floorstyle indoor\n"
144  "wallstyle wooden\n"
145  "monsterstyle %s\n"
146  "dungeon_level 1\n"
147  "dungeon_depth 1\n"
148  "decorstyle furniture\n"
149  "random_seed %d\n",
150  zone->monsterstyle,
151  get_exit_seed(exit, map));
152 
153  exit->slaying = add_string("/!");
154  exit->msg = add_string(params);
155 }
156 
162 static void add_exits_to_map(const mapstruct *map) {
163  int x, y;
164  const house_zone_struct *zone = get_map_zone(map);
165 
166  if (!zone)
167  return;
168 
169  for (x = 0; x < MAP_WIDTH(map); x++) {
170  for (y = 0; y < MAP_HEIGHT(map); y++) {
171  FOR_MAP_PREPARE(map, x, y, item) {
172  if (is_suitable_exit(item))
173  add_exit_to_item(item, zone, map);
174  } FOR_MAP_FINISH();
175  }
176  }
177 }
178 
186 static int cfrhg_globalEventListener(int *type, ...) {
187  va_list args;
188  int rv = 0;
189  mapstruct *map;
190  int code;
191 
192  va_start(args, type);
193  code = va_arg(args, int);
194 
195  switch (code) {
196  case EVENT_MAPLOAD:
197  map = va_arg(args, mapstruct *);
198  add_exits_to_map(map);
199  break;
200  }
201  va_end(args);
202 
203  return rv;
204 }
205 
207 
213 
214  /* Disable the plugin in case it's still there */
215  settings->disabled_plugins.push_back(strdup("cfrhg"));
216 }
217 
223 }
global.h
settings
struct Settings settings
Global settings.
Definition: init.cpp:139
FOR_MAP_FINISH
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
Definition: define.h:724
add_exit_to_item
static void add_exit_to_item(object *exit, const house_zone_struct *zone, const mapstruct *map)
Change an empty exit to point to a random map.
Definition: random_house_generator.cpp:136
object::arch
struct archetype * arch
Pointer to archetype.
Definition: object.h:424
is_suitable_exit
static int is_suitable_exit(object *exit)
Should we add a random map to this exit?
Definition: random_house_generator.cpp:90
object::x
int16_t x
Definition: object.h:335
add_exits_to_map
static void add_exits_to_map(const mapstruct *map)
Checks if the map should be processed, and if so process it.
Definition: random_house_generator.cpp:162
mapstruct::path
char path[HUGE_BUF]
Filename of the map.
Definition: map.h:355
HUGE_BUF
#define HUGE_BUF
Used for messages - some can be quite long.
Definition: define.h:37
zones
static const house_zone_struct zones[]
Maps we work on.
Definition: random_house_generator.cpp:51
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::y
int16_t y
Position in the map for this object.
Definition: object.h:335
random_house_generator_init
void random_house_generator_init(Settings *, ServerSettings *settings)
Module initialisation.
Definition: random_house_generator.cpp:211
random_house_generator_close
void random_house_generator_close()
Close the module.
Definition: random_house_generator.cpp:221
events_unregister_global_handler
void events_unregister_global_handler(int eventcode, event_registration id)
Remove a global event handler.
Definition: events.cpp:26
add_string
sstring add_string(const char *str)
This will add 'str' to the hash table.
Definition: shstr.cpp:124
object::type
uint8_t type
PLAYER, BULLET, etc.
Definition: object.h:348
house_zone_struct::monsterstyle
const char * monsterstyle
Style of monsters.
Definition: random_house_generator.cpp:47
sproto.h
MAP_WIDTH
#define MAP_WIDTH(m)
Map width.
Definition: map.h:73
house_zone_struct::mappath
const char * mappath
Full map path.
Definition: random_house_generator.cpp:46
MAX_BUF
#define MAX_BUF
Used for all kinds of things.
Definition: define.h:35
eg
static event_registration eg
Definition: random_house_generator.cpp:206
FOR_MAP_PREPARE
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:717
EXIT
@ EXIT
Definition: object.h:186
Settings
Server settings.
Definition: global.h:241
object::slaying
sstring slaying
Which race to do double damage to.
Definition: object.h:327
event_registration
unsigned long event_registration
Registration identifier type.
Definition: events.h:84
cfrhg_globalEventListener
static int cfrhg_globalEventListener(int *type,...)
Global server event handling.
Definition: random_house_generator.cpp:186
mapstruct
This is a game-map.
Definition: map.h:315
ServerSettings
Definition: server.h:15
object::msg
sstring msg
If this is a book/sign/magic mouth/etc.
Definition: object.h:330
MAP_HEIGHT
#define MAP_HEIGHT(m)
Map height.
Definition: map.h:75
get_exit_seed
static int get_exit_seed(const object *exit, const mapstruct *map)
Get the random map seed.
Definition: random_house_generator.cpp:110
get_map_zone
static const house_zone_struct * get_map_zone(const mapstruct *map)
Get the random map parameters of a map.
Definition: random_house_generator.cpp:72
house_zone_struct
Link between a map and the exits to generate for it.
Definition: random_house_generator.cpp:45
EVENT_MAPLOAD
#define EVENT_MAPLOAD
A map is loaded (pristine state)
Definition: events.h:61
code
Crossfire Architecture the general intention is to enhance the enjoyability and playability of CF In this code
Definition: arch-handbook.txt:14
archetype::name
sstring name
More definite name, like "generate_kobold".
Definition: object.h:484
server.h
object.h
is_valid_types_gen.type
list type
Definition: is_valid_types_gen.py:25