2010-08-08 10:06:34 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
|
|
|
|
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"
|
|
|
|
#include "race.h"
|
|
|
|
|
|
|
|
/* util includes */
|
2011-02-26 03:16:14 +01:00
|
|
|
#include <util/quicklist.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>
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static equipment *equipment_sets;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment *create_equipment(const char *eqname)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment **eqp = &equipment_sets;
|
2010-08-08 10:06:34 +02:00
|
|
|
for (;;) {
|
2011-03-07 08:02:35 +01:00
|
|
|
struct equipment *eq = *eqp;
|
|
|
|
int i = eq ? strcmp(eq->name, eqname) : 1;
|
|
|
|
if (i > 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
eq = malloc(sizeof(equipment));
|
|
|
|
eq->name = strdup(eqname);
|
|
|
|
eq->next = *eqp;
|
|
|
|
eq->items = NULL;
|
|
|
|
eq->spells = NULL;
|
|
|
|
eq->subsets = NULL;
|
|
|
|
eq->callback = NULL;
|
|
|
|
memset(eq->skills, 0, sizeof(eq->skills));
|
|
|
|
*eqp = eq;
|
|
|
|
break;
|
2011-03-07 08:02:35 +01:00
|
|
|
} else if (i == 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
eqp = &eq->next;
|
|
|
|
}
|
|
|
|
return *eqp;
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment *get_equipment(const char *eqname)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment *eq = equipment_sets;
|
|
|
|
for (; eq; eq = eq->next) {
|
2010-08-08 10:06:34 +02:00
|
|
|
int i = strcmp(eq->name, eqname);
|
2011-03-07 08:02:35 +01:00
|
|
|
if (i == 0)
|
|
|
|
return eq;
|
|
|
|
else if (i > 0)
|
|
|
|
break;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
if (eq != NULL) {
|
|
|
|
if (value != NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
eq->skills[sk] = strdup(value);
|
|
|
|
} else if (eq->skills[sk]) {
|
|
|
|
free(eq->skills[sk]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void equipment_addspell(equipment * eq, spell * sp)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
if (eq != NULL) {
|
2011-02-26 03:16:14 +01:00
|
|
|
ql_set_insert(&eq->spells, sp);
|
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
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
if (eq != NULL) {
|
|
|
|
if (itype != NULL) {
|
|
|
|
itemdata *idata = eq->items;
|
|
|
|
while (idata && idata->itype != itype) {
|
2010-08-08 10:06:34 +02:00
|
|
|
idata = idata->next;
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
if (idata == NULL) {
|
|
|
|
idata = (itemdata *) malloc(sizeof(itemdata));
|
2010-08-08 10:06:34 +02:00
|
|
|
idata->itype = itype;
|
|
|
|
idata->value = strdup(value);
|
|
|
|
idata->next = eq->items;
|
|
|
|
eq->items = idata;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-07 08:02:35 +01:00
|
|
|
equipment_setcallback(struct equipment *eq,
|
|
|
|
void (*callback) (const struct equipment *, struct unit *))
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
|
|
|
eq->callback = callback;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
equip_unit_mask(u, eq, EQUIP_ALL);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (eq) {
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
if (mask & EQUIP_SKILLS) {
|
2010-08-08 10:06:34 +02:00
|
|
|
skill_t sk;
|
2011-03-07 08:02:35 +01:00
|
|
|
for (sk = 0; sk != MAXSKILLS; ++sk) {
|
|
|
|
if (eq->skills[sk] != NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
int i = dice_rand(eq->skills[sk]);
|
2011-03-07 08:02:35 +01:00
|
|
|
if (i > 0)
|
|
|
|
set_level(u, sk, i);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
if (mask & EQUIP_SPELLS) {
|
|
|
|
quicklist *ql = eq->spells;
|
2011-02-26 03:16:14 +01:00
|
|
|
if (ql) {
|
2011-03-07 08:02:35 +01:00
|
|
|
sc_mage *m = get_mage(u);
|
|
|
|
if (m == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
assert(!"trying to equip spells on a non-mage!");
|
|
|
|
} else {
|
2011-02-26 03:16:14 +01:00
|
|
|
int qi;
|
2011-03-07 08:02:35 +01:00
|
|
|
for (qi = 0; ql; ql_advance(&ql, &qi, 1)) {
|
|
|
|
spell *sp = (spell *) ql_get(ql, qi);
|
2011-02-26 03:16:14 +01:00
|
|
|
add_spell(get_spelllist(m, u->faction), sp);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
if (mask & EQUIP_ITEMS) {
|
|
|
|
itemdata *idata;
|
|
|
|
for (idata = eq->items; idata != NULL; idata = idata->next) {
|
2010-08-08 10:06:34 +02:00
|
|
|
int i = u->number * dice_rand(idata->value);
|
2011-03-07 08:02:35 +01:00
|
|
|
if (i > 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
i_add(&u->items, i_new(idata->itype, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eq->subsets) {
|
|
|
|
int i;
|
2011-03-07 08:02:35 +01:00
|
|
|
for (i = 0; eq->subsets[i].sets; ++i) {
|
2010-08-08 10:06:34 +02:00
|
|
|
if (chance(eq->subsets[i].chance)) {
|
2011-03-07 08:02:35 +01:00
|
|
|
float rnd = (1 + rng_int() % 1000) / 1000.0f;
|
2010-08-08 10:06:34 +02:00
|
|
|
int k;
|
2011-03-07 08:02:35 +01:00
|
|
|
for (k = 0; eq->subsets[i].sets[k].set; ++k) {
|
|
|
|
if (rnd <= eq->subsets[i].sets[k].chance) {
|
2010-08-08 10:06:34 +02:00
|
|
|
equip_unit_mask(u, eq->subsets[i].sets[k].set, mask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rnd -= eq->subsets[i].sets[k].chance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
if (mask & EQUIP_SPECIAL) {
|
|
|
|
if (eq->callback)
|
|
|
|
eq->callback(eq, u);
|
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
|
|
|
{
|
|
|
|
if (eq) {
|
2011-03-07 08:02:35 +01:00
|
|
|
itemdata *idata;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
for (idata = eq->items; idata != NULL; idata = idata->next) {
|
2010-08-08 10:06:34 +02:00
|
|
|
int i = dice_rand(idata->value);
|
2011-03-07 08:02:35 +01:00
|
|
|
if (i > 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
i_add(items, i_new(idata->itype, i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (eq->subsets) {
|
|
|
|
int i;
|
2011-03-07 08:02:35 +01:00
|
|
|
for (i = 0; eq->subsets[i].sets; ++i) {
|
2010-08-08 10:06:34 +02:00
|
|
|
if (chance(eq->subsets[i].chance)) {
|
2011-03-07 08:02:35 +01:00
|
|
|
float rnd = (1 + rng_int() % 1000) / 1000.0f;
|
2010-08-08 10:06:34 +02:00
|
|
|
int k;
|
2011-03-07 08:02:35 +01:00
|
|
|
for (k = 0; eq->subsets[i].sets[k].set; ++k) {
|
|
|
|
if (rnd <= eq->subsets[i].sets[k].chance) {
|
2010-08-08 10:06:34 +02:00
|
|
|
equip_items(items, eq->subsets[i].sets[k].set);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rnd -= eq->subsets[i].sets[k].chance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|