]> git.pond.sub.org Git - empserver/commitdiff
tend: Factor can_tend_to() out of tend(), tend_land()
authorMarkus Armbruster <armbru@pond.sub.org>
Fri, 15 Sep 2017 06:32:10 +0000 (08:32 +0200)
committerMarkus Armbruster <armbru@pond.sub.org>
Tue, 5 Jan 2021 06:25:17 +0000 (07:25 +0100)
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
src/lib/commands/tend.c

index bb50f4b61c7cb27aee098df0c2bf5c9514a891e4..f88db9a64ffc89fba36c071eb6116debf6478c2d 100644 (file)
@@ -41,6 +41,7 @@
 #include "plague.h"
 #include "ship.h"
 
+static int can_tend_to(struct shpstr *, struct shpstr *);
 static void expose_ship(struct shpstr *s1, struct shpstr *s2);
 static int tend_land(struct shpstr *tenderp, char *units);
 
@@ -134,23 +135,15 @@ tend(void)
            return RET_SYN;
        total = 0;
        while (nxtitem(&targets, &target)) {
-           if (!player->owner
-               && relations_with(target.shp_own, player->cnum) < FRIENDLY)
-               continue;
-           if (target.shp_uid == tender.shp_uid)
-               continue;
-           if (tender.shp_x != target.shp_x ||
-               tender.shp_y != target.shp_y)
-               continue;
            if (ip->i_uid == I_CIVIL && tender.shp_own != target.shp_own)
                continue;
-           ontarget = target.shp_item[ip->i_uid];
-           vbase = &mchr[(int)target.shp_type];
-           maxtarget = vbase->m_item[ip->i_uid];
            if (amt < 0) {
                /* take from target and give to tender */
                if (!player->owner)
                    continue;
+               if (!can_tend_to(&target, &tender))
+                   continue;
+               ontarget = target.shp_item[ip->i_uid];
                if (ontarget == 0) {
                    pr("No %s on %s\n", ip->i_name, prship(&target));
                    continue;
@@ -164,6 +157,11 @@ tend(void)
                total += transfer;
            } else {
                /* give to target from tender */
+               if (!can_tend_to(&tender, &target))
+                   continue;
+               ontarget = target.shp_item[ip->i_uid];
+               vbase = &mchr[(int)target.shp_type];
+               maxtarget = vbase->m_item[ip->i_uid];
                transfer = MIN(ontender, amt);
                transfer = MIN(transfer, maxtarget - ontarget);
                if (transfer == 0)
@@ -194,6 +192,19 @@ tend(void)
     return RET_OK;
 }
 
+static int
+can_tend_to(struct shpstr *from, struct shpstr *to)
+{
+    if (to->shp_own != player->cnum && !player->god
+       && relations_with(to->shp_own, player->cnum) < FRIENDLY)
+       return 0;
+    if (from->shp_uid == to->shp_uid)
+       return 0;
+    if (from->shp_x != to->shp_x || from->shp_y != to->shp_y)
+       return 0;
+    return 1;
+}
+
 static void
 expose_ship(struct shpstr *s1, struct shpstr *s2)
 {
@@ -237,13 +248,7 @@ tend_land(struct shpstr *tenderp, char *units)
        if (!check_ship_ok(tenderp) || !check_land_ok(&land))
            return RET_SYN;
        while (nxtitem(&targets, &target)) {
-           if (!player->owner
-               && relations_with(target.shp_own, player->cnum) < FRIENDLY)
-               continue;
-           if (target.shp_uid == tenderp->shp_uid)
-               continue;
-           if (tenderp->shp_x != target.shp_x ||
-               tenderp->shp_y != target.shp_y)
+           if (!can_tend_to(tenderp, &target))
                continue;
 
            /* Fit unit on ship */