Atrinik Server  4.0
creator.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 <object.h>
34 #include <object_methods.h>
35 
47 static bool
48 creator_obj_exists (object *op, object *check)
49 {
50  object *tmp;
51  FOR_MAP_LAYER_BEGIN(op->map, op->x, op->y, check->layer, -1, tmp) {
52  if (tmp->arch == check->arch &&
53  tmp->name == check->name &&
54  tmp->type == check->type) {
55  return true;
56  }
58 
59  return false;
60 }
61 
63 static int
64 trigger_func (object *op, object *cause, int state)
65 {
66  HARD_ASSERT(op != NULL);
67  HARD_ASSERT(cause != NULL);
68 
69  if (op->stats.hp <= 0 && !QUERY_FLAG(op, FLAG_LIFESAVE)) {
70  return OBJECT_METHOD_OK;
71  }
72 
73  bool created = false;
74  int roll = -1;
75  if (QUERY_FLAG(op, FLAG_SPLITTING)) {
76  int num_objs = 0;
77 
78  FOR_INV_PREPARE(op, tmp) {
79  if (tmp->type == EVENT_OBJECT) {
80  continue;
81  }
82 
83  num_objs++;
84  } FOR_INV_FINISH();
85 
86  roll = rndm(0, num_objs - 1);
87  }
88 
89  int idx = 0;
90  FOR_INV_PREPARE(op, tmp) {
91  if (tmp->type == EVENT_OBJECT) {
92  continue;
93  }
94 
95  if (roll != -1 && roll != idx++) {
96  continue;
97  }
98 
99  if (QUERY_FLAG(op, FLAG_ONE_DROP) && creator_obj_exists(op, tmp)) {
100  continue;
101  }
102 
103  object *clone = object_clone(tmp);
104  clone->x = op->x;
105  clone->y = op->y;
106  clone = object_insert_map(clone, op->map, op, 0);
107  if (clone != NULL) {
108  created = true;
109  }
110  } FOR_INV_FINISH();
111 
112  if (created && !QUERY_FLAG(op, FLAG_LIFESAVE)) {
113  op->stats.hp--;
114  }
115 
116  return OBJECT_METHOD_OK;
117 }
118 
123 {
124  OBJECT_METHODS(CREATOR)->trigger_func = trigger_func;
125 }
#define CREATOR
Definition: define.h:242
#define FOR_INV_PREPARE(op_, it_)
Definition: define.h:1691
#define FOR_INV_FINISH()
Definition: define.h:1698
OBJECT_TYPE_INIT_DEFINE(creator)
Definition: creator.c:122
static bool creator_obj_exists(object *op, object *check)
Definition: creator.c:48
uint8_t layer
Definition: object.h:405
static int trigger_func(object *op, object *cause, int state)
Definition: creator.c:64
#define QUERY_FLAG(xyz, p)
Definition: define.h:761
#define EVENT_OBJECT
Definition: define.h:481
struct archetype * arch
Definition: object.h:225
int32_t hp
Definition: living.h:72
object * object_clone(const object *op)
Definition: object.c:2385
int16_t y
Definition: object.h:276
#define FLAG_LIFESAVE
Definition: define.h:1125
#define FLAG_SPLITTING
Definition: define.h:996
struct mapdef * map
Definition: object.h:139
const char * name
Definition: object.h:168
#define FLAG_ONE_DROP
Definition: define.h:1302
#define FOR_MAP_LAYER_END
Definition: define.h:1657
int16_t x
Definition: object.h:273
#define FOR_MAP_LAYER_BEGIN(_m, _x, _y, _layer, _sub_layer, _obj)
Definition: define.h:1630
object * object_insert_map(object *op, mapstruct *m, object *originator, int flag)
Definition: object.c:1741
living stats
Definition: object.h:481
uint8_t type
Definition: object.h:360
#define OBJECT_METHODS(type)
#define OBJECT_METHOD_OK