Crossfire Server, Trunk
power_crystal.cpp
Go to the documentation of this file.
1 /*
2  CrossFire, A Multiplayer game for X-windows
3 
4  Copyright (C) 2007 Mark Wedel & Crossfire Development Team
5  Copyright (C) 1992 Frank Tore Johansen
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21  The authors can be reached via e-mail at crossfire-devel@real-time.com
22 */
27 #include <global.h>
28 #include <ob_methods.h>
29 #include <ob_types.h>
30 #include <sproto.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34 
35 static method_ret power_crystal_type_apply(object *op, object *applier, int aflags);
36 
37 #define LEVELS 7
38 
40 static const char* crystal_levels[LEVELS] = {
41  "empty.",
42  "almost empty.",
43  "partially filled.",
44  "half full.",
45  "well charged.",
46  "almost full.",
47  "fully charged.",
48 };
49 
56 static int crystal_level(const object *op, int sp) {
57  int i = (sp*10) / op->stats.maxsp;
58  if (sp == 0)
59  return 0;
60  else if (i == 0)
61  return 1;
62  else if (i < 3)
63  return 2;
64  else if (i < 6)
65  return 3;
66  else if (i < 9)
67  return 4;
68  else if (sp == op->stats.maxsp)
69  return 6;
70  else
71  return 5;
72 }
73 
81 static void add_capacity(const object *op, StringBuffer *buf, int previous_sp) {
82  int current = crystal_level(op, op->stats.sp);
83 
84  assert(current >= 0 && current < LEVELS);
85 
86  if (previous_sp != -1) {
87  int previous = crystal_level(op, previous_sp);
88  assert(previous >= 0 && previous < LEVELS);
89  if (current == previous) {
91  } else {
93  }
94  }
95 
97 }
98 
99 static void power_crystal_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size) {
101  char *final;
102  (void)observer;
103  (void)use_media_tags;
104 
105  buf[0] = '\0';
106  query_name(op, buf, size-1);
107  buf[size-1] = 0;
109 
110  /* Avoid division by zero... */
111  if (op->stats.maxsp == 0) {
112  stringbuffer_append_printf(sb, "(capacity %d).", op->stats.maxsp);
113  } else {
114  int i;
115  if (op->stats.maxsp > 1000) { /*higher capacity crystals*/
116  i = (op->stats.maxsp%1000)/100;
117  if (i)
118  stringbuffer_append_printf(sb, "(capacity %d.%dk). It is ", op->stats.maxsp/1000, i);
119  else
120  stringbuffer_append_printf(sb, "(capacity %dk). It is ", op->stats.maxsp/1000);
121  } else
122  stringbuffer_append_printf(sb, "(capacity %d). It is ", op->stats.maxsp);
123  add_capacity(op, sb, -1);
124  }
125 
126  final = stringbuffer_finish(sb);
127  strncat(buf, final, size);
128  free(final);
129 }
130 
137 }
138 
149 static method_ret power_crystal_type_apply(object *op, object *applier, int aflags) {
150  int available_power;
151  int power_space;
152  int power_grab;
154  char name[MAX_BUF], *message;
155  (void)aflags;
156 
157  available_power = applier->stats.sp-applier->stats.maxsp;
158  power_space = op->stats.maxsp-op->stats.sp;
159  power_grab = 0;
160  query_name(op, name, sizeof(name));
161  if (available_power >= 0 && power_space > 0) {
162  power_grab = MIN(power_space, 0.5*applier->stats.sp);
163  stringbuffer_append_string(sb, "You transfer power to the ");
164  }
165  if (available_power < 0 && op->stats.sp > 0) {
166  power_grab = -MIN(-available_power, op->stats.sp);
167  stringbuffer_append_string(sb, "You grab power from the ");
168  }
169 
170  if (power_grab == 0)
171  stringbuffer_append_string(sb, "Nothing happens.");
172  else {
173  int sp = op->stats.sp;
174  stringbuffer_append_printf(sb, "%s. It is ", name);
175  applier->stats.sp -= power_grab;
176  op->stats.sp += power_grab;
177  op->speed = (float)op->stats.sp/(float)op->stats.maxsp;
179  if (applier->type == PLAYER)
180  esrv_update_item(UPD_ANIMSPEED, applier, op);
181  add_capacity(op, sb, sp);
182  }
183 
185  if (applier->type == PLAYER)
187  free(message);
188 
189  return METHOD_OK;
190 }
PLAYER
@ PLAYER
Definition: object.h:112
global.h
crystal_level
static int crystal_level(const object *op, int sp)
Definition: power_crystal.cpp:56
say.previous
dictionary previous
Definition: say.py:205
stringbuffer_append_printf
void stringbuffer_append_printf(StringBuffer *sb, const char *format,...)
Definition: stringbuffer.cpp:138
stringbuffer_new
StringBuffer * stringbuffer_new(void)
Definition: stringbuffer.cpp:57
register_apply
void register_apply(int ob_type, apply_func method)
Definition: ob_types.cpp:62
METHOD_OK
#define METHOD_OK
Definition: ob_methods.h:15
if
if(!(yy_init))
Definition: loader.cpp:36428
MIN
#define MIN(x, y)
Definition: compat.h:21
LEVELS
#define LEVELS
Definition: power_crystal.cpp:37
POWER_CRYSTAL
@ POWER_CRYSTAL
Definition: object.h:247
buf
StringBuffer * buf
Definition: readable.cpp:1565
power_crystal_describe
static void power_crystal_describe(const object *op, const object *observer, int use_media_tags, char *buf, size_t size)
Definition: power_crystal.cpp:99
stringbuffer_finish
char * stringbuffer_finish(StringBuffer *sb)
Definition: stringbuffer.cpp:76
MSG_TYPE_APPLY_SUCCESS
#define MSG_TYPE_APPLY_SUCCESS
Definition: newclient.h:606
query_name
void query_name(const object *op, char *buf, size_t size)
Definition: item.cpp:588
object_update_speed
void object_update_speed(object *op)
Definition: object.cpp:1349
object::type
uint8_t type
Definition: object.h:348
sproto.h
living::sp
int16_t sp
Definition: living.h:42
stringbuffer_append_string
void stringbuffer_append_string(StringBuffer *sb, const char *str)
Definition: stringbuffer.cpp:95
nlohmann::detail::void
j template void())
Definition: json.hpp:4099
MAX_BUF
#define MAX_BUF
Definition: define.h:35
StringBuffer
Definition: stringbuffer.cpp:25
UPD_ANIMSPEED
#define UPD_ANIMSPEED
Definition: newclient.h:323
method_ret
char method_ret
Definition: ob_methods.h:14
ob_types.h
diamondslots.message
string message
Definition: diamondslots.py:57
NDI_UNIQUE
#define NDI_UNIQUE
Definition: newclient.h:265
power_crystal_type_apply
static method_ret power_crystal_type_apply(object *op, object *applier, int aflags)
Definition: power_crystal.cpp:149
living::maxsp
int16_t maxsp
Definition: living.h:43
give.op
op
Definition: give.py:33
esrv_update_item
void esrv_update_item(int flags, object *pl, object *op)
Definition: main.cpp:359
draw_ext_info
void draw_ext_info(int flags, int pri, const object *pl, uint8_t type, uint8_t subtype, const char *message)
Definition: main.cpp:308
ob_methods.h
object::stats
living stats
Definition: object.h:378
init_type_power_crystal
void init_type_power_crystal(void)
Definition: power_crystal.cpp:134
crystal_levels
static const char * crystal_levels[LEVELS]
Definition: power_crystal.cpp:40
replace.current
current
Definition: replace.py:64
MSG_TYPE_APPLY
#define MSG_TYPE_APPLY
Definition: newclient.h:411
register_describe
void register_describe(int ob_type, describe_func method)
Definition: ob_types.cpp:80
give.name
name
Definition: give.py:27
add_capacity
static void add_capacity(const object *op, StringBuffer *buf, int previous_sp)
Definition: power_crystal.cpp:81