# Moe - MFF-Berater - Rekursive Baumereiproduktberechnung

a guest
Jun 13th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. // Replaces the existing calcTotalrecursive-Method
2. function calcTotalRecursive() {
3.     var type, id, req, i, amountMissingProducts, j;
4.     try {
5.         if (DEVMODE_FUNCTION) {
6.             var trackingHandle = tracking.start("berater", "calcTotalRecursive", []);
7.         }
8.
9.         totalRecursive = new Array(new Object(), new Object(), new Object(), new Object());
10.         // Type is set to '1', so only forestry products are inspected
11.         type = 1;
12.
13.         // Iterate over all products starting with those which are no pre-product. It is important that an inspected product won't be needed by a non-yet-inspected product!
14.         for (i = prodNameSort[type].length - 1; i >= 0; i--) {
15.             // Id of currently inspected product
16.             id = prodNameSort[type][i];
17.
18.             /** Calculate the amount of missing products:
19.              *  - Amount of directly needed product and currently in production in 'prodMinRack'
20.              *  - Amount of recursivly needed product in 'totalRecursive'
21.              *  - Amount of already produced/existing product in stock in 'ProdStock'
22.              */
23.             amountMissingProducts = prodMinRack[type][id] + (totalRecursive[type][id] ? totalRecursive[type][id] : 0) - prodStock[type][id];
24.
25.             // If there is a need to gain this product...
26.             if (amountMissingProducts > 0) {
27.                 // If this product requires other products... (e.g. tree logs DON'T!)
28.                 if (prodRequire[type][id]) {
29.                     // Iterate over pre-products
30.                     for (j = 0; j < prodRequire[type][id].length; j++) {
31.                         // Cache the currently inspected pre-product
32.                         req = prodRequire[type][id][j];
33.                         if (!totalRecursive[req[0]][req[1]]) {
34.                             // Initialize result storage of pre-product, if necessary
35.                             totalRecursive[req[0]][req[1]] = 0;
36.                         }
37.
38.                         /** Some real magic is done here: We calculate, how many pre-products are recursivly needed!
39.                          *  We know the amount to produce ('amountMissingProducts') and divide it by the yielded amount per production
40.                          *  cycle ('prodYield'). Then we (should) multiply with the amount of needed pre-product ('req[2]') to initiate
41.                          *  the production. Finally, we round up, since we can't produce fractions ('Math.ceil()').
42.                          *  If the production needs more than one unit of the pre-product, we need to round up to a multiple of 'req[2]'.
43.                          *  The formula therefore is 'Math.ceil(x / req[2]) * req[2]'. We then reduce 'req[2]' inside the ceiling-function.
44.                          */
45.                         totalRecursive[req[0]][req[1]] += Math.ceil((amountMissingProducts) / prodYield[type][id]) * req[2]; // Anzahl rekursiv benötigte Stämme = (direkt + rekursiv benötigte Bretter) / Bretter je Stamm
46.                     }
47.                 }
48.             }
49.         }
50.         if (DEVMODE_FUNCTION) {
51.             tracking.end("berater", trackingHandle);
52.         }
53.     } catch (err) {
54.         GM_logError("calcTotalRecursive\ntype=" + type + " id=" + id + " req=" + implode(req) + " i=" + i + " j=" + j + "\n" + err);
55.     }
56. }
57.
58. // In 'function calcProdMinRack(caller)', the call of calcTotalRecursive must be fitted to the new signiture
59. if (valMinRackRecursive) {
60.     calcTotalRecursive(); // recursive need products calculation
61.     // Some iteration over variable totalRecursive to adjust prodMinRack
62. }
63.
64. // German menu-item translation in function startScript()
65. texte["de"]["settings_valMinRackRecursive"] = ["Rekursive Produkte", "Berechne die rekursiv benötigten Produkte (in der Baumerei) und addiere sie zur Liste der (direkt) benötigten Produkte hinzu."];