Exemple #1
0
static const terrain_type *preferred_terrain(const struct race *rc)
{
    terrain_t t = T_PLAIN;
    if (rc == rc_find("dwarf"))
        t = T_MOUNTAIN;
    if (rc == rc_find("insect"))
        t = T_DESERT;
    if (rc == rc_find("halfling"))
        t = T_SWAMP;
    if (rc == rc_find("troll"))
        t = T_MOUNTAIN;
    return newterrain(t);
}
Exemple #2
0
/*
 * open rcfile and load its content, if already open - return previous handle
 */
int
rc_open(const char *filename, const char *mode,struct rcfile **rcfile)
{
	struct rcfile *rcp;
	FILE *f;
	rcp = rc_find(filename);
	if (rcp) {
		*rcfile = rcp;
		return (0);
	}
	f = fopen (filename, mode);
	if (f == NULL)
		return errno;
	rcp = malloc(sizeof(struct rcfile));
	if (rcp == NULL) {
		fclose(f);
		return ENOMEM;
	}
	bzero(rcp, sizeof(struct rcfile));
	rcp->rf_name = strdup(filename);
	rcp->rf_f = f;
	SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
	rc_parse(rcp);
	*rcfile = rcp;
	return (0);
}
Exemple #3
0
int sp_shadowcall(struct castorder * co)
{
    fighter * fi = co->magician.fig;
    int level = co->level;
    double power = co->force;
    battle *b = fi->side->battle;
    region *r = b->region;
    unit *mage = fi->unit;
    attrib *a;
    int force = (int)(get_force(power, 3) / 2);
    unit *u;
    const char *races[3] = { "shadowbat", "nightmare", "vampunicorn" };
    const race *rc = rc_find(races[rng_int() % 3]);
    message *msg;

    u = create_unit(r, mage->faction, force, rc, 0, NULL, mage);
    setstatus(u, ST_FIGHT);

    set_level(u, SK_WEAPONLESS, (int)(power / 2));
    set_level(u, SK_STAMINA, (int)(power / 2));
    u->hp = u->number * unit_max_hp(u);

    a = a_new(&at_unitdissolve);
    a->data.ca[0] = 0;
    a->data.ca[1] = 100;
    a_add(&u->attribs, a);

    make_fighter(b, u, fi->side, is_attacker(fi));
    msg =
        msg_message("sp_shadowcall_effect", "mage amount race", mage, u->number,
        u_race(u));
    message_all(b, msg);
    msg_release(msg);
    return level;
}
Exemple #4
0
static void test_ship_set_owner(CuTest * tc)
{
    struct region *r;
    struct ship *sh;
    struct unit *u1, *u2;
    struct faction *f;
    const struct ship_type *stype;
    const struct race *human;

    test_setup();
    test_create_world();

    human = rc_find("human");
    stype = st_find("boat");
    f = test_create_faction(human);
    r = findregion(0, 0);

    sh = test_create_ship(r, stype);
    u1 = test_create_unit(f, r);
    u_set_ship(u1, sh);
    CuAssertPtrEquals(tc, u1, ship_owner(sh));

    u2 = test_create_unit(f, r);
    u_set_ship(u2, sh);
    CuAssertPtrEquals(tc, u1, ship_owner(sh));
    ship_set_owner(u2);
    CuAssertPtrEquals(tc, u2, ship_owner(sh));
    test_teardown();
}
Exemple #5
0
static void test_shipowner_goes_to_other_when_empty(CuTest * tc)
{
    struct region *r;
    struct ship *sh;
    struct unit *u, *u2;
    struct faction *f;
    const struct ship_type *stype;
    const struct race *human;

    test_setup();
    test_create_world();

    human = rc_find("human");
    CuAssertPtrNotNull(tc, human);

    stype = st_find("boat");
    CuAssertPtrNotNull(tc, stype);

    f = test_create_faction(human);
    r = findregion(0, 0);

    sh = test_create_ship(r, stype);
    CuAssertPtrNotNull(tc, sh);

    u2 = test_create_unit(f, r);
    u = test_create_unit(f, r);
    CuAssertPtrNotNull(tc, u);
    u_set_ship(u, sh);
    u_set_ship(u2, sh);
    CuAssertPtrEquals(tc, u, ship_owner(sh));
    u->number = 0;
    CuAssertPtrEquals(tc, u2, ship_owner(sh));
    test_teardown();
}
Exemple #6
0
static void test_rename_building_twice(CuTest * tc)
{
  region *r;
  building *b;
  unit *u;
  faction *f;
  building_type *btype;

  test_cleanup();
  test_create_world();

  btype = bt_get_or_create("castle");

  r = findregion(-1, 0);
  b = new_building(btype, r, default_locale);
  f = test_create_faction(rc_find("human"));
  u = test_create_unit(f, r);
  u_set_building(u, b);

  rename_building(u, NULL, b, "Villa Nagel");
  CuAssertStrEquals(tc, "Villa Nagel", b->name);

  rename_building(u, NULL, b, "Villa Kunterbunt");
  CuAssertStrEquals(tc, "Villa Kunterbunt", b->name);
}
Exemple #7
0
static void test_fishing_feeds_2_people(CuTest * tc)
{
    const resource_type *rtype;
    region *r;
    faction *f;
    unit *u;
    ship *sh;
    
    test_cleanup();
    test_create_world();
    r = findregion(-1, 0);
    CuAssertStrEquals(tc, "ocean", r->terrain->_name);    /* test_create_world needs coverage */
    f = test_create_faction(rc_find("human"));
    u = test_create_unit(f, r);
    sh = new_ship(st_find("boat"), r, 0);
    u_set_ship(u, sh);
    rtype = get_resourcetype(R_SILVER);
    i_change(&u->items, rtype->itype, 42);
    
    scale_number(u, 1);
    sh->flags |= SF_FISHING;
    get_food(r);
    CuAssertIntEquals(tc, 42, i_get(u->items, rtype->itype));

    scale_number(u, 2);
    sh->flags |= SF_FISHING;
    get_food(r);
    CuAssertIntEquals(tc, 42, i_get(u->items, rtype->itype));

    scale_number(u, 3);
    sh->flags |= SF_FISHING;
    get_food(r);
    CuAssertIntEquals(tc, 32, i_get(u->items, rtype->itype));
}
Exemple #8
0
static const struct race * race_with_flag(const char * name) {
    char data[1024];
    cJSON *json;
    sprintf(data, "{\"races\" : { \"orc\": { \"speed\" : 1, \"flags\" : [ \"%s\"] }}}", name);

    json = cJSON_Parse(data);
    free_races();
    json_config(json);
    return rc_find("orc");
}
Exemple #9
0
static int tolua_faction_set_race(lua_State * L)
{
    faction *self = (faction *)tolua_tousertype(L, 1, 0);
    const char *name = tolua_tostring(L, 2, 0);
    const race *rc = rc_find(name);
    if (rc != NULL) {
        self->race = rc;
    }

    return 0;
}
Exemple #10
0
static void test_cannot_create_unit_above_limit(CuTest * tc)
{
  faction *f;

  test_cleanup();
  test_create_world();
  f = test_create_faction(rc_find("human"));
  set_param(&global.parameters, "rules.limit.faction", "4");

  CuAssertIntEquals(tc, 0, checkunitnumber(f, 4));
  CuAssertIntEquals(tc, 2, checkunitnumber(f, 5));

  set_param(&global.parameters, "rules.limit.alliance", "3");
  CuAssertIntEquals(tc, 0, checkunitnumber(f, 3));
  CuAssertIntEquals(tc, 1, checkunitnumber(f, 4));
}
Exemple #11
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;
}
Exemple #12
0
static int tolua_faction_create(lua_State * L)
{
    const char *email = tolua_tostring(L, 1, 0);
    const char *racename = tolua_tostring(L, 2, 0);
    const char *lang = tolua_tostring(L, 3, 0);
    struct locale *loc = get_locale(lang);
    faction *f = NULL;
    const struct race *frace = rc_find(racename);
    if (frace != NULL) {
        f = addfaction(email, NULL, frace, loc, 0);
    }
    if (!f) {
        log_error("faction.create(%s, %s, %s)\n", email, racename, lang);
    }
    tolua_pushusertype(L, f, TOLUA_CAST "faction");
    return 1;
}
Exemple #13
0
static void test_races(CuTest * tc)
{
    const char * data = "{\"races\": { \"orc\" : { "
        "\"damage\" : \"1d4\","
        "\"magres\" : 1.0,"
        "\"maxaura\" : 2.0,"
        "\"regaura\" : 3.0,"
        "\"speed\" : 4.0,"
        "\"recruitcost\" : 1,"
        "\"maintenance\" : 2,"
        "\"weight\" : 3,"
        "\"capacity\" : 4,"
        "\"hp\" : 5,"
        "\"ac\" : 6,"
        "\"flags\" : [ \"npc\", \"walk\", \"undead\" ]"
        "}}}";
    cJSON *json = cJSON_Parse(data);
    const struct race *rc;

    test_cleanup();

    CuAssertPtrNotNull(tc, json);
    CuAssertPtrEquals(tc, 0, races);
    json_config(json);

    CuAssertPtrNotNull(tc, races);
    rc = rc_find("orc");
    CuAssertPtrNotNull(tc, rc);
    CuAssertIntEquals(tc, RCF_NPC|RCF_WALK|RCF_UNDEAD, rc->flags);
    CuAssertStrEquals(tc, "1d4", rc->def_damage);
    CuAssertDblEquals(tc, 1.0, rc->magres, 0.0);
    CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0);
    CuAssertDblEquals(tc, 3.0, rc->regaura, 0.0);
    CuAssertDblEquals(tc, 4.0, rc->speed, 0.0);
    CuAssertIntEquals(tc, 1, rc->recruitcost);
    CuAssertIntEquals(tc, 2, rc->maintenance);
    CuAssertIntEquals(tc, 3, rc->weight);
    CuAssertIntEquals(tc, 4, rc->capacity);
    CuAssertIntEquals(tc, 5, rc->hitpoints);
    CuAssertIntEquals(tc, 6, rc->armor);
    test_cleanup();
}
Exemple #14
0
void test_shipowner_goes_to_empty_unit_after_leave(CuTest * tc)
{
    struct region *r;
    struct ship *sh;
    struct unit *u1, *u2, *u3;
    struct faction *f1;
    const struct ship_type *stype;
    const struct race *human;

    test_setup();
    test_create_world();

    human = rc_find("human");
    CuAssertPtrNotNull(tc, human);

    stype = st_find("boat");
    CuAssertPtrNotNull(tc, stype);

    f1 = test_create_faction(human);
    r = findregion(0, 0);

    sh = test_create_ship(r, stype);
    CuAssertPtrNotNull(tc, sh);

    u1 = test_create_unit(f1, r);
    u2 = test_create_unit(f1, r);
    u3 = test_create_unit(f1, r);
    u_set_ship(u1, sh);
    u_set_ship(u2, sh);
    u_set_ship(u3, sh);

    CuAssertPtrEquals(tc, u1, ship_owner(sh));
    u2->number = 0;
    leave_ship(u1);
    CuAssertPtrEquals(tc, u3, ship_owner(sh));
    leave_ship(u3);
    CuAssertPtrEquals(tc, NULL, ship_owner(sh));
    u2->number = 1;
    CuAssertPtrEquals(tc, u2, ship_owner(sh));
    test_teardown();
}
Exemple #15
0
static void test_shipowner_goes_to_same_faction_after_leave(CuTest * tc)
{
    struct region *r;
    struct ship *sh;
    struct unit *u, *u2, *u3;
    struct faction *f1, *f2;
    const struct ship_type *stype;
    const struct race *human;

    test_cleanup();
    test_create_world();

    human = rc_find("human");
    CuAssertPtrNotNull(tc, human);

    stype = st_find("boat");
    CuAssertPtrNotNull(tc, stype);

    f1 = test_create_faction(human);
    f2 = test_create_faction(human);
    r = findregion(0, 0);

    sh = test_create_ship(r, stype);
    CuAssertPtrNotNull(tc, sh);

    u2 = test_create_unit(f2, r);
    u3 = test_create_unit(f1, r);
    u = test_create_unit(f1, r);
    CuAssertPtrNotNull(tc, u);
    u_set_ship(u, sh);
    u_set_ship(u2, sh);
    u_set_ship(u3, sh);
    CuAssertPtrEquals(tc, u, ship_owner(sh));
    leave_ship(u);
    CuAssertPtrEquals(tc, u3, ship_owner(sh));
    leave_ship(u3);
    CuAssertPtrEquals(tc, u2, ship_owner(sh));
    leave_ship(u2);
    CuAssertPtrEquals(tc, 0, ship_owner(sh));
}
Exemple #16
0
static void create_monsters(faction **player, faction **monsters, region **r, unit **u, unit **m) {
    race* rc;

    test_cleanup();

    init_language();

    test_create_world();
    *player = test_create_faction(NULL);
    *monsters = get_or_create_monsters();
    assert(rc_find((*monsters)->race->_name));
    rc = rc_get_or_create((*monsters)->race->_name);
    fset(rc, RCF_UNARMEDGUARD);
    fset(rc, RCF_NPC);
    fset(*monsters, FFL_NOIDLEOUT);
    assert(fval(*monsters, FFL_NPC) && fval((*monsters)->race, RCF_UNARMEDGUARD) && fval((*monsters)->race, RCF_NPC) && fval(*monsters, FFL_NOIDLEOUT));
    (*monsters)->locale = default_locale;

    *r = findregion(0, 0);

    *u = test_create_unit(*player, *r);
    unit_setid(*u, 1);
    *m = test_create_unit(*monsters, *r);
}
Exemple #17
0
static void create_monsters(faction **player, faction **monsters, unit **u, unit **m) {
    race* rc;
    region *r;

    test_cleanup();

    test_create_horse();
    default_locale = test_create_locale();
    *player = test_create_faction(NULL);
    *monsters = get_or_create_monsters();
    assert(rc_find((*monsters)->race->_name));
    rc = rc_get_or_create((*monsters)->race->_name);
    fset(rc, RCF_UNARMEDGUARD|RCF_NPC|RCF_DRAGON);
    fset(*monsters, FFL_NOIDLEOUT);
    assert(fval(*monsters, FFL_NPC) && fval((*monsters)->race, RCF_UNARMEDGUARD) && fval((*monsters)->race, RCF_NPC) && fval(*monsters, FFL_NOIDLEOUT));

    test_create_region(-1, 0, test_create_terrain("ocean", SEA_REGION | SWIM_INTO | FLY_INTO));
    test_create_region(1, 0, 0);
    r = test_create_region(0, 0, 0);

    *u = test_create_unit(*player, r);
    unit_setid(*u, 1);
    *m = test_create_unit(*monsters, r);
}
Exemple #18
0
newfaction *read_newfactions(const char *filename)
{
    newfaction *newfactions = NULL;
    FILE *F = fopen(filename, "r");
    char buf[1024];

    if (F == NULL)
        return NULL;
    for (;;) {
        faction *f;
        char race[20], email[64], lang[8], password[16];
        newfaction *nf, **nfi;
        int bonus = 0, subscription = 0;
        int alliance = 0;

        if (fgets(buf, sizeof(buf), F) == NULL)
            break;

        email[0] = '\0';
        password[0] = '\0';

        if (sscanf(buf, "%54s %20s %8s %d %d %16s %d", email, race, lang, &bonus,
            &subscription, password, &alliance) < 3)
            break;
        if (email[0] == '\0')
            break;
        if (password[0] == '\0') {
            size_t sz;
            sz = strlcpy(password, itoa36(rng_int()), sizeof(password));
            sz += strlcat(password, itoa36(rng_int()), sizeof(password));
        }
        for (f = factions; f; f = f->next) {
            if (strcmp(f->email, email) == 0 && f->subscription
                && f->age < MINAGE_MULTI)
                break;
        }
        if (f && f->units)
            continue;                 /* skip the ones we've already got */
        for (nf = newfactions; nf; nf = nf->next) {
            if (strcmp(nf->email, email) == 0)
                break;
        }
        if (nf)
            continue;
        nf = calloc(sizeof(newfaction), 1);
        if (set_email(&nf->email, email) != 0) {
            log_error("Invalid email address for subscription %s: %s\n", itoa36(subscription), email);
            continue;
        }
        nf->password = _strdup(password);
        nf->race = rc_find(race);
        nf->subscription = subscription;
        if (alliances != NULL) {
            struct alliance *al = findalliance(alliance);
            if (al == NULL) {
                char zText[64];
                sprintf(zText, "Allianz %d", alliance);
                al = makealliance(alliance, zText);
            }
            nf->allies = al;
        }
        else {
            nf->allies = NULL;
        }
        if (nf->race == NULL) {
            /* if the script didn't supply the race as a token, then it gives us a
             * race in the default locale (which means that itis a UTF8 string) */
            nf->race = findrace(race, default_locale);
            if (nf->race == NULL) {
                char buffer[32];
                size_t outbytes = sizeof(buffer) - 1;
                size_t inbytes = strlen(race);
                unicode_latin1_to_utf8(buffer, &outbytes, race, &inbytes);
                buffer[outbytes] = 0;
                nf->race = findrace(buffer, default_locale);
                if (nf->race == NULL) {
                    log_error("new faction has unknown race '%s'.\n", race);
                    free(nf);
                    continue;
                }
            }
        }
        nf->lang = get_locale(lang);
        nf->bonus = bonus;
        assert(nf->race && nf->email && nf->lang);
        nfi = &newfactions;
        while (*nfi) {
            if ((*nfi)->race == nf->race)
                break;
            nfi = &(*nfi)->next;
        }
        nf->next = *nfi;
        *nfi = nf;
    }
    fclose(F);
    return newfactions;
}
Exemple #19
0
struct faction *test_create_faction(const struct race *rc)
{
  faction *f = addfaction("*****@*****.**", NULL, rc?rc:rc_find("human"), default_locale, 0);
  return f;
}
Exemple #20
0
struct unit *test_create_unit(struct faction *f, struct region *r)
{
  unit *u = create_unit(r, f, 1, f?f->race:rc_find("human"), 0, 0, 0);
  return u;
}