2010-08-08 10:06:34 +02:00
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2015-01-30 20:37:14 +01:00
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include "equipment.h"
|
|
|
|
|
|
|
|
/* kernel includes */
|
|
|
|
#include "item.h"
|
|
|
|
#include "unit.h"
|
2012-05-25 08:50:27 +02:00
|
|
|
#include "faction.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
#include "race.h"
|
2016-09-24 17:29:55 +02:00
|
|
|
#include "spell.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
/* util includes */
|
2017-01-26 17:41:21 +01:00
|
|
|
#include <selist.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/rand.h>
|
|
|
|
#include <util/rng.h>
|
|
|
|
|
|
|
|
/* libc includes */
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static equipment *equipment_sets;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2017-01-22 18:28:04 +01:00
|
|
|
equipment *get_or_create_equipment(const char *eqname)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
equipment **eqp = &equipment_sets;
|
|
|
|
for (;;) {
|
|
|
|
struct equipment *eq = *eqp;
|
|
|
|
int i = eq ? strcmp(eq->name, eqname) : 1;
|
|
|
|
if (i > 0) {
|
|
|
|
eq = (equipment *)calloc(1, sizeof(equipment));
|
2017-01-10 16:31:05 +01:00
|
|
|
eq->name = strdup(eqname);
|
2015-01-30 20:37:14 +01:00
|
|
|
eq->next = *eqp;
|
|
|
|
memset(eq->skills, 0, sizeof(eq->skills));
|
|
|
|
*eqp = eq;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (i == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
eqp = &eq->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return *eqp;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment *get_equipment(const char *eqname)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
equipment *eq = equipment_sets;
|
|
|
|
for (; eq; eq = eq->next) {
|
|
|
|
int i = strcmp(eq->name, eqname);
|
|
|
|
if (i == 0)
|
|
|
|
return eq;
|
|
|
|
else if (i > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void equipment_setskill(equipment * eq, skill_t sk, const char *value)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (eq != NULL) {
|
|
|
|
if (value != NULL) {
|
2017-01-10 16:31:05 +01:00
|
|
|
eq->skills[sk] = strdup(value);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
else if (eq->skills[sk]) {
|
|
|
|
free(eq->skills[sk]);
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 17:29:55 +02:00
|
|
|
typedef struct lazy_spell {
|
2017-02-02 20:12:52 +01:00
|
|
|
struct spellref *spref;
|
2016-09-24 17:29:55 +02:00
|
|
|
int level;
|
|
|
|
} lazy_spell;
|
|
|
|
|
|
|
|
void equipment_addspell(equipment * eq, const char * name, int level)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (eq) {
|
2016-09-24 17:29:55 +02:00
|
|
|
lazy_spell *ls = malloc(sizeof(lazy_spell));
|
2017-02-02 20:12:52 +01:00
|
|
|
ls->spref = spellref_create(NULL, name);
|
2016-09-24 17:29:55 +02:00
|
|
|
ls->level = level;
|
2017-01-26 17:41:21 +01:00
|
|
|
selist_push(&eq->spells, ls);
|
2012-05-25 08:50:27 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void
|
|
|
|
equipment_setitem(equipment * eq, const item_type * itype, const char *value)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (eq != NULL) {
|
|
|
|
if (itype != NULL) {
|
|
|
|
itemdata *idata = eq->items;
|
|
|
|
while (idata && idata->itype != itype) {
|
|
|
|
idata = idata->next;
|
|
|
|
}
|
|
|
|
if (idata == NULL) {
|
|
|
|
idata = (itemdata *)malloc(sizeof(itemdata));
|
|
|
|
idata->itype = itype;
|
2017-01-10 16:31:05 +01:00
|
|
|
idata->value = strdup(value);
|
2015-01-30 20:37:14 +01:00
|
|
|
idata->next = eq->items;
|
|
|
|
eq->items = idata;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment_setcallback(struct equipment *eq,
|
2015-01-30 20:37:14 +01:00
|
|
|
void(*callback) (const struct equipment *, struct unit *))
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
eq->callback = callback;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void equip_unit(struct unit *u, const struct equipment *eq)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
equip_unit_mask(u, eq, EQUIP_ALL);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void equip_unit_mask(struct unit *u, const struct equipment *eq, int mask)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-07-24 08:04:30 +02:00
|
|
|
if (eq) {
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2014-07-24 08:04:30 +02:00
|
|
|
if (mask & EQUIP_SKILLS) {
|
|
|
|
int sk;
|
|
|
|
for (sk = 0; sk != MAXSKILLS; ++sk) {
|
|
|
|
if (eq->skills[sk] != NULL) {
|
|
|
|
int i = dice_rand(eq->skills[sk]);
|
|
|
|
if (i > 0) {
|
|
|
|
set_level(u, (skill_t)sk, i);
|
2015-01-30 20:37:14 +01:00
|
|
|
if (sk == SK_STAMINA) {
|
2014-07-24 08:04:30 +02:00
|
|
|
u->hp = unit_max_hp(u) * u->number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2014-07-24 08:04:30 +02:00
|
|
|
if (mask & EQUIP_SPELLS) {
|
2016-09-24 17:29:55 +02:00
|
|
|
if (eq->spells) {
|
2017-01-26 17:41:21 +01:00
|
|
|
selist * ql = eq->spells;
|
2014-07-24 08:04:30 +02:00
|
|
|
int qi;
|
|
|
|
sc_mage * mage = get_mage(u);
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2017-01-26 17:41:21 +01:00
|
|
|
for (qi = 0; ql; selist_advance(&ql, &qi, 1)) {
|
|
|
|
lazy_spell *sbe = (lazy_spell *)selist_get(ql, qi);
|
2017-02-02 20:12:52 +01:00
|
|
|
spell *sp = spellref_get(sbe->spref);
|
|
|
|
unit_add_spell(u, mage, sp, sbe->level);
|
2014-07-24 08:04:30 +02:00
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2014-07-24 08:04:30 +02:00
|
|
|
if (mask & EQUIP_ITEMS) {
|
|
|
|
itemdata *idata;
|
|
|
|
for (idata = eq->items; idata != NULL; idata = idata->next) {
|
|
|
|
int i = u->number * dice_rand(idata->value);
|
|
|
|
if (i > 0) {
|
|
|
|
i_add(&u->items, i_new(idata->itype, i));
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2014-07-24 08:04:30 +02:00
|
|
|
if (eq->subsets) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; eq->subsets[i].sets; ++i) {
|
|
|
|
if (chance(eq->subsets[i].chance)) {
|
2015-05-15 20:35:36 +02:00
|
|
|
double rnd = (1 + rng_int() % 1000) / 1000.0;
|
2014-07-24 08:04:30 +02:00
|
|
|
int k;
|
|
|
|
for (k = 0; eq->subsets[i].sets[k].set; ++k) {
|
|
|
|
if (rnd <= eq->subsets[i].sets[k].chance) {
|
|
|
|
equip_unit_mask(u, eq->subsets[i].sets[k].set, mask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rnd -= eq->subsets[i].sets[k].chance;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2014-07-24 08:04:30 +02:00
|
|
|
if (mask & EQUIP_SPECIAL) {
|
|
|
|
if (eq->callback)
|
2015-01-30 20:37:14 +01:00
|
|
|
eq->callback(eq, u);
|
2014-07-24 08:04:30 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void equip_items(struct item **items, const struct equipment *eq)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
if (eq) {
|
|
|
|
itemdata *idata;
|
|
|
|
|
|
|
|
for (idata = eq->items; idata != NULL; idata = idata->next) {
|
|
|
|
int i = dice_rand(idata->value);
|
|
|
|
if (i > 0) {
|
|
|
|
i_add(items, i_new(idata->itype, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (eq->subsets) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; eq->subsets[i].sets; ++i) {
|
|
|
|
if (chance(eq->subsets[i].chance)) {
|
2015-05-15 20:35:36 +02:00
|
|
|
double rnd = (1 + rng_int() % 1000) / 1000.0;
|
2015-01-30 20:37:14 +01:00
|
|
|
int k;
|
|
|
|
for (k = 0; eq->subsets[i].sets[k].set; ++k) {
|
|
|
|
if (rnd <= eq->subsets[i].sets[k].chance) {
|
|
|
|
equip_items(items, eq->subsets[i].sets[k].set);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rnd -= eq->subsets[i].sets[k].chance;
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-07 18:09:37 +02:00
|
|
|
|
2016-09-24 17:29:55 +02:00
|
|
|
void free_ls(void *arg) {
|
|
|
|
lazy_spell *ls = (lazy_spell*)arg;
|
2017-02-02 20:12:52 +01:00
|
|
|
spellref_free(ls->spref);
|
2016-09-24 17:29:55 +02:00
|
|
|
free(ls);
|
|
|
|
}
|
|
|
|
|
2016-09-07 20:46:41 +02:00
|
|
|
void equipment_done(void) {
|
2016-09-07 18:09:37 +02:00
|
|
|
equipment **eqp = &equipment_sets;
|
|
|
|
while (*eqp) {
|
2016-09-08 06:56:16 +02:00
|
|
|
int i;
|
2016-09-07 18:09:37 +02:00
|
|
|
equipment *eq = *eqp;
|
|
|
|
*eqp = eq->next;
|
|
|
|
free(eq->name);
|
2016-09-24 17:29:55 +02:00
|
|
|
if (eq->spells) {
|
2017-01-26 17:41:21 +01:00
|
|
|
selist_foreach(eq->spells, free_ls);
|
|
|
|
selist_free(eq->spells);
|
2016-09-07 19:02:03 +02:00
|
|
|
}
|
2016-09-07 21:29:54 +02:00
|
|
|
while (eq->items) {
|
|
|
|
itemdata *next = eq->items->next;
|
|
|
|
free(eq->items->value);
|
|
|
|
free(eq->items);
|
|
|
|
eq->items = next;
|
|
|
|
}
|
2017-02-18 21:15:14 +01:00
|
|
|
/* TODO: subsets, skills */
|
2016-09-08 06:56:16 +02:00
|
|
|
for (i=0;i!=MAXSKILLS;++i) {
|
|
|
|
free(eq->skills[i]);
|
|
|
|
}
|
2016-09-07 18:09:37 +02:00
|
|
|
free(eq);
|
|
|
|
}
|
|
|
|
}
|