Crossfire Server, Trunk  R20513
decor.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 <assert.h>
20 #include <stdlib.h>
21 
22 #include "global.h"
23 #include "random_map.h"
24 #include "rproto.h"
25 
27 #define NR_DECOR_OPTIONS 1
28 
39 int obj_count_in_map(mapstruct *map, int x, int y)
40 {
41  int count = 0;
42 
43  FOR_MAP_PREPARE(map, x, y, tmp)
44  count++;
46  return count;
47 }
48 
65 void put_decor(mapstruct *map, char **maze, char *decorstyle, int decor_option, RMParms *RP)
66 {
67  mapstruct *decor_map;
68  char style_name[256];
69 
70  snprintf(style_name, sizeof(style_name), "/styles/decorstyles");
71 
72  decor_map = find_style(style_name, decorstyle, -1);
73  if (decor_map == NULL) {
74  return;
75  }
76 
77  /* pick a random option, only 1 option right now. */
78  if (decor_option == 0) {
79  decor_option = RANDOM()%NR_DECOR_OPTIONS+1;
80  }
81 
82  assert(decor_option != 0);
83 
84  switch (decor_option) {
85  case 1: { /* random placement of decor objects. */
86  int number_to_place = RANDOM()%((RP->Xsize*RP->Ysize)/5);
87  int failures = 0;
88  object *new_decor_object;
89 
90  while (failures < 100 && number_to_place > 0) {
91  int x, y;
92 
93  x = RANDOM()%(RP->Xsize-2)+1;
94  y = RANDOM()%(RP->Ysize-2)+1;
95  if (maze[x][y] == 0 && obj_count_in_map(map, x, y) < 2) { /* empty */
96  object *this_object;
97 
98  new_decor_object = pick_random_object(decor_map);
99  this_object = arch_to_object(new_decor_object->arch);
100  object_copy(new_decor_object, this_object);
101  /*
102  * Don't change move_block, this prevents item merging.
103  * Instead, fix the item on the style map if blocking
104  * is bad.
105  */
106  /*this_object->move_block = MOVE_BLOCK_DEFAULT;*/
107  object_insert_in_map_at(this_object, map, NULL, 0, x, y);
108  number_to_place--;
109  } else {
110  failures++;
111  }
112  }
113  break;
114  }
115 
116  default: { /* place decor objects everywhere: tile the map. */
117  int i, j;
118 
119  for (i = 1; i < RP->Xsize-1; i++)
120  for (j = 1; j < RP->Ysize-1; j++) {
121  if (maze[i][j] == 0) {
122  object *new_decor_object, *this_object;
123 
124  new_decor_object = pick_random_object(decor_map);
125  this_object = arch_to_object(new_decor_object->arch);
126  object_copy(new_decor_object, this_object);
127  /*
128  * Don't change move_block, this prevents item merging.
129  * Instead, fix the item on the style map if blocking
130  * is bad.
131  */
132  /*this_object->move_block = MOVE_BLOCK_DEFAULT;*/
133  object_insert_in_map_at(this_object, map, NULL, 0, i, j);
134  }
135  }
136  break;
137  }
138  }
139 }
Random map parameters.
Definition: random_map.h:14
Random map related variables.
#define NR_DECOR_OPTIONS
Number of decor styles that can be chosen if none specified.
Definition: decor.c:27
Global type definitions and header inclusions.
object * pick_random_object(mapstruct *style)
Picks a random object from a style map.
Definition: style.c:286
object * object_insert_in_map_at(object *op, mapstruct *m, object *originator, int flag, int x, int y)
Same as object_insert_in_map() except it handle separate coordinates and do a clean job preparing mul...
Definition: object.c:1921
void put_decor(mapstruct *map, char **maze, char *decorstyle, int decor_option, RMParms *RP)
Put the decor into the map.
Definition: decor.c:65
#define snprintf
Definition: win32.h:46
int obj_count_in_map(mapstruct *map, int x, int y)
Count objects at a spot.
Definition: decor.c:39
int Ysize
Definition: random_map.h:70
#define FOR_MAP_FINISH()
Finishes FOR_MAP_PREPARE().
Definition: define.h:765
int Xsize
Definition: random_map.h:69
#define RANDOM()
Definition: define.h:679
struct archt * arch
Pointer to archetype.
Definition: object.h:412
void object_copy(const object *src_ob, object *dest_ob)
Copy object first frees everything allocated by the second object, and then copies the contents of th...
Definition: object.c:838
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Constructs a loop iterating over all objects of a map tile.
Definition: define.h:758
mapstruct * find_style(const char *dirname, const char *stylename, int difficulty)
Loads and returns the map requested.
Definition: style.c:180
This is a game-map.
Definition: map.h:325
object * arch_to_object(archetype *at)
Creates and returns a new object which is a copy of the given archetype.
Definition: arch.c:571