Crossfire Server, Trunk  R20513
monster.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
19 #include "global.h"
20 
21 #include <stdlib.h>
22 
23 #include "random_map.h"
24 #include "rproto.h"
25 
38 void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP)
39 {
40  char styledirname[256];
41  mapstruct *style_map = NULL;
42  int failed_placements;
43  int64_t exp_per_sq, total_experience;
44  int number_monsters = 0;
45  archetype *at;
46 
47  snprintf(styledirname, sizeof(styledirname), "%s", "/styles/monsterstyles");
48  style_map = find_style(styledirname, monsterstyle, difficulty);
49  if (style_map == NULL) {
50  return;
51  }
52 
53  /* fill up the map with random monsters from the monster style*/
54 
55  total_experience = 0;
56  failed_placements = 0;
57  exp_per_sq = 0;
58  while (exp_per_sq <= level_exp(difficulty, 1.0)
59  && failed_placements < 100
60  && number_monsters < (RP->Xsize*RP->Ysize)/8) {
61  object *this_monster = pick_random_object(style_map);
62  int x, y, freeindex;
63 
64  if (this_monster == NULL) {
65  return; /* no monster?? */
66  }
67  x = RANDOM()%RP->Xsize;
68  y = RANDOM()%RP->Ysize;
69  freeindex = object_find_first_free_spot(this_monster, map, x, y);
70  if (freeindex != -1) {
71  object *new_monster = object_create_arch(this_monster->arch);
72  object_copy_with_inv(this_monster, new_monster);
73  x += freearr_x[freeindex];
74  y += freearr_y[freeindex];
75  object_insert_in_map_at(new_monster, map, NULL, INS_NO_MERGE|INS_NO_WALK_ON, x, y);
76 
77  total_experience += this_monster->stats.exp;
78  for (at = new_monster->arch; at != NULL; at = at->more) {
79  number_monsters++;
80  }
81  RP->total_map_hp += new_monster->stats.hp; /* a global count */
82  } else {
83  failed_placements++;
84  }
85  exp_per_sq = ((double)1000*total_experience)/(MAP_WIDTH(map)*MAP_HEIGHT(map)+1);
86  }
87 }
Random map parameters.
Definition: random_map.h:14
int64_t level_exp(int level, double expmul)
Returns how much experience is needed for a player to become the given level.
Definition: living.c:1822
void object_copy_with_inv(const object *src_ob, object *dest_ob)
copy an object with an inventory...
Definition: object.c:975
Random map related variables.
#define MAP_HEIGHT(m)
Map height.
Definition: map.h:80
short freearr_x[SIZEOFFREE]
X offset when searching around a spot.
Definition: object.c:65
int64_t exp
Experience.
Definition: living.h:46
Global type definitions and header inclusions.
object * pick_random_object(mapstruct *style)
Picks a random object from a style map.
Definition: style.c:286
The archetype structure is a set of rules on how to generate and manipulate objects which point to ar...
Definition: object.h:465
int16_t hp
Hit Points.
Definition: living.h:39
short freearr_y[SIZEOFFREE]
Y offset when searching around a spot.
Definition: object.c:71
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.c:1921
long unsigned int total_map_hp
Total hit points of the monsters in the map, used for treasure generation.
Definition: random_map.h:88
#define snprintf
Definition: win32.h:46
struct archt * more
Next part of a linked object.
Definition: object.h:469
#define INS_NO_WALK_ON
Don&#39;t call check_walk_on against the originator.
Definition: object.h:570
object * object_create_arch(archetype *at)
Create a full object using the given archetype.
Definition: arch.c:736
int Ysize
Definition: random_map.h:70
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
object_find_first_free_spot(archetype, mapstruct, x, y) works like object_find_free_spot(), but it will search max number of squares.
Definition: object.c:3458
signed __int64 int64_t
Definition: win32.h:168
int Xsize
Definition: random_map.h:69
#define INS_NO_MERGE
Don&#39;t try to merge with other items.
Definition: object.h:568
#define RANDOM()
Definition: define.h:679
living stats
Str, Con, Dex, etc.
Definition: object.h:368
struct archt * arch
Pointer to archetype.
Definition: object.h:412
#define MAP_WIDTH(m)
Map width.
Definition: map.h:78
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Loads and returns the map requested.
Definition: style.c:180
This is a game-map.
Definition: map.h:325
void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP)
Place some monsters into the map.
Definition: monster.c:38