Files
wagic/projects/mtg/src/ActionLayer.cpp
omegablast2002@yahoo.com 6399917d25 changes:
added abilities:
proliferate
ProliferateChooser:new targetchooser for cards with counter and poison counters "proliferation".

MenuAbility:new internal ability to create custom menus of abilities which can be activated in sequence one after another.

multikicker, syntax kicker=multi{b}
works with variable word "kicked", the amount of times it was kicked.

target=<number>tc,target=<upto:>tc,target=<anyamount>tc,target(<number>tc),target(<upto:>tc),target(<anynumber>tc);
multitarget is now supported with the exception of "devided any way you choose" which can not be supported becuase we allow detoggling of targeted cards with a "second" click....so you can not click the same card 2 times to add it to the targets list twice for example.
this is minor, as the bulk of multitarget is not "devided"
removed 's' parsing for multitarget, added a limit of 1000 to "unlimited" for easier handling; we currently can't handle activation of an ability on a 1000 cards very well on any platform(infact i don't suggest it)

Countershroud(counterstring), this MTGAbility allows you to denote that a card can not have counters of the type "counterstring" put on it.
"any" is for no counters allowed at all. this is a replacement effect. cards state that they can still be the targets of counter effects, however on resolve nothing is placed on them instead.

@counteradded(counterstring) from(target):,@counterremoved(counterstring) from(target):: these are triggers for cards which state "whenever you add a counter of "counterstring" to "target"; added counterEvents struct; 

other changes:
added support for ai handling of multitargeted spells.

changed a few of delete( into SAFE_DELETE(, safed up a couple areas where they did not seem safe to me;

added better handling of menus presented to ai, it will try to select the best based on eff returns.

added varible lastactioncontroller for ai use, it keeps it truely from ever tripping over itself and brings ai more inline with MTG rules.

converted TC into a protected member.
added "abilitybelongsto" string to tc, and set "owner" of the tc. a tc should never belong to "no one" it should always have a owner.
abilitybelongs to string is solely for easier debugging, i found it was a pain to never know what ability created a tc while i coded multitarget. the owner of the tc is the only one that should be using it, if an ability needs to declare the opponent as the owner (choose discard which is currently unsupported for example) this will allow us to better handle that situation by setting the tc owner in the ability which called it.

rewrote the logic of "checkonly" in ai choose targets, the only time it is "checkonly" is when it is trying to see if it had a target for a spell before it cast it, i now set this in the actual function call instead, the old method was far to error prone.

wrote logic for ai checking of menu objects presented to it,
ai will now make better choices when a menu is presented to it based on what it already knows. this changes it from it's old method of "just click the first option".

taught ai how to use multi-mana producers such as birds and duel lands by adding a method for it to find it's mana for a payment. it can effectively use cards like birds of paradise and sol ring(without locking up). It's primary method of pMana searching was maintain for performance(no need to deep search if we have it in pMana).

added a vector to actionlayer to store mana abilities for pMana. this provides us with a dramatic improvement when mana lords are present by reducing the amount of objects that need checking when ai checks pMana.
with 80 mana objects and a ton of lords one instance i checked went from 8000ish checks down to 80<===big difference.

added "tapped" green coloring(sorry i missed that!)...added red coloring to current actionLayers current action card (usually the source).

changed "type(" restrictions second amount from atoi into wparsedint for more flexiable coding.

add "&" parsing to CD targetchooser, removed "iscolorandcolor" variables and functions becuase they were a hack the real fix was this.
cretaure[dragon&black&blue] a creature that is a dragon, and black and also blue.

changed some of the ai computeactions and
removed unneeded gaurds in ai chooseblockers, they did more harm then good.
2011-09-01 20:03:26 +00:00

483 lines
13 KiB
C++

#include "PrecompiledHeader.h"
#include "ActionLayer.h"
#include "GameObserver.h"
#include "Targetable.h"
#include "WEvent.h"
#include "AllAbilities.h"
MTGAbility* ActionLayer::getAbility(int type)
{
for (size_t i = 1; i < mObjects.size(); i++)
{
MTGAbility * a = ((MTGAbility *) mObjects[i]);
if (a->aType == type)
{
return a;
}
}
return NULL;
}
int ActionLayer::removeFromGame(ActionElement * e)
{
mReactions.erase(e);
int i = getIndexOf(e);
if (i == -1)
return 0;
if (isWaitingForAnswer() == e)
setCurrentWaitingAction(NULL);
assert(e);
e->destroy();
i = getIndexOf(e); //the destroy event might have changed the contents of mObjects, so we get the index again
if (i == -1)
return 0; //Should not happen, it means we deleted thesame object twice?
AManaProducer * manaObject = dynamic_cast<AManaProducer*>(e);
if(manaObject)
{
for (size_t i = 0; i < manaObjects.size(); i++)
if (manaObjects[i] == e)
{
manaObjects.erase(manaObjects.begin() + i);
}
}
mObjects.erase(mObjects.begin() + i);
return 1;
}
bool ActionLayer::moveToGarbage(ActionElement * e)
{
if (removeFromGame(e))
{
garbage.push_back(e);
return true;
}
return false;
}
void ActionLayer::cleanGarbage()
{
for (size_t i = 0; i < garbage.size(); ++i)
{
delete (garbage[i]);
}
garbage.clear();
}
int ActionLayer::reactToClick(ActionElement * ability, MTGCardInstance * card)
{
int result = ability->reactToClick(card);
if (result)
stuffHappened = 1;
return result;
}
int ActionLayer::reactToTargetClick(ActionElement* ability, Targetable * card)
{
int result = ability->reactToTargetClick(card);
if (result)
stuffHappened = 1;
return result;
}
bool ActionLayer::CheckUserInput(JButton key)
{
GameObserver * g = GameObserver::GetInstance();
if (g->mExtraPayment && key == JGE_BTN_SEC)
{
g->mExtraPayment = NULL;
return 1;
}
if (menuObject)
{
return false;
}
for (size_t i = 0; i < mObjects.size(); i++)
{
if (mObjects[i] != NULL)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
if (currentAction->CheckUserInput(key))
return true;
}
}
return false;
}
void ActionLayer::Update(float dt)
{
stuffHappened = 0;
if (menuObject)
{
abilitiesMenu->Update(dt);
return;
}
modal = 0;
GameObserver* game = GameObserver::GetInstance();
for (int i = (int)(mObjects.size()) - 1; i >= 0; i--)
{
//a dirty hack, there might be cases when the mObject array gets reshaped if an ability removes some of its children abilites
if ((int) mObjects.size() <= i)
{
i = (int) (mObjects.size()) - 1;
if (i<0)
break;
}
if (mObjects[i] != NULL)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
if (currentAction->testDestroy())
game->removeObserver(currentAction);
}
}
int newPhase = game->getCurrentGamePhase();
for (size_t i = 0; i < mObjects.size(); i++)
{
if (mObjects[i] != NULL)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
currentAction->newPhase = newPhase;
currentAction->Update(dt);
currentAction->currentPhase = newPhase;
}
}
if (cantCancel)
{
ActionElement * ae = isWaitingForAnswer();
int countTargets = 0;
int maxTargets = 0;
if(ae && ae->getActionTc())
{
if (!ae->getActionTc()->validTargetsExist())
{
cantCancel = 0;
cancelCurrentAction();
return;
}
countTargets = ae->getActionTc()->countValidTargets();
maxTargets = ae->getActionTc()->maxtargets;
if (countTargets < maxTargets)
{
/*
@movedto(this|mygraveyard) from(mybattlefield):moveto(mybattlefield)
target(<2>creature[elf]|opponentgraveyard)
and there were 3 in the grave, you have the valid amount needed, this function should not trigger
...however if you had only 1 in the grave, then the max targets is reset to the maximum you CAN
use this effect on...in line with "up to" wording found on the cards with such abilities.
without this, the game locks into a freeze state while you try to select the targets and dont have enough to
fill the maxtargets list.
*/
if(int(ae->getActionTc()->targets.size()) == countTargets-1)
ae->getActionTc()->done = true;
}
}
}
}
void ActionLayer::Render()
{
if (menuObject)
{
abilitiesMenu->Render();
return;
}
currentActionCard = NULL;
for (size_t i = 0; i < mObjects.size(); i++)
{
if (mObjects[i] != NULL)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
currentAction->Render();
}
}
}
void ActionLayer::setCurrentWaitingAction(ActionElement * ae)
{
assert(!ae || !currentWaitingAction);//this assert causes crashes when may abilities overlap each other on ai. this conidiation is preexsiting.
currentWaitingAction = ae;
if (!ae)
cantCancel = 0;
}
TargetChooser * ActionLayer::getCurrentTargetChooser()
{
if (currentWaitingAction && currentWaitingAction->waitingForAnswer)
return currentWaitingAction->getActionTc();
return NULL;
}
int ActionLayer::cancelCurrentAction()
{
ActionElement * ae = isWaitingForAnswer();
if (!ae)
return 0;
if (cantCancel && ae->getActionTc()->validTargetsExist())
return 0;
ae->waitingForAnswer = 0; //TODO MOVE THIS IN ActionElement
setCurrentWaitingAction(NULL);
return 1;
}
ActionElement * ActionLayer::isWaitingForAnswer()
{
if (currentWaitingAction && currentWaitingAction->waitingForAnswer)
return currentWaitingAction;
return NULL;
}
int ActionLayer::stillInUse(MTGCardInstance * card)
{
for (size_t i = 0; i < mObjects.size(); i++)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
if (currentAction->stillInUse(card))
return 1;
}
return 0;
}
int ActionLayer::receiveEventPlus(WEvent * event)
{
int result = 0;
for (size_t i = 0; i < mObjects.size(); i++)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
result += currentAction->receiveEvent(event);
}
return 0;
}
int ActionLayer::isReactingToTargetClick(Targetable * card)
{
int result = 0;
if (isWaitingForAnswer())
return -1;
for (size_t i = 0; i < mObjects.size(); i++)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
result += currentAction->isReactingToTargetClick(card);
}
return result;
}
int ActionLayer::reactToTargetClick(Targetable * card)
{
int result = 0;
ActionElement * ae = isWaitingForAnswer();
if (ae)
return reactToTargetClick(ae, card);
for (size_t i = 0; i < mObjects.size(); i++)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
result += currentAction->reactToTargetClick(card);
}
return result;
}
//TODO Simplify with only object !!!
int ActionLayer::isReactingToClick(MTGCardInstance * card)
{
int result = 0;
if (isWaitingForAnswer())
return -1;
for (size_t i = 0; i < mObjects.size(); i++)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
if (currentAction->isReactingToClick(card))
{
++result;
mReactions.insert(currentAction);
}
}
return result;
}
int ActionLayer::reactToClick(MTGCardInstance * card)
{
int result = 0;
ActionElement * ae = isWaitingForAnswer();
if (ae)
return reactToClick(ae, card);
std::set<ActionElement*>::const_iterator iter = mReactions.begin();
std::set<ActionElement*>::const_iterator end = mReactions.end();
for (; iter !=end; ++iter)
{
result += reactToClick(*iter, card);
if (result)
break;
}
#ifdef WIN32
// if we hit this, then something strange has happened with the click logic - reactToClick()
// should never be called if isReactingToClick() previously didn't have an object return true
assert(!mReactions.empty());
#endif
mReactions.clear();
return result;
}
void ActionLayer::setMenuObject(Targetable * object, bool must)
{
if (!object)
{
DebugTrace("FATAL: ActionLayer::setMenuObject");
return;
}
menuObject = object;
SAFE_DELETE(abilitiesMenu);
abilitiesMenu = NEW SimpleMenu(10, this, Fonts::MAIN_FONT, 100, 100, object->getDisplayName().c_str());
currentActionCard = NULL;
for (size_t i = 0; i < mObjects.size(); i++)
{
ActionElement * currentAction = (ActionElement *) mObjects[i];
if (currentAction->isReactingToTargetClick(object))
{
abilitiesMenu->Add(i, currentAction->getMenuText());
}
}
if (!must)
abilitiesMenu->Add(kCancelMenuID, "Cancel");
else
cantCancel = 1;
modal = 1;
}
void ActionLayer::setCustomMenuObject(Targetable * object, bool must,vector<MTGAbility*>abilities)
{
if (!object)
{
DebugTrace("FATAL: ActionLayer::setCustomMenuObject");
return;
}
menuObject = object;
SAFE_DELETE(abilitiesMenu);
abilitiesMenu = NEW SimpleMenu(10, this, Fonts::MAIN_FONT, 100, 100, object->getDisplayName().c_str());
currentActionCard = NULL;
abilitiesMenu->isMultipleChoice = false;
if(abilities.size())
{
abilitiesMenu->isMultipleChoice = true;
ActionElement * currentAction = NULL;
for(int w = 0; w < int(abilities.size());w++)
{
currentAction = (ActionElement*)abilities[w];
currentActionCard = (MTGCardInstance*)abilities[0]->target;
abilitiesMenu->Add(mObjects.size()-1, currentAction->getMenuText(),"",false);
}
}
if (!must)
abilitiesMenu->Add(kCancelMenuID, "Cancel");
else
cantCancel = 1;
modal = 1;
}
void ActionLayer::doReactTo(int menuIndex)
{
if (menuObject)
{
int controlid = abilitiesMenu->mObjects[menuIndex]->GetId();
DebugTrace("ActionLayer::doReactTo " << controlid);
ButtonPressed(0, controlid);
}
}
void ActionLayer::doMultipleChoice(int choice)
{
if (menuObject)
{
DebugTrace("ActionLayer::doReactToChoice " << choice);
ButtonPressedOnMultipleChoice(choice);
}
}
void ActionLayer::ButtonPressed(int controllerid, int controlid)
{
if(this->abilitiesMenu && this->abilitiesMenu->isMultipleChoice)
{
return ButtonPressedOnMultipleChoice();
}
if (controlid >= 0 && controlid < static_cast<int>(mObjects.size()))
{
ActionElement * currentAction = (ActionElement *) mObjects[controlid];
currentAction->reactToTargetClick(menuObject);
menuObject = 0;
}
else if (controlid == kCancelMenuID)
{
GameObserver::GetInstance()->mLayers->stackLayer()->endOfInterruption();
menuObject = 0;
}
else
{
// fallthrough case. We have an id we don't recognize - do nothing, don't clear the menu!
//assert(false);
}
}
void ActionLayer::ButtonPressedOnMultipleChoice(int choice)
{
int currentMenuObject = -1;
for(int i = int(mObjects.size()-1);i > 0;i--)
{
//the currently displayed menu is not always the currently listenning action object
//find the menu which is displayed.
MenuAbility * ma = dynamic_cast<MenuAbility *>(mObjects[i]);//find the active menu
if(ma && ma->triggered)
{
currentMenuObject = i;
break;
}
}
if (currentMenuObject >= 0 && currentMenuObject < static_cast<int>(mObjects.size()))
{
ActionElement * currentAction = (ActionElement *) mObjects[currentMenuObject];
currentAction->reactToChoiceClick(menuObject,choice > -1?choice:this->abilitiesMenu->getmCurr(),currentMenuObject);
MenuAbility * ma = dynamic_cast<MenuAbility *>(mObjects[currentMenuObject]);
if(ma)
ma->removeMenu = true;//we clicked something, close menu now.
}
else if (currentMenuObject == kCancelMenuID)
{
GameObserver::GetInstance()->mLayers->stackLayer()->endOfInterruption();
}
menuObject = 0;
}
ActionLayer::ActionLayer()
{
menuObject = NULL;
abilitiesMenu = NULL;
stuffHappened = 0;
currentWaitingAction = NULL;
cantCancel = 0;
}
ActionLayer::~ActionLayer()
{
while(mObjects.size())
moveToGarbage((ActionElement *) mObjects[mObjects.size() - 1]);
SAFE_DELETE(abilitiesMenu);
cleanGarbage();
}