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  x += freearr_x[freeindex];
73  y += freearr_y[freeindex];
75 
76  total_experience += this_monster->stats.exp;
77  for (at = new_monster->arch; at != NULL; at = at->more) {
78  number_monsters++;
79  }
80  RP->total_map_hp += new_monster->stats.hp; /* a global count */
81  } else {
82  failed_placements++;
83  }
84  exp_per_sq = ((double)1000*total_experience)/(MAP_WIDTH(map)*MAP_HEIGHT(map)+1);
85  }
86 }
global.h
INS_NO_WALK_ON
#define INS_NO_WALK_ON
Definition: object.h:571
random_map.h
diamondslots.x
x
Definition: diamondslots.py:15
place_monsters
void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP)
Definition: monster.cpp:38
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:3592
liv::hp
int16_t hp
Definition: living.h:40
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
RMParms
Definition: random_map.h:14
archt
Definition: object.h:472
liv::exp
int64_t exp
Definition: living.h:47
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:569
object_create_arch
object * object_create_arch(archetype *at)
Definition: arch.cpp:299
archt::more
struct archt * more
Definition: object.h:475
level_exp
int64_t level_exp(int level, double expmul)
Definition: living.cpp:1872
rproto.h
mapdef
Definition: map.h:317
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:2098
MAP_WIDTH
#define MAP_WIDTH(m)
Definition: map.h:78
RANDOM
#define RANDOM()
Definition: define.h:644
obj::arch
struct archt * arch
Definition: object.h:420
obj::stats
living stats
Definition: object.h:376
RMParms::total_map_hp
long unsigned int total_map_hp
Definition: random_map.h:93
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:80
freearr_x
short freearr_x[SIZEOFFREE]
Definition: object.cpp:299
RMParms::Xsize
int Xsize
Definition: random_map.h:74