Atrinik Server  4.0
magic_mirror.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
37 #include <global.h>
38 #include <object.h>
39 #include <object_methods.h>
40 #include <magic_mirror.h>
41 
43 static void
44 init_func (object *op)
45 {
46  HARD_ASSERT(op != NULL);
47 
48  if (op->map == NULL) {
49  LOG(ERROR, "Magic mirror not on map.");
50  return;
51  }
52 
53  int mirror_x = (op->stats.hp == -1 ? op->x : op->stats.hp);
54  int mirror_y = (op->stats.sp == -1 ? op->y : op->stats.sp);
55 
56  /* X/Y adjust. */
57  if (op->stats.maxhp != 0) {
58  mirror_x += op->stats.maxhp;
59  }
60 
61  if (op->stats.maxsp != 0) {
62  mirror_y += op->stats.maxsp;
63  }
64 
65  /* No point in doing anything special if we're mirroring the same map. */
66  if (op->slaying == NULL && mirror_x == op->x && mirror_y == op->y) {
67  return;
68  }
69 
70  /* No map path specified, use mirror's map path. */
71  if (op->slaying == NULL) {
73  } else if (!map_path_isabs(op->slaying)) {
74  char *path = map_get_path(op->map,
75  op->slaying,
76  MAP_UNIQUE(op->map),
77  NULL);
78  FREE_AND_COPY_HASH(op->slaying, path);
79  efree(path);
80  }
81 
82  op->custom_attrset = emalloc(sizeof(magic_mirror_struct));
83  /* Save x/y and clear map. */
84  MMIRROR(op)->x = mirror_x;
85  MMIRROR(op)->y = mirror_y;
86  MMIRROR(op)->map = NULL;
87 }
88 
90 static void
91 deinit_func (object *op)
92 {
93  HARD_ASSERT(op != NULL);
94 
95  if (op->custom_attrset != NULL) {
96  efree(op->custom_attrset);
97  op->custom_attrset = NULL;
98  }
99 }
100 
105 {
106  OBJECT_METHODS(MAGIC_MIRROR)->init_func = init_func;
107  OBJECT_METHODS(MAGIC_MIRROR)->deinit_func = deinit_func;
108 }
109 
120 mapstruct *
122 {
123  HARD_ASSERT(op != NULL);
124 
126 
127  /* Map good to go? */
128  if (data->map && data->map->in_memory == MAP_IN_MEMORY) {
129  /* Reset timeout so the mirrored map doesn't get swapped out. */
131  return data->map;
132  }
133 
134  /* Try to load the map. */
135  data->map = ready_map_name(op->slaying, NULL, MAP_NAME_SHARED);
136  if (data->map == NULL) {
137  LOG(ERROR, "Could not load map '%s': %s",
138  op->slaying,
139  object_get_str(op));
140  return NULL;
141  }
142 
143  return data->map;
144 }
#define FREE_AND_ADD_REF_HASH(_sv_, _nv_)
Definition: global.h:116
#define FREE_AND_COPY_HASH(_sv_, _nv_)
Definition: global.h:100
#define MAP_IN_MEMORY
Definition: map.h:170
void * custom_attrset
Definition: object.h:160
shstr * path
Definition: map.h:568
uint32_t in_memory
Definition: map.h:627
static void init_func(object *op)
Definition: magic_mirror.c:44
static void deinit_func(object *op)
Definition: magic_mirror.c:91
const char * slaying
Definition: object.h:180
mapstruct * magic_mirror_get_map(object *op)
Definition: magic_mirror.c:121
int16_t sp
Definition: living.h:78
#define MMIRROR(ob)
Definition: object.h:678
#define MAGIC_MIRROR
Definition: define.h:206
int16_t maxsp
Definition: living.h:81
int32_t hp
Definition: living.h:72
int16_t y
Definition: object.h:276
int32_t maxhp
Definition: living.h:75
#define MAP_DEFAULTTIMEOUT
Definition: config.h:108
const char * object_get_str(const object *op)
Definition: object.c:3151
struct mapdef * map
Definition: object.h:664
struct mapdef * map
Definition: object.h:139
union @21 data
Data about the rule.
OBJECT_TYPE_INIT_DEFINE(magic_mirror)
Definition: magic_mirror.c:104
int16_t x
Definition: object.h:273
#define MAP_TIMEOUT(m)
Definition: map.h:90
#define MAP_NAME_SHARED
Definition: map.h:157
living stats
Definition: object.h:481
#define OBJECT_METHODS(type)
#define MAP_UNIQUE(m)
Definition: map.h:96
mapstruct * ready_map_name(const char *name, mapstruct *originator, int flags)
Definition: map.c:1584
Definition: map.h:536