Atrinik Server  4.0
handle.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 #include <key.h>
36 
38 static int
39 apply_func (object *op, object *applier, int aflags)
40 {
41  HARD_ASSERT(op != NULL);
42  HARD_ASSERT(applier != NULL);
43 
44  if (op->slaying != NULL) {
45  if (key_match(applier, op) == NULL) {
46  draw_info_format(COLOR_WHITE, applier,
47  "The %s is locked.",
48  op->name);
49  return OBJECT_METHOD_OK;
50  }
51  } else if (!DBL_EQUAL(op->speed, 0.0) ||
52  (op->stats.exp == -1 && op->value != 0)) {
53  if (op->msg != NULL) {
54  draw_info(COLOR_WHITE, applier, op->msg);
55  } else {
56  draw_info_format(COLOR_WHITE, applier,
57  "The %s won't budge.",
58  op->name);
59  }
60 
61  return OBJECT_METHOD_OK;
62  }
63 
64  /* Toggle the state. */
65  op->value = !op->value;
66  op->state = op->value;
67  SET_ANIMATION(op, (((NUM_ANIMATIONS(op) / NUM_FACINGS(op)) *
68  op->direction) + op->value));
70 
71  /* Inform the applier. */
72  if (op->msg != NULL) {
73  draw_info(COLOR_WHITE, applier, op->msg);
74  } else {
75  draw_info_format(COLOR_WHITE, applier,
76  "You turn the %s.",
77  op->name);
78  play_sound_map(op->map,
79  CMD_SOUND_EFFECT,
80  "pull.ogg",
81  op->x,
82  op->y,
83  0,
84  0);
85  }
86 
87  connection_trigger(op, op->value);
88 
89  if (op->stats.exp != 0) {
90  op->speed = 1.0 / op->stats.exp;
91  op->speed_left = -1;
92  } else {
93  op->speed = 0;
94  }
95 
97  return OBJECT_METHOD_OK;
98 }
99 
101 static int
102 trigger_func (object *op, object *cause, int state)
103 {
104  HARD_ASSERT(op != NULL);
105  HARD_ASSERT(cause != NULL);
106 
107  if (!DBL_EQUAL(op->speed, 0.0) ||
108  (op->stats.exp == -1 && op->value != 0)) {
109  return OBJECT_METHOD_OK;
110  }
111 
112  op->value = state;
113  op->state = op->value;
114  SET_ANIMATION(op, (((NUM_ANIMATIONS(op) / NUM_FACINGS(op)) *
115  op->direction) + op->value));
117 
118  return OBJECT_METHOD_OK;
119 }
120 
122 static void
123 process_func (object *op)
124 {
125  HARD_ASSERT(op != NULL);
126 
127  op->speed = 0;
129 
130  if (op->stats.exp == -1) {
131  return;
132  }
133 
134  op->value = 0;
135  op->state = op->value;
136  SET_ANIMATION(op, (((NUM_ANIMATIONS(op) / NUM_FACINGS(op)) *
137  op->direction) + op->value));
139 
140  connection_trigger(op, op->value);
141 }
142 
147 {
148  OBJECT_METHODS(TYPE_HANDLE)->apply_func = apply_func;
149  OBJECT_METHODS(TYPE_HANDLE)->trigger_func = trigger_func;
150  OBJECT_METHODS(TYPE_HANDLE)->process_func = process_func;
151 }
#define UP_OBJ_FACE
Definition: object.h:537
#define NUM_FACINGS(ob)
Definition: global.h:300
double speed_left
Definition: object.h:472
#define SET_ANIMATION(ob, newanim)
Definition: global.h:282
const char * slaying
Definition: object.h:180
int64_t exp
Definition: living.h:69
OBJECT_TYPE_INIT_DEFINE(handle)
Definition: handle.c:146
#define TYPE_HANDLE
Definition: define.h:401
int16_t y
Definition: object.h:276
void object_update(object *op, int action)
Definition: object.c:1117
int8_t direction
Definition: object.h:350
struct mapdef * map
Definition: object.h:139
const char * name
Definition: object.h:168
uint8_t state
Definition: object.h:344
double speed
Definition: object.h:469
void connection_trigger(object *op, int state)
Definition: connection.c:258
int16_t x
Definition: object.h:273
void play_sound_map(mapstruct *map, int type, const char *filename, int x, int y, int loop, int volume)
Definition: sounds.c:159
#define NUM_ANIMATIONS(ob)
Definition: global.h:298
living stats
Definition: object.h:481
#define OBJECT_METHODS(type)
const char * msg
Definition: object.h:183
static int trigger_func(object *op, object *cause, int state)
Definition: handle.c:102
#define OBJECT_METHOD_OK
static int apply_func(object *op, object *applier, int aflags)
Definition: handle.c:39
static void process_func(object *op)
Definition: handle.c:123
void object_update_speed(object *op)
Definition: object.c:1043
int64_t value
Definition: object.h:240
object * key_match(object *op, const object *locked)
Definition: key.c:48