-
Notifications
You must be signed in to change notification settings - Fork 11
/
action.js
165 lines (137 loc) · 6.09 KB
/
action.js
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
/*
TM AI
Copyright (C) 2013-2014 by Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
//Action class and a few functions about the action enum types.
//A single action performed during a player's turn
//However, a turn exists out of an array of these actions: some parts, such as converting resources, do not count as
//a full player's turn. Also, the chaos magicions can have 2 more actions if there is their double action action.
// constructor
function Action(type) {
this.type = type;
// Coordinate for dig, build, ... e.g. [0,0]
this.co = null;
this.cos = []; //array of coordinates for multi-coordinate actions such as bridge
this.bontile = T_NONE; //passing tile
this.favtiles = []; //favor tiles taken. Can be multiple (for chaos magicians)
this.twtiles = []; //town tiles. Can be multiple (e.g. when taking 6 town size tile)
this.cult = C_NONE; //for cult track actions
this.color = N; //for color related actions
}
function makeActionWithXY(type, x, y) {
var result = new Action(type);
result.co = [x, y];
return result;
}
function makeActionWithCult(type, cult) {
var result = new Action(type);
result.cult = cult;
return result;
}
//Whether it's an action that you can do once per turn. If false, it's an auxiliary action such as burn/convert.
//Even though they can be combined, every spade, transform and build action is also a turn action. Further code is needed to enforce their rules during a turn.
function isTurnAction(action) {
return action.type >= A_PASS;
}
//Does NOT include halflings SH upgrade.
function isSpadeGivingAction(actiontype) {
return actiontype >= A_SPADE && actiontype <= A_GIANTS_2SPADE;
}
//A transform action is any action that transforms terrain and allows also doing A_BUILD afterwards.
function isTransformAction(actiontype) {
return actiontype >= A_TRANSFORM_CW && actiontype <= A_SANDSTORM;
}
//This excludes the sandstorm. This are actions that consume spades, so are ok in the same turn after using a power or bonus action that gives spades.
function isSpadeConsumingAction(actiontype) {
return actiontype >= A_TRANSFORM_CW && actiontype <= A_TRANSFORM_SPECIAL;
}
//returns how many spades the action produces (positive) or consumes (negative). Includes halflings SH etc...
function spadesDifference(player, action) {
if(action.type == A_SPADE) return 1;
if(action.type == A_BONUS_SPADE) return 1;
if(action.type == A_POWER_SPADE) return 1;
if(action.type == A_POWER_2SPADE) return 2;
if(action.type == A_GIANTS_2SPADE) return 2;
if(action.type == A_UPGRADE_SH && player.faction == F_HALFLINGS) return 3;
if(action.type == A_TRANSFORM_CW) return -1;
if(action.type == A_TRANSFORM_CCW) return -1;
if(action.type == A_GIANTS_TRANSFORM) return -2;
if(action.type == A_TRANSFORM_SPECIAL) return -1;
// A_SANDSTORM and A_TRANSFORM_SPECIAL2: 0!
return 0;
}
function actionsSpadesDifference(player, actions) {
var result = 0;
for(var i = 0; i < actions.length; i++) result += spadesDifference(player, actions[i]);
return result;
}
function isUpgradeAction(action) {
return action.type >= A_UPGRADE_TP && action.type <= A_SWARMLINGS_TP;
}
//any build action that can be involved in founding a town, includes dwelling and mermaids watertown tile
function isTownyBuildAction(action) {
return action.type == A_BUILD || (action.type == A_WITCHES_D && action.co != null) || action.type == A_CONNECT_WATER_TOWN;
}
function isBuildDwellingAction(action) {
return action.type == A_BUILD || (action.type == A_WITCHES_D && action.co != null);
}
//any build action that can be involved in founding a town, includes dwelling and mermaids watertown tile
function isBridgeAction(action) {
return action.type == A_PLACE_BRIDGE;
}
function isConvertAction(action) {
return action.type > A_BURN && action.type <= A_CONVERT_1W_1P;
}
function isConvertOrBurnAction(action) {
return action.type >= A_BURN && action.type <= A_CONVERT_1W_1P;
}
//whether this is a faction-specific action
function isFactionAction(action) {
if(action == A_CONNECT_WATER_TOWN) return true;
if(action == A_CONVERT_1W_1P) return true;
if(action == A_CONVERT_1VP_1C) return true;
if(action == A_CONVERT_2C_1VP) return true;
if(action == A_DOUBLE) return true;
if(action == A_TUNNEL) return true;
if(action == A_CARPET) return true;
if(action == A_GIANTS_2SPADE) return true;
if(action == A_GIANTS_TRANSFORM) return true;
if(action == A_TRANSFORM_SPECIAL) return true;
if(action == A_TRANSFORM_SPECIAL2) return true;
if(action == A_SANDSTORM) return true;
if(action == A_WITCHES_D) return true;
if(action == A_SWARMLINGS_TP) return true;
if(action == A_AUREN_CULT) return true;
if(action == A_ENGINEERS_BRIDGE) return true;
if(action == A_SHIFT) return true;
if(action == A_SHIFT2) return true;
return false;
}
function actionRequiresTownClusterRecalc(action) {
return (isUpgradeAction(action) && action.type != A_UPGRADE_TE) || isTownyBuildAction(action) || isBridgeAction(action);
}
function actionMightFormTown(action) {
if(actionRequiresTownClusterRecalc(action)) return true;
for(var i = 0; i < action.favtiles.length; i++) {
if(action.favtiles[i] == T_FAV_2F_6TW) return true;
}
return false;
}
function actionsRequireTownClusterRecalc(actions) {
for(var i = 0; i < actions.length; i++) if(actionRequiresTownClusterRecalc(actions[i])) return true;
return false;
}