Skip to content

Commit

Permalink
#726 Move armed freighters mechanics to government.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomount committed Feb 3, 2024
1 parent 723901e commit a1c6385
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ public void focusLost(final FocusEvent e) {

@Override
public void focusGained(final FocusEvent e) {
// Nothing to do here
}
});
designNameText.addKeyListener(new KeyListener() {
Expand Down Expand Up @@ -632,12 +633,14 @@ public ShipComponent[] filterComponents(final String filter) {
public void updatePanels() {
ShipHull hull = (ShipHull) hullSelect.getSelectedItem();
if (hull != null) {
hullInfoText.setText(hull.toString());
hullInfoText.setText(hull.getDescription(
player.getGovernment().allowArmedFreighters()));
hullImage.setImage(hull.getImage());
}
if (design != null) {
designNameText.setText(design.getName());
String flaws = design.getFlaws(banNukes, banPrivateer);
String flaws = design.getFlaws(banNukes, banPrivateer,
player.getGovernment().allowArmedFreighters());
boolean duplicate = player.duplicateShipDesignName(design.getName());
if (duplicate) {
illegalName = true;
Expand Down Expand Up @@ -825,7 +828,8 @@ public void handleAction(final ActionEvent arg0) {
public boolean isDesignOK() {
updatePanels();
if (design != null
&& design.getFlaws(banNukes, banPrivateer).equals(
&& design.getFlaws(banNukes, banPrivateer,
player.getGovernment().allowArmedFreighters()).equals(
ShipDesignConsts.DESIGN_OK) && !illegalName) {
return true;
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/openRealmOfStars/player/fleet/Fleet.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.openRealmOfStars.player.fleet;
/*
* Open Realm of Stars game project
* Copyright (C) 2016-2023 Tuomo Untinen
* Copyright (C) 2016-2024 Tuomo Untinen
* Copyright (C) 2023 BottledByte
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -33,7 +33,6 @@
import org.openRealmOfStars.player.leader.Leader;
import org.openRealmOfStars.player.leader.Perk;
import org.openRealmOfStars.player.race.SpaceRace;
import org.openRealmOfStars.player.race.trait.TraitIds;
import org.openRealmOfStars.player.ship.Ship;
import org.openRealmOfStars.player.ship.ShipHull;
import org.openRealmOfStars.player.ship.ShipHullType;
Expand Down Expand Up @@ -980,17 +979,17 @@ public int getMilitaryValue() {

/**
* Smuggler fleet is one with no apparent military power on fleet.
* Only Freighter ships are allowed to have weapons, if they are
* Smaugirians.
* Only Freighter ships are allowed to have weapons, if realm has certain
* government. This is resticted in ship design. Generally speaking
* Armed freighter is a smuggler fleet.
* @return True if fleet is smuggler fleet.
*/
public boolean isSmugglerFleet() {
int result = 0;
for (Ship ship : ships) {
if (!ship.isStarBase() || ship.getFlag(Ship.FLAG_STARBASE_DEPLOYED)) {
result = result + ship.getTotalMilitaryPower();
if (ship.getHull().getRace().hasTrait(TraitIds.ARMED_FREIGHTERS)
&& ship.getHull().getHullType() == ShipHullType.FREIGHTER) {
if (ship.isSmuggler()) {
result = result - ship.getTotalMilitaryPower();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.openRealmOfStars.player.government;
/*
* Open Realm of Stars game project
* Copyright (C) 2018-2021 Tuomo Untinen
* Copyright (C) 2018-2024 Tuomo Untinen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -357,6 +357,17 @@ public boolean hasCreditRush() {
return false;
}

/**
* Allow building armed freighters.
* @return True if armed freigters are ok.
*/
public boolean allowArmedFreighters() {
if (this == SPACE_PIRATES || this == REGIME || this == SYNDICATE) {
return true;
}
return false;
}

@Override
public String toString() {
return name;
Expand Down Expand Up @@ -624,6 +635,11 @@ public String getDescription(final boolean markDown) {
sb.append(" Possibility to internal power struggle");
sb.append(lf);
}
if (allowArmedFreighters()) {
sb.append(dot);
sb.append(" Armed freighters");
sb.append(lf);
}
if (isImmuneToHappiness()) {
sb.append(dot);
sb.append(" No effects on happiness nor war fatigue");
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/openRealmOfStars/player/ship/Ship.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.openRealmOfStars.player.ship;
/*
* Open Realm of Stars game project
* Copyright (C) 2016-2023 Tuomo Untinen
* Copyright (C) 2016-2024 Tuomo Untinen
* Copyright (C) 2017 Lucas
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -1900,8 +1900,7 @@ && getHull().getHullType() != ShipHullType.STARBASE) {
* @return True if smuggler ship.
*/
public boolean isSmuggler() {
if (getHull().getRace().hasTrait(TraitIds.ARMED_FREIGHTERS)
&& getHull().getHullType() == ShipHullType.FREIGHTER) {
if (getHull().getHullType() == ShipHullType.FREIGHTER && hasWeapons()) {
return true;
}
return false;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/openRealmOfStars/player/ship/ShipHull.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ public void setFleetCapacity(final double fleetCapacity) {

@Override
public String toString() {
return getDescription(false);
}

/**
* Get hull description.
* @param allowArmedFreighter True if arms are allowed in freighters
* @return description as a string.
*/
public String getDescription(final boolean allowArmedFreighter) {
String hullDescription = IOUtilities.stringWrapper(
getHullType().getDescription(), LINE_LENGTH);
if (getHullType() == ShipHullType.PROBE
Expand All @@ -278,8 +287,7 @@ && getName().startsWith("Probe Mk")) {
"Probe, no weapons allowed. Faster regular and FTL speed.",
LINE_LENGTH);
}
if (originalBuilder.hasTrait(TraitIds.ARMED_FREIGHTERS)
&& getHullType() == ShipHullType.FREIGHTER) {
if (allowArmedFreighter && getHullType() == ShipHullType.FREIGHTER) {
hullDescription = IOUtilities.stringWrapper(
"Freighter, single weapon and privateer module allowed. Cargo ship",
LINE_LENGTH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.openRealmOfStars.player.ship.generator;
/*
* Open Realm of Stars game project
* Copyright (C) 2016-2023 Tuomo Untinen
* Copyright (C) 2016-2024 Tuomo Untinen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -1186,7 +1186,7 @@ public static ShipDesign createFreighter(final PlayerInfo player,
ShipComponent power = ShipComponentFactory.createByName(
player.getTechList().getBestEnergySource().getComponent());
result.addComponent(power);
if (player.getRace().hasTrait(TraitIds.ARMED_FREIGHTERS)
if (player.getGovernment().allowArmedFreighters()
&& result.getFreeSlots() > 2) {
ShipComponent weapon = ShipComponentFactory
.createByName(player.getTechList().getBestWeapon().getComponent());
Expand Down Expand Up @@ -1254,14 +1254,6 @@ public static ShipDesign createFreighter(final PlayerInfo player,
}
}
}
if (player.getRace().hasTrait(TraitIds.ARMED_FREIGHTERS)
&& result.getFreeSlots() > 3
&& (player.getTechList().hasTech("Privateer Mk1")
|| player.getTechList().hasTech("Privateer Mk2")
|| player.getTechList().hasTech("Privateer Mk3"))) {
result.addComponent(ShipComponentFactory.createByName(
"Privateer module"));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.openRealmOfStars.player.ship.shipdesign;
/*
* Open Realm of Stars game project
* Copyright (C) 2016-2021 Tuomo Untinen
* Copyright (C) 2016-2024 Tuomo Untinen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -837,9 +837,11 @@ public String getDesignInfo() {
* Get ship design flaws as a string.
* @param banNukes True if nuclear weapons are banned.
* @param banPrivateer true if privateer ships are banned
* @param allowArmedFreighters True if armed freighters are allowed.
* @return Design flaws in a string
*/
public String getFlaws(final boolean banNukes, final boolean banPrivateer) {
public String getFlaws(final boolean banNukes, final boolean banPrivateer,
final boolean allowArmedFreighters) {
boolean designOk = true;
boolean flawNoCargoSpace = false;
StringBuilder sb = new StringBuilder();
Expand All @@ -862,8 +864,7 @@ public String getFlaws(final boolean banNukes, final boolean banPrivateer) {
if (getFreeSlots() == 0 && hull.getHullType() == ShipHullType.FREIGHTER) {
flawNoCargoSpace = true;
}
if (hull.getRace().hasTrait(TraitIds.ARMED_FREIGHTERS)
&& hull.getHullType() == ShipHullType.FREIGHTER) {
if (allowArmedFreighters && hull.getHullType() == ShipHullType.FREIGHTER) {
if (countWeapons() > 1) {
designOk = false;
sb.append(ShipDesignConsts.SINGLE_WEAPON_ALLOWED)
Expand Down Expand Up @@ -1024,7 +1025,7 @@ public String getFlaws(final boolean banNukes, final boolean banPrivateer) {
* @return Design flaws in a string
*/
public String getFlaws() {
return getFlaws(false, false);
return getFlaws(false, false, false);
}

/**
Expand Down
33 changes: 0 additions & 33 deletions src/test/java/org/openRealmOfStars/ai/research/ResearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,39 +315,6 @@ public void testResearchHandlingExpansionist() {
info.getTechList().getTechFocus(TechType.Electrics));
}

@Test
@Category(org.openRealmOfStars.BehaviourTest.class)
public void testResearchHandlingBackstabbing() {
PlayerInfo info = new PlayerInfo(SpaceRace.TEUTHIDAES);
Leader leader = LeaderUtility.createLeader(info, null, 0);
leader.addPerk(Perk.CORRUPTED);
leader.addPerk(Perk.CONVICT);
leader.addPerk(Perk.CRUEL);
info.setRuler(leader);
Research.handle(info);
assertEquals(Research.FOCUS_FOR_LAB,
info.getTechList().getTechFocus(TechType.Improvements));
info.getTechList().addTech(TechFactory.createImprovementTech("Basic lab", 1));
Research.handle(info);
assertEquals(Research.HIGH_FOCUS_LEVEL,
info.getTechList().getTechFocus(TechType.Combat));
assertEquals(Research.DEFAULT_FOCUS_LEVEL,
info.getTechList().getTechFocus(TechType.Defense));
assertEquals(Research.DEFAULT_FOCUS_LEVEL,
info.getTechList().getTechFocus(TechType.Hulls));
assertEquals(Research.LOW_FOCUS_LEVEL,
info.getTechList().getTechFocus(TechType.Improvements));
assertEquals(Research.DEFAULT_FOCUS_LEVEL,
info.getTechList().getTechFocus(TechType.Propulsion));
assertEquals(Research.HIGH_FOCUS_LEVEL,
info.getTechList().getTechFocus(TechType.Electrics));
int amount = info.getShipStatList().length;
info.getTechList().addTech(TechFactory.createHullTech("Probe", 2));
info.getTechList().addTech(TechFactory.createElectronicsTech("Espionage module Mk1", 2));
Research.handleShipDesigns(info);
assertEquals(amount + 2, info.getShipStatList().length);
}

@Test
@Category(org.openRealmOfStars.BehaviourTest.class)
public void testResearchHandlingPeaceful() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public void testDescriptions() {
+ "* Leader cost: 10\n"
+ "* Reign length: life time\n"
+ "* Possibility to internal power struggle\n"
+ "* Armed freighters\n"
+ "* Generic happiness: -1\n"
+ "* Diplomatic bonus: -1\n"
+ "* War resistance: 1\n"
Expand Down Expand Up @@ -244,12 +245,14 @@ public void testDescriptions() {
+ "* Leader capacity: 6\n"
+ "* Leader cost: 5\n"
+ "* Reign length: 100 star years\n"
+ "* Armed freighters\n"
+ "* No effects on happiness nor war fatigue\n";
expected[22] = "### Syndicate\n"
+ "* Base fleet capacity: 3\n"
+ "* Leader capacity: 12\n"
+ "* Leader cost: 12\n"
+ "* Reign length: life time\n"
+ "* Armed freighters\n"
+ "* Trade bonus: 2\n"
+ "* War resistance: 1\n"
+ "* War happiness\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ public void testPrivateerDesign() {
assertEquals(true, tmp.contains(ShipDesignConsts.PRIVATEER_MODULE_MISSING));
design.addComponent(module);
assertEquals(true,ShipDesignConsts.DESIGN_OK.equals(design.getFlaws()));
assertEquals(false,ShipDesignConsts.DESIGN_OK.equals(design.getFlaws(false, true)));
assertEquals(false,ShipDesignConsts.DESIGN_OK.equals(design.getFlaws(false,
true, false)));
}

@Test
Expand Down Expand Up @@ -294,7 +295,8 @@ public void testNuclearBomberShipDesign() {
assertEquals(true,ShipDesignConsts.DESIGN_OK.equals(design.getFlaws()));
assertEquals(true, design.isBomberShip());
assertEquals(false, design.isTrooperShip());
assertEquals(false,ShipDesignConsts.DESIGN_OK.equals(design.getFlaws(true, false)));
assertEquals(false,ShipDesignConsts.DESIGN_OK.equals(design.getFlaws(true,
false, false)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testShipHullSmaugirians() {
+ "Slots:6 Hull:6\n"
+ "Size:Medium Fleet capacity: 0.1\n"
+ "Freighter, single weapon and privateer\n"
+ "module allowed. Cargo ship", hull.toString());
+ "module allowed. Cargo ship", hull.getDescription(true));
}

}

0 comments on commit a1c6385

Please sign in to comment.