Empyrion: Galactic Survival Wiki
Template-info.png Documentation

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"

|include={Craft{{!}}Craft raw recursive}

The template begins with a check to see that name 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 ingredients, which will hold the "raw resource string" once it's generated. Ignoring the dplreplace call for the moment, we have a dpl call that looks at a page called name. Note that skipthispage is set to "no", since usually this template will be called on the page name. The entire purpose of the dpl 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 dplreplace removes it.

Compute raw resource totals

{{#vardefine:allraw|([0-9]+#Iron Ore[&$])([0-9]+#Copper Ore[&$])([0-9]+#Cobalt Ore[&$])([0-9]+#Silicon Ore[&$])([0-9]+#Neodymium Ore[&$])([0-9]+#Promethium Ore[&$])([0-9]+#Magnesium Ore[&$])([0-9]+#Crushed Stone[&$])([0-9]+#Sathium Ore[&$])([0-9]+#Erestrum Ore[&$])([0-9]+#Zascosium Ore[&$])([0-9]+#Corn[&$])}}

Here we have a variable called allraw 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 ([0-9]+#<resource name>[&$]) needs to be added after the last entry, where <resource name> 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).

{{#vardefine:iron|{{#expr:5*ceil({{#dplreplace:{{#dplreplace:{{#var:ingredients}}|{{#dplreplace:{{#dplreplace:{{#var:allraw}}|\(\[0-9\]\+#Iron Ore\[&\$\]\)|}}|\)\(|){{!}}(}}|0}}|#Iron Ore[&$]|}}/5)}}}}

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. {{#var:allraw}} invokes the variable allraw we defined above.
  2. {{#dplreplace:{{#var:allraw}}|\(\[0-9\]\+#Iron Ore\[&\$\]\)|}} removes the text ([0-9]+#Iron Ore[&$]) from allraw. 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. {{#dplreplace:...|\)\(|){{!}}(}} 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. {{#dplreplace:{{#var:ingredients}}|...|0}} uses the previous result as the regular expression to operate on ingredients, setting all raw resources to 0 other than "Iron Ore".
  5. {{#dplreplace:...|#Iron Ore[&$]|}} removes instances of the desired raw resource name ("Iron Ore" in this case); only the associated number remains.
  6. {{#expr:5*ceil(.../5)}} wraps the previous result in the ceil() 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 iron, 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

{{#ifeq:{{#var:iron}}|0||<br>{{item link|Iron Ore}} ({{#var:iron}})}}

Now it's time to format the raw resource totals for display. Ignoring the dplreplace 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 dplreplace 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).

This is the documentation page, it should be transcluded into the main template page. See Template:Doc for more information.