use of games.strategy.engine.data.RelationshipType in project triplea by triplea-game.
the class TriggerAttachment method triggerRelationshipChange.
public static void triggerRelationshipChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
final GameData data = bridge.getData();
Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, relationshipChangeMatch());
if (testWhen) {
trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
}
if (testUses) {
trigs = CollectionUtils.getMatches(trigs, availableUses);
}
final CompositeChange change = new CompositeChange();
for (final TriggerAttachment t : trigs) {
if (testChance && !t.testChance(bridge)) {
continue;
}
if (useUses) {
t.use(bridge);
}
for (final String relationshipChange : t.getRelationshipChange()) {
final String[] s = relationshipChange.split(":");
final PlayerID player1 = data.getPlayerList().getPlayerId(s[0]);
final PlayerID player2 = data.getPlayerList().getPlayerId(s[1]);
final RelationshipType currentRelation = data.getRelationshipTracker().getRelationshipType(player1, player2);
if (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_NEUTRAL) && Matches.relationshipTypeIsNeutral().test(currentRelation)) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_ALLIED) && Matches.relationshipTypeIsAllied().test(currentRelation)) || (s[2].equals(Constants.RELATIONSHIP_CONDITION_ANY_WAR) && Matches.relationshipTypeIsAtWar().test(currentRelation)) || currentRelation.equals(data.getRelationshipTypeList().getRelationshipType(s[2]))) {
final RelationshipType triggerNewRelation = data.getRelationshipTypeList().getRelationshipType(s[3]);
change.add(ChangeFactory.relationshipChange(player1, player2, currentRelation, triggerNewRelation));
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Changing Relationship for " + player1.getName() + " and " + player2.getName() + " from " + currentRelation.getName() + " to " + triggerNewRelation.getName());
AbstractMoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(player1, player2, currentRelation, triggerNewRelation);
/*
* creation of new battles is handled at the beginning of the battle delegate, in
* "setupUnitsInSameTerritoryBattles", not here.
* if (Matches.relationshipTypeIsAtWar().test(triggerNewRelation))
* triggerMustFightBattle(player1, player2, aBridge);
*/
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
}
use of games.strategy.engine.data.RelationshipType in project triplea by triplea-game.
the class TriggerAttachment method triggerRelationshipTypePropertyChange.
public static void triggerRelationshipTypePropertyChange(final Set<TriggerAttachment> satisfiedTriggers, final IDelegateBridge bridge, final String beforeOrAfter, final String stepName, final boolean useUses, final boolean testUses, final boolean testChance, final boolean testWhen) {
Collection<TriggerAttachment> trigs = CollectionUtils.getMatches(satisfiedTriggers, relationshipTypePropertyMatch());
if (testWhen) {
trigs = CollectionUtils.getMatches(trigs, whenOrDefaultMatch(beforeOrAfter, stepName));
}
if (testUses) {
trigs = CollectionUtils.getMatches(trigs, availableUses);
}
final CompositeChange change = new CompositeChange();
for (final TriggerAttachment t : trigs) {
if (testChance && !t.testChance(bridge)) {
continue;
}
if (useUses) {
t.use(bridge);
}
for (final Tuple<String, String> property : t.getRelationshipTypeProperty()) {
for (final RelationshipType relationshipType : t.getRelationshipTypes()) {
String newValue = property.getSecond();
boolean clearFirst = false;
// test if we are resetting the variable first, and if so, remove the leading "-reset-" or "-clear-"
if (newValue.length() > 0 && (newValue.startsWith(PREFIX_CLEAR) || newValue.startsWith(PREFIX_RESET))) {
newValue = newValue.replaceFirst(PREFIX_CLEAR, "").replaceFirst(PREFIX_RESET, "");
clearFirst = true;
}
// covers RelationshipTypeAttachment
if (t.getRelationshipTypeAttachmentName().getFirst().equals("RelationshipTypeAttachment")) {
final RelationshipTypeAttachment attachment = RelationshipTypeAttachment.get(relationshipType, t.getRelationshipTypeAttachmentName().getSecond());
if (newValue.equals(attachment.getRawPropertyString(property.getFirst()))) {
continue;
}
if (clearFirst && newValue.length() < 1) {
change.add(ChangeFactory.attachmentPropertyReset(attachment, property.getFirst()));
} else {
change.add(ChangeFactory.attachmentPropertyChange(attachment, newValue, property.getFirst(), clearFirst));
}
bridge.getHistoryWriter().startEvent(MyFormatter.attachmentNameToText(t.getName()) + ": Setting " + property.getFirst() + (newValue.length() > 0 ? " to " + newValue : " cleared ") + " for " + t.getRelationshipTypeAttachmentName().getSecond() + " attached to " + relationshipType.getName());
}
// TODO add other attachment changes here if they attach to a territory
}
}
}
if (!change.isEmpty()) {
bridge.addChange(change);
}
}
use of games.strategy.engine.data.RelationshipType in project triplea by triplea-game.
the class PoliticsDelegate method chainAlliancesTogether.
static void chainAlliancesTogether(final IDelegateBridge bridge) {
final GameData data = bridge.getData();
if (!Properties.getAlliancesCanChainTogether(data)) {
return;
}
final Collection<RelationshipType> allTypes = data.getRelationshipTypeList().getAllRelationshipTypes();
RelationshipType alliedType = null;
RelationshipType warType = null;
for (final RelationshipType type : allTypes) {
if (type.getRelationshipTypeAttachment().isDefaultWarPosition()) {
warType = type;
} else if (type.getRelationshipTypeAttachment().canAlliancesChainTogether()) {
alliedType = type;
}
}
if (alliedType == null) {
return;
}
// first do alliances. then, do war (since we don't want to declare war on a potential ally).
final Collection<PlayerID> players = data.getPlayerList().getPlayers();
for (final PlayerID p1 : players) {
final HashSet<PlayerID> p1NewAllies = new HashSet<>();
final Collection<PlayerID> p1AlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p1, data));
for (final PlayerID p2 : p1AlliedWith) {
p1NewAllies.addAll(CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p2, data)));
}
p1NewAllies.removeAll(p1AlliedWith);
p1NewAllies.remove(p1);
for (final PlayerID p3 : p1NewAllies) {
if (!data.getRelationshipTracker().getRelationshipType(p1, p3).equals(alliedType)) {
final RelationshipType current = data.getRelationshipTracker().getRelationshipType(p1, p3);
bridge.addChange(ChangeFactory.relationshipChange(p1, p3, current, alliedType));
bridge.getHistoryWriter().addChildToEvent(p1.getName() + " and " + p3.getName() + " are joined together in an " + alliedType.getName() + " treaty");
MoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(p1, p3, current, alliedType);
}
}
}
// now war
if (warType == null) {
return;
}
for (final PlayerID p1 : players) {
final HashSet<PlayerID> p1NewWar = new HashSet<>();
final Collection<PlayerID> p1WarWith = CollectionUtils.getMatches(players, Matches.isAtWar(p1, data));
final Collection<PlayerID> p1AlliedWith = CollectionUtils.getMatches(players, Matches.isAlliedAndAlliancesCanChainTogether(p1, data));
for (final PlayerID p2 : p1AlliedWith) {
p1NewWar.addAll(CollectionUtils.getMatches(players, Matches.isAtWar(p2, data)));
}
p1NewWar.removeAll(p1WarWith);
p1NewWar.remove(p1);
for (final PlayerID p3 : p1NewWar) {
if (!data.getRelationshipTracker().getRelationshipType(p1, p3).equals(warType)) {
final RelationshipType current = data.getRelationshipTracker().getRelationshipType(p1, p3);
bridge.addChange(ChangeFactory.relationshipChange(p1, p3, current, warType));
bridge.getHistoryWriter().addChildToEvent(p1.getName() + " and " + p3.getName() + " declare " + warType.getName() + " on each other");
MoveDelegate.getBattleTracker(data).addRelationshipChangesThisTurn(p1, p3, current, warType);
}
}
}
}
use of games.strategy.engine.data.RelationshipType in project triplea by triplea-game.
the class EditDelegate method changePoliticalRelationships.
@Override
public String changePoliticalRelationships(final Collection<Triple<PlayerID, PlayerID, RelationshipType>> relationshipChanges) {
if (relationshipChanges == null || relationshipChanges.isEmpty()) {
return null;
}
String result = checkEditMode();
if (result != null) {
return result;
}
result = EditValidator.validateChangePoliticalRelationships(relationshipChanges);
if (result != null) {
return result;
}
final BattleTracker battleTracker = AbstractMoveDelegate.getBattleTracker(getData());
for (final Triple<PlayerID, PlayerID, RelationshipType> relationshipChange : relationshipChanges) {
final RelationshipType currentRelation = getData().getRelationshipTracker().getRelationshipType(relationshipChange.getFirst(), relationshipChange.getSecond());
if (!currentRelation.equals(relationshipChange.getThird())) {
logEvent("Editing Political Relationship for " + relationshipChange.getFirst().getName() + " and " + relationshipChange.getSecond().getName() + " from " + currentRelation.getName() + " to " + relationshipChange.getThird().getName(), null);
bridge.addChange(ChangeFactory.relationshipChange(relationshipChange.getFirst(), relationshipChange.getSecond(), currentRelation, relationshipChange.getThird()));
battleTracker.addRelationshipChangesThisTurn(relationshipChange.getFirst(), relationshipChange.getSecond(), currentRelation, relationshipChange.getThird());
}
}
return null;
}
use of games.strategy.engine.data.RelationshipType in project triplea by triplea-game.
the class GameDataExporter method relationshipTypeList.
private void relationshipTypeList(final GameData data) {
final Collection<RelationshipType> types = data.getRelationshipTypeList().getAllRelationshipTypes();
if (types.size() <= 4) {
return;
}
xmlfile.append("\n");
xmlfile.append(" <relationshipTypes>\n");
for (final RelationshipType current : types) {
final String name = current.getName();
if (name.equals(Constants.RELATIONSHIP_TYPE_SELF) || name.equals(Constants.RELATIONSHIP_TYPE_NULL) || name.equals(Constants.RELATIONSHIP_TYPE_DEFAULT_WAR) || name.equals(Constants.RELATIONSHIP_TYPE_DEFAULT_ALLIED)) {
continue;
}
xmlfile.append(" <relationshipType name=\"").append(name).append("\"/>\n");
}
xmlfile.append(" </relationshipTypes>\n");
}
Aggregations