2013-08-18 02:21:02 -07:00
|
|
|
package magic.model.action;
|
|
|
|
|
2020-01-15 12:02:42 -08:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2013-08-18 02:21:02 -07:00
|
|
|
import magic.ai.ArtificialScoringSystem;
|
|
|
|
import magic.model.MagicGame;
|
|
|
|
import magic.model.MagicPermanent;
|
2014-03-03 03:49:42 -08:00
|
|
|
import magic.model.choice.MagicTargetChoice;
|
2013-08-18 02:21:02 -07:00
|
|
|
import magic.model.mstatic.MagicPermanentStatic;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unattaches attachable from currently attached creature.
|
|
|
|
* Attaches attachable to new creature when not MagicPermanent.NONE.
|
|
|
|
*/
|
2015-04-13 00:39:40 -07:00
|
|
|
public class AttachAction extends MagicAction {
|
2013-08-18 02:21:02 -07:00
|
|
|
|
|
|
|
private final MagicPermanent attachable;
|
|
|
|
private final MagicPermanent creature;
|
|
|
|
private MagicPermanent oldAttachedCreature = MagicPermanent.NONE;
|
|
|
|
private Collection<MagicPermanentStatic> oldStatics = Collections.emptyList();
|
|
|
|
private boolean validCreature;
|
|
|
|
|
2015-04-13 00:39:40 -07:00
|
|
|
public AttachAction(final MagicPermanent aAttachable,final MagicPermanent aCreature) {
|
2013-08-18 02:21:02 -07:00
|
|
|
attachable = aAttachable;
|
|
|
|
creature = aCreature;
|
|
|
|
}
|
2015-12-31 02:54:52 -08:00
|
|
|
|
2013-08-18 02:21:02 -07:00
|
|
|
private MagicPermanent getAttached() {
|
|
|
|
return attachable.isEquipment() ?
|
|
|
|
attachable.getEquippedCreature() :
|
2014-03-03 21:34:03 -08:00
|
|
|
attachable.getEnchantedPermanent();
|
2013-08-18 02:21:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void detach(final MagicPermanent old) {
|
|
|
|
if (attachable.isEquipment()) {
|
|
|
|
old.removeEquipment(attachable);
|
|
|
|
} else {
|
|
|
|
old.removeAura(attachable);
|
|
|
|
}
|
|
|
|
}
|
2015-12-31 02:54:52 -08:00
|
|
|
|
2013-08-18 02:21:02 -07:00
|
|
|
private void attach(final MagicPermanent perm) {
|
|
|
|
if (attachable.isEquipment()) {
|
|
|
|
perm.addEquipment(attachable);
|
|
|
|
attachable.setEquippedCreature(perm);
|
|
|
|
} else {
|
|
|
|
perm.addAura(attachable);
|
2014-03-03 21:34:03 -08:00
|
|
|
attachable.setEnchantedPermanent(perm);
|
2013-08-18 02:21:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-19 20:13:35 -07:00
|
|
|
public boolean isLegal(final MagicGame game) {
|
2018-04-07 17:59:14 -07:00
|
|
|
if (!attachable.isValid()) {
|
2017-08-19 20:13:35 -07:00
|
|
|
return false;
|
2013-08-18 02:21:02 -07:00
|
|
|
}
|
2014-03-03 03:49:42 -08:00
|
|
|
|
|
|
|
if (attachable.isEquipment() && creature.isValid() && !creature.isCreature()) {
|
2017-08-19 20:13:35 -07:00
|
|
|
return false;
|
2014-03-03 03:49:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (attachable.isAura() && creature.isValid()) {
|
|
|
|
final MagicTargetChoice tchoice = new MagicTargetChoice(attachable.getAuraTargetChoice(), false);
|
2018-04-07 17:59:14 -07:00
|
|
|
if (!game.isLegalTarget(attachable.getController(), attachable, tchoice, creature)) {
|
2017-08-19 20:13:35 -07:00
|
|
|
return false;
|
2014-03-03 03:49:42 -08:00
|
|
|
}
|
|
|
|
}
|
2017-08-19 20:13:35 -07:00
|
|
|
return true;
|
|
|
|
}
|
2014-03-03 03:49:42 -08:00
|
|
|
|
2017-08-19 20:13:35 -07:00
|
|
|
@Override
|
|
|
|
public void doAction(final MagicGame game) {
|
2013-08-18 02:21:02 -07:00
|
|
|
int score = ArtificialScoringSystem.getTurnScore(game);
|
|
|
|
|
|
|
|
oldAttachedCreature = getAttached();
|
|
|
|
if (oldAttachedCreature.isValid()) {
|
|
|
|
score-=oldAttachedCreature.getScore();
|
|
|
|
detach(oldAttachedCreature);
|
|
|
|
score+=oldAttachedCreature.getScore();
|
|
|
|
if (oldAttachedCreature.isFriend(attachable)) {
|
|
|
|
// Prevent unnecessary equips.
|
|
|
|
if (oldAttachedCreature == creature) {
|
|
|
|
score += ArtificialScoringSystem.UNNECESSARY_EQUIP_SCORE;
|
|
|
|
} else {
|
|
|
|
score += ArtificialScoringSystem.UNEQUIP_SCORE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
score = -score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validCreature = creature.isValid();
|
|
|
|
if (validCreature) {
|
|
|
|
score-=creature.getScore();
|
|
|
|
|
|
|
|
attach(creature);
|
|
|
|
|
|
|
|
//update the timestamp of the attachable's effects
|
2014-02-02 17:13:38 -08:00
|
|
|
oldStatics = game.removeSelfStatics(attachable);
|
2013-08-18 02:21:02 -07:00
|
|
|
game.addStatics(attachable);
|
|
|
|
|
|
|
|
score+=creature.getScore();
|
|
|
|
} else {
|
|
|
|
attach(MagicPermanent.NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
setScore(attachable.getController(),score);
|
|
|
|
game.setStateCheckRequired();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void undoAction(final MagicGame game) {
|
|
|
|
if (validCreature) {
|
|
|
|
detach(creature);
|
2014-02-02 17:13:38 -08:00
|
|
|
game.removeSelfStatics(attachable);
|
2013-08-18 02:21:02 -07:00
|
|
|
game.addStatics(oldStatics);
|
|
|
|
}
|
|
|
|
|
|
|
|
attach(oldAttachedCreature);
|
|
|
|
}
|
|
|
|
}
|