Files
wagic/projects/mtg/include/ExtraCost.h
wrenczes@gmail.com 356ce1b8ba Giant refactor of all the ExtraCost variants. Reduced the .cpp code by literally half (~350 lines!)
Some of the things refactored to the base class:
- isPaymentSet()
- canPay()
- Render()
- setSource()
- setPayment()
- the target instance
- duplicated constructor initialization crap

I'm not interested in pointing fingers as to how the code got this way, but I'll ask that everyone who's altered this code in one fashion or another to carefully diff this change and understand what I refactored.  This code is a poster child for neglect & what happens when people start blindly copy & pasting code instead of paying attention to the commonality between code paths.  If you see the same lines of code happening over & over, and you're about to make yet another copy, please stop yourself & think about how you can refactor the code to be in a single shared function (or, more explicitly, a shared base member function when possible).

If you don't understand what I mean by a shared base member function, then I would suggest at the very least reading up on this topic:

http://www.cplusplus.com/doc/tutorial/inheritance/

(And, after all that, if you're still unsure how to proceed, ping someone else for advice!)
2010-10-14 04:45:10 +00:00

134 lines
3.1 KiB
C++

#ifndef _EXTRACOST_H_
#define _EXTRACOST_H_
#include <vector>
#include "Counters.h"
using std::vector;
class TargetChooser;
class MTGCardInstance;
class MTGAbility;
class ExtraCost{
public:
TargetChooser * tc;
MTGCardInstance * source;
MTGCardInstance * target;
std::string mCostRenderString;
ExtraCost(const std::string& inCostRenderString, TargetChooser *_tc = NULL);
virtual ~ExtraCost();
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet() { return (target != NULL); }
virtual int canPay() { return 1; }
virtual int doPay() = 0;
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual ExtraCost* clone() const = 0;
};
class ExtraCosts{
public:
vector<ExtraCost *>costs;
MTGCardInstance * source;
MTGAbility * action;
ExtraCosts();
~ExtraCosts();
void Render();
int tryToSetPayment(MTGCardInstance * card);
int isPaymentSet();
int canPay();
int doPay();
int reset();
int setAction(MTGAbility * _action, MTGCardInstance * _source);
void Dump();
ExtraCosts * clone() const;
};
class SacrificeCost: public ExtraCost{
public:
SacrificeCost(TargetChooser *_tc = NULL);
virtual int doPay();
virtual SacrificeCost * clone() const;
};
//life cost
class LifeCost: public ExtraCost{
public:
LifeCost(TargetChooser *_tc = NULL);
virtual int doPay();
virtual LifeCost * clone() const;
};
//Discard a random card cost
class DiscardRandomCost: public ExtraCost{
public:
DiscardRandomCost(TargetChooser *_tc = NULL);
virtual int canPay();
virtual int doPay();
virtual DiscardRandomCost * clone() const;
};
//tolibrary cost
class ToLibraryCost: public ExtraCost{
public:
ToLibraryCost(TargetChooser *_tc = NULL);
virtual int doPay();
virtual ToLibraryCost * clone() const;
};
//Millyourself cost
class MillCost: public ExtraCost{
public:
MillCost(TargetChooser *_tc = NULL);
virtual int canPay();
virtual int doPay();
virtual MillCost * clone() const;
};
//Mill to exile yourself cost
class MillExileCost: public MillCost{
public:
MillExileCost(TargetChooser *_tc = NULL);
};
//tap other cost
class TapTargetCost: public ExtraCost{
public:
TapTargetCost(TargetChooser *_tc = NULL);
virtual int doPay();
virtual TapTargetCost * clone() const;
};
//exile as cost
class ExileTargetCost: public ExtraCost{
public:
ExileTargetCost(TargetChooser *_tc = NULL);
virtual int doPay();
virtual ExileTargetCost * clone() const;
};
//bounce cost
class BounceTargetCost: public ExtraCost{
public:
BounceTargetCost(TargetChooser *_tc = NULL);
virtual int doPay();
virtual BounceTargetCost * clone() const;
};
class CounterCost: public ExtraCost{
public:
Counter * counter;
int hasCounters;
CounterCost(Counter * _counter,TargetChooser *_tc = NULL);
~CounterCost();
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual CounterCost * clone() const;
};
#endif