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!)
This commit is contained in:
wrenczes@gmail.com
2010-10-14 04:45:10 +00:00
parent 25cb9dde54
commit 356ce1b8ba
2 changed files with 79 additions and 475 deletions

View File

@@ -13,13 +13,16 @@ class ExtraCost{
public:
TargetChooser * tc;
MTGCardInstance * source;
ExtraCost(TargetChooser *_tc = NULL);
MTGCardInstance * target;
std::string mCostRenderString;
ExtraCost(const std::string& inCostRenderString, TargetChooser *_tc = NULL);
virtual ~ExtraCost();
virtual int setPayment(MTGCardInstance * card) = 0;
virtual int isPaymentSet() = 0;
virtual int canPay() = 0;
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet() { return (target != NULL); }
virtual int canPay() { return 1; }
virtual int doPay() = 0;
virtual void Render(){};
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual ExtraCost* clone() const = 0;
};
@@ -44,126 +47,79 @@ public:
class SacrificeCost: public ExtraCost{
public:
MTGCardInstance * target;
SacrificeCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual SacrificeCost * clone() const;
};
//life cost
class LifeCost: public ExtraCost{
public:
MTGCardInstance * target;
LifeCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual LifeCost * clone() const;
};
//Discard a random card cost
class DiscardRandomCost: public ExtraCost{
public:
MTGCardInstance * target;
DiscardRandomCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual DiscardRandomCost * clone() const;
};
//tolibrary cost
class ToLibraryCost: public ExtraCost{
public:
MTGCardInstance * target;
ToLibraryCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual ToLibraryCost * clone() const;
};
//Millyourself cost
class MillCost: public ExtraCost{
public:
MTGCardInstance * target;
MillCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual MillCost * clone() const;
};
//Mill to exile yourself cost
class MillExileCost: public ExtraCost{
class MillExileCost: public MillCost{
public:
MTGCardInstance * target;
MillExileCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual MillExileCost * clone() const;
MillExileCost(TargetChooser *_tc = NULL);
};
//tap other cost
class TapTargetCost: public ExtraCost{
public:
MTGCardInstance * target;
TapTargetCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual TapTargetCost * clone() const;
};
//exile as cost
class ExileTargetCost: public ExtraCost{
public:
MTGCardInstance * target;
ExileTargetCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual ExileTargetCost * clone() const;
};
//bounce cost
class BounceTargetCost: public ExtraCost{
public:
MTGCardInstance * target;
BounceTargetCost(TargetChooser *_tc = NULL);
virtual int setPayment(MTGCardInstance * card);
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual BounceTargetCost * clone() const;
};
class CounterCost: public ExtraCost{
public:
Counter * counter;
MTGCardInstance * target;
int hasCounters;
CounterCost(Counter * _counter,TargetChooser *_tc = NULL);
~CounterCost();
@@ -171,8 +127,6 @@ public:
virtual int isPaymentSet();
virtual int canPay();
virtual int doPay();
virtual void Render();
virtual int setSource(MTGCardInstance * _source);
virtual CounterCost * clone() const;
};