Crossfire Server, Trunk  R20608
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 
69  decor_map = find_style("/styles/decorstyles", decorstyle, -1);
70  if (decor_map == NULL) {
71  return;
72  }
73 
74  /* pick a random option, only 1 option right now. */
75  if (decor_option == 0) {
76  decor_option = RANDOM()%NR_DECOR_OPTIONS+1;
77  }
78 
79  assert(decor_option != 0);
80 
81  switch (decor_option) {
82  case 1: { /* random placement of decor objects. */
83  int number_to_place = RANDOM()%((RP->Xsize*RP->Ysize)/5);
84  int failures = 0;
85  object *new_decor_object;
86 
87  while (failures < 100 && number_to_place > 0) {
88  int x, y;
89 
90  x = RANDOM()%(RP->Xsize-2)+1;
91  y = RANDOM()%(RP->Ysize-2)+1;
92  if (maze[x][y] == 0 && obj_count_in_map(map, x, y) < 2) { /* empty */
93  object *this_object;
94 
95  new_decor_object = pick_random_object(decor_map);
96  this_object = arch_to_object(new_decor_object->arch);
97  object_copy(new_decor_object, this_object);
98  /*
99  * Don't change move_block, this prevents item merging.
100  * Instead, fix the item on the style map if blocking
101  * is bad.
102  */
103  /*this_object->move_block = MOVE_BLOCK_DEFAULT;*/
104  object_insert_in_map_at(this_object, map, NULL, 0, x, y);
105  number_to_place--;
106  } else {
107  failures++;
108  }
109  }
110  break;
111  }
112 
113  default: { /* place decor objects everywhere: tile the map. */
114  int i, j;
115 
116  for (i = 1; i < RP->Xsize-1; i++)
117  for (j = 1; j < RP->Ysize-1; j++) {
118  if (maze[i][j] == 0) {
119  object *new_decor_object, *this_object;
120 
121  new_decor_object = pick_random_object(decor_map);
122  this_object = arch_to_object(new_decor_object->arch);
123  object_copy(new_decor_object, this_object);
124  /*
125  * Don't change move_block, this prevents item merging.
126  * Instead, fix the item on the style map if blocking
127  * is bad.
128  */
129  /*this_object->move_block = MOVE_BLOCK_DEFAULT;*/
130  object_insert_in_map_at(this_object, map, NULL, 0, i, j);
131  }
132  }
133  break;
134  }
135  }
136 }
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
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:569