Crossfire Server, Trunk  1.75.0
creator.cpp
Go to the documentation of this file.
1 /*
2  CrossFire, A Multiplayer game for X-windows
3 
4  Copyright (C) 2008 Crossfire Development Team
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 
20  The authors can be reached via e-mail at crossfire-devel@real-time.com
21 */
22 
28 #include <global.h>
29 #include <ob_methods.h>
30 #include <ob_types.h>
31 #include <sounds.h>
32 #include <sproto.h>
33 
34 static method_ret creator_type_process(object *op);
35 static method_ret creator_type_trigger(object *op, object *cause, int state);
36 
40 void init_type_creator(void) {
43 }
44 
64 static void move_creator(object *creator) {
65  object *new_ob;
66 
67  if (!QUERY_FLAG(creator, FLAG_LIFESAVE) && --creator->stats.hp < 0) {
68  creator->stats.hp = -1;
69  return;
70  }
71 
72  if (creator->inv != NULL) {
73  int i;
74  object *ob_to_copy;
75 
76  /* select random object from inventory to copy */
77  ob_to_copy = creator->inv;
78  i = 1;
79  FOR_BELOW_PREPARE(creator->inv, ob) {
80  if (rndm(0, i) == 0) {
81  ob_to_copy = ob;
82  }
83  i++;
84  } FOR_BELOW_FINISH();
85  new_ob = object_create_clone(ob_to_copy);
88  } else {
89  if (creator->other_arch == NULL) {
90  LOG(llevError, "move_creator: Creator doesn't have other arch set: %s (%s, %d, %d)\n", creator->name ? creator->name : "(null)", creator->map->path, creator->x, creator->y);
91  return;
92  }
93 
94  new_ob = object_create_arch(creator->other_arch);
95  fix_generated_item(new_ob, creator, 0, 0, GT_MINIMAL);
96  }
97 
98  /* Make sure this multipart object fits */
99  if (new_ob->arch->more && ob_blocked(new_ob, creator->map, creator->x, creator->y)) {
101  return;
102  }
103 
104  if (creator->level != 0)
105  new_ob->level = creator->level;
106 
107  int flags = 0;
108  // Use the INS_BELOW_ORIGINATOR when creator is labelled with IS_FLOOR
109  if (QUERY_FLAG(creator, FLAG_IS_FLOOR))
111 
112  object_insert_in_map_at(new_ob, creator->map, creator, flags, creator->x, creator->y);
113  if (QUERY_FLAG(new_ob, FLAG_FREED))
114  return;
115 
116  if (creator->slaying) {
117  FREE_AND_COPY(new_ob->name, creator->slaying);
118  if (new_ob->title) {
119  FREE_AND_CLEAR_STR(new_ob->title);
120  }
121  }
122 }
123 
129 static method_ret creator_type_process(object *op) {
130  move_creator(op);
131  return METHOD_OK;
132 }
133 
141 static method_ret creator_type_trigger(object *op, object *cause, int state) {
142  (void)cause;
143  (void)state;
144  move_creator(op);
145  return METHOD_OK;
146 }
creator_type_trigger
static method_ret creator_type_trigger(object *op, object *cause, int state)
A creator is triggered.
Definition: creator.cpp:141
creator_type_process
static method_ret creator_type_process(object *op)
Processes a creator.
Definition: creator.cpp:129
global.h
llevError
@ llevError
Error, serious thing.
Definition: logger.h:11
LOG
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.cpp:58
archetype::more
archetype * more
Next part of a linked object.
Definition: object.h:486
object::inv
object * inv
Pointer to the first object in the inventory.
Definition: object.h:298
QUERY_FLAG
#define QUERY_FLAG(xyz, p)
Definition: define.h:226
object::arch
struct archetype * arch
Pointer to archetype.
Definition: object.h:424
FOR_BELOW_PREPARE
#define FOR_BELOW_PREPARE(op_, it_)
Constructs a loop iterating over all objects below an object.
Definition: define.h:698
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
object::x
int16_t x
Definition: object.h:335
object::map
struct mapstruct * map
Pointer to the map in which this object is present.
Definition: object.h:305
fix_generated_item
void fix_generated_item(object *op, object *creator, int difficulty, int max_magic, int flags)
fix_generated_item(): This is called after an item is generated, in order to set it up right.
Definition: treasure.cpp:927
flags
static const flag_definition flags[]
Flag mapping.
Definition: gridarta-types-convert.cpp:101
CREATOR
@ CREATOR
Definition: object.h:147
rndm
int rndm(int min, int max)
Returns a number between min and max.
Definition: utils.cpp:162
register_process
void register_process(int ob_type, process_func method)
Registers the process method for the given type.
Definition: ob_types.cpp:71
object::title
sstring title
Of foo, etc.
Definition: object.h:325
mapstruct::path
char path[HUGE_BUF]
Filename of the map.
Definition: map.h:355
object::level
int16_t level
Level of creature or object.
Definition: object.h:361
FLAG_IS_FLOOR
#define FLAG_IS_FLOOR
Can't see what's underneath this object.
Definition: define.h:302
FOR_BELOW_FINISH
#define FOR_BELOW_FINISH()
Finishes FOR_BELOW_PREPARE().
Definition: define.h:705
init_type_creator
void init_type_creator(void)
Initializer for the creator object type.
Definition: creator.cpp:40
FLAG_IS_A_TEMPLATE
#define FLAG_IS_A_TEMPLATE
Object has no ingame life until instantiated.
Definition: define.h:366
object::y
int16_t y
Position in the map for this object.
Definition: object.h:335
object_free_drop_inventory
void object_free_drop_inventory(object *ob)
Frees everything allocated by an object, removes it from the list of used objects,...
Definition: object.cpp:1545
FLAG_FREED
#define FLAG_FREED
Object is in the list of free objects.
Definition: define.h:233
FREE_AND_COPY
#define FREE_AND_COPY(sv, nv)
Release the shared string if not NULL, and make it a reference to nv.
Definition: global.h:206
object_create_arch
object * object_create_arch(archetype *at)
Create a full object using the given archetype.
Definition: arch.cpp:296
INS_BELOW_ORIGINATOR
#define INS_BELOW_ORIGINATOR
Insert new object immediately below originator.
Definition: object.h:584
sproto.h
ob_blocked
int ob_blocked(const object *ob, mapstruct *m, int16_t x, int16_t y)
Returns true if the given object can't fit in the given spot.
Definition: map.cpp:489
object_insert_in_map_at
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.cpp:2085
move_creator
static void move_creator(object *creator)
Move function for creator objects.
Definition: creator.cpp:64
object::other_arch
struct archetype * other_arch
Pointer used for various things - mostly used for what this objects turns into or what this object cr...
Definition: object.h:425
object_create_clone
object * object_create_clone(object *asrc)
Create clone from object to another.
Definition: object.cpp:3894
FREE_AND_CLEAR_STR
#define FREE_AND_CLEAR_STR(xyz)
Release the shared string, and set it to NULL.
Definition: global.h:200
register_trigger
void register_trigger(int ob_type, trigger_func method)
Registers the trigger method for the given type.
Definition: ob_types.cpp:98
method_ret
char method_ret
Define some standard return values for callbacks which don't need to return any other results.
Definition: ob_methods.h:14
ob_types.h
sounds.h
object::slaying
sstring slaying
Which race to do double damage to.
Definition: object.h:327
object::name
sstring name
The name of the object, obviously...
Definition: object.h:319
object_unset_flag_inv
void object_unset_flag_inv(object *op, int flag)
Desactivate recursively a flag on an object inventory.
Definition: object.cpp:3241
CLEAR_FLAG
#define CLEAR_FLAG(xyz, p)
Definition: define.h:225
ob_methods.h
object::stats
living stats
Str, Con, Dex, etc.
Definition: object.h:378
GT_MINIMAL
@ GT_MINIMAL
Do minimal adjustments, don't make artifacts, and so on.
Definition: treasure.h:36
living::hp
int16_t hp
Hit Points.
Definition: living.h:40
FLAG_LIFESAVE
#define FLAG_LIFESAVE
Saves a players' life once, then destr.
Definition: define.h:305