Atrinik Server  4.0
button.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 
37 static int
38 move_on_func (object *op, object *victim, object *originator, int state)
39 {
40  HARD_ASSERT(op != NULL);
41  HARD_ASSERT(victim != NULL);
42 
43  if (!DBL_EQUAL(op->speed, 0.0) ||
44  (op->stats.exp == -1 && op->value != 0)) {
45  return OBJECT_METHOD_OK;
46  }
47 
48  connection_trigger_button(op, state);
49  return OBJECT_METHOD_OK;
50 }
51 
53 static int
54 trigger_func (object *op, object *cause, int state)
55 {
56  HARD_ASSERT(op != NULL);
57  HARD_ASSERT(cause != NULL);
58 
59  op->value = state;
60 
61  if (state != 0 && cause->stats.exp != 0) {
62  op->speed = 1.0 / cause->stats.exp;
64  op->speed_left = -1;
65  }
66 
67  return OBJECT_METHOD_OK;
68 }
69 
71 static int
72 trigger_button_func (object *op, object *cause, int state)
73 {
74  HARD_ASSERT(op != NULL);
75  HARD_ASSERT(cause != NULL);
76 
77  uint32_t total = 0;
78  FOR_MAP_PREPARE(op->map, op->x, op->y, tmp) {
79  tmp = HEAD(tmp);
80  if (tmp == op) {
81  continue;
82  }
83 
84  if (QUERY_FLAG(tmp, FLAG_FLYING)) {
85  if (!QUERY_FLAG(op, FLAG_FLY_ON)) {
86  continue;
87  }
88  } else {
89  if (!QUERY_FLAG(op, FLAG_WALK_ON)) {
90  continue;
91  }
92  }
93 
94  total += tmp->weight * MAX(1, tmp->nrof) + tmp->carrying;
95  } FOR_MAP_FINISH();
96 
97  op->value = total >= op->weight;
98 
99  return OBJECT_METHOD_OK;
100 }
101 
103 static void
104 process_func (object *op)
105 {
106  HARD_ASSERT(op != NULL);
107 
108  op->speed = 0;
110 
111  if (op->stats.exp == -1) {
112  return;
113  }
114 
115  op->value = 0;
116  connection_trigger(op, op->value);
117 }
118 
123 {
124  OBJECT_METHODS(BUTTON)->move_on_func = move_on_func;
125  OBJECT_METHODS(BUTTON)->trigger_func = trigger_func;
126  OBJECT_METHODS(BUTTON)->trigger_button_func = trigger_button_func;
127  OBJECT_METHODS(BUTTON)->process_func = process_func;
128 }
static void process_func(object *op)
Definition: button.c:104
double speed_left
Definition: object.h:472
#define FLAG_FLY_ON
Definition: define.h:968
#define BUTTON
Definition: define.h:397
void connection_trigger_button(object *op, int state)
Definition: connection.c:272
int64_t exp
Definition: living.h:69
OBJECT_TYPE_INIT_DEFINE(button)
Definition: button.c:122
#define QUERY_FLAG(xyz, p)
Definition: define.h:761
static int trigger_button_func(object *op, object *cause, int state)
Definition: button.c:72
int16_t y
Definition: object.h:276
uint32_t weight
Definition: object.h:246
struct mapdef * map
Definition: object.h:139
#define FLAG_FLYING
Definition: define.h:918
double speed
Definition: object.h:469
static int trigger_func(object *op, object *cause, int state)
Definition: button.c:54
#define HEAD(op)
Definition: object.h:657
#define FOR_MAP_FINISH()
Definition: define.h:1759
static int move_on_func(object *op, object *victim, object *originator, int state)
Definition: button.c:38
void connection_trigger(object *op, int state)
Definition: connection.c:258
int16_t x
Definition: object.h:273
living stats
Definition: object.h:481
#define OBJECT_METHODS(type)
#define OBJECT_METHOD_OK
#define FLAG_WALK_ON
Definition: define.h:904
void object_update_speed(object *op)
Definition: object.c:1043
int64_t value
Definition: object.h:240
#define FOR_MAP_PREPARE(map_, mx_, my_, it_)
Definition: define.h:1752