Atrinik Server  4.0
beacon.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 
32 #include <global.h>
33 #include <toolkit/string.h>
34 #include <object.h>
35 #include <object_methods.h>
36 #include <beacon.h>
37 #include <toolkit/path.h>
38 
42 typedef struct beacon {
44  object *obj;
45 
47  UT_hash_handle hh;
48 } beacon_t;
49 
51 static beacon_t *beacons = NULL;
52 
54 static void
55 init_func (object *op)
56 {
57  HARD_ASSERT(op != NULL);
58 
59  /* Figure out where the beacon is. */
60  object *env = object_get_env(op);
61 
62  /* If the beacon is on a unique map, we need to make the beacon name
63  * unique to the player that owns the unique map. */
64  if (MAP_UNIQUE(env->map) && !map_path_isabs(op->name)) {
65  char *filedir = path_dirname(env->map->path);
66  char *pl_name = path_basename(filedir);
67  char *joined = string_join("-", "/", pl_name, op->name, NULL);
68 
69  FREE_AND_COPY_HASH(op->name, joined);
70 
71  efree(joined);
72  efree(pl_name);
73  efree(filedir);
74  }
75 
76  beacon_t *beacon = emalloc(sizeof(*beacon));
77  beacon->obj = op;
78  HASH_ADD(hh, beacons, obj->name, sizeof(shstr *), beacon);
79 }
80 
82 static void
83 deinit_func (object *op)
84 {
86  HASH_FIND(hh, beacons, &op->name, sizeof(shstr *), beacon);
87  if (unlikely(beacon == NULL)) {
88  LOG(ERROR, "Beacon %s not found in hashtable", op->name);
89  return;
90  }
91 
92  HASH_DEL(beacons, beacon);
93  efree(beacon);
94 }
95 
100 {
101  OBJECT_METHODS(BEACON)->init_func = init_func;
102  OBJECT_METHODS(BEACON)->deinit_func = deinit_func;
103 }
104 
113 object *
114 beacon_locate (shstr *name)
115 {
116  HARD_ASSERT(name != NULL);
117 
118  beacon_t *beacon;
119  HASH_FIND(hh, beacons, &name, sizeof(shstr *), beacon);
120 
121  if (beacon != NULL) {
122  return beacon->obj;
123  }
124 
125  return NULL;
126 }
Definition: object.h:94
#define FREE_AND_COPY_HASH(_sv_, _nv_)
Definition: global.h:100
object * object_get_env(object *op)
Definition: object.c:631
shstr * path
Definition: map.h:568
static beacon_t * beacons
Definition: beacon.c:51
static void init_func(object *op)
Definition: beacon.c:55
OBJECT_TYPE_INIT_DEFINE(beacon)
Definition: beacon.c:99
UT_hash_handle hh
Definition: beacon.c:47
struct mapdef * map
Definition: object.h:139
const char * name
Definition: object.h:168
object * obj
Definition: beacon.c:44
object * beacon_locate(shstr *name)
Definition: beacon.c:114
static void deinit_func(object *op)
Definition: beacon.c:83
struct beacon beacon_t
#define OBJECT_METHODS(type)
#define MAP_UNIQUE(m)
Definition: map.h:96
Definition: beacon.c:42
#define BEACON
Definition: define.h:502