-
Notifications
You must be signed in to change notification settings - Fork 11
/
ui_plant3.js
305 lines (259 loc) · 10.9 KB
/
ui_plant3.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Ethereal Farm
Copyright (C) 2020-2024 Lode Vandevenne
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// ui for planting a new infinity plant
function makePlantChip3(crop, x, y, w, parent, opt_plantfun, opt_showfun, opt_tooltipfun, opt_replace, opt_recoup, opt_field) {
var flex = new Flex(parent, x * w + 0.01, [0, 0, y * w + 0.010, 0.5], (x + 1) * w - 0.01, [0, 0, (y + 1) * w - 0.01, 0.5]);
var div = flex.div;
div.className = 'efEtherealPlantChip';
var canvasFlex = new Flex(flex, 0, [0.5, 0, -0.35], [0, 0, 0.7], [0.5, 0, 0.35]);
var canvas = createCanvas('0%', '0%', '100%', '100%', canvasFlex.div);
renderImage(crop.image[4], canvas);
var infoFlex = new Flex(flex, [0, 0, 0.7], 0, 1, [0, 0, 1]);
var text = '';
if(opt_replace) {
text += '<b>' + upper(crop.name) + '</b><br>';
} else {
text += '<b>Plant ' + crop.name + '</b><br>';
}
var cost = crop.getCost();
if(opt_recoup) cost = cost.sub(opt_recoup);
if(opt_replace && opt_field && opt_field.cropIndex() == crop.index && crop.type != CROPTYPE_BRASSICA) {
cost = Res(); // recoup - crop.getCost() gives wrong value since when planting same, crop amount used in cost computation is one less. Exception: brassica, where it's the opposite situation, crop amount doesn't affect cost, but has variable lifespan based recoup cost
}
text += 'type: ' + getCropTypeName(crop.type) + '<br>';
text += 'cost: ' + cost.toString();
var buyFlex = undefined;
if(opt_showfun) {
styleButton0(canvasFlex.div, true);
addButtonAction(canvasFlex.div, opt_showfun, upper(crop.name) + ' info');
}
if(opt_plantfun) {
buyFlex = new Flex(flex, [0, 0, 0.7], [0, 0, 0.0], [1, 0, -0.02], [0, 0, 0.98]);
addButtonAction(buyFlex.div, opt_plantfun, (opt_replace ? 'Replace with ' : 'Plant ') + crop.name);
styleButton0(buyFlex.div);
}
if(opt_tooltipfun) {
if(opt_showfun) {
registerTooltip(canvasFlex.div, function() {
return 'Show infinity ' + crop.name + ' info';
}, true);
}
if(opt_plantfun) {
registerTooltip(buyFlex.div, function() {
return (opt_replace ? 'Replace with infinity ' : 'Plant infinity ') + crop.name + '<br><br>' + opt_tooltipfun();
}, true);
}
registerTooltip(infoFlex.div, function() {
return 'Infinity ' + crop.name + '<br><br>' + opt_tooltipfun();
}, true);
} else {
if(opt_showfun) registerTooltip(canvasFlex.div, 'Show ' + crop.name + ' info');
if(opt_plantfun) registerTooltip(canvasFlex.div, (opt_replace ? 'Replace with infinity ' : 'Plant infinity ') + crop.name);
}
infoFlex.div.innerHTML = text;
if(opt_plantfun && state.res.lt(cost)) {
flex.div.className = 'efButtonTranslucentCantAfford';
registerUpdateListener(function() {
if(!flex || !document.body.contains(infoFlex.div)) return false;
var cost = crop.getCost();
if(opt_recoup) cost = cost.sub(opt_recoup);
if(state.res.gte(cost)) {
flex.div.className = 'efEtherealPlantChip';
return false;
}
return true;
});
}
return flex;
}
function crop3SortFun(a, b) {
var ac = crops3[a].cost.infseeds;
var bc = crops3[b].cost.infseeds;
if(ac.eqr(0) || bc.eqr(0)) {
// for crops that don't use infseeds as cost
ac = Num(crops3[a].tier);
bc = Num(crops3[b].tier);
}
return ac.lt(bc) ? 1 : -1;
}
// get the array of unlocked crops in the plant dialog, in order they should be displayed:
// most expensive ones first, but some of each type represented at the top (the latter is not yet implemented due to not yet needed for any supported crop types)
function getCrop3Order() {
var unlocked = [];
for(var i = 0; i < registered_crops3.length; i++) {
if(state.crops3[registered_crops3[i]].unlocked) unlocked.push(registered_crops3[i]);
}
var result = [];
var added = {};
var array;
// most expensive one of each unlocked type; this makes it so the runestone won't disappear to the bottom as more crops get unlocked
array = [];
for(var type = 0; type < NUM_CROPTYPES; type++) {
var highest = undefined;
for(var i = 0; i < unlocked.length; i++) {
if(added[unlocked[i]]) continue;
var c = crops3[unlocked[i]];
if(c.type == type && (!highest || c.cost.gt(highest.cost))) highest = c;
}
if(highest) {
array.push(highest.index);
}
}
array.sort(crop3SortFun);
for(var i = 0; i < array.length; i++) {
result.push(array[i]);
added[array[i]] = true;
}
// everything else
array = [];
for(var i = 0; i < unlocked.length; i++) {
if(added[unlocked[i]]) continue;
array.push(unlocked[i]);
}
array.sort(crop3SortFun);
for(var i = 0; i < array.length; i++) {
result.push(array[i]);
added[array[i]] = true;
}
return result;
}
// infinity version
// TODO: share code with makePlantDialog
function makePlantDialog3(x, y, opt_replace, opt_recoup) {
var numplants = 0;
for(var i = 0; i < registered_crops3.length; i++) {
if(state.crops3[registered_crops3[i]].unlocked) numplants++;
}
var dialog = createDialog({
title:(opt_replace ? 'Replace infinity crop' : 'Plant infinity crop'),
bgstyle:'efDialogEthereal'
});
var tx = 0;
var ty = 0;
var contentFlex = dialog.content;
var flex = new Flex(contentFlex, 0, 0, 1, 0.05);
centerText2(flex.div);
if(opt_replace) {
centerText2(flex.div);
flex.div.textEl.innerHTML = 'Replace crop with...';
} else {
flex.div.textEl.innerHTML = 'Choose an infinity crop to plant. Click the icon for more info, or the text to plant it now.';
}
flex = new Flex(contentFlex, 0, 0.1, 1, 1);
makeScrollable(flex);
var crops_order = getCrop3Order();
for(var i = 0; i < crops_order.length; i++) {
var index = crops_order[i];
var c = crops3[index];
var c2 = state.crops3[index];
if(!c2.unlocked) continue;
var tooltipfun = bind(function(index, opt_detailed) {
var result = '';
var c = crops3[index];
var f = state.field3[y][x];
result += 'Crop type: Infinity ' + getCropTypeName(c.type);
if(c.tier == -1) {
result += ' (tier: translucent)';
} else if(c.tier > 0 || (state.infinity_ascend && c.tier >= 0)) {
result += ' (tier ' + (c.tier + 1) + ')';
}
result += '<br>';
var help = getCropTypeHelp3(c.type, state);
if(help) {
result += help;
result += '<br><br>';
}
var cost = c.getCost();
var replacementcost = cost;
if(opt_replace) {
replacementcost = cost.sub(opt_recoup);
if(f.cropIndex() == c.index && c.type != CROPTYPE_BRASSICA) {
replacementcost = Res(); // recoup - crop.getCost() gives wrong value since when planting same, crop amount used in cost computation is one less. Exception: brassica, where it's the opposite situation, crop amount doesn't affect cost, but has variable lifespan based recoup cost
}
}
if(opt_detailed) {
result += '<br>Base cost: ' + c.cost.toString();
result += '<br>Next cost: ' + cost.toString() + ' (' + getCostAffordTimer(cost) + ')';
} else {
result += 'Cost: ' + cost.toString() + ' (' + getCostAffordTimer(cost) + ')';
}
if(opt_replace) result += '<br>Replacement cost: ' + replacementcost.toString() + ' (' + getCostAffordTimer(replacementcost) + ')';
if(c.type == CROPTYPE_BRASSICA) {
result += '<br><br>Finite lifetime: ' + util.formatDuration(c.getPlantTime());
} else {
result += '<br><br>Grow time: ' + util.formatDuration(c.getPlantTime());
}
var prod = c.getProd(f);
if(!prod.empty()) {
result += '<br><br>Production: ' + prod.toString() + '/s';
if(c.type == CROPTYPE_BERRY) result += ' (boostable)'; // added for clarity, since at higher tiers, like electrum, the base production starts getting lower than that of watercress and it may be misleading to see that, whey they can be boosted 10K times as high with flowers/bees
if(prod.infseeds.neqr(0) && prod.infseeds.ltr(0.1) && prod.infseeds.gtr(-0.1)) result += ' (' + prod.infseeds.mulr(3600).toString() + '/h)';
if(c.type == CROPTYPE_BRASSICA) {
var totalprod = prod.mulr(c.getPlantTime());
result += '<br>Total production over full lifetime: ' + totalprod.toString();
}
}
var infboost = c.getInfBoost();
if(infboost.neqr(0)) {
result += '<br><br>';
if(c.type == CROPTYPE_RUNESTONE) {
result += 'Boost to neighboring crops basic field boost: ' + infboost.toPercentString();
} else if(c.type == CROPTYPE_BEE) {
result += 'Boost to neighboring flowers: ' + infboost.toPercentString();
} else if(c.type == CROPTYPE_FLOWER) {
result += 'Boost to neighboring berries: ' + infboost.toPercentString();
} else if(c.type == CROPTYPE_STINGING) {
result += 'Boost to neighboring mushrooms: ' + infboost.toPercentString();
} else if(c.type == CROPTYPE_FERN) {
result += 'Copy: ' + infboost.toPercentString();
} else {
result += 'Boost: ' + infboost.toPercentString();
}
}
var basicboost = c.getBasicBoost();
if(basicboost.neqr(0)) {
result += '<br><br>Production boost to basic field: ' + basicboost.toPercentString();
}
return result;
}, index);
var plantfun = bind(function(index) {
var c = crops3[index];
if(opt_replace) addAction({type:ACTION_REPLACE3, x:x, y:y, crop:c});
else addAction({type:ACTION_PLANT3, x:x, y:y, crop:c});
state.lastPlanted3 = index; // for shift key
closeAllDialogs();
update(); // do update immediately rather than wait for tick, for faster feeling response time
return true;
}, index);
var showfun = bind(function(tooltipfun, plantfun, c) {
var text = upper(c.name) + '<br><br>' + tooltipfun(true);
var dialog = createDialog({
size:(text.length < 350 ? DIALOG_SMALL : DIALOG_MEDIUM),
title:'Infinity crop info',
names:'plant',
functions:plantfun,
icon:c.image[4]
});
dialog.content.div.innerHTML = text;
}, tooltipfun, plantfun, c);
var chip = makePlantChip3(c, tx, ty, 0.33, flex, plantfun, showfun, tooltipfun, opt_replace, opt_recoup, state.field3[y][x]);
tx++;
if(tx >= 3) {
tx = 0;
ty++;
}
}
flex.update(); // something about the makeScrollable above misplaces some of the flex-managed sub positions, this update fixes it
}