server/src/triggers/shock.c

136 lines
3.1 KiB
C
Raw Normal View History

2019-01-24 16:34:07 +01:00
#ifdef _MSC_VER
# include <platform.h>
#endif
2010-08-08 10:06:34 +02:00
#include "shock.h"
#include <magic.h>
2010-08-08 10:06:34 +02:00
/* kernel includes */
#include <kernel/curse.h>
#include <kernel/faction.h>
#include <kernel/messages.h>
2010-08-08 10:06:34 +02:00
#include <kernel/spell.h>
#include <kernel/unit.h>
/* util includes */
#include <kernel/attrib.h>
2010-08-08 10:06:34 +02:00
#include <util/base36.h>
#include <kernel/event.h>
2018-09-29 13:21:46 +02:00
#include <kernel/gamedata.h>
2010-08-08 10:06:34 +02:00
#include <util/log.h>
#include <util/macros.h>
2010-08-08 10:06:34 +02:00
#include <util/resolve.h>
#include <util/rng.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
/* libc includes */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/***
** shock
**/
/* ------------------------------------------------------------- */
/* do_shock - Schockt die Einheit, z.B. bei Verlust eines */
/* Vertrauten. */
/* ------------------------------------------------------------- */
2011-03-07 08:02:35 +01:00
static void do_shock(unit * u, const char *reason)
2010-08-08 10:06:34 +02:00
{
int i;
if (u->number > 0) {
/* HP - Verlust */
int hp = (unit_max_hp(u) * u->number) / 10;
2019-01-24 16:34:07 +01:00
if (hp > u->hp) hp = u->hp;
u->hp = (hp > 1) ? hp : 1;
}
/* Aura - Verlust */
if (is_mage(u)) {
2018-11-24 12:26:52 +01:00
int aura = max_spellpoints_depr(u->region, u) / 10;
int now = get_spellpoints(u);
if (now > aura) {
set_spellpoints(u, aura);
}
2011-03-07 08:02:35 +01:00
}
2010-08-08 10:06:34 +02:00
/* Evt. Talenttageverlust */
for (i = 0; i != u->skill_size; ++i)
if (rng_int() % 5 == 0) {
skill *sv = u->skills + i;
int weeks = (sv->level * sv->level - sv->level) / 2;
int change = (weeks + 9) / 10;
reduce_skill(u, sv, change);
}
/* Dies ist ein Hack, um das skillmod und familiar-Attribut beim Mage
2019-02-09 13:39:28 +01:00
* zu loeschen wenn der Familiar getoetet wird. Da sollten wir ueber eine
* saubere Implementation nachdenken. */
if (strcmp(reason, "trigger") == 0) {
remove_familiar(u);
}
if (u->faction != NULL) {
ADDMSG(&u->faction->msgs, msg_message("shock",
"mage reason", u, reason));
}
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static int shock_handle(trigger * t, void *data)
2010-08-08 10:06:34 +02:00
{
/* destroy the unit */
unit *u = (unit *)t->data.v;
if (u && u->number) {
do_shock(u, "trigger");
}
UNUSED_ARG(data);
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static void shock_write(const trigger * t, struct storage *store)
2010-08-08 10:06:34 +02:00
{
unit *u = (unit *)t->data.v;
trigger *next = t->next;
while (next) {
/* make sure it is unique! */
if (next->type == t->type && next->data.v == t->data.v)
break;
next = next->next;
}
if (next && u) {
2016-11-17 21:27:19 +01:00
log_error("more than one shock-attribut for %s on a unit. FIXED.\n", itoa36(u->no));
write_unit_reference(NULL, store);
}
else {
write_unit_reference(u, store);
}
2010-08-08 10:06:34 +02:00
}
2017-09-21 16:26:53 +02:00
static int shock_read(trigger * t, gamedata *data) {
if (read_unit_reference(data, (unit **)&t->data.v, NULL) <= 0) {
return AT_READ_FAIL;
}
return AT_READ_OK;
2010-08-08 10:06:34 +02:00
}
trigger_type tt_shock = {
"shock",
NULL,
NULL,
shock_handle,
shock_write,
shock_read
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
trigger *trigger_shock(unit * u)
2010-08-08 10:06:34 +02:00
{
trigger *t = t_new(&tt_shock);
t->data.v = (void *)u;
return t;
2010-08-08 10:06:34 +02:00
}