All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.softwaremagico.tm.character.equipment.weapons.RandomRangeWeapon Maven / Gradle / Ivy

There is a newer version: 0.10.5
Show newest version
package com.softwaremagico.tm.character.equipment.weapons;

import com.softwaremagico.tm.InvalidXmlElementException;
import com.softwaremagico.tm.character.CharacterPlayer;
import com.softwaremagico.tm.character.characteristics.CharacteristicName;
import com.softwaremagico.tm.character.exceptions.RestrictedElementException;
import com.softwaremagico.tm.character.exceptions.UnofficialElementNotAllowedException;
import com.softwaremagico.tm.log.RandomGenerationLog;
import com.softwaremagico.tm.random.exceptions.InvalidCostElementSelectedException;
import com.softwaremagico.tm.random.exceptions.InvalidRandomElementSelectedException;
import com.softwaremagico.tm.random.selectors.IRandomPreference;
import com.softwaremagico.tm.random.selectors.WeaponsPreferences;

import java.util.Set;

/*-
 * #%L
 * Think Machine (Core)
 * %%
 * Copyright (C) 2017 - 2018 Softwaremagico
 * %%
 * This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
 *  Valencia (Spain).
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; If not, see .
 * #L%
 */

public class RandomRangeWeapon extends RandomWeapon {

    public RandomRangeWeapon(CharacterPlayer characterPlayer, Set> preferences,
                             Set mandatoryWeapons) throws InvalidXmlElementException, RestrictedElementException, UnofficialElementNotAllowedException {
        super(characterPlayer, preferences, mandatoryWeapons);
    }

    @Override
    public void assign() throws InvalidRandomElementSelectedException {
        final WeaponsPreferences weaponPreferences = WeaponsPreferences.getSelected(getPreferences());
        if (weaponPreferences != null) {
            float probabilityOfRangedWeapon = weaponPreferences.getRangeWeaponProbability();
            while (probabilityOfRangedWeapon > 0) {
                if (random.nextFloat() < probabilityOfRangedWeapon) {
                    try {
                        super.assign();
                    } catch (InvalidCostElementSelectedException | UnofficialElementNotAllowedException e) {
                        //Try again with a different weapon.
                        continue;
                    } catch (InvalidRandomElementSelectedException ires) {
                        RandomGenerationLog.warning(this.getClass().getName(),
                                "No ranged weapons available for '{}'.", getCharacterPlayer());
                    }
                }
                probabilityOfRangedWeapon -= 0.3f;
            }
        }
    }

    @Override
    protected Set weaponTypesFilter() {
        return WeaponType.getRangedTypes();
    }

    @Override
    protected int getWeightCostModificator(Weapon weapon) throws InvalidRandomElementSelectedException {
        if (weapon.getCost() > getCurrentMoney()) {
            throw new InvalidRandomElementSelectedException("Not enough money!");
        } else if (weapon.getCost() > getCurrentMoney() / 1.1) {
            return 100;
        } else if (weapon.getCost() > getCurrentMoney() / (double) 2) {
            return 7;
        } else if (weapon.getCost() > getCurrentMoney() / (double) 3) {
            return 5;
        } else if (weapon.getCost() > getCurrentMoney() / (double) 4) {
            return 4;
        } else if (weapon.getCost() > getCurrentMoney() / (double) 5) {
            return 3;
        } else if (weapon.getCost() > getCurrentMoney() / (double) 10) {
            return 2;
        } else if (weapon.getCost() > getCurrentMoney() / (double) 20) {
            return 1;
        } else {
            // No so cheap weapons.
            return MAX_PROBABILITY;
        }
    }

    @Override
    protected int getWeightTechModificator(Weapon weapon) {
        int weight = 0;
        // Similar tech level preferred.
        weight += MAX_PROBABILITY / Math.pow(10,
                2d * (getCharacterPlayer().getCharacteristicValue(CharacteristicName.TECH) - weapon.getTechLevel()));
        RandomGenerationLog.debug(this.getClass().getName(), "Weight tech bonus for '{}' is '{}'.", weapon, MAX_PROBABILITY
                / Math.pow(10, 2d * (getCharacterPlayer().getCharacteristic(CharacteristicName.TECH).getValue()
                - weapon.getTechLevel())));
        if (weight <= 0) {
            weight = 0;
        }
        return weight;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy