Skip to content

Commit

Permalink
Dumpfbackenbrot (#1047)
Browse files Browse the repository at this point in the history
* fool potion: no more than 10 effects per person

* fix bad tests, allow stacking up to 10 effects
  • Loading branch information
ennorehling authored Dec 10, 2023
1 parent f3eb4a2 commit 85c83b4
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 48 deletions.
1 change: 0 additions & 1 deletion scripts/tests/e2/astral.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ function test_pull_astral()
assert_equal(u1.region, u2.region)

u1.aura = 12 -- 2 Aura pro Stufe
u1.name = 'Xykon'
u1:clear_orders()
u1:add_order("ZAUBERE Astraler~Ruf -1 0 " .. itoa36(u2.id))
r = region.create(-1,0, 'plain')
Expand Down
17 changes: 7 additions & 10 deletions scripts/tests/e2/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,23 @@ function test_use_foolpotion()
local f = faction.create("human")
local u = unit.create(f, r, 1)
turn_begin()
u:add_item('p7', 2)
u:clear_orders()
u:add_order("BENUTZEN 1 Dumpfbackenbrot 4242")
u:add_item('p7', 3)
u:set_orders("BENUTZEN 1 Dumpfbackenbrot 4242")
turn_process()
assert_equal(2, u:get_item('p7'))
assert_equal(3, u:get_item('p7'))
assert_equal(1, f:count_msg_type('feedback_unit_not_found'))
local u2 = unit.create(f, r, 1)

u:clear_orders()
u:add_order("BENUTZEN 2 Dumpfbackenbrot " .. itoa36(u2.id))
u:set_orders("BENUTZEN 2 Dumpfbackenbrot " .. itoa36(u2.id))
turn_process()
assert_equal(2, u:get_item('p7'))
assert_equal(3, u:get_item('p7'))
assert_equal(1, f:count_msg_type('error64'))

u:set_skill("stealth", 1)
u2.name = 'Xolgrim'
u2:set_skill('crossbow', 1)
turn_process()
assert_equal(0, u:get_item('p7'))
assert_equal(0, u2:effect('p7'))
assert_equal(2, u:get_item('p7'))
assert_equal(9, u2:effect('p7'))
assert_equal(1, f:count_msg_type('givedumb'))
turn_end()
end
Expand Down
32 changes: 32 additions & 0 deletions src/alchemy.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,35 @@ void scale_effects(attrib* alist, int n, int size)
data->value = (long long)data->value * n / size;
}
}

int use_foolpotion(unit *user, const item_type *itype, int amount,
struct order *ord)
{
int targetno = read_unitid(user->faction, user->region);
unit *u = findunit(targetno);
int max_effects;
if (u == NULL || user->region != u->region) {
ADDMSG(&user->faction->msgs, msg_feedback(user, ord, "feedback_unit_not_found",
""));
return ECUSTOM;
}
if (effskill(user, SK_STEALTH, NULL) <= effskill(u, SK_PERCEPTION, NULL)) {
cmistake(user, ord, 64, MSG_EVENT);
return ECUSTOM;
}
max_effects = u->number * 10 - get_effect(u, itype);
if (max_effects > 0) {
int use = (max_effects + 9) / 10;
int effects = max_effects;
if (use > amount) {
use = amount;
effects = use * 10;
}
change_effect(u, itype, effects);
ADDMSG(&user->faction->msgs, msg_message("givedumb",
"unit recipient amount", user, u, use));
return use;
}
return 0;
}

6 changes: 3 additions & 3 deletions src/alchemy.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#pragma once
#ifndef H_KRNL_ALCHEMY_H
#define H_KRNL_ALCHEMY_H

#include <stdbool.h>

Expand Down Expand Up @@ -53,4 +51,6 @@ bool display_potions(struct unit* u);
int effect_value(const struct attrib* a);
const struct item_type *effect_type(const struct attrib* a);

#endif /* ALCHEMY_H */
int use_foolpotion(struct unit *user, const struct item_type *itype,
int amount, struct order *ord);

79 changes: 76 additions & 3 deletions src/alchemy.test.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include "alchemy.h"

#include "guard.h"
#include "laws.h"

#include <util/base36.h>
#include <util/keyword.h>

#include <kernel/attrib.h>
#include <kernel/faction.h>
#include <kernel/unit.h>
#include <kernel/order.h>
#include <kernel/race.h>
#include <kernel/item.h>
#include <kernel/region.h>
#include <kernel/skill.h>

#include "guard.h"
#include <kernel/skills.h>

#include <CuTest.h>
#include "tests.h"
Expand Down Expand Up @@ -80,7 +87,71 @@ static void test_herbsearch(CuTest * tc)
test_teardown();
}

static void test_scale_effects(CuTest* tc) {
static void test_foolpotion_effect(CuTest *tc) {
unit *u;
const struct item_type *itype;

test_setup();
itype = oldpotiontype[P_FOOL] = it_get_or_create(rt_get_or_create("hodor"));
u = test_create_unit(test_create_faction(), test_create_plain(0, 0));
test_set_skill(u, SK_MAGIC, 3, 1);
test_set_skill(u, SK_CROSSBOW, 2, 1);
change_effect(u, itype, 2);
demographics();
CuAssertIntEquals(tc, 1, get_effect(u, itype));
CuAssertIntEquals(tc, 2, unit_skill(u, SK_MAGIC)->weeks);
CuAssertIntEquals(tc, 1, unit_skill(u, SK_CROSSBOW)->weeks);
test_teardown();
}

static void test_use_foolpotion(CuTest *tc) {
unit *u, *u2;
const struct item_type *itype;

test_setup();
itype = oldpotiontype[P_FOOL] = it_get_or_create(rt_get_or_create("hodor"));
u = test_create_unit(test_create_faction(), test_create_plain(0, 0));
u2 = test_create_unit(test_create_faction(), u->region);
u->thisorder = create_order(K_USE, u->faction->locale, itoa36(u2->no), NULL);

init_order(u->thisorder, u->faction->locale);
CuAssertIntEquals(tc, ECUSTOM, use_foolpotion(u, itype, 2, u->thisorder));

/* Maximal 10 Wirkungen pro Person: */
test_set_skill(u, SK_STEALTH, 1, 1);
init_order(u->thisorder, u->faction->locale);
CuAssertIntEquals(tc, 1, use_foolpotion(u, itype, 2, u->thisorder));
CuAssertIntEquals(tc, 0, get_effect(u, itype));
CuAssertIntEquals(tc, 10, get_effect(u2, itype));

a_removeall(&u2->attribs, &at_effect);
scale_number(u2, 2);
init_order(u->thisorder, u->faction->locale);
CuAssertIntEquals(tc, 2, use_foolpotion(u, itype, 3, u->thisorder));
CuAssertIntEquals(tc, 20, get_effect(u2, itype));

a_removeall(&u2->attribs, &at_effect);
scale_number(u2, 10);
init_order(u->thisorder, u->faction->locale);
CuAssertIntEquals(tc, 10, use_foolpotion(u, itype, 20, u->thisorder));
CuAssertIntEquals(tc, 100, get_effect(u2, itype));

/* limited use: */
a_removeall(&u2->attribs, &at_effect);
scale_number(u2, 2);
init_order(u->thisorder, u->faction->locale);
CuAssertIntEquals(tc, 1, use_foolpotion(u, itype, 1, u->thisorder));
CuAssertIntEquals(tc, 10, get_effect(u2, itype));

/* stacking only up to 10 effect/person: */
init_order(u->thisorder, u->faction->locale);
CuAssertIntEquals(tc, 1, use_foolpotion(u, itype, 2, u->thisorder));
CuAssertIntEquals(tc, 20, get_effect(u2, itype));

test_teardown();
}

static void test_scale_effects(CuTest *tc) {
unit* u;
const struct item_type* ptype;

Expand Down Expand Up @@ -119,5 +190,7 @@ CuSuite *get_alchemy_suite(void)
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_herbsearch);
SUITE_ADD_TEST(suite, test_scale_effects);
SUITE_ADD_TEST(suite, test_foolpotion_effect);
SUITE_ADD_TEST(suite, test_use_foolpotion);
return suite;
}
22 changes: 0 additions & 22 deletions src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,28 +255,6 @@ struct order *ord)
return 0;
}

static int use_foolpotion(unit *u, const item_type *itype, int amount,
struct order *ord)
{
int targetno = read_unitid(u->faction, u->region);
unit *target = findunit(targetno);
if (target == NULL || u->region != target->region) {
ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "feedback_unit_not_found",
""));
return ECUSTOM;
}
if (effskill(u, SK_STEALTH, NULL) <= effskill(target, SK_PERCEPTION, NULL)) {
cmistake(u, ord, 64, MSG_EVENT);
return ECUSTOM;
}
ADDMSG(&u->faction->msgs, msg_message("givedumb",
"unit recipient amount", u, target, amount));

change_effect(target, itype, amount);
use_pooled(u, itype->rtype, GET_DEFAULT, amount);
return 0;
}

static int
use_bloodpotion(struct unit *u, const struct item_type *itype, int amount,
struct order *ord)
Expand Down
6 changes: 3 additions & 3 deletions src/laws.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ static bool RemoveNMRNewbie(void)
static void dumbeffect(unit *u) {
int effect = get_effect(u, oldpotiontype[P_FOOL]);
if (effect > 0) { /* Trank "Dumpfbackenbrot" */
int weeks = u->number;
if (weeks > effect) weeks = effect;
ptrdiff_t s, n = arrlen(u->skills);
skill *sb = NULL;
for (s = 0; s != n; ++s) {
Expand All @@ -186,13 +188,11 @@ static void dumbeffect(unit *u) {
}
/* bestes Talent raussuchen */
if (sb != NULL) {
int weeks = u->number;
if (weeks > effect) weeks = effect;
reduce_skill(u, sb, weeks);
ADDMSG(&u->faction->msgs, msg_message("dumbeffect",
"unit weeks skill", u, weeks, (skill_t)sb->id));
} /* sonst Glueck gehabt: wer nix weiss, kann nix vergessen... */
change_effect(u, oldpotiontype[P_FOOL], -effect);
change_effect(u, oldpotiontype[P_FOOL], -weeks);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/laws.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,7 @@ static void test_demon_hunger(CuTest * tc)
unit *u;

test_setup();
config_set("hunger.damage", NULL);
init_resources();
r = test_create_plain(0, 0);
rc = test_create_race("demon");
Expand Down
1 change: 1 addition & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ void test_use_astral(void)
void test_setup_test(CuTest *tc, const char *file, int line) {
test_log_stderr(LOG_CPERROR);
test_reset_full();
config_set("hunger.damage", "0");
message_handle_missing(MESSAGE_MISSING_REPLACE);
if (tc) {
log_debug("start test: %s", tc->name);
Expand Down
18 changes: 12 additions & 6 deletions src/upkeep.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@

#include <assert.h>

static void setup_upkeep(void)
{
test_setup();
config_set("hunger.damage", NULL);
}

void test_upkeep_default(CuTest * tc)
{
region *r;
unit *u1, *u2;
faction *f1, *f2;
const item_type *i_silver;

test_setup();
setup_upkeep();
init_resources();
i_silver = it_find("money");
assert(i_silver);
Expand Down Expand Up @@ -49,7 +55,7 @@ void test_upkeep_hunger_damage(CuTest * tc)
unit *u1;
faction *f1;

test_setup();
setup_upkeep();
init_resources();

r = test_create_plain(0, 0);
Expand All @@ -72,7 +78,7 @@ void test_upkeep_from_pool(CuTest * tc)
unit *u1, *u2;
const item_type *i_silver;

test_setup();
setup_upkeep();
init_resources();

i_silver = it_find("money");
Expand Down Expand Up @@ -106,7 +112,7 @@ void test_upkeep_from_friend(CuTest * tc)
faction *f1, *f2;
const item_type *i_silver;

test_setup();
setup_upkeep();
init_resources();

i_silver = it_find("money");
Expand Down Expand Up @@ -139,7 +145,7 @@ void test_lifestyle(CuTest *tc)
unit *u;
race *rc;

test_setup();
setup_upkeep();
u = test_create_unit(test_create_faction(), test_create_plain(0, 0));
CuAssertIntEquals(tc, 10, lifestyle(u));
u->number = 2;
Expand All @@ -159,7 +165,7 @@ void test_upkeep_free(CuTest * tc)
unit *u;
const item_type *i_silver;

test_setup();
setup_upkeep();
init_resources();

i_silver = it_find("money");
Expand Down

0 comments on commit 85c83b4

Please sign in to comment.