Template:Craft raw/doc

Internal template for Template:Craft that computes total raw resource requirements for items constructed by the Advanced Constructor. This template calls Template:Craft raw recursive to perform the actual calculations, the details of which can be found at Template:Craft raw recursive/doc.

This template performs 3 main operations in sequence:


 * 1) obtain the "raw resource string" from Template:Craft raw recursive
 * 2) compute the total for each raw resource from the "raw resource string"
 * 3) select only the raw resources that are used and format them for display in a table cell

Obtain "raw resource string"
The template begins with a check to see that  has something in it; although not strictly necessary for the proper functioning of the template, without it the page may fail to load when doing edits.

Next, we define a variable called, which will hold the "raw resource string" once it's generated. Ignoring the  call for the moment, we have a   call that looks at a page called. Note that  is set to "no", since usually this template will be called on the page. The entire purpose of the  is to give all the arguments that would normally go to Template:Craft, to Template:Craft raw recursive instead. The resulting output from Template:Craft raw recursive will always have an extra "+" before the last ")", so the  removes it.

Compute raw resource totals
Here we have a variable called  which holds a bunch of regular expressions for each of the raw resources. If more raw resources are added in the future, then another entry of the form  needs to be added after the last entry, where   is the exact name of the raw resource (in addition, Template:Craft raw recursive will have to be modified, but those modifications are discussed in its documentation).

Each raw resource has a variable defined for it in a similar fashion as the example for "Iron Ore" above. Admittedly, the nested regular expressions are difficult to interpret, so here they are broken down in order ("..." indicates previously discussed code):


 * 1)  invokes the variable   we defined above.
 * 2)  removes the text   from  . Note that a lot of the characters must be escaped with "\", as we need them to be interpreted literally by the regular expression parser.
 * 3)  takes the previous result and inserts   between the regular expressions for each raw resource. Again, the parentheses have to be escaped in order for the matching to work. This step is necessary to separate each regular expression so that each can be matched separately in the next step
 * 4)  uses the previous result as the regular expression to operate on , setting all raw resources to 0 other than "Iron Ore".
 * 5)  removes instances of the desired raw resource name ("Iron Ore" in this case); only the associated number remains.
 * 6)  wraps the previous result in the   function and then evaluates it. The multiplication and division by 5 is needed because the recipe for converting raw "Iron Ore" into "Iron Ingot" requires 5 "Iron Ore"; in principle, it would be nice to not have this be hard-coded . ..

The result of the above steps is stored into the variable, and is repeated for each of the different raw resource types. Note that only one of some raw resources, like "Promethium Ore" and "Corn", is required for their refining recipes, so the multiplication and division by 5 is omitted for those.

Select and display raw resources
Now it's time to format the raw resource totals for display. Ignoring the  calls for the moment, the variables for each raw resource are tested to see if they are nonzero, and if so, are displayed with a linebreak. The  calls simply serve to remove extraneous new line characters and the extra linebreak that occurs at the beginning of the output. This is the final result returned by this template, and is suitable for insertion into a table cell (which is exactly what Template:Craft does with it).