Crossfire Server, Branches 1.12  R18729
monster.c
Go to the documentation of this file.
1 /*
2  * static char *rcsid_monster_c =
3  * "$Id: monster.c 11578 2009-02-23 22:02:27Z lalo $";
4  */
5 
6 /*
7  CrossFire, A Multiplayer game for X-windows
8 
9  Copyright (C) 2002 Mark Wedel & Crossfire Development Team
10  Copyright (C) 1992 Frank Tore Johansen
11 
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 
26  The authors can be reached via e-mail at crossfire-devel@real-time.com
27 */
28 
34 #include <global.h>
35 #include <random_map.h>
36 #include <rproto.h>
37 
49 void insert_multisquare_ob_in_map(object *new_obj, mapstruct *map) {
50  int x, y;
51  archetype *at;
52  object *old_seg;
53  object *head;
54 
55  /* first insert the head */
56  insert_ob_in_map(new_obj, map, new_obj, INS_NO_MERGE|INS_NO_WALK_ON);
57 
58  x = new_obj->x;
59  y = new_obj->y;
60  old_seg = new_obj;
61  head = new_obj;
62  for (at = new_obj->arch->more; at != NULL; at = at->more) {
63  object *new_seg;
64 
65  new_seg = arch_to_object(at);
66  new_seg->x = x+at->clone.x;
67  new_seg->y = y+at->clone.y;
68  new_seg->map = old_seg->map;
69  insert_ob_in_map(new_seg, new_seg->map, new_seg, INS_NO_MERGE|INS_NO_WALK_ON);
70  new_seg->head = head;
71  old_seg->more = new_seg;
72  old_seg = new_seg;
73  }
74  old_seg->more = NULL;
75 }
76 
89 void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP) {
90  char styledirname[256];
91  mapstruct *style_map = NULL;
92  int failed_placements;
93  sint64 exp_per_sq, total_experience;
94  int number_monsters = 0;
95  archetype *at;
96 
97  snprintf(styledirname, sizeof(styledirname), "%s", "/styles/monsterstyles");
98  style_map = find_style(styledirname, monsterstyle, difficulty);
99  if (style_map == NULL)
100  return;
101 
102  /* fill up the map with random monsters from the monster style*/
103 
104  total_experience = 0;
105  failed_placements = 0;
106  exp_per_sq = 0;
107  while (exp_per_sq <= level_exp(difficulty, 1.0)
108  && failed_placements < 100
109  && number_monsters < (RP->Xsize*RP->Ysize)/8) {
110  object *this_monster = pick_random_object(style_map);
111  int x, y, freeindex;
112 
113  if (this_monster == NULL)
114  return; /* no monster?? */
115  x = RANDOM()%RP->Xsize;
116  y = RANDOM()%RP->Ysize;
117  freeindex = find_first_free_spot(this_monster, map, x, y);
118  if (freeindex != -1) {
119  object *new_monster = arch_to_object(this_monster->arch);
120 
121  x += freearr_x[freeindex];
122  y += freearr_y[freeindex];
123  copy_object_with_inv(this_monster, new_monster);
124  new_monster->x = x;
125  new_monster->y = y;
126  insert_multisquare_ob_in_map(new_monster, map);
127  total_experience += this_monster->stats.exp;
128  for (at = new_monster->arch; at != NULL; at = at->more)
129  number_monsters++;
130  RP->total_map_hp += new_monster->stats.hp; /* a global count */
131  } else {
132  failed_placements++;
133  }
134  exp_per_sq = ((double)1000*total_experience)/(MAP_WIDTH(map)*MAP_HEIGHT(map)+1);
135  }
136 }
#define MAP_HEIGHT(m)
Definition: map.h:99
object clone
Definition: object.h:326
short freearr_x[SIZEOFFREE]
Definition: object.c:75
sint64 level_exp(int level, double expmul)
Definition: living.c:1788
sint64 exp
Definition: living.h:88
sint16 x
Definition: object.h:179
object * pick_random_object(mapstruct *style)
Definition: style.c:275
Definition: object.h:321
sint16 hp
Definition: living.h:81
short freearr_y[SIZEOFFREE]
Definition: object.c:81
long unsigned int total_map_hp
Definition: random_map.h:77
struct mapdef * map
Definition: object.h:155
int find_first_free_spot(const object *ob, mapstruct *m, int x, int y)
Definition: object.c:3240
struct archt * more
Definition: object.h:325
#define INS_NO_WALK_ON
Definition: object.h:396
sint16 y
Definition: object.h:179
object * insert_ob_in_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:1992
int Ysize
Definition: random_map.h:60
int Xsize
Definition: random_map.h:59
int snprintf(char *dest, int max, const char *format,...)
Definition: porting.c:498
#define INS_NO_MERGE
Definition: object.h:394
living stats
Definition: object.h:219
struct archt * arch
Definition: object.h:263
void insert_multisquare_ob_in_map(object *new_obj, mapstruct *map)
Definition: monster.c:49
#define MAP_WIDTH(m)
Definition: map.h:97
void copy_object_with_inv(object *src_ob, object *dest_ob)
Definition: object.c:862
struct obj * head
Definition: object.h:154
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Definition: style.c:177
Definition: map.h:346
void place_monsters(mapstruct *map, char *monsterstyle, int difficulty, RMParms *RP)
Definition: monster.c:89
struct obj * more
Definition: object.h:153
object * arch_to_object(archetype *at)
Definition: arch.c:576