Crossfire Server, Trunk  R21226
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  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);
72  x += freearr_x[freeindex];
73  y += freearr_y[freeindex];
74  object_insert_in_map_at(new_monster, map, NULL, INS_NO_MERGE|INS_NO_WALK_ON, x, y);
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 }
int64_t level_exp(int level, double expmul)
Definition: living.c:1847
void object_copy_with_inv(const object *src_ob, object *dest_ob)
Definition: object.c:1006
#define MAP_HEIGHT(m)
Definition: map.h:80
short freearr_x[SIZEOFFREE]
Definition: object.c:65
int64_t exp
Definition: living.h:47
object * pick_random_object(mapstruct *style)
Definition: style.c:289
Definition: object.h:465
int16_t hp
Definition: living.h:40
short freearr_y[SIZEOFFREE]
Definition: object.c:71
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Definition: object.c:1933
long unsigned int total_map_hp
Definition: random_map.h:88
struct archt * more
Definition: object.h:469
#define INS_NO_WALK_ON
Definition: object.h:568
object * object_create_arch(archetype *at)
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)
Definition: object.c:3475
signed __int64 int64_t
Definition: win32.h:168
int Xsize
Definition: random_map.h:69
#define INS_NO_MERGE
Definition: object.h:566
#define RANDOM()
Definition: define.h:681
living stats
Definition: object.h:368
struct archt * arch
Definition: object.h:412
#define MAP_WIDTH(m)
Definition: map.h:78
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Definition: style.c:180
Definition: map.h:325
void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP)
Definition: monster.c:38