Esempio n. 1
0
void test_pay_spell(CuTest * tc)
{
  spell *sp;
  unit * u;
  faction * f;
  region * r;
  int level;

  test_cleanup();
  test_create_world();
  r = findregion(0, 0);
  f = test_create_faction(0);
  u = test_create_unit(f, r);
  CuAssertPtrNotNull(tc, u);

  sp = test_magic_create_spell();
  CuAssertPtrNotNull(tc, sp);

  set_level(u, SK_MAGIC, 5);
  unit_add_spell(u, 0, sp, 1);

  change_resource(u, rt_find("money"), 1);
  change_resource(u, rt_find("aura"), 3);
  change_resource(u, rt_find("horse"), 3);

  level = eff_spelllevel(u, sp, 3, 1);
  CuAssertIntEquals(tc, 3, level);
  pay_spell(u, sp, level, 1);
  CuAssertIntEquals(tc, 0, get_resource(u, rt_find("money")));
  CuAssertIntEquals(tc, 0, get_resource(u, rt_find("aura")));
  CuAssertIntEquals(tc, 0, get_resource(u, rt_find("horse")));
}
Esempio n. 2
0
void test_change_resource(CuTest * tc)
{
  struct unit * u;
  struct faction * f;
  struct region * r;
  const char * names[] = { "money", "aura", "permaura", "horse", "hp", 0 };
  int i;

  test_cleanup();
  test_create_world();
  skill_enabled[SK_MAGIC] = 1;

  r = findregion(0, 0);
  f = test_create_faction(0);
  u = test_create_unit(f, r);
  CuAssertPtrNotNull(tc, u);
  set_level(u, SK_MAGIC, 5);
  create_mage(u, M_DRAIG);

  for (i=0;names[i];++i) {
    const struct resource_type *rtype = rt_find(names[i]);
    int have = get_resource(u, rtype);
    CuAssertIntEquals(tc, have+1, change_resource(u, rtype, 1));
    CuAssertIntEquals(tc, have+1, get_resource(u, rtype));
  }
}
Esempio n. 3
0
static void reduce_weight(unit * u)
{
    int capacity, weight = 0;
    item **itmp = &u->items;
    int horses = get_resource(u, get_resourcetype(R_HORSE));

    if (horses > 0) {
        horses = _min(horses, (u->number * 2));
        change_resource(u, get_resourcetype(R_HORSE), -horses);
    }

    /* 0. ditch any vehicles */
    while (*itmp != NULL) {
        item *itm = *itmp;
        const item_type *itype = itm->type;
        if (itype->flags & ITF_VEHICLE) {
            give_peasants(u, itm->type, itm->number);
        }
        else {
            weight += itm->number * itype->weight;
        }
        if (*itmp == itm)
            itmp = &itm->next;
    }

    capacity = walkingcapacity(u);

    /* 1. get rid of anything that isn't silver or really lightweight or helpful in combat */
    for (itmp = &u->items; *itmp && capacity > 0;) {
        item *itm = *itmp;
        const item_type *itype = itm->type;
        weight += itm->number * itype->weight;
        if (weight > capacity) {
            if (itype->weight >= 10 && itype->rtype->wtype == 0
                && itype->rtype->atype == 0) {
                if (itype->capacity < itype->weight) {
                    int reduce = _min(itm->number, -((capacity - weight) / itype->weight));
                    give_peasants(u, itm->type, reduce);
                    weight -= reduce * itype->weight;
                }
            }
        }
        if (*itmp == itm)
            itmp = &itm->next;
    }

    for (itmp = &u->items; *itmp && weight > capacity;) {
        item *itm = *itmp;
        const item_type *itype = itm->type;
        weight += itm->number * itype->weight;
        if (itype->capacity < itype->weight) {
            int reduce = _min(itm->number, -((capacity - weight) / itype->weight));
            give_peasants(u, itm->type, reduce);
            weight -= reduce * itype->weight;
        }
        if (*itmp == itm)
            itmp = &itm->next;
    }
}
Esempio n. 4
0
static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) {
    int n;
    change_resource(u, rtype, 4);
    n = get_resource(u, rtype);
    CuAssertPtrNotNull(tc, rtype->uchange);
    CuAssertIntEquals(tc, n, rtype->uchange(u, rtype, 0));
    CuAssertIntEquals(tc, n - 3, rtype->uchange(u, rtype, -3));
    CuAssertIntEquals(tc, n - 3, get_resource(u, rtype));
    CuAssertIntEquals(tc, 0, rtype->uchange(u, rtype, -n));
}
Esempio n. 5
0
static struct unit *create_recruiter(void) {
    region *r;
    faction *f;
    unit *u;
    const resource_type* rtype;

    test_cleanup();
    test_create_world();

    r=findregion(0, 0);
    rsetpeasants(r, 999);
    f = test_create_faction(rc_find("human"));
    u = test_create_unit(f, r);
    rtype = get_resourcetype(R_SILVER);
    change_resource(u, rtype, 1000);
    return u;
}
Esempio n. 6
0
int destroy_cmd(unit * u, struct order *ord)
{
    char token[128];
    ship *sh;
    unit *u2;
    region *r = u->region;
    const construction *con = NULL;
    int size = 0;
    const char *s;
    int n = INT_MAX;

    if (u->number < 1)
        return 0;

    init_order(ord);
    s = gettoken(token, sizeof(token));

    if (findparam(s, u->faction->locale) == P_ROAD) {
        destroy_road(u, INT_MAX, ord);
        return 0;
    }

    if (s && *s) {
        n = atoi((const char *)s);
        if (n <= 0) {
            cmistake(u, ord, 288, MSG_PRODUCE);
            return 0;
        }
    }

    if (getparam(u->faction->locale) == P_ROAD) {
        destroy_road(u, n, ord);
        return 0;
    }

    if (u->building) {
        building *b = u->building;

        if (u != building_owner(b)) {
            cmistake(u, ord, 138, MSG_PRODUCE);
            return 0;
        }
        if (fval(b->type, BTF_INDESTRUCTIBLE)) {
            cmistake(u, ord, 138, MSG_PRODUCE);
            return 0;
        }
        if (n >= b->size) {
            /* destroy completly */
            /* all units leave the building */
            for (u2 = r->units; u2; u2 = u2->next) {
                if (u2->building == b) {
                    leave_building(u2);
                }
            }
            ADDMSG(&u->faction->msgs, msg_message("destroy", "building unit", b, u));
            con = b->type->construction;
            remove_building(&r->buildings, b);
        }
        else {
            /* partial destroy */
            b->size -= n;
            ADDMSG(&u->faction->msgs, msg_message("destroy_partial",
                "building unit", b, u));
        }
    }
    else if (u->ship) {
        sh = u->ship;

        if (u != ship_owner(sh)) {
            cmistake(u, ord, 138, MSG_PRODUCE);
            return 0;
        }
        if (fval(r->terrain, SEA_REGION)) {
            cmistake(u, ord, 14, MSG_EVENT);
            return 0;
        }

        if (n >= (sh->size * 100) / sh->type->construction->maxsize) {
            /* destroy completly */
            /* all units leave the ship */
            for (u2 = r->units; u2; u2 = u2->next) {
                if (u2->ship == sh) {
                    leave_ship(u2);
                }
            }
            ADDMSG(&u->faction->msgs, msg_message("shipdestroy",
                "unit region ship", u, r, sh));
            con = sh->type->construction;
            remove_ship(&sh->region->ships, sh);
        }
        else {
            /* partial destroy */
            sh->size -= (sh->type->construction->maxsize * n) / 100;
            ADDMSG(&u->faction->msgs, msg_message("shipdestroy_partial",
                "unit region ship", u, r, sh));
        }
    }
    else {
        cmistake(u, ord, 138, MSG_PRODUCE);
        return 0;
    }

    if (con) {
        /* TODO: Nicht an ZERSTÖRE mit Punktangabe angepaßt! */
        int c;
        for (c = 0; con->materials[c].number; ++c) {
            const requirement *rq = con->materials + c;
            int recycle = (rq->number * size / con->reqsize) / 2;
            if (recycle) {
                change_resource(u, rq->rtype, recycle);
            }
        }
    }
    return 0;
}