| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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(ob_methods *context, object *op, object *applier, int aflags); |
| 36 |
|
| 37 |
#define LEVELS 7 |
| 38 |
|
| 39 |
|
| 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 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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) { |
| 90 |
stringbuffer_append_string(buf, "still "); |
| 91 |
} else { |
| 92 |
stringbuffer_append_string(buf, "now "); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
stringbuffer_append_string(buf, crystal_levels[current]); |
| 97 |
} |
| 98 |
|
| 99 |
static void power_crystal_describe(const ob_methods *context, const object *op, const object *observer, int use_media_tags, char *buf, size_t size) { |
| 100 |
StringBuffer *sb = stringbuffer_new(); |
| 101 |
char *final; |
| 102 |
|
| 103 |
buf[0] = '\0'; |
| 104 |
query_name(op, buf, size-1); |
| 105 |
buf[size-1] = 0; |
| 106 |
|
| 107 |
|
| 108 |
if (op->stats.maxsp == 0) { |
| 109 |
stringbuffer_append_printf(sb, "(capacity %d).", op->stats.maxsp); |
| 110 |
} else { |
| 111 |
int i; |
| 112 |
if (op->stats.maxsp > 1000) { |
| 113 |
i = (op->stats.maxsp%1000)/100; |
| 114 |
if (i) |
| 115 |
stringbuffer_append_printf(sb, "(capacity %d.%dk). It is ", op->stats.maxsp/1000, i); |
| 116 |
else |
| 117 |
stringbuffer_append_printf(sb, "(capacity %dk). It is ", op->stats.maxsp/1000); |
| 118 |
} else |
| 119 |
stringbuffer_append_printf(sb, "(capacity %d). It is ", op->stats.maxsp); |
| 120 |
add_capacity(op, sb, -1); |
| 121 |
} |
| 122 |
|
| 123 |
final = stringbuffer_finish(sb); |
| 124 |
strncat(buf, final, size); |
| 125 |
free(final); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
void init_type_power_crystal(void) { |
| 132 |
register_apply(POWER_CRYSTAL, power_crystal_type_apply); |
| 133 |
register_describe(POWER_CRYSTAL, power_crystal_describe); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static method_ret power_crystal_type_apply(ob_methods *context, object *op, object *applier, int aflags) { |
| 148 |
int available_power; |
| 149 |
int power_space; |
| 150 |
int power_grab; |
| 151 |
StringBuffer *sb = stringbuffer_new(); |
| 152 |
char name[MAX_BUF], *message; |
| 153 |
|
| 154 |
available_power = applier->stats.sp-applier->stats.maxsp; |
| 155 |
power_space = op->stats.maxsp-op->stats.sp; |
| 156 |
power_grab = 0; |
| 157 |
query_name(op, name, sizeof(name)); |
| 158 |
if (available_power >= 0 && power_space > 0) { |
| 159 |
power_grab = MIN(power_space, 0.5*applier->stats.sp); |
| 160 |
stringbuffer_append_string(sb, "You transfer power to the "); |
| 161 |
} |
| 162 |
if (available_power < 0 && op->stats.sp > 0) { |
| 163 |
power_grab = -MIN(-available_power, op->stats.sp); |
| 164 |
stringbuffer_append_string(sb, "You grab power from the "); |
| 165 |
} |
| 166 |
|
| 167 |
if (power_grab == 0) |
| 168 |
stringbuffer_append_string(sb, "Nothing happens."); |
| 169 |
else { |
| 170 |
int sp = op->stats.sp; |
| 171 |
stringbuffer_append_printf(sb, "%s. It is ", name); |
| 172 |
applier->stats.sp -= power_grab; |
| 173 |
op->stats.sp += power_grab; |
| 174 |
op->speed = (float)op->stats.sp/(float)op->stats.maxsp; |
| 175 |
object_update_speed(op); |
| 176 |
if (applier->type == PLAYER) |
| 177 |
esrv_update_item(UPD_ANIMSPEED, applier, op); |
| 178 |
add_capacity(op, sb, sp); |
| 179 |
} |
| 180 |
|
| 181 |
message = stringbuffer_finish(sb); |
| 182 |
if (applier->type == PLAYER) |
| 183 |
draw_ext_info(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS, message); |
| 184 |
free(message); |
| 185 |
|
| 186 |
return METHOD_OK; |
| 187 |
} |