Crossfire Server, Trunk
monster.cpp
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  mapstruct *style_map = NULL;
41  int failed_placements;
42  int64_t exp_per_sq, total_experience;
43  int number_monsters = 0;
44  archetype *at;
45 
46  const char *styledirname = "/styles/monsterstyles";
47  style_map = find_style(styledirname, monsterstyle, difficulty);
48  if (style_map == NULL) {
49  return;
50  }
51 
52  /* fill up the map with random monsters from the monster style*/
53 
54  total_experience = 0;
55  failed_placements = 0;
56  exp_per_sq = 0;
57  while (exp_per_sq <= level_exp(difficulty, 1.0)
58  && failed_placements < 100
59  && number_monsters < (RP->Xsize*RP->Ysize)/8) {
60  object *this_monster = pick_random_object(style_map);
61  int x, y, freeindex;
62 
63  if (this_monster == NULL) {
64  return; /* no monster?? */
65  }
66  x = RANDOM()%RP->Xsize;
67  y = RANDOM()%RP->Ysize;
68  freeindex = object_find_first_free_spot(this_monster, map, x, y);
69  if (freeindex != -1) {
70  object *new_monster = object_create_arch(this_monster->arch);
71  object_copy_with_inv(this_monster, new_monster, true);
72  // If we have the means to spawn artifact monsters, let's try to.
73  if (rndm(0,9))
74  generate_artifact(new_monster, difficulty);
75  x += freearr_x[freeindex];
76  y += freearr_y[freeindex];
78 
79  total_experience += this_monster->stats.exp;
80  for (at = new_monster->arch; at != NULL; at = at->more) {
81  number_monsters++;
82  }
83  RP->total_map_hp += new_monster->stats.hp; /* a global count */
84  } else {
85  failed_placements++;
86  }
87  exp_per_sq = ((double)1000*total_experience)/(MAP_WIDTH(map)*MAP_HEIGHT(map)+1);
88  }
89 }
living::exp
int64_t exp
Definition: living.h:47
global.h
INS_NO_WALK_ON
#define INS_NO_WALK_ON
Definition: object.h:582
random_map.h
archetype::more
archetype * more
Definition: object.h:486
diamondslots.x
x
Definition: diamondslots.py:15
place_monsters
void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP)
Definition: monster.cpp:38
object::arch
struct archetype * arch
Definition: object.h:424
RMParms::Ysize
int Ysize
Definition: random_map.h:75
object_find_first_free_spot
int object_find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
Definition: object.cpp:3599
find_style
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Definition: style.cpp:180
object_copy_with_inv
void object_copy_with_inv(const object *src_ob, object *dest_ob, bool update_speed)
Definition: object.cpp:1208
rndm
int rndm(int min, int max)
Definition: utils.cpp:162
RMParms
Definition: random_map.h:14
disinfect.map
map
Definition: disinfect.py:4
freearr_y
short freearr_y[SIZEOFFREE]
Definition: object.cpp:305
INS_NO_MERGE
#define INS_NO_MERGE
Definition: object.h:580
object_create_arch
object * object_create_arch(archetype *at)
Definition: arch.cpp:298
level_exp
int64_t level_exp(int level, double expmul)
Definition: living.cpp:1881
archetype
Definition: object.h:483
rproto.h
object_insert_in_map_at
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.cpp:2100
MAP_WIDTH
#define MAP_WIDTH(m)
Definition: map.h:73
RANDOM
#define RANDOM()
Definition: define.h:644
RMParms::total_map_hp
long unsigned int total_map_hp
Definition: random_map.h:93
mapstruct
Definition: map.h:313
pick_random_object
object * pick_random_object(mapstruct *style)
Definition: style.cpp:289
diamondslots.y
y
Definition: diamondslots.py:16
MAP_HEIGHT
#define MAP_HEIGHT(m)
Definition: map.h:75
object::stats
living stats
Definition: object.h:378
freearr_x
short freearr_x[SIZEOFFREE]
Definition: object.cpp:299
generate_artifact
void generate_artifact(object *op, int difficulty)
Definition: artifact.cpp:177
RMParms::Xsize
int Xsize
Definition: random_map.h:74
living::hp
int16_t hp
Definition: living.h:40