Merge pull request #241 from badgerman/feature/bug-1890-monster-give-orders

bug 1890: make monsters give GIVE orders
This commit is contained in:
Enno Rehling 2015-07-02 11:23:13 +02:00
commit a70b0d4902
4 changed files with 52 additions and 11 deletions

View File

@ -16,7 +16,7 @@ end
local function repair_ents(r) local function repair_ents(r)
for u in r.units do for u in r.units do
if u.faction.id==666 and u.race == "undead" and u.name == "Wütende Ents" then if u.faction.id==666 and u.race == "undead" and u.name == "Wütende Ents" then
print("ent repair", u) eressea.log.info("ent repair: " .. tostring(u))
u.race = "ent" u.race = "ent"
end end
end end

View File

@ -1,3 +1,4 @@
require 'tests.e2.undead'
require 'tests.e2.shiplanding' require 'tests.e2.shiplanding'
require 'tests.e2.e2features' require 'tests.e2.e2features'
require 'tests.e2.movement' require 'tests.e2.movement'

View File

@ -0,0 +1,31 @@
require "lunit"
module("tests.e2.undead", package.seeall, lunit.testcase)
function setup()
eressea.free_game()
end
function test_undead_give_item()
local r1 = region.create(0, 0, "plain")
local f1 = faction.create("hodor@eressea.de", "human", "de")
local u1 = unit.create(f1, r1, 1)
u1.race = "undead"
u1:clear_orders()
u1:add_item("log", 1)
u1:add_order("GIB 0 1 Holz")
process_orders()
assert_equal(0, u1:get_item("log"))
end
function test_undead_dont_give_person()
local r1 = region.create(0, 0, "plain")
local f1 = faction.create("hodor@eressea.de", "human", "de")
local u1 = unit.create(f1, r1, 2)
u1.race = "undead"
u1:clear_orders()
u1:add_item("log", 1)
u1:add_order("GIB 0 1 Person")
process_orders()
assert_equal(2, u1.number)
end

View File

@ -74,6 +74,16 @@
#define DRAGON_RANGE 20 /* Max. Distanz zum nächsten Drachenziel */ #define DRAGON_RANGE 20 /* Max. Distanz zum nächsten Drachenziel */
#define MAXILLUSION_TEXTS 3 #define MAXILLUSION_TEXTS 3
static void give_peasants(unit *u, const item_type *itype, int reduce) {
char buf[64];
slprintf(buf, sizeof(buf), "%s 0 %d %s", LOC(u->faction->locale, keyword(K_GIVE)), reduce, LOC(u->faction->locale, itype->rtype->_name));
unit_addorder(u, parse_order(buf, u->faction->locale));
}
static float monster_attack_chance(void) {
return get_param_flt(global.parameters, "rules.monsters.attack_chance", 0.4f);
}
static void reduce_weight(unit * u) static void reduce_weight(unit * u)
{ {
int capacity, weight = 0; int capacity, weight = 0;
@ -89,9 +99,11 @@ static void reduce_weight(unit * u)
while (*itmp != NULL) { while (*itmp != NULL) {
item *itm = *itmp; item *itm = *itmp;
const item_type *itype = itm->type; const item_type *itype = itm->type;
weight += itm->number * itype->weight;
if (itype->flags & ITF_VEHICLE) { if (itype->flags & ITF_VEHICLE) {
give_item(itm->number, itm->type, u, NULL, NULL); give_peasants(u, itm->type, itm->number);
}
else {
weight += itm->number * itype->weight;
} }
if (*itmp == itm) if (*itmp == itm)
itmp = &itm->next; itmp = &itm->next;
@ -109,7 +121,7 @@ static void reduce_weight(unit * u)
&& itype->rtype->atype == 0) { && itype->rtype->atype == 0) {
if (itype->capacity < itype->weight) { if (itype->capacity < itype->weight) {
int reduce = _min(itm->number, -((capacity - weight) / itype->weight)); int reduce = _min(itm->number, -((capacity - weight) / itype->weight));
give_item(reduce, itm->type, u, NULL, NULL); give_peasants(u, itm->type, reduce);
weight -= reduce * itype->weight; weight -= reduce * itype->weight;
} }
} }
@ -124,7 +136,7 @@ static void reduce_weight(unit * u)
weight += itm->number * itype->weight; weight += itm->number * itype->weight;
if (itype->capacity < itype->weight) { if (itype->capacity < itype->weight) {
int reduce = _min(itm->number, -((capacity - weight) / itype->weight)); int reduce = _min(itm->number, -((capacity - weight) / itype->weight));
give_item(reduce, itm->type, u, NULL, NULL); give_peasants(u, itm->type, reduce);
weight -= reduce * itype->weight; weight -= reduce * itype->weight;
} }
if (*itmp == itm) if (*itmp == itm)
@ -132,10 +144,6 @@ static void reduce_weight(unit * u)
} }
} }
static float monster_attack_chance(void) {
return get_param_flt(global.parameters, "rules.monsters.attack_chance", 0.4f);
}
static order *monster_attack(unit * u, const unit * target) static order *monster_attack(unit * u, const unit * target)
{ {
if (u->region != target->region) if (u->region != target->region)
@ -665,8 +673,6 @@ static order *plan_dragon(unit * u)
bool move = false; bool move = false;
order *long_order = NULL; order *long_order = NULL;
reduce_weight(u);
if (ta == NULL) { if (ta == NULL) {
move |= (r->land == 0 || r->land->peasants == 0); /* when no peasants, move */ move |= (r->land == 0 || r->land->peasants == 0); /* when no peasants, move */
move |= (r->land == 0 || r->land->money == 0); /* when no money, move */ move |= (r->land == 0 || r->land->money == 0); /* when no money, move */
@ -717,6 +723,9 @@ static order *plan_dragon(unit * u)
default: default:
break; break;
} }
if (long_order) {
reduce_weight(u);
}
if (rng_int() % 100 < 15) { if (rng_int() % 100 < 15) {
const struct locale *lang = u->faction->locale; const struct locale *lang = u->faction->locale;
/* do a growl */ /* do a growl */