Difference between revisions of "Action2/Industries"

From GRFSpecs
Jump to navigationJump to search
m (7 revisions)
(Updated table syntax)
Line 12: Line 12:
 
The production callback is handled in an action 2 chain.  Typically there will be a sub-chain of multiple varaction 2s for logic before the final varaction 2 handling the production cb.  The ID of the first varaction 2 in the subchain should be handled explicitly by a check for production cb using var 0C.  Do not rely on handling production cb sub-chain as default ID for the varaction 2 checking 0C, this can have unexpected results.  However the ID of the varaction 2 handling the production cb itself should be the default ID for the 0C check.  Makes sense?
 
The production callback is handled in an action 2 chain.  Typically there will be a sub-chain of multiple varaction 2s for logic before the final varaction 2 handling the production cb.  The ID of the first varaction 2 in the subchain should be handled explicitly by a check for production cb using var 0C.  Do not rely on handling production cb sub-chain as default ID for the varaction 2 checking 0C, this can have unexpected results.  However the ID of the varaction 2 handling the production cb itself should be the default ID for the 0C check.  Makes sense?
   
-=Format=-
+
==Format==
   
 
For the production callback, the action 2 data looks as follows:
 
For the production callback, the action 2 data looks as follows:
Line 18: Line 18:
 
Version 0:
 
Version 0:
   
<pre> -+&lt;Sprite-number&gt; * &lt;Length&gt; 02 0A &lt;set-id&gt; &lt;version&gt; &lt;subtract-in-1&gt; &lt;subtract-in-2&gt; &lt;subtract-in-3&gt; &lt;add-out-1&gt; &lt;add-out-2&gt; &lt;again&gt;+- </pre>
+
<pre> &lt;Sprite-number&gt; * &lt;Length&gt; 02 0A &lt;set-id&gt; &lt;version&gt; &lt;subtract-in-1&gt; &lt;subtract-in-2&gt; &lt;subtract-in-3&gt; &lt;add-out-1&gt; &lt;add-out-2&gt; &lt;again&gt; </pre>
   
  +
{| class="wikitable
||'''Element'''|[[GRFActionsDetailed|'''Size''']]|'''Description'''
 
  +
|-
 
  +
!'''Element'''
&lt;Sprite-number&gt;|dec|A sequential sprite number
 
  +
![[GRFActionsDetailed|'''Size''']]
 
  +
!'''Description'''
&lt;length&gt;|dec|The total number of bytes used in this action
 
  +
|-
 
  +
|&lt;Sprite-number&gt;
02|B|Defines action 02
 
  +
|dec
 
  +
|A sequential sprite number
0A|B|feature 0A=industries
 
  +
|-
 
  +
|&lt;length&gt;
&lt;set-id&gt;|B|ID for this definition
 
  +
|dec
 
  +
|The total number of bytes used in this action
&lt;version&gt;|B|version of the production-callback format, 00
 
  +
|-
 
  +
|02
&lt;subtract-in-1..3&gt;|W|Amounts to be subtracted from the incoming cargoes waiting to be processed. (Signed)
 
  +
|B
 
  +
|Defines action 02
&lt;add-out-1/2&gt;|W|Amounts to be added to the produced cargoes. (Unsigned)
 
  +
|-
 
  +
|0A
&lt;again&gt;|B|(before 2.0.1 alpha 73) Repeat callback if 01, do not repeat if 00 (after 2.0.1 alpha 73) Repeat callback if nonzero, do not repeat if 00||
 
  +
|B
  +
|feature 0A=industries
  +
|-
  +
|&lt;set-id&gt;
  +
|B
  +
|ID for this definition
  +
|-
  +
|&lt;version&gt;
  +
|B
  +
|version of the production-callback format, 00
  +
|-
  +
|&lt;subtract-in-1..3&gt;
  +
|W
  +
|Amounts to be subtracted from the incoming cargoes waiting to be processed. (Signed)
  +
|-
  +
|&lt;add-out-1/2&gt;
  +
|W
  +
|Amounts to be added to the produced cargoes. (Unsigned)
  +
|-
  +
|&lt;again&gt;
  +
|B
  +
|(before 2.0.1 alpha 73) Repeat callback if 01, do not repeat if 00 (after 2.0.1 alpha 73) Repeat callback if nonzero, do not repeat if 00
  +
|}
   
 
Version 1 (available since r1307):
 
Version 1 (available since r1307):
   
<pre> -+&lt;Sprite-number&gt; * &lt;Length&gt; 02 0A &lt;set-id&gt; &lt;version&gt; &lt;subtract-in-1&gt; &lt;subtract-in-2&gt; &lt;subtract-in-3&gt; &lt;add-out-1&gt; &lt;add-out-2&gt; &lt;again&gt;+- </pre>
+
<pre> &lt;Sprite-number&gt; * &lt;Length&gt; 02 0A &lt;set-id&gt; &lt;version&gt; &lt;subtract-in-1&gt; &lt;subtract-in-2&gt; &lt;subtract-in-3&gt; &lt;add-out-1&gt; &lt;add-out-2&gt; &lt;again&gt; </pre>
 
||'''Element'''|[[GRFActionsDetailed|'''Size''']]|'''Description'''
 
 
&lt;Sprite-number&gt;|dec|A sequential sprite number
 
 
&lt;length&gt;|dec|The total number of bytes used in this action
 
 
02|B|Defines action 02
 
 
0A|B|feature 0A=industries
 
 
&lt;set-id&gt;|B|ID for this definition
 
 
&lt;version&gt;|B|version of the production-callback format, 01
 
 
&lt;subtract-in-1..3&gt;|B|Numbers of registers that contain the amounts to be subtracted from the incoming cargoes waiting to be processed.
 
   
&lt;add-out-1/2&gt;|B|Numbers of registers that hold the amounts to be added to the produced cargoes.
 
   
  +
{| class="wikitable"
&lt;again&gt;|B|Number of &lt;again&gt; register. Repeat callback if the value of the register &nbsp;isn't zero, do not repeat otherwise||
 
  +
|-
  +
!'''Element'''
  +
![[GRFActionsDetailed|'''Size''']]
  +
!'''Description'''
  +
|-
  +
|&lt;Sprite-number&gt;
  +
|dec
  +
|A sequential sprite number
  +
|-
  +
|&lt;length&gt;
  +
|dec
  +
|The total number of bytes used in this action
  +
|-
  +
|02
  +
|B
  +
|Defines action 02
  +
|-
  +
|0A
  +
|B
  +
|feature 0A=industries
  +
|-
  +
|&lt;set-id&gt;
  +
|B
  +
|ID for this definition
  +
|-
  +
|&lt;version&gt;
  +
|B
  +
|version of the production-callback format, 01
  +
|-
  +
|&lt;subtract-in-1..3&gt;
  +
|B
  +
|Numbers of registers that contain the amounts to be subtracted from the incoming cargoes waiting to be processed.
  +
|-
  +
|&lt;add-out-1/2&gt;
  +
|B
  +
|Numbers of registers that hold the amounts to be added to the produced cargoes.
  +
|-
  +
|&lt;again&gt;
  +
|B
  +
|Number of &lt;again&gt; register. Repeat callback if the value of the register &nbsp;isn't zero, do not repeat otherwise
  +
|}
   
-=Details=-
+
==Details==
   
==Subtract-in-1..3==
+
===Subtract-in-1..3===
   
 
The total amount of cargo waiting cannot go negative. &nbsp;If you specify more than the amount actually waiting, the incoming amount will be zeroed instead. You can use negative subtract-in values to increase the amount of cargo waiting.
 
The total amount of cargo waiting cannot go negative. &nbsp;If you specify more than the amount actually waiting, the incoming amount will be zeroed instead. You can use negative subtract-in values to increase the amount of cargo waiting.

Revision as of 12:26, 14 June 2011

Defining the industry production callback

Industry Production Callback

Defining the industry production callback

Industries don't have graphics directly associated with them (they have graphics through their tiles), so they can't have a traditional action 2. However, they still can use action 2 for a special purpose: production callbacks. A production callback can define exactly how an industry is producing cargo, but this needs more than the 15 bits a normal callback can return.

When an industry has bits 1 or 2 set in property 21, its production is changed. Instead of processing the incoming cargo instantly, it remembers how many units are waiting, and uses the production callback mechanism to decide how much cargo must be produced.

The production callback is handled in an action 2 chain.  Typically there will be a sub-chain of multiple varaction 2s for logic before the final varaction 2 handling the production cb.  The ID of the first varaction 2 in the subchain should be handled explicitly by a check for production cb using var 0C.  Do not rely on handling production cb sub-chain as default ID for the varaction 2 checking 0C, this can have unexpected results.  However the ID of the varaction 2 handling the production cb itself should be the default ID for the 0C check.  Makes sense?

Format

For the production callback, the action 2 data looks as follows:

Version 0:

 <Sprite-number> * <Length> 02 0A <set-id> <version> <subtract-in-1> <subtract-in-2> <subtract-in-3> <add-out-1> <add-out-2> <again> 
Element Size Description
<Sprite-number> dec A sequential sprite number
<length> dec The total number of bytes used in this action
02 B Defines action 02
0A B feature 0A=industries
<set-id> B ID for this definition
<version> B version of the production-callback format, 00
<subtract-in-1..3> W Amounts to be subtracted from the incoming cargoes waiting to be processed. (Signed)
<add-out-1/2> W Amounts to be added to the produced cargoes. (Unsigned)
<again> B (before 2.0.1 alpha 73) Repeat callback if 01, do not repeat if 00 (after 2.0.1 alpha 73) Repeat callback if nonzero, do not repeat if 00

Version 1 (available since r1307):

 <Sprite-number> * <Length> 02 0A <set-id> <version> <subtract-in-1> <subtract-in-2> <subtract-in-3> <add-out-1> <add-out-2> <again> 


Element Size Description
<Sprite-number> dec A sequential sprite number
<length> dec The total number of bytes used in this action
02 B Defines action 02
0A B feature 0A=industries
<set-id> B ID for this definition
<version> B version of the production-callback format, 01
<subtract-in-1..3> B Numbers of registers that contain the amounts to be subtracted from the incoming cargoes waiting to be processed.
<add-out-1/2> B Numbers of registers that hold the amounts to be added to the produced cargoes.
<again> B Number of <again> register. Repeat callback if the value of the register  isn't zero, do not repeat otherwise

Details

Subtract-in-1..3

The total amount of cargo waiting cannot go negative.  If you specify more than the amount actually waiting, the incoming amount will be zeroed instead. You can use negative subtract-in values to increase the amount of cargo waiting.

During this callback, the lowest byte of variable 18 contains the reason the callback is called: 0 if it was called because of incoming cargo, 1 if it was called in the periodic processing. Bits 8-23 contain how many times the callback was already called in this loop, i.e. 0 for the first call, 1 for the second etc.)

For TTDPatch 2.0.1 alpha 73 and above, bits 24-31 of variable 18 contain the <again> value of the previous call (the lowest byte only in case of version 1), or 0 if this is the first call. This can be used to have state information during the loop since you can pass information to the next iteration.

Since TTDPatch 2.5 beta 5, you can request some random bits in variable 10 by setting bit 15 of property 1A. These bits won't be re-randomized when you call the callback again by setting a non-zero "again" value; they're randomized only for the first call.

Using the version 1 format, aviailable since TTDPatch 2.6 r1307, you can decide the instructions dynamically. Instead of constants, you give register numbers that contain the needed values; you can compute these values earlier in the action2 chain. Registers are 4 bytes long and are considered to be signed. Negative add-out values don't make sense and are ignored. The results are clamped to the allowed range (0..65535 for both incoming and outgoing cargo amounts).