implemented a suggested change to RESERVE.

http://bugs.eressea.de/view.php?id=1675#c5240
new setting "rule.reserve.twophase" reserves from self first, before reserving from others.
fix a crash when trying to flush logs and there is no logfile.
This commit is contained in:
Enno Rehling 2014-07-20 06:19:21 +02:00
parent 5458fce59c
commit e44d4aa9b9
7 changed files with 90 additions and 6 deletions

View File

@ -223,6 +223,10 @@ void process_explain(void) {
}
void process_reserve(void) {
int rule = get_param_int(global.parameters, "rule.reserve.twophase", 0);
if (rule) {
process_cmd(K_RESERVE, reserve_self, 0);
}
process_cmd(K_RESERVE, reserve_cmd, 0);
}

View File

@ -4038,8 +4038,7 @@ int pay_cmd(unit * u, struct order *ord)
return 0;
}
int reserve_cmd(unit * u, struct order *ord)
static int reserve_i(unit * u, struct order *ord, int flags)
{
if (u->number > 0 && (urace(u)->ec_flags & GETITEM)) {
int use, count;
@ -4060,7 +4059,7 @@ int reserve_cmd(unit * u, struct order *ord)
return 0;
set_resvalue(u, rtype, 0); /* make sure the pool is empty */
use = use_pooled(u, rtype, GET_DEFAULT, count);
use = use_pooled(u, rtype, flags, count);
if (use) {
set_resvalue(u, rtype, use);
change_resource(u, rtype, use);
@ -4070,6 +4069,14 @@ int reserve_cmd(unit * u, struct order *ord)
return 0;
}
int reserve_cmd(unit * u, struct order *ord) {
return reserve_i(u, ord, GET_DEFAULT);
}
int reserve_self(unit * u, struct order *ord) {
return reserve_i(u, ord, GET_RESERVE | GET_SLACK);
}
int claim_cmd(unit * u, struct order *ord)
{
const char *t;

View File

@ -85,6 +85,7 @@ extern "C" {
extern int reshow_cmd(struct unit *u, struct order *ord);
extern int mail_cmd(struct unit *u, struct order *ord);
extern int reserve_cmd(struct unit *u, struct order *ord);
extern int reserve_self(struct unit *u, struct order *ord);
extern int claim_cmd(struct unit *u, struct order *ord);
extern int follow_cmd(struct unit *u, struct order *ord);

View File

@ -6,6 +6,7 @@
#include <kernel/building.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/order.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/ship.h>
@ -17,6 +18,8 @@
#include <CuTest.h>
#include <tests.h>
#include <assert.h>
static void test_new_building_can_be_renamed(CuTest * tc)
{
region *r;
@ -209,6 +212,68 @@ static void test_cannot_create_unit_above_limit(CuTest * tc)
CuAssertIntEquals(tc, 1, checkunitnumber(f, 4));
}
static void test_reserve_cmd(CuTest *tc) {
unit *u1, *u2;
faction *f;
region *r;
order *ord;
const resource_type *rtype;
const struct locale *loc;
test_cleanup();
test_create_world();
rtype = get_resourcetype(R_SILVER);
assert(rtype && rtype->itype);
f = test_create_faction(rc_find("human"));
r = findregion(0, 0);
assert(r && f);
u1 = test_create_unit(f, r);
u2 = test_create_unit(f, r);
assert(u1 && u2);
loc = get_locale("de");
assert(loc);
ord = create_order(K_RESERVE, loc, "200 SILBER");
assert(ord);
i_change(&u1->items, rtype->itype, 100);
i_change(&u2->items, rtype->itype, 100);
CuAssertIntEquals(tc, 200, reserve_cmd(u1, ord));
CuAssertIntEquals(tc, 200, i_get(u1->items, rtype->itype));
CuAssertIntEquals(tc, 0, i_get(u2->items, rtype->itype));
test_cleanup();
}
static void test_reserve_self(CuTest *tc) {
unit *u1, *u2;
faction *f;
region *r;
order *ord;
const resource_type *rtype;
const struct locale *loc;
test_cleanup();
test_create_world();
rtype = get_resourcetype(R_SILVER);
assert(rtype && rtype->itype);
f = test_create_faction(rc_find("human"));
r = findregion(0, 0);
assert(r && f);
u1 = test_create_unit(f, r);
u2 = test_create_unit(f, r);
assert(u1 && u2);
loc = get_locale("de");
assert(loc);
ord = create_order(K_RESERVE, loc, "200 SILBER");
assert(ord);
i_change(&u1->items, rtype->itype, 100);
i_change(&u2->items, rtype->itype, 100);
CuAssertIntEquals(tc, 100, reserve_self(u1, ord));
CuAssertIntEquals(tc, 100, i_get(u1->items, rtype->itype));
CuAssertIntEquals(tc, 100, i_get(u2->items, rtype->itype));
test_cleanup();
}
CuSuite *get_laws_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -219,6 +284,8 @@ CuSuite *get_laws_suite(void)
SUITE_ADD_TEST(suite, test_fishing_does_not_give_goblins_money);
SUITE_ADD_TEST(suite, test_fishing_gets_reset);
SUITE_ADD_TEST(suite, test_unit_limit);
SUITE_ADD_TEST(suite, test_reserve_self);
SUITE_ADD_TEST(suite, test_reserve_cmd);
SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit);
return suite;
}

View File

@ -1,6 +1,7 @@
#include <platform.h>
#include <kernel/types.h>
#include "tests.h"
#include "keyword.h"
#include <kernel/config.h>
#include <kernel/region.h>
@ -26,6 +27,7 @@ struct race *test_create_race(const char *name)
{
race *rc = rc_get_or_create(name);
rc->maintenance = 10;
rc->ec_flags |= GETITEM | GIVEITEM;
return rc;
}
@ -141,8 +143,11 @@ void test_create_world(void)
region *island[2];
int i;
item_type * itype;
struct locale * loc;
get_or_create_locale("de");
loc = get_or_create_locale("de");
locale_setstring(loc, keyword(K_RESERVE), "RESERVIEREN");
locale_setstring(loc, "money", "SILBER");
init_resources();
itype = test_create_itemtype("horse");

View File

@ -35,7 +35,7 @@ static FILE *logfile;
#define LOG_MAXBACKUPS 5
void log_flush(void)
{
fflush(logfile);
if (logfile) fflush(logfile);
}
void log_puts(const char *str)