/**
 * @brief Write the Human data back to a file.
 */
void Human::writeToFile( QFile &unitFile )
{
    QTextStream unitStream(&unitFile);

    unitStream << profession() << "/"
               << QString("%1").arg(posX(),0,'g',8) << "/"
               << QString("%1").arg(posY(),0,'g',8) << "/"
               << QString("%1").arg(posZ(),0,'g',8) << "/"
               << name() << "/";
    unitStream.flush();

    unitFile.write(Utils::toBinary(archerLevel()));
    unitFile.write(Utils::toBinary(blacksmithLevel()));
    unitFile.write(Utils::toBinary(builderLevel()));
    unitFile.write(Utils::toBinary(carpenterLevel()).constData());
    unitFile.write(Utils::toBinary(engineerLevel()).constData());
    unitFile.write(Utils::toBinary(farmerLevel()).constData());
    unitFile.write(Utils::toBinary(fishermanLevel()).constData());
    unitFile.write(Utils::toBinary(foragerLevel()).constData());
    unitFile.write(Utils::toBinary(infantryLevel()).constData());
    unitFile.write(Utils::toBinary(minerLevel()).constData());
    unitFile.write(Utils::toBinary(stoneMasonLevel()).constData());
    unitFile.write(Utils::toBinary(woodChopperLevel()).constData());
    unitFile.write(Utils::toBinary(tailorLevel()).constData());
    unitFile.write(Utils::toBinary(traderLevel()).constData());
    unitFile.write(Utils::toBinary(herderLevel()).constData());
    unitFile.write(Utils::toBinary(adventurerLevel()).constData());
    unitFile.write(Utils::toBinary(unknown1Level()).constData());
    unitFile.write(Utils::toBinary(unknown2Level()).constData());
    unitFile.write(Utils::toBinary(unknown3Level()).constData());
    unitFile.write(Utils::toBinary(unknown4Level()).constData());
    unitStream << "/"; unitStream.flush();

    unitFile.write(Utils::toBinary(experience()).constData());
    unitStream << "/"; unitStream.flush();

    unitStream << QString(autoChop()?"True":"False") << "/"
               << QString(gatherBerries()?"True":"False") << "/"
               << QString(huntChicken()?"True":"False") << "/"
               << QString(huntBoar()?"True":"False") << "/"
               << QString(showBowRange()?"True":"False") << "/";

    unitStream << QString(trainNearTarget()?"True":"False") << "/";

    unitStream << QString("%1").arg(rotation(),0,'g',8) << "/";
    unitStream.flush();

    unitFile.write(Utils::toBinary(equipHand()).constData());
    unitStream << "/"; unitStream.flush();
    unitFile.write(Utils::toBinary(equipOffhand()).constData());
    unitStream << "/"; unitStream.flush();
    unitFile.write(Utils::toBinary(equipHead()).constData());
    unitStream << "/"; unitStream.flush();
    unitFile.write(Utils::toBinary(equipBody()).constData());
    unitStream << "/"; unitStream.flush();
    unitFile.write(Utils::toBinary(equipFeet()).constData());
    unitStream << "/"; unitStream.flush();
    unitFile.write(Utils::toBinary(health()).constData());
    unitStream << "/"; unitStream.flush();

    // Dump some of the options in the file.
    for (unsigned int i = 0; i<52; i++) {
        unitStream << QString(option(i)?"True":"False") << "/";
    }
    unitStream << timeToEat() << "/"
               << morale() << "/"
               << fatigue() << "/"
               << hunger() << "/";

    // Dump more options in the file.
    for (unsigned int i = 52; i<52+12; i++) {
        unitStream << QString(option(i)?"True":"False") << "/";
    }

    // Inventory Preferences
    for (int i = 0; i<inventoryPreferences()->length(); i++) {
        unitStream << inventoryPreferences()->at(i) << "/";
    }

    // Inventory Items
    unitStream << inventoryItems()->length() << "/";
    for (int i = 0; i<inventoryItems()->length(); i++) {
        unitStream << inventoryItems()->at(i) << "/";
    }

    // Spare Inventory
    unitStream << spareInventory()->length() << "/";
    for (int i = 0; i<spareInventory()->length(); i++) {
        unitStream << spareInventory()->at(i) << "/";
    }

    // Patrol
    unitStream << patrolSetpoints()->length() << "/";
    for (int i = 0; i<patrolSetpoints()->length(); i++) {
        unitStream << patrolSetpoints()->at(i) << "/";
    }
    unitStream << patrolIndex() << "/";

    unitStream << guardedUnit() << "/";

    // Profession Experience
    for (int i = 0; i<professionEXP()->length(); i++) {
        unitStream << professionEXP()->at(i) << "/";
    }

    unitStream << maxWeight() << "/";

    unitStream << endl;
    unitStream.flush();

}
QVariant Human::data(int role) const
{
    switch(role) {
    case IdRole:
        return (unsigned int)id();
    case FilterStringRole:
        return filterString();

    case ProfessionRole:
        return profession();
    case PosXRole:
        return posX();
    case PosYRole:
        return posY();
    case PosZRole:
        return posZ();
    case NameRole:
        return name();

    case ArcherLevelRole:
        return archerLevel();
    case BlacksmithLevelRole:
        return blacksmithLevel();
    case BuilderLevelRole:
        return builderLevel();
    case CarpenterLevelRole:
        return carpenterLevel();
    case EngineerLeveRole:
        return engineerLevel();
    case FarmerLevelRole:
        return farmerLevel();
    case FishermanLevelRole:
        return fishermanLevel();
    case ForagerLevelRole:
        return foragerLevel();
    case InfantryLevelRole:
        return infantryLevel();
    case MinerLevelRole:
        return minerLevel();
    case StoneMasonLevelRole:
        return stoneMasonLevel();
    case WoodChopperLevelRole:
        return woodChopperLevel();
    case TailorLevelRole:
        return tailorLevel();
    case TraderLevelRole:
        return traderLevel();
    case HerderLevelRole:
        return herderLevel();
    case AdventurerLevelRole:
        return adventurerLevel();
    case Unknown1Role:
        return unknown1Level();
    case Unknown2Role:
        return unknown2Level();
    case Unknown3Role:
        return unknown3Level();
    case Unknown4Role:
        return unknown4Level();

    case ExperienceRole:
        return experience();

    case AutoChopTreesRole:
        return autoChop();
    case GatherBerriesRole:
        return gatherBerries();
    case HuntChickenRole:
        return huntChicken();
    case HuntBoarRole:
        return huntBoar();
    case ShowBowRangeRole:
        return showBowRange();
    case TrainNearTargetRole:
        return trainNearTarget();

    case RotationRole:
        return rotation();

    case EquipHandRole:
        return equipHand();
    case EquipOffhandRole:
        return equipOffhand();
    case EquipHeadRole:
        return equipHead();
    case EquipBodyRole:
        return equipBody();
    case EquipFeetRole:
        return equipFeet();

    case HealthRole:
        return health();

    case MoraleRole:
        return morale();
    case FatigueRole:
        return fatigue();
    case HungerRole:
        return hunger();

    default:
        return QVariant();
    }
}
Beispiel #3
0
void get_food(region * r)
{
    plane *pl = rplane(r);
    unit *u;
    int peasantfood = rpeasants(r) * 10;
    static int food_rules = -1;
    static int gamecookie = -1;

    if (food_rules < 0 || gamecookie != global.cookie) {
        gamecookie = global.cookie;
        food_rules = get_param_int(global.parameters, "rules.economy.food", 0);
    }

    if (food_rules & FOOD_IS_FREE) {
        return;
    }
    /* 1. Versorgung von eigenen Einheiten. Das vorhandene Silber
    * wird zunächst so auf die Einheiten aufgeteilt, dass idealerweise
    * jede Einheit genug Silber für ihren Unterhalt hat. */

    for (u = r->units; u; u = u->next) {
        int need = lifestyle(u);

        /* Erstmal zurücksetzen */
        freset(u, UFL_HUNGER);

        if (u->ship && (u->ship->flags & SF_FISHING)) {
            unit *v;
            int c = 2;
            for (v = u; c > 0 && v; v = v->next) {
                if (v->ship == u->ship) {
                    int get = 0;
                    if (v->number <= c) {
                        get = lifestyle(v);
                    }
                    else {
                        get = lifestyle(v) * c / v->number;
                    }
                    if (get) {
                        change_money(v, get);
                    }
                }
                c -= v->number;
            }
            u->ship->flags -= SF_FISHING;
        }

        if (food_rules & FOOD_FROM_PEASANTS) {
            struct faction *owner = region_get_owner(r);
            /* if the region is owned, and the owner is nice, then we'll get
            * food from the peasants - should not be used with WORK */
            if (owner != NULL && (get_alliance(owner, u->faction) & HELP_MONEY)) {
                int rm = rmoney(r);
                int use = _min(rm, need);
                rsetmoney(r, rm - use);
                need -= use;
            }
        }

        need -= get_money(u);
        if (need > 0) {
            unit *v;

            for (v = r->units; need && v; v = v->next) {
                if (v->faction == u->faction && help_money(v)) {
                    int give = get_money(v) - lifestyle(v);
                    give = _min(need, give);
                    if (give > 0) {
                        change_money(v, -give);
                        change_money(u, give);
                        need -= give;
                    }
                }
            }
        }
    }

    /* 2. Versorgung durch Fremde. Das Silber alliierter Einheiten wird
    * entsprechend verteilt. */
    for (u = r->units; u; u = u->next) {
        int need = lifestyle(u);
        faction *f = u->faction;

        need -= _max(0, get_money(u));

        if (need > 0) {
            unit *v;

            if (food_rules & FOOD_FROM_OWNER) {
                /* the owner of the region is the first faction to help out when you're hungry */
                faction *owner = region_get_owner(r);
                if (owner && owner != u->faction) {
                    for (v = r->units; v; v = v->next) {
                        if (v->faction == owner && alliedunit(v, f, HELP_MONEY)
                            && help_money(v)) {
                            help_feed(v, u, &need);
                            break;
                        }
                    }
                }
            }
            for (v = r->units; need && v; v = v->next) {
                if (v->faction != f && alliedunit(v, f, HELP_MONEY)
                    && help_money(v)) {
                    help_feed(v, u, &need);
                }
            }

            /* Die Einheit hat nicht genug Geld zusammengekratzt und
            * nimmt Schaden: */
            if (need > 0) {
                int lspp = lifestyle(u) / u->number;
                if (lspp > 0) {
                    int number = (need + lspp - 1) / lspp;
                    if (hunger(number, u))
                        fset(u, UFL_HUNGER);
                }
            }
        }
    }

    /* 3. bestimmen, wie viele Bauern gefressen werden.
    * bei fehlenden Bauern den Dämon hungern lassen
    */
    for (u = r->units; u; u = u->next) {
        if (u_race(u) == get_race(RC_DAEMON)) {
            int hungry = u->number;

            /* use peasantblood before eating the peasants themselves */
            const struct potion_type *pt_blood = 0;
            const resource_type *rt_blood = rt_find("peasantblood");
            if (rt_blood) {
                pt_blood = rt_blood->ptype;
            }
            if (pt_blood) {
                /* always start with the unit itself, then the first known unit that may have some blood */
                unit *donor = u;
                while (donor != NULL && hungry > 0) {
                    int blut = get_effect(donor, pt_blood);
                    blut = _min(blut, hungry);
                    if (blut) {
                        change_effect(donor, pt_blood, -blut);
                        hungry -= blut;
                    }
                    if (donor == u)
                        donor = r->units;
                    while (donor != NULL) {
                        if (u_race(donor) == get_race(RC_DAEMON) && donor != u) {
                            if (get_effect(donor, pt_blood)) {
                                /* if he's in our faction, drain him: */
                                if (donor->faction == u->faction)
                                    break;
                            }
                        }
                        donor = donor->next;
                    }
                }
            }
            /* remaining demons feed on peasants */
            if (pl == NULL || !fval(pl, PFL_NOFEED)) {
                if (peasantfood >= hungry) {
                    peasantfood -= hungry;
                    hungry = 0;
                }
                else {
                    hungry -= peasantfood;
                    peasantfood = 0;
                }
                if (hungry > 0) {
                    static int demon_hunger = -1;
                    if (demon_hunger < 0) {
                        demon_hunger = get_param_int(global.parameters, "hunger.demons", 0);
                    }
                    if (demon_hunger == 0) {
                        /* demons who don't feed are hungry */
                        if (hunger(hungry, u))
                            fset(u, UFL_HUNGER);
                    }
                    else {
                        /* no damage, but set the hungry-flag */
                        fset(u, UFL_HUNGER);
                    }
                }
            }
        }
    }
    rsetpeasants(r, peasantfood / 10);

    /* 3. Von den überlebenden das Geld abziehen: */
    for (u = r->units; u; u = u->next) {
        int need = _min(get_money(u), lifestyle(u));
        change_money(u, -need);
    }
}