@mark1russell7/stack-endurance 0.8.19 → 0.8.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,9 +11,21 @@ function inputsOf(converter) {
11
11
  for (const input of converter.takes) if (input.amount > 0) out.set(input.resource, (out.get(input.resource) ?? 0) + input.amount);
12
12
  return out;
13
13
  }
14
- /** This function tells if a converter can run in a case. Its device works, it takes a resource, and it gives an amount more than 0. */
14
+ /**
15
+ * This function tells if a converter is a source (M2-solar): it takes nothing, and it has a limit `perDay` more than 0.
16
+ * Its runs of a day are lost when no draw uses them. Thus a take uses a source before a store.
17
+ */
18
+ function isSource(converter) {
19
+ if (!(converter.perDay !== void 0 && converter.perDay > 0)) return false;
20
+ for (const input of converter.takes) if (input.amount > 0) return false;
21
+ return true;
22
+ }
23
+ /**
24
+ * This function tells if a converter can run in a case. Its device works, it takes a resource or it is a source, and it
25
+ * gives an amount more than 0.
26
+ */
15
27
  function canRun(converter, stoppedParts) {
16
- return converter.gives.amount > 0 && inputsOf(converter).size > 0 && (converter.device === void 0 || !stoppedParts.has(converter.device));
28
+ return converter.gives.amount > 0 && (inputsOf(converter).size > 0 || isSource(converter)) && (converter.device === void 0 || !stoppedParts.has(converter.device));
17
29
  }
18
30
  /**
19
31
  * This function gives the key of the device-days that a converter uses. The converters of one device share one key.
@@ -271,23 +283,43 @@ function reachOf(parts, needs) {
271
283
  }
272
284
  // src/seasonal.ts
273
285
  /**
286
+ * This function gives the capacity of each resource whose stores have a capacity (M2-solar-charge). A store whose part is
287
+ * in `stoppedParts` has no capacity. A store with a due date has no charge: the gate refuses a capacity with a due date.
288
+ */
289
+ function storeCapacities(layer, stoppedParts) {
290
+ const out = /* @__PURE__ */ new Map();
291
+ for (const store of layer.stores) {
292
+ if (store.capacity === void 0 || store.due !== void 0) continue;
293
+ if (store.part !== void 0 && stoppedParts.has(store.part)) continue;
294
+ out.set(store.resource, (out.get(store.resource) ?? 0) + store.capacity);
295
+ }
296
+ return out;
297
+ }
298
+ /**
274
299
  * This function gives the parts of the programs of a case with a season. `parts` are the parts of the case with the
275
300
  * stock after the sowings (`seasonSupply`).
276
301
  */
277
- function seasonPlan(supply, parts, caseCuts, draws, devices) {
302
+ function seasonPlan(supply, parts, caseCuts, draws, devices, capacities = /* @__PURE__ */ new Map()) {
278
303
  const total = new Map(supply.stock);
279
304
  const harvests = supply.lots.map((lot, origin) => ({
280
305
  ...lot,
281
306
  origin
282
307
  })).filter((lot) => parts.resources.has(lot.resource));
283
308
  for (const lot of harvests) total.set(lot.resource, (total.get(lot.resource) ?? 0) + lot.amount);
284
- const all = partsOf(total, parts.direct, parts.cooks, parts.makers);
285
- const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0).map((resource) => ({
309
+ const found = partsOf(total, parts.direct, parts.cooks, parts.makers);
310
+ const sourcesOf = (resource) => found.makers.filter((maker) => isSource(maker) && giftResource(maker) === resource);
311
+ const charged = [...capacities.keys()].filter((resource) => found.resources.has(resource) && sourcesOf(resource).length > 0).sort();
312
+ const all = {
313
+ ...found,
314
+ stocked: [.../* @__PURE__ */ new Set([...found.stocked, ...charged])].sort()
315
+ };
316
+ const storeLot = (resource) => ({
286
317
  resource,
287
318
  day: 0,
288
319
  spoils: 1e400,
289
320
  amount: exact(supply.stock.get(resource) ?? 0)
290
- })), ...harvests.map((lot) => ({
321
+ });
322
+ const lots = [...all.stocked.filter((resource) => (supply.stock.get(resource) ?? 0) > 0 || charged.includes(resource)).map(storeLot), ...harvests.map((lot) => ({
291
323
  resource: lot.resource,
292
324
  day: Math.max(0, lot.day),
293
325
  spoils: lot.spoils,
@@ -295,17 +327,33 @@ function seasonPlan(supply, parts, caseCuts, draws, devices) {
295
327
  origin: lot.origin
296
328
  }))];
297
329
  const inside = (day) => day > 0 && day < supply.horizon;
330
+ const shares = monthlyShares([...all.cooks, ...all.makers]);
331
+ const months = shares.size === 0 ? [] : monthEdges(supply.start, supply.horizon);
332
+ const edges = [.../* @__PURE__ */ new Set([
333
+ 0,
334
+ supply.horizon,
335
+ ...months,
336
+ ...lots.flatMap((lot) => [lot.day, lot.spoils]).filter(inside)
337
+ ])].sort((a, b) => a - b);
338
+ const charges = charged.map((resource) => {
339
+ const lot = lots.find((item) => item.resource === resource && item.origin === void 0);
340
+ return {
341
+ resource,
342
+ capacity: exact(Math.max(capacities.get(resource) ?? 0, toNumber(lot.amount))),
343
+ lot,
344
+ sources: sourcesOf(resource)
345
+ };
346
+ });
298
347
  return {
299
348
  parts: all,
300
349
  caseCuts,
301
350
  draws,
302
351
  devices,
303
352
  lots,
304
- edges: [.../* @__PURE__ */ new Set([
305
- 0,
306
- supply.horizon,
307
- ...lots.flatMap((lot) => [lot.day, lot.spoils]).filter(inside)
308
- ])].sort((a, b) => a - b)
353
+ edges,
354
+ shares,
355
+ months: edges.slice(0, -1).map((edge) => shares.size === 0 ? 0 : monthOf(supply.start, edge)),
356
+ charges
309
357
  };
310
358
  }
311
359
  const edgeOf = (plan, index) => exact(plan.edges[index] ?? 0);
@@ -350,9 +398,11 @@ function addCell(program, plan, draw, segment, fed) {
350
398
  takes
351
399
  };
352
400
  }
353
- /** This function adds the row of each lot: the takes of all the cells are not more than its amount. */
401
+ /** This function adds the row of each lot: the takes of all the cells are not more than its amount. The rows of `addCharges` cover the lot of a charge. */
354
402
  function lotRows(program, plan, cells) {
403
+ const charged = new Set(plan.charges.map((charge) => charge.lot));
355
404
  for (const lot of plan.lots) {
405
+ if (charged.has(lot)) continue;
356
406
  const row = /* @__PURE__ */ new Map();
357
407
  for (const cell of cells) {
358
408
  const column = cell.takes.get(lot);
@@ -362,6 +412,50 @@ function lotRows(program, plan, cells) {
362
412
  }
363
413
  }
364
414
  /**
415
+ * This function adds the charge of each store with a capacity in the segments before `through` (M2-solar-charge).
416
+ * Each source has one charge column in each segment before the last one: no take of the program uses the charge of the last one.
417
+ * The level at the start of a segment is the amount of the lot plus the charges of the segments before it, less their takes.
418
+ * - The takes of a segment are not more than the level at its start.
419
+ * - The level at the start of a segment plus its charge is not more than the capacity.
420
+ * Thus each order of the takes and the charges in a segment is a plan. The caller adds the charge columns to the rows of the device-days.
421
+ */
422
+ function addCharges(program, plan, cells, through) {
423
+ const pairs = /* @__PURE__ */ new Map();
424
+ const gifts = /* @__PURE__ */ new Map();
425
+ for (const charge of plan.charges) {
426
+ const level = /* @__PURE__ */ new Map();
427
+ const given = /* @__PURE__ */ new Map();
428
+ gifts.set(charge.resource, given);
429
+ for (let segment = 0; segment < through; segment += 1) {
430
+ const takes = /* @__PURE__ */ new Map();
431
+ for (const cell of cells) {
432
+ const column = cell.segment === segment ? cell.takes.get(charge.lot) : void 0;
433
+ if (column !== void 0) addTo(takes, column, ONE);
434
+ }
435
+ const most = new Map(takes);
436
+ for (const [column, value] of level) addTo(most, column, neg(value));
437
+ program.push(most, "<=", charge.lot.amount);
438
+ if (segment + 1 < through) {
439
+ const full = new Map(level);
440
+ for (const source of charge.sources) {
441
+ const column = program.next();
442
+ pairs.set(segment, [...pairs.get(segment) ?? [], [source, column]]);
443
+ const each = exact(source.gives.amount);
444
+ addTo(full, column, each);
445
+ addTo(level, column, each);
446
+ addTo(given, column, each);
447
+ }
448
+ program.push(full, "<=", sub(charge.capacity, charge.lot.amount));
449
+ }
450
+ for (const [column, value] of takes) addTo(level, column, neg(value));
451
+ }
452
+ }
453
+ return {
454
+ pairs,
455
+ gifts
456
+ };
457
+ }
458
+ /**
365
459
  * This function adds the cells of the fixed draws. Each one is fed to its first stop, and its runs use the device-days of each day
366
460
  * that it keeps. It gives the device-days of each day that the fixed draws keep, by the index of the segment, then the key.
367
461
  */
@@ -386,9 +480,13 @@ function addFixed(program, plan, fixed, cells) {
386
480
  }
387
481
  return kept;
388
482
  }
389
- /** This function gives the device-days of each day that the fixed draws leave in a segment. */
483
+ /** The share of the device-days of a key in a segment (M2-solar): its share `monthly` in the month of the segment, or 1. */
484
+ function shareIn(plan, segment, key) {
485
+ return shareOf(plan.shares, key, plan.months[segment] ?? 0);
486
+ }
487
+ /** This function gives the device-days of each day that the fixed draws leave in a segment: the devices times the share, less the kept device-days. */
390
488
  function roomOf(plan, kept, segment, key, devices) {
391
- const left = sub(exact(plan.devices.get(key) ?? devices), kept.get(segment)?.get(key) ?? ZERO);
489
+ const left = sub(mul(exact(plan.devices.get(key) ?? devices), exact(shareIn(plan, segment, key))), kept.get(segment)?.get(key) ?? ZERO);
392
490
  return sign(left) > 0 ? left : ZERO;
393
491
  }
394
492
  /**
@@ -412,9 +510,10 @@ function buildStep(plan, fixed, open, segment) {
412
510
  floors.set(draw.need, program.push(/* @__PURE__ */ new Map([[fed, ONE], [t, neg(ONE)]]), ">=", neg(edgeOf(plan, segment))));
413
511
  cells.push(addCell(program, plan, draw, segment, fed));
414
512
  }
513
+ const charges = addCharges(program, plan, cells, segment + 1);
415
514
  const shared = /* @__PURE__ */ new Map();
416
515
  for (let index = 0; index <= segment; index += 1) {
417
- const { limits, stopped } = deviceUse(cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]));
516
+ const { limits, stopped } = deviceUse([...cells.filter((cell) => cell.segment === index && opened.has(cell.draw.need)).flatMap((cell) => [...cell.block.runs]), ...charges.pairs.get(index) ?? []]);
418
517
  for (const column of stopped) program.push(/* @__PURE__ */ new Map([[column, ONE]]), "<=", ZERO);
419
518
  const bySegment = /* @__PURE__ */ new Map();
420
519
  shared.set(index, bySegment);
@@ -440,7 +539,8 @@ function buildStep(plan, fixed, open, segment) {
440
539
  cells,
441
540
  floors,
442
541
  shared,
443
- segment
542
+ segment,
543
+ charges
444
544
  };
445
545
  }
446
546
  /**
@@ -485,7 +585,8 @@ function fewestOpen(step, open) {
485
585
  /** This function gives the smallest sum of the shares of the stocks, each over the total of its lots, as an objective. */
486
586
  function leastShares(plan, cells, count) {
487
587
  const least = /* @__PURE__ */ new Map();
488
- for (const cell of cells) for (const [resource, index] of cell.block.shares) addTo(least, index, div(ONE, exact(plan.parts.stock.get(resource) ?? 1)));
588
+ const weight = (resource) => div(ONE, exact((plan.parts.stock.get(resource) ?? 0) > 0 ? plan.parts.stock.get(resource) : 1));
589
+ for (const cell of cells) for (const [resource, index] of cell.block.shares) addTo(least, index, weight(resource));
489
590
  return {
490
591
  sense: "min",
491
592
  coefficients: dense(least, count)
@@ -533,10 +634,11 @@ function takesOf(cells, need, x) {
533
634
  return out;
534
635
  }
535
636
  /** This function gives the resources of `closure` with a store in the layer whose lots are used up at `x`. They are in the order of their codes. */
536
- function usedUp(plan, cells, x, closure, stored) {
637
+ function usedUp(plan, cells, x, closure, stored, charges) {
537
638
  return [...closure].filter((resource) => stored.has(resource)).filter((resource) => {
538
639
  let taken = ZERO;
539
- let amount = ZERO;
640
+ const given = charges?.gifts.get(resource);
641
+ let amount = given === void 0 ? ZERO : valueAt(given, x);
540
642
  for (const lot of plan.lots) {
541
643
  if (lot.resource !== resource) continue;
542
644
  amount = add(amount, lot.amount);
@@ -600,6 +702,12 @@ function seasonBest(plan, fixed, levels, stored) {
600
702
  }
601
703
  }
602
704
  }
705
+ const charges = addCharges(program, plan, cells, segments);
706
+ for (const [segment, list] of charges.pairs) {
707
+ const { limits } = deviceUse([...[...again.keys()].filter((cell) => cell.segment === segment).flatMap((cell) => [...cell.block.runs]), ...list]);
708
+ const keys = new Set(list.map(([source]) => deviceKey(source)));
709
+ for (const [key, limit] of limits) if (keys.has(key)) program.push(limit.row, "<=", mul(roomOf(plan, kept, segment, key, limit.devices), lengthOf(plan, segment)));
710
+ }
603
711
  lotRows(program, plan, cells);
604
712
  const objectives = levels.map((level) => {
605
713
  const needs = new Set(level.map((draw) => draw.need));
@@ -653,7 +761,7 @@ function seasonBest(plan, fixed, levels, stored) {
653
761
  const { closure } = reachOf(plan.parts, /* @__PURE__ */ new Set([draw.need]));
654
762
  needs[draw.need] = {
655
763
  days: toNumber(fix.stop),
656
- binding: fix.atHorizon ? [] : usedUp(plan, cells, x, closure, stored),
764
+ binding: fix.atHorizon ? [] : usedUp(plan, cells, x, closure, stored, charges),
657
765
  takes: takesOf(cells, draw.need, x),
658
766
  ...fix.capped.length === 0 ? {} : { capped: fix.capped },
659
767
  ...fix.atHorizon ? { atHorizon: true } : {},
@@ -672,7 +780,10 @@ function seasonBest(plan, fixed, levels, stored) {
672
780
  if (sign(fed) <= 0) continue;
673
781
  const into = perDay.get(cell.draw.need) ?? /* @__PURE__ */ new Map();
674
782
  perDay.set(cell.draw.need, into);
675
- for (const [key, limit] of deviceUse(cell.block.runs).limits) into.set(key, Math.max(into.get(key) ?? 0, toNumber(div(valueAt(limit.row, x), fed))));
783
+ for (const [key, limit] of deviceUse(cell.block.runs).limits) {
784
+ const share = shareIn(plan, cell.segment, key);
785
+ if (share > 0) into.set(key, Math.max(into.get(key) ?? 0, toNumber(div(valueAt(limit.row, x), fed)) / share));
786
+ }
676
787
  }
677
788
  return {
678
789
  needs,
@@ -703,7 +814,7 @@ function seasonJoint(plan, stored) {
703
814
  days: toNumber(stop.days),
704
815
  atHorizon: stop.atHorizon,
705
816
  takes,
706
- binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored),
817
+ binding: usedUp(plan, step.cells, x, reachOf(plan.parts, open).closure, stored, step.charges),
707
818
  runs,
708
819
  capped
709
820
  };
@@ -715,14 +826,16 @@ function seasonJoint(plan, stored) {
715
826
  *
716
827
  * @param stock - The stocks that the higher draws leave.
717
828
  * @param lots - The rest of each harvest lot that the higher draws leave.
718
- * @param room - The device-days of each day that the higher draws leave, by the key of the device.
829
+ * @param room - The device-days of each day that the higher draws leave, by the key of the device, before the shares `monthly`.
830
+ * @param start - The first day of the event, for the months of the shares.
719
831
  */
720
- function seasonDraw(layer, stoppedParts, caseCuts, draw, stock, lots, horizon, room) {
832
+ function seasonDraw(layer, stoppedParts, caseCuts, draw, stock, lots, horizon, room, start) {
721
833
  const supply = {
722
834
  stock: new Map(stock),
723
835
  harvests: [],
724
836
  lots,
725
- horizon
837
+ horizon,
838
+ start
726
839
  };
727
840
  const plan = seasonPlan(supply, caseParts(layer, stoppedParts, supply.stock, /* @__PURE__ */ new Set([draw.need])), caseCuts, [draw], room);
728
841
  const fixed = /* @__PURE__ */ new Map();
@@ -823,6 +936,38 @@ function dayOf(date) {
823
936
  if (day === void 0) throw new RangeError(`The date "${date}" is not a date YYYY-MM-DD.`);
824
937
  return day;
825
938
  }
939
+ const DAY_MS = 864e5;
940
+ /** This function gives the month of the day `day` after the start, 0 for January (M2-solar). It throws a `RangeError` for a bad date. */
941
+ function monthOf(start, day) {
942
+ dayOf(start);
943
+ return new Date(Date.parse(`${start}T00:00:00Z`) + Math.floor(day) * DAY_MS).getUTCMonth();
944
+ }
945
+ /**
946
+ * This function gives the days from the start to the first day of each month, in increasing order (M2-solar). Each
947
+ * day is more than 0 and less than the horizon. It throws a `RangeError` for a date that is not `YYYY-MM-DD`.
948
+ */
949
+ function monthEdges(start, horizon) {
950
+ dayOf(start);
951
+ const first = Date.parse(`${start}T00:00:00Z`);
952
+ const date = new Date(first);
953
+ const out = [];
954
+ for (let month = date.getUTCMonth() + 1;; month += 1) {
955
+ const day = Math.round((Date.UTC(date.getUTCFullYear(), month, 1) - first) / DAY_MS);
956
+ if (!(day < horizon)) break;
957
+ out.push(day);
958
+ }
959
+ return out;
960
+ }
961
+ /** This function gives the shares `monthly` of each device key of the converters with a limit and shares (M2-solar). */
962
+ function monthlyShares(converters) {
963
+ const out = /* @__PURE__ */ new Map();
964
+ for (const converter of converters) if (converter.monthly !== void 0 && converter.perDay !== void 0) out.set(deviceKey(converter), converter.monthly);
965
+ return out;
966
+ }
967
+ /** This function gives the share of the device-days of a key in a month: its share `monthly`, or 1. */
968
+ function shareOf(shares, key, month) {
969
+ return shares.get(key)?.[month] ?? 1;
970
+ }
826
971
  /**
827
972
  * This function gives the amount of each resource in the stores at a date, by the code of the resource.
828
973
  *
@@ -872,10 +1017,15 @@ function makersOf(layer, stoppedParts) {
872
1017
  return out;
873
1018
  }
874
1019
  function waysOf(layer, need, stoppedParts, makers) {
1020
+ const converters = (layer.converters ?? []).filter((converter) => giftNeed(converter) === need && canRun(converter, stoppedParts));
1021
+ const sources = converters.filter(isSource);
875
1022
  return {
876
1023
  direct: (layer.direct ?? []).filter((use) => use.need === need && use.amount > 0),
877
- converters: (layer.converters ?? []).filter((converter) => giftNeed(converter) === need && canRun(converter, stoppedParts)),
878
- makers
1024
+ converters,
1025
+ makers,
1026
+ sources,
1027
+ others: sources.length === 0 ? converters : converters.filter((converter) => !isSource(converter)),
1028
+ open: [.../* @__PURE__ */ new Set([...sources, ...[...makers.values()].flat().filter(isSource)])]
879
1029
  };
880
1030
  }
881
1031
  const NO_PATH = /* @__PURE__ */ new Set();
@@ -914,15 +1064,25 @@ function mostRuns(left, makers, converter, path) {
914
1064
  * It changes `left`, and it gives the amount that it could not take.
915
1065
  */
916
1066
  function takeResource(left, makers, resource, amount, path) {
1067
+ let rest = amount;
1068
+ const ways = path.has(resource) ? void 0 : makers.get(resource);
1069
+ let next;
1070
+ if (ways !== void 0 && ways.some(isSource)) {
1071
+ next = new Set(path).add(resource);
1072
+ for (const maker of ways) {
1073
+ if (rest <= 0) break;
1074
+ if (isSource(maker)) rest -= run(left, makers, maker, rest / maker.gives.amount, next) * maker.gives.amount;
1075
+ }
1076
+ }
917
1077
  const has = left.get(resource) ?? 0;
918
- const got = Math.min(amount, has);
1078
+ const got = Math.min(Math.max(0, rest), has);
919
1079
  left.set(resource, has - got);
920
- let rest = amount - got;
921
- if (rest <= 0 || path.has(resource)) return Math.max(0, rest);
922
- const next = new Set(path).add(resource);
923
- for (const maker of makers.get(resource) ?? []) {
1080
+ rest -= got;
1081
+ if (rest <= 0 || ways === void 0) return Math.max(0, rest);
1082
+ next ??= new Set(path).add(resource);
1083
+ for (const maker of ways) {
924
1084
  if (rest <= 0) break;
925
- rest -= run(left, makers, maker, rest / maker.gives.amount, next) * maker.gives.amount;
1085
+ if (!isSource(maker)) rest -= run(left, makers, maker, rest / maker.gives.amount, next) * maker.gives.amount;
926
1086
  }
927
1087
  return Math.max(0, rest);
928
1088
  }
@@ -968,12 +1128,16 @@ function run(left, makers, converter, wanted, path) {
968
1128
  */
969
1129
  function take(left, ways, amount) {
970
1130
  let rest = amount;
1131
+ for (const converter of ways.sources) {
1132
+ if (rest <= 0) break;
1133
+ rest -= run(left, ways.makers, converter, rest / converter.gives.amount, NO_PATH) * converter.gives.amount;
1134
+ }
971
1135
  for (const use of ways.direct) {
972
1136
  if (rest <= 0) break;
973
1137
  const wanted = rest / use.amount;
974
1138
  rest -= (wanted - takeResource(left, ways.makers, use.resource, wanted, NO_PATH)) * use.amount;
975
1139
  }
976
- for (const converter of ways.converters) {
1140
+ for (const converter of ways.others) {
977
1141
  if (rest <= 0) break;
978
1142
  rest -= run(left, ways.makers, converter, rest / converter.gives.amount, NO_PATH) * converter.gives.amount;
979
1143
  }
@@ -1107,7 +1271,8 @@ function sowings(layer, stoppedParts, stock, makers, start, horizon) {
1107
1271
  function seasonSupply(layer, stoppedParts, options) {
1108
1272
  const stock = storeAmounts(layer, options.at, stoppedParts);
1109
1273
  const horizon = seasonHorizon(options.horizon);
1110
- const sown = sowings(layer, stoppedParts, stock, makersOf(layer, stoppedParts), options.start ?? options.at, horizon);
1274
+ const start = options.start ?? options.at;
1275
+ const sown = sowings(layer, stoppedParts, stock, makersOf(layer, stoppedParts), start, horizon);
1111
1276
  const lots = sown.lots.filter((lot) => lot.day < horizon && lot.left > 0).sort((a, b) => a.day - b.day).map((lot) => ({
1112
1277
  resource: lot.resource,
1113
1278
  day: lot.day,
@@ -1118,12 +1283,17 @@ function seasonSupply(layer, stoppedParts, options) {
1118
1283
  stock,
1119
1284
  harvests: sown.harvests,
1120
1285
  lots,
1121
- horizon
1286
+ horizon,
1287
+ start
1122
1288
  };
1123
1289
  }
1124
- /** This function tells if a layer has a converter with a season that can run in a case. */
1290
+ /**
1291
+ * This function tells if a layer has a converter with a season, with shares `monthly` or a source (M2-solar) that can run
1292
+ * in a case. Such a layer needs the queries in segments of days to a horizon. A harvest, a spoil day and the start of a
1293
+ * month end a segment. A source can feed a draw with no end, thus the horizon is the upper limit of its days.
1294
+ */
1125
1295
  function hasSeason(layer, stoppedParts) {
1126
- return (layer.converters ?? []).some((converter) => converter.season !== void 0 && canRun(converter, stoppedParts));
1296
+ return (layer.converters ?? []).some((converter) => (converter.season !== void 0 || converter.monthly !== void 0 || isSource(converter)) && canRun(converter, stoppedParts));
1127
1297
  }
1128
1298
  /**
1129
1299
  * This function gives the days that each draw holds when a case stops its supply, and the resources that are used up then.
@@ -1153,6 +1323,9 @@ function hasSeason(layer, stoppedParts) {
1153
1323
  * A lower draw gets the stocks that the higher draws leave, the earliest stocks first taken. This is the safe side.
1154
1324
  * - A draw with a shared device and a season gets the programs of `bestEndurance` for itself alone (M2-grow-share-sweep). Its stocks,
1155
1325
  * its harvest lots and its device-days are those that the higher draws leave. It has no field `spoiled`.
1326
+ * - A source (M2-solar) takes nothing and has a limit `perDay`. A take uses it first: its runs of a day are lost when no
1327
+ * draw uses them. With shares `monthly`, the device-days of a day are the devices times the share of its month. Each
1328
+ * span also ends at the start of a month, and a draw can hold again from the start of a month.
1156
1329
  *
1157
1330
  * The function is pure, but for an internal count of the tries of its fits for the tests. It throws a `RangeError` for a date that is not `YYYY-MM-DD`. The gate `checkEndurance` examines the layer before.
1158
1331
  *
@@ -1170,6 +1343,11 @@ function enduranceOf(layer, caseCuts, options) {
1170
1343
  const stocked = new Set(layer.stores.map((store) => store.resource));
1171
1344
  const limited = (layer.converters ?? []).filter((converter) => converter.perDay !== void 0 && canRun(converter, stoppedParts));
1172
1345
  const devices = new Map(limited.map((converter) => [deviceKey(converter), converter.devices ?? 1]));
1346
+ const monthShares = monthlyShares(limited);
1347
+ const begin = options.start ?? options.at;
1348
+ const shareAt = (key, at) => monthShares.size === 0 ? 1 : shareOf(monthShares, key, monthOf(begin, at));
1349
+ const months = monthShares.size === 0 || supply === void 0 ? [] : monthEdges(begin, supply.horizon);
1350
+ const nextMonth = (at) => months.find((day) => day > at) ?? 1e400;
1173
1351
  const used = /* @__PURE__ */ new Map();
1174
1352
  let before = new Map(start);
1175
1353
  const horizon = supply?.horizon ?? 1e400;
@@ -1182,17 +1360,17 @@ function enduranceOf(layer, caseCuts, options) {
1182
1360
  const room = new Map([...devices].map(([key, count]) => [key, Math.max(0, count - (used.get(key) ?? 0))]));
1183
1361
  const closure = inputClosure(ways);
1184
1362
  const reach = /* @__PURE__ */ new Set([...ways.converters, ...[...closure].flatMap((resource) => makers.get(resource) ?? [])]);
1185
- const after = (stock, days) => {
1363
+ const after = (stock, days, at = 0) => {
1186
1364
  const tried = new Map(stock);
1187
- for (const [key, free] of room) tried.set(RUNS + key, free * days);
1365
+ for (const [key, free] of room) tried.set(RUNS + key, free * shareAt(key, at) * days);
1188
1366
  return {
1189
1367
  left: tried,
1190
1368
  short: take(tried, ways, days * demand)
1191
1369
  };
1192
1370
  };
1193
- const span = (stock, limit, bound) => {
1371
+ const span = (stock, limit, bound, at = 0) => {
1194
1372
  const fit = largestFit(Math.min(limit, bound / demand), (days) => {
1195
- const tried = after(stock, days);
1373
+ const tried = after(stock, days, at);
1196
1374
  return {
1197
1375
  short: tried.short,
1198
1376
  need: days * demand,
@@ -1201,7 +1379,7 @@ function enduranceOf(layer, caseCuts, options) {
1201
1379
  });
1202
1380
  return {
1203
1381
  days: fit.x,
1204
- left: fit.value ?? after(stock, fit.x).left
1382
+ left: fit.value ?? after(stock, fit.x, at).left
1205
1383
  };
1206
1384
  };
1207
1385
  const shares = sharesDevice(reach);
@@ -1212,7 +1390,7 @@ function enduranceOf(layer, caseCuts, options) {
1212
1390
  spoils: lot.spoils,
1213
1391
  amount: lot.amount
1214
1392
  }));
1215
- const held = seasonDraw(layer, stoppedParts, caseCuts, draw, resourcesOnly(before), lots, horizon, room);
1393
+ const held = seasonDraw(layer, stoppedParts, caseCuts, draw, resourcesOnly(before), lots, horizon, room, begin);
1216
1394
  needs[draw.need] = held.need;
1217
1395
  for (const [key, each] of held.perDay) used.set(key, (used.get(key) ?? 0) + each);
1218
1396
  before = held.stock;
@@ -1238,13 +1416,16 @@ function enduranceOf(layer, caseCuts, options) {
1238
1416
  const again = [];
1239
1417
  let atHorizon = false;
1240
1418
  const perDay = /* @__PURE__ */ new Map();
1241
- const count = (spanLeft, days) => {
1419
+ const count = (spanLeft, days, at = 0) => {
1242
1420
  if (days > 0) for (const [key, free] of room) {
1243
- const each = (free * days - (spanLeft.get(RUNS + key) ?? 0)) / days;
1421
+ const share = shareAt(key, at);
1422
+ if (!(share > 0)) continue;
1423
+ const each = (free * share * days - (spanLeft.get(RUNS + key) ?? 0)) / days / share;
1244
1424
  perDay.set(key, perDay.has(key) ? Math.max(perDay.get(key), each) : each);
1245
1425
  }
1246
1426
  };
1247
1427
  let spanDays;
1428
+ let spanAt = 0;
1248
1429
  if (share !== void 0) {
1249
1430
  low = share.days;
1250
1431
  spanDays = low;
@@ -1294,9 +1475,9 @@ function enduranceOf(layer, caseCuts, options) {
1294
1475
  advance();
1295
1476
  for (;;) {
1296
1477
  const spoil = Math.min(...pool.filter((lot) => lot.left > 0).map((lot) => lot.spoils));
1297
- const end = Math.min(next < later.length ? later[next].day : 1e400, spoil, horizon);
1298
- const part = span(stock, end - at, capacity(stock, ways));
1299
- count(part.left, part.days);
1478
+ const end = Math.min(next < later.length ? later[next].day : 1e400, spoil, horizon, nextMonth(at));
1479
+ const part = span(stock, end - at, capacity(stock, ways, shareAt, at), at);
1480
+ count(part.left, part.days, at);
1300
1481
  const rest = resourcesOnly(part.left);
1301
1482
  useLots(pool, stock, rest);
1302
1483
  stock = rest;
@@ -1305,14 +1486,16 @@ function enduranceOf(layer, caseCuts, options) {
1305
1486
  if (first === void 0) first = {
1306
1487
  days: stop,
1307
1488
  left: part.left,
1308
- spanDays: part.days
1489
+ spanDays: part.days,
1490
+ spanAt: at
1309
1491
  };
1310
1492
  else if (stop > resumed) again.push({
1311
1493
  from: resumed,
1312
1494
  to: stop
1313
1495
  });
1314
- if (next >= later.length) break;
1315
- at = later[next].day;
1496
+ const resume = Math.min(next < later.length ? later[next].day : 1e400, nextMonth(at));
1497
+ if (!(resume < horizon)) break;
1498
+ at = resume;
1316
1499
  resumed = at;
1317
1500
  } else {
1318
1501
  at = end;
@@ -1330,6 +1513,7 @@ function enduranceOf(layer, caseCuts, options) {
1330
1513
  low = first?.days ?? horizon;
1331
1514
  left = first?.left ?? stock;
1332
1515
  spanDays = first?.spanDays ?? horizon;
1516
+ spanAt = first?.spanAt ?? 0;
1333
1517
  final = stock;
1334
1518
  }
1335
1519
  const stores = resourcesOnly(left);
@@ -1341,7 +1525,7 @@ function enduranceOf(layer, caseCuts, options) {
1341
1525
  }
1342
1526
  const spoiled = Object.fromEntries([...lost].filter(([, amount]) => amount > 0));
1343
1527
  const runsLeft = (converter) => (daysLeft(left, converter) ?? 0) * (converter.perDay ?? 0);
1344
- const runsGiven = (converter) => (devices.get(deviceKey(converter)) ?? 1) * (converter.perDay ?? 0) * spanDays;
1528
+ const runsGiven = (converter) => (devices.get(deviceKey(converter)) ?? 1) * shareAt(deviceKey(converter), spanAt) * (converter.perDay ?? 0) * spanDays;
1345
1529
  const capped = atHorizon ? [] : limited.filter((converter) => reach.has(converter)).filter((converter) => runsLeft(converter) <= USED_UP * Math.max(1, runsGiven(converter))).filter((converter) => mostRuns(stores, makers, converter, NO_PATH) > USED_UP).map((converter) => converter.id).sort();
1346
1530
  needs[draw.need] = {
1347
1531
  days: low,
@@ -1385,10 +1569,11 @@ function sharesDevice(reach) {
1385
1569
  return false;
1386
1570
  }
1387
1571
  /**
1388
- * This function gives an upper limit of a need that the stores give with no other draw, through each chain.
1389
- * A converter that takes nothing gives nothing: the gate refuses it.
1572
+ * This function gives an upper limit of a need that the stores give with no other draw, through each chain. A source
1573
+ * (M2-solar) with a share more than 0 gives no upper limit, thus the limit is Infinity. The span of the sweep is finite.
1390
1574
  */
1391
- function capacity(start, ways) {
1575
+ function capacity(start, ways, shareAt, at = 0) {
1576
+ for (const converter of ways.open) if ((shareAt === void 0 ? 1 : shareAt(deviceKey(converter), at)) > 0) return 1e400;
1392
1577
  let total = 0;
1393
1578
  for (const use of ways.direct) total += mostResource(start, ways.makers, use.resource, NO_PATH) * use.amount;
1394
1579
  for (const converter of ways.converters) total += mostRuns(start, ways.makers, converter, NO_PATH) * converter.gives.amount;
@@ -1405,7 +1590,8 @@ function capacity(start, ways) {
1405
1590
  * - `endurance-part-unknown`: a supply, a device, the part of a store or a cut is not a part of the base stack.
1406
1591
  * - `endurance-range`: an amount, a rate, a count or a factor is not a finite number of 0 or more. Or a reach is not from 0 to 1.
1407
1592
  * - `endurance-date`: a date is not a date `YYYY-MM-DD`.
1408
- * - `endurance-converter-empty`: a converter takes nothing, or it gives an amount of 0.
1593
+ * - `endurance-converter-empty`: a converter takes nothing and has no limit `perDay` more than 0, or it gives an amount of 0.
1594
+ * A converter with a limit can take nothing: it is a source, for example a solar panel. Since 0.8.20 (M2-solar).
1409
1595
  * - `endurance-curve`: a survival curve does not start at `[0, 1]`, or it does not end at a chance of 0.
1410
1596
  * Or its days do not increase, or its chance increases.
1411
1597
  * - `endurance-bound`: the bound of a cut is not a value of `CUT_BOUNDS` of the core. Since 0.8.4.
@@ -1419,6 +1605,11 @@ function capacity(start, ways) {
1419
1605
  * - `endurance-season`: the season of a converter has a day that is not `MM-DD`. Or its delay or its shelf life is not more than 0.
1420
1606
  * Or its count of sowings is not a finite number of 0 or more. Or the converter with a season gives a need, or it has
1421
1607
  * `perDay` or `devices`. Since 0.8.16 (M2-grow).
1608
+ * - `endurance-monthly`: the shares `monthly` of a converter are not 12 numbers from 0 to 1. Or the converter has
1609
+ * `monthly` and no `perDay`, or a season. Or two converters of one device have different shares. Since 0.8.20 (M2-solar).
1610
+ * - `endurance-capacity`: the capacity of a store is not a finite number more than 0, or it is less than the amount.
1611
+ * Or a store with a capacity has a due date. Or a resource has a store with a capacity and a store with no capacity.
1612
+ * Since 0.8.21 (M2-solar-charge).
1422
1613
  */
1423
1614
  const ENDURANCE_CODES = [
1424
1615
  "endurance-twice",
@@ -1436,7 +1627,9 @@ const ENDURANCE_CODES = [
1436
1627
  "endurance-device-mix",
1437
1628
  "endurance-no-way",
1438
1629
  "endurance-cut-no-curve",
1439
- "endurance-season"
1630
+ "endurance-season",
1631
+ "endurance-monthly",
1632
+ "endurance-capacity"
1440
1633
  ];
1441
1634
  /**
1442
1635
  * This function gives each fault of an endurance layer. With a base stack, it also examines the `id` of each part. A list with no error tells that the gate passes.
@@ -1494,6 +1687,15 @@ function checkEndurance(layer, stack) {
1494
1687
  range(store.id, "amount", store.amount);
1495
1688
  if (store.due !== void 0) date(store.id, store.due);
1496
1689
  part(store.id, "part", store.part);
1690
+ if (store.capacity !== void 0) {
1691
+ if (!(Number.isFinite(store.capacity) && store.capacity > 0 && store.capacity >= store.amount)) add("error", "endurance-capacity", store.id, `The capacity of "${store.id}" is ${String(store.capacity)}. It must be a finite number more than 0, and not less than the amount.`);
1692
+ if (store.due !== void 0) add("error", "endurance-capacity", store.id, `The store "${store.id}" has a capacity and a due date. A store with a charge has no due date.`);
1693
+ }
1694
+ }
1695
+ for (const code of new Set(layer.stores.map((store) => store.resource))) {
1696
+ const all = layer.stores.filter((store) => store.resource === code);
1697
+ const charged = all.filter((store) => store.capacity !== void 0);
1698
+ if (charged.length > 0 && charged.length < all.length) add("error", "endurance-capacity", code, `The resource "${code}" has a store with a capacity and a store with no capacity. Its stores all have a capacity, or none has one.`);
1497
1699
  }
1498
1700
  const levels = new Set(layer.levels);
1499
1701
  for (const draw of layer.draws) {
@@ -1517,11 +1719,19 @@ function checkEndurance(layer, stack) {
1517
1719
  range(converter.id, "amount", converter.gives.amount);
1518
1720
  const made = giftResource(converter);
1519
1721
  if (made !== void 0) resource(converter.id, made);
1520
- if (!converter.takes.some((input) => input.amount > 0) || converter.gives.amount === 0) add("error", "endurance-converter-empty", converter.id, `The converter "${converter.id}" takes nothing, or it gives nothing.`);
1722
+ if (!(converter.perDay !== void 0 && converter.perDay > 0) && !converter.takes.some((input) => input.amount > 0) || converter.gives.amount === 0) add("error", "endurance-converter-empty", converter.id, `The converter "${converter.id}" takes nothing, or it gives nothing.`);
1521
1723
  part(converter.id, "device", converter.device);
1522
1724
  if (converter.perDay !== void 0) range(converter.id, "rate perDay", converter.perDay);
1523
1725
  if (converter.devices !== void 0 && !(Number.isSafeInteger(converter.devices) && converter.devices >= 1)) add("error", "endurance-range", converter.id, `The count of devices of "${converter.id}" is ${String(converter.devices)}. It must be a whole number of 1 or more.`);
1524
1726
  if (converter.season !== void 0) for (const message of seasonFaults(converter)) add("error", "endurance-season", converter.id, message);
1727
+ for (const message of monthlyFaults(converter)) add("error", "endurance-monthly", converter.id, message);
1728
+ }
1729
+ const shares = /* @__PURE__ */ new Map();
1730
+ for (const converter of converters) {
1731
+ if (converter.device === void 0 || converter.perDay === void 0) continue;
1732
+ const seen = shares.get(converter.device);
1733
+ if (seen === void 0) shares.set(converter.device, converter);
1734
+ else if (!sameShares(seen.monthly, converter.monthly)) add("error", "endurance-monthly", converter.id, `The converter "${converter.id}" and the converter "${seen.id}" of the device "${converter.device}" have different shares monthly. The converters of one device have the same shares.`);
1525
1735
  }
1526
1736
  const counts = /* @__PURE__ */ new Map();
1527
1737
  for (const converter of converters) {
@@ -1593,6 +1803,23 @@ function seasonFaults(converter) {
1593
1803
  if (converter.perDay !== void 0 || converter.devices !== void 0) out.push(`The converter "${converter.id}" has a season and a limit perDay or devices. A converter with a season has no such limit.`);
1594
1804
  return out;
1595
1805
  }
1806
+ /** This function gives the messages of the faults of the shares `monthly` of a converter (M2-solar). */
1807
+ function monthlyFaults(converter) {
1808
+ const monthly = converter.monthly;
1809
+ if (monthly === void 0) return [];
1810
+ const out = [];
1811
+ if (monthly.length !== 12 || !monthly.every((share) => Number.isFinite(share) && share >= 0 && share <= 1)) out.push(`The shares monthly of "${converter.id}" are not 12 numbers from 0 to 1.`);
1812
+ if (converter.perDay === void 0) out.push(`The converter "${converter.id}" has shares monthly and no limit perDay. The shares are parts of the limit.`);
1813
+ if (converter.season !== void 0) out.push(`The converter "${converter.id}" has shares monthly and a season. A converter has one or the other.`);
1814
+ return out;
1815
+ }
1816
+ /** This function tells if two lists of shares `monthly` are equal. No list is equal to a list of 12 ones. */
1817
+ function sameShares(a, b) {
1818
+ const full = (shares) => shares ?? Array.from({ length: 12 }, () => 1);
1819
+ const left = full(a);
1820
+ const right = full(b);
1821
+ return left.length === right.length && left.every((share, index) => share === right[index]);
1822
+ }
1596
1823
  /**
1597
1824
  * This function gives each cycle of resources: a resource is made from a resource that a converter makes from it. Each cycle is a list of codes.
1598
1825
  * A converter with a season is not part of a cycle: its harvest comes after its sowing, thus a chain of it ends.
@@ -1675,6 +1902,6 @@ function joinEndurance(pieces) {
1675
1902
  }
1676
1903
  };
1677
1904
  }
1678
- export { addTo as A, Program as C, fewestRow as D, drawBlock as E, exact as M, reachOf as O, seasonPlan as S, deviceUse as T, fewestOpen as _, hasEnduranceError as a, seasonBest as b, caseStopped as c, giftNeed as d, giftResource as f, storeAmounts as g, sowDays as h, curveFault as i, dense as j, valueAt as k, drawDemand as l, seasonSupply as m, ENDURANCE_CODES as n, SEASON_HORIZON_DAYS as o, hasSeason as p, checkEndurance as r, caseDraws as s, joinEndurance as t, enduranceOf as u, firstStop as v, caseParts as w, seasonJoint as x, fixStops as y };
1905
+ export { deviceUse as A, fixStops as C, storeCapacities as D, seasonPlan as E, addTo as F, dense as I, exact as L, fewestRow as M, reachOf as N, Program as O, valueAt as P, isSource as R, firstStop as S, seasonJoint as T, seasonSupply as _, hasEnduranceError as a, storeAmounts as b, caseStopped as c, giftNeed as d, giftResource as f, monthlyShares as g, monthOf as h, curveFault as i, drawBlock as j, caseParts as k, drawDemand as l, monthEdges as m, ENDURANCE_CODES as n, SEASON_HORIZON_DAYS as o, hasSeason as p, checkEndurance as r, caseDraws as s, joinEndurance as t, enduranceOf as u, shareOf as v, seasonBest as w, fewestOpen as x, sowDays as y };
1679
1906
 
1680
- //# sourceMappingURL=join-Du2gsJJ6.js.map
1907
+ //# sourceMappingURL=join-CM_zwI78.js.map