-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.as
212 lines (185 loc) · 7.73 KB
/
rule.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
class rule {
// rule is something, that can be owned by a player, which shows
// a permanent or temporary availabilities of doing something
// each rule, which can be used should return the folowwing
// -1 -- REJECT action, it's regulating
// 1 -- accept
// 0 -- do not regulates
static function createRule(
playerObject:Object, // player, which has this rule
description:String, // description of rule, like 'can play zombies from graveyard this turn'
ruleType:Number, // key code number, which shows the rule meaning
ruleLong:Number, // keycode, shows when keycode ends
parameters, // other params of rule are number/array
source:Object // something, that gives you this rule
):Object // return rule object
{
// a special object for default rule's source
if (gameLogick._name == undefined) gameLogick._name = "Game Logick";
var res = new Object();
res.description = description;
res.host = playerObject;
res.ruleType = ruleType;
res.ruleLong = ruleLong;
if (source != undefined) res.source = source; else res.source = gameLogick;
// parameters special
res.parameters = consts.makeNumberArray(parameters);
return res;
}
static function ruleToString(rO:Object):String{
var isP = (rO.ruleLong == permanent);
var isPs = timeToString(rO.ruleLong) + " ";
return rO.host._name + " " + ((isP)? isPs : "") + rO.description + ((!isP)? isPs : " ") + "caused by " + rO.source._name;
}
static var gameLogick = new Object();
static var permanent = 50;
static var untilTurnEnd = 100;
static function timeToString(tim:Number):String{
switch (tim){
case permanent: return "permanently";
case untilTurnEnd: return "until turn end";
default: break;
}
return "?time?" + tim;
}
// acceptance
static var playerCanLandDropFromX = 1000;
static var playerCanCastAnySpellFromX = 1001;
static var playerCanPlayCardsDuringHisTurnAtPhaseX = 1002;
static var playerCanAlwaysCastSpellTypeX = 1003;
static var playerCanAlwaysCastSpellWithAbilityX = 1004;
static var playerCannotPlayNonLandCards = -1000;
static var castRulesFrom = 1000;
static var castRuleTo = 1100;
// . . . . .
// rO - is a rule
static function RuleApply (rO:Object,p2,p3,p4,p5){
var cardObj = p2;
if (rO.ruleType == playerCanLandDropFromX){
var isLand = (card.isType(cardObj, typ.Land));
if (!isLand) return 0;
if (cardObj.isin == rO.parameters[0]) // if it is this direct place
return 1;
return 0; // this rule can not reject
};
if (rO.ruleType == playerCanCastAnySpellFromX){
var isLand = (card.isType(cardObj, typ.Land));
if (isLand) return 0;
if (cardObj.isin == rO.parameters[0]) // if it is this direct place
return 1;
return 0;
}
// 1002
if (rO.ruleType == playerCanPlayCardsDuringHisTurnAtPhaseX){
var isMyTurn = (rO.host.game.currentTurnPlayerIndex == rO.host.PID);
var phase = rO.host.game.phase;
var isNeedPhase = (phase == rO.parameters[0]);
return 1 * (isMyTurn & isNeedPhase);
}
//1003
if (rO.ruleType == playerCanAlwaysCastSpellTypeX){
return 1 * card.isType(cardObj, rO.parameters[0]);
}
//1004
if (rO.ruleType == playerCanAlwaysCastSpellWithAbilityX){
return 1 * abilities.has(cardObj, rO.parameters[0]);
}
//-1000
if (rO.ruleType == playerCannotPlayNonLandCards){
return -1 * !(card.isType(cardObj, typ.Land));
}
return 0;
}
static function r1(rO:Object, cardObj:Object):Number { // for lands
var isLand = (card.isType(cardObj, typ.Land));
if (!isLand) return 0;
if (cardObj.isin == rO.parameters[0]) // if it is this direct place
return 1;
return 0; // this rule can not reject
}
static function defaultPlayerPlayCardRules(playerHost:Object):Array{
var res = new Array();
res.push(createRule(playerHost, "can play lands from hand", playerCanLandDropFromX, permanent, places.hand));
res.push(createRule(playerHost, "can cast spells from hand", playerCanCastAnySpellFromX, permanent, places.hand));
res.push(createRule(playerHost, "can play cards from hand during his main phase", playerCanPlayCardsDuringHisTurnAtPhaseX, permanent, gameengine.main));
res.push(createRule(playerHost, "can play cards from hand during his second main phase", playerCanPlayCardsDuringHisTurnAtPhaseX, permanent, gameengine.secondMain));
res.push(createRule(playerHost, "can cast instants any time", playerCanAlwaysCastSpellTypeX, permanent, typ.Instant));
res.push(createRule(playerHost, "can cast spells with flash any time he can cast instant", playerCanAlwaysCastSpellWithAbilityX, permanent, abilities.flash));
var heh = new Object();
heh._name = "Stupid author";
//res.push(createRule(playerHost, "can not play non-land cards", playerCannotPlayNonLandCards, permanent, new Array(), heh));
return res;
}
// solving function, that uses playing card (1000..1100) rules
static function addCanPlayResolveFunction(playerHost:Object):Void{
playerHost.canCast = function(cardObject:Object):Boolean{
// . . .
var acceptance = new Array();
var finalScore = 0;
for (var i = 0; i < this.cardPlayingRules.length; ++i){
var curRule = this.cardPlayingRules[i];
if (Math.abs(curRule.ruleType) < castRulesFrom || Math.abs(curRule.ruleType) > castRuleTo){
//consts.LOG("X Error: rule has no cast checking function! "+ curRule.description);
continue;
}
var ruleRes = RuleApply(curRule, cardObject);
//consts.LOG(ruleRes + " " + curRule.description);
if (ruleRes == undefined)
continue;
if (ruleRes < 0){
consts.LOG(cardObject._name + " is rejected by rule:");
consts.LOG(ruleToString(curRule));
return false;
}
finalScore += ruleRes;
if (ruleRes > 0)acceptance.push(ruleToString(curRule));
}
if (finalScore > 0){
consts.LOG(cardObject._name + " is accepted by rule(s):");
for (var i = 0; i < acceptance.length; ++i)
consts.LOG((i+1) + ". " + (acceptance[i]));
return true;
}
consts.LOG(cardObject._name + " card playing is not refulating by any of existed rules. So, it can not be played.");
return false;
}
}
/*
newPlayer.canCastSpellFilters = defaultcanCastFilter();
///if at least one rule resolve, than you can cast it
newPlayer.canCast = function (cardObj:Object):Boolean {
for (var i = 0; i < this.canCastSpellFilters.length; ++i)
if (this.canCastSpellFilters[i](cardObj, this) == false){
consts.LOG(cardObj._name + " casting is rejected");
return false;
}
consts.LOG(cardObj._name + " casting is available");
return true; // no way to cast!
}
static function defaultcanCastFilter():Array{
var res = new Array(); // where from you can cast spells? by default
res.push( // cast ANY (-1) SPELL // from HAND
function (spell:Object, playerO:Object):Boolean {
var isMyTurn = (gameengine.game.currentTurnPlayerIndex == playerO.PID);
var phase = gameengine.game.phase;
var isMainPhase = (phase == gameengine.main || phase == gameengine.secondMain);
var hasFlash = abilities.has(spell, abilities.flash);
var isInstant = card.isType(spell, typ.Instant);
var isInHand = spell.isin == places.hand;
consts.LOG(spell._name + " is :");
consts.LOG(" your turn? " + isMyTurn);
consts.LOG(" main phase? " + isMainPhase);
consts.LOG(" is instant? " + isInstant);
consts.LOG(" has flash? " + hasFlash);
return (isInstant || hasFlash || (isMyTurn && isMainPhase));
}
);
// now you can not play creatures just because
res.push(function (spell:Object, playerO:Object):Boolean
{
return !(card.isType(spell, typ.Creature));
}
);
return res;
}*/
}