Debian Codegraph @

random_maps/monster.c

raw symbols SCIP
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 <[email protected]>.
12 */
13
14 /**
15 * @file
16 * Monster placement for random maps.
17 */
18
19 #include "global.h"
20
21 #include <stdlib.h>
22
23 #include "random_map.h"
24 #include "rproto.h"
25
26 /**
27 * Place some monsters into the map. Their experience is linked to difficulty to insert a
28 * suitable number of critters.
29 * @param map
30 * where to put monsters on.
31 * @param monsterstyle
32 * style. Can be NULL, in which case a random one is used.
33 * @param difficulty
34 * how difficult the monsters should be, and how many there should be.
35 * @param RP
36 * random map parameters.
37 */
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 }