move is_guarded from move.h to guard.h

Conflicts:
	src/move.c
This commit is contained in:
Enno Rehling 2016-10-26 11:35:20 +02:00
parent 59c8fbc5a5
commit 5bb2dbfd87
5 changed files with 83 additions and 78 deletions

View File

@ -19,6 +19,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
#include "alchemy.h" #include "alchemy.h"
#include "guard.h"
#include "move.h" #include "move.h"
#include "skill.h" #include "skill.h"
#include "study.h" #include "study.h"

View File

@ -20,7 +20,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
#include "guard.h" #include "guard.h"
#include "laws.h"
#include "monster.h"
#include <kernel/ally.h>
#include <kernel/save.h> #include <kernel/save.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/faction.h> #include <kernel/faction.h>
@ -145,3 +148,79 @@ void guard(unit * u, unsigned int mask)
unsigned int flags = guard_flags(u); unsigned int flags = guard_flags(u);
setguard(u, flags & mask); setguard(u, flags & mask);
} }
static bool is_guardian_u(const unit * guard, unit * u, unsigned int mask)
{
if (guard->faction == u->faction)
return false;
if (is_guard(guard, mask) == 0)
return false;
if (alliedunit(guard, u->faction, HELP_GUARD))
return false;
if (ucontact(guard, u))
return false;
if (!cansee(guard->faction, u->region, u, 0))
return false;
if (!(u_race(guard)->flags & RCF_FLY) && u_race(u)->flags & RCF_FLY)
return false;
return true;
}
static bool is_guardian_r(const unit * guard)
{
if (guard->number == 0)
return false;
if (besieged(guard))
return false;
/* if region_owners exist then they may be guardians: */
if (guard->building && rule_region_owners() && guard == building_owner(guard->building)) {
faction *owner = region_get_owner(guard->region);
if (owner == guard->faction) {
building *bowner = largestbuilding(guard->region, &cmp_taxes, false);
if (bowner == guard->building) {
return true;
}
}
}
if ((guard->flags & UFL_GUARD) == 0)
return false;
return fval(u_race(guard), RCF_UNARMEDGUARD) || is_monsters(guard->faction) || (armedmen(guard, true) > 0);
}
bool is_guard(const struct unit * u, unsigned int mask)
{
return is_guardian_r(u) && (getguard(u) & mask) != 0;
}
unit *is_guarded(region * r, unit * u, unsigned int mask)
{
unit *u2;
int noguards = 1;
if (!fval(r, RF_GUARDED)) {
return NULL;
}
/* at this point, u2 is the last unit we tested to
* be a guard (and failed), or NULL
* i is the position of the first free slot in the cache */
for (u2 = r->units; u2; u2 = u2->next) {
if (is_guardian_r(u2)) {
noguards = 0;
if (is_guardian_u(u2, u, mask)) {
/* u2 is our guard. stop processing (we might have to go further next time) */
return u2;
}
}
}
if (noguards) {
/* you are mistaken, sir. there are no guards in these lands */
freset(r, RF_GUARDED);
}
return NULL;
}

View File

@ -20,6 +20,9 @@ extern "C" {
void guard(struct unit * u, unsigned int mask); void guard(struct unit * u, unsigned int mask);
struct unit *is_guarded(struct region *r, struct unit *u, unsigned int mask);
bool is_guard(const struct unit *u, unsigned int mask);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -977,82 +977,6 @@ static unit *bewegung_blockiert_von(unit * reisender, region * r)
return NULL; return NULL;
} }
static bool is_guardian_u(const unit * guard, unit * u, unsigned int mask)
{
if (guard->faction == u->faction)
return false;
if (is_guard(guard, mask) == 0)
return false;
if (alliedunit(guard, u->faction, HELP_GUARD))
return false;
if (ucontact(guard, u))
return false;
if (!cansee(guard->faction, u->region, u, 0))
return false;
if (!(u_race(guard)->flags & RCF_FLY) && u_race(u)->flags & RCF_FLY)
return false;
return true;
}
static bool is_guardian_r(const unit * guard)
{
if (guard->number == 0)
return false;
if (besieged(guard))
return false;
/* if region_owners exist then they may be guardians: */
if (guard->building && rule_region_owners() && guard == building_owner(guard->building)) {
faction *owner = region_get_owner(guard->region);
if (owner == guard->faction) {
building *bowner = largestbuilding(guard->region, &cmp_taxes, false);
if (bowner == guard->building) {
return true;
}
}
}
if ((guard->flags & UFL_GUARD) == 0)
return false;
return fval(u_race(guard), RCF_UNARMEDGUARD) || is_monsters(guard->faction) || (armedmen(guard, true) > 0);
}
bool is_guard(const struct unit * u, unsigned int mask)
{
return is_guardian_r(u) && (getguard(u) & mask) != 0;
}
unit *is_guarded(region * r, unit * u, unsigned int mask)
{
unit *u2;
int noguards = 1;
if (!fval(r, RF_GUARDED)) {
return NULL;
}
/* at this point, u2 is the last unit we tested to
* be a guard (and failed), or NULL
* i is the position of the first free slot in the cache */
for (u2 = r->units; u2; u2 = u2->next) {
if (is_guardian_r(u2)) {
noguards = 0;
if (is_guardian_u(u2, u, mask)) {
/* u2 is our guard. stop processing (we might have to go further next time) */
return u2;
}
}
}
if (noguards) {
/* you are mistaken, sir. there are no guards in these lands */
freset(r, RF_GUARDED);
}
return NULL;
}
bool move_blocked(const unit * u, const region * r, const region * r2) bool move_blocked(const unit * u, const region * r, const region * r2)
{ {
connection *b; connection *b;

View File

@ -60,8 +60,6 @@ extern "C" {
int personcapacity(const struct unit *u); int personcapacity(const struct unit *u);
void movement(void); void movement(void);
void run_to(struct unit *u, struct region *to); void run_to(struct unit *u, struct region *to);
struct unit *is_guarded(struct region *r, struct unit *u, unsigned int mask);
bool is_guard(const struct unit *u, unsigned int mask);
int enoughsailors(const struct ship *sh, int sumskill); int enoughsailors(const struct ship *sh, int sumskill);
bool canswim(struct unit *u); bool canswim(struct unit *u);
bool canfly(struct unit *u); bool canfly(struct unit *u);