Source

firs / templates / template_accept_only_action23_A.pnfo

The default branch has multiple heads

Full commit
// Action2/3-chain for cargo processing
// Use for an 'accept only' industry like Power Station
// includes closing behaviour - these industries normally need regular deliveries

// How to use:
// Add to the top of a primary industry a few #define lines. For example:
// #define THIS_INDUSTRY         IND_POWERSTATION
// #define THIS_STR_INFO_CB38    STR_INFO_GENERIC
// #define THIS_STR_INFO         STR_INFO_POWERSTATION_0
// #define THIS_STR_PRODUCING    STR_INFO_POWERSTATION_1
// #define THIS_STR_RESERVES     STR_INFO_POWERSTATION_2
// #define THIS_STR_CLOSURE      STR_INFO_POWERSTATION_3

// Optional: #define THIS_HANDLE_CB28 [Action 2 ID]  - make sure Action 2 chain is also present in industry, above template include.

// Local defines - to improve readability
// Not everything can be defined due to some shenanigans with dwords etc.  So some values are still hardcoded in action 2s. 
#define LOCAL_DELIVERY_TICKER_STORE         00
#define LOCAL_CLOSURE_SPREAD_TICKER_STORE   01
#define LOCAL_CLOSURE_SPREAD_LIMIT_STORE    02
#define LOCAL_INDUSTRY_COUNT_STORE_1        03
#define LOCAL_INDUSTRY_COUNT_STORE_2        04
#define LOCAL_PROTECTION_OFFSET_STORE       05

// then add as the 2nd last line, prior to the end of the climate avialability label
// #include "../../templates/template_primary_action23.pnfo"

// In principle you can also add the defines immediately above this #define line...

// Preferred action 2 ID ranges, to keep life simple...
//   production code	00-0F, A0-AF
//   cargo acceptance	B0-BF
//   misc branching, handle callbacks etc	C0-CF
//   monthly / random production change	D0-DF
//   texts	E0-FF
//   industry location permissibility (CB 28)  10-1F  (NB CB 28 is not templated - handled on a per-industry basis)
//   industry availability (CB 22) 20
//   generally unused and available	2A-9F


// production code
	
-1 * 0 02 0A A3 //production callback with consumption (9999 should clear most amounts - possibly could replace with an accurate amount in a register)
	00 //version
	\w9999 \w9999 \w00 //<subtract-in-1..3>
	\w00 \w00 //<add-out-1..2>
	00 //don't repeat

-1 * 0 02 0A A2 //case some cargo waiting
  89 // 
    1A 20  \d00 //set 0 for 'delivery ticker'
    10 1A 20 \dx00 //store in persistent storage
    0F // renum gets sad about passing 0 to the range check, so create 1 and pass that instead <roll>   
    1A 00 \d01 //create value 1
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
    A3 00  \d00  \d00 
  A3 00 //default

-1 * 0 02 0A A1 //
  89 // 
    41 00  \dxFFFFFFFF //cargo 2 waiting 
  \b1 //range
    A3 00  \d00  \d00 
  A2 00 //some cargo waiting

-1 * 0 02 0A A0 //
  89 // 
    40 00  \dxFFFFFFFF //cargo 1 waiting 
  \b1 //range
    A1 00  \d00  \d00 
  A2 00 //some cargo waiting


// production changes  
-1 * 0 02 0A D8 //case protection period has ended and closure spread limit reached - high random chance of closure  
  81 18 00  0F //randomise 
  \b1 //range
    03 80 00 03 //lookat cb 35 or 29 in wiki to see what the returned value means
  00 80 //else no change	

// suppress warning about nonexistent variable (trips up on var 5F)
//@@WARNING DISABLE 86
-1 * 0 02 0A D7 //case we've reached the spread ticker limit - reset the spread ticker and spread ticker limit. 
  89 // 
    1A 20  \d00 //create value 0
    10  1A 20 \dx01 // store in persistent storage
    0F
    18 20 \dx02 //access some random bits
    \2+ 1A 20 \d01 //add 1 to handle possibility that previous random may be 0
    \2* 5F 28 \dx0F //multiply by this industry's random bits
    \2+ 1A 20 \d01 //add 1 again in case result of multiplying both randoms is 0
    10  1A 00 \dx02 // store in persistent storage
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
     D8 00  \d00  \d00 //
  D8 00 //limit reached
// enable warning about nonexistent variable
//@@WARNING ENABLE 86

-1 * 0 02 0A D6 //case we have not reached the spread ticker limit - increment closure spread ticker, then return cb result
  89 // 
      1A 20  \d01 //create value 1
      \2+ 7C LOCAL_CLOSURE_SPREAD_TICKER_STORE 20  \dxFFFFFFFF //add to 'closure spread ticker' store   
      10  1A 00 \dx01 // store in persistent storage
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
     00 80  \d00  \d00 //lookat cb 35 or 29 in wiki to see what the returned value means
  00 80 //

-1 * 0 02 0A D5 //case industry count same or more than last month - now check if closure spread limit reached
  89 // 
    7C LOCAL_CLOSURE_SPREAD_TICKER_STORE 20  \dxFFFFFFFF //
    12 7C LOCAL_CLOSURE_SPREAD_LIMIT_STORE 00  \dxFFFFFFFF//compare ticker with limit
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
     D7 00  \d01  \d02 //if result=1, we're at the limit, if 2 we're over the limit (somehow!)
  D6 00 //default
  
-1 * 0 02 0A D4 //case industry count less than last month - decrement closure spread ticker to add delay, then return cb result
  89 // 
      1A 20  \d09 //create value 9 (minimum delay in months between industry closure)
      \2- 7C LOCAL_CLOSURE_SPREAD_TICKER_STORE 20  \dxFFFFFFFF //decrement 'closure spread ticker' store
      \2> 1A 20 \d00 //take maximum of (previous result, 0)...to avoid negative value for closure spread ticker 
      10  1A 00 \dx01 //store in persistent storage
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
     00 80  \d00  \d00 //lookat cb 35 or 29 in wiki to see what the returned value means
  00 80 //  
  
-1 * 0 02 0A D3 //case protection limit reached - now check if other industries of this type closed last month 
  89 // 
    7C LOCAL_INDUSTRY_COUNT_STORE_1 20  \dxFFFFFFFF //get stored count of industries this month
    12 7C LOCAL_INDUSTRY_COUNT_STORE_2 00  \dxFFFFFFFF //compare industries this month with industries last month
  \b1 //range
     D5 00  \d01  \d02 //if result=1 industry count same, if 2 we have more industries now than last month
  D4 00 //otherwise assume result=0 so fewer industries now than last month

-1 * 0 02 0A D2 //case protection limit not reached - increment delivery (protection) ticker
  89 // 
      1A 20  \d01 //create value 1
      \2+ 7C 00 20  \dxFFFFFFFF //add to 'delivery ticker' store   
      10  1A 00 \dx00 // store in persistent storage
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
    00 80  \d00  \d00  //return result to cb - lookat cb 35 or 29 in wiki to see what the returned value means
  00 80 // default

-1 * 0 02 0A D1 //handle cb 35 monthly production change 
  89 // 
    7C LOCAL_DELIVERY_TICKER_STORE 20  \dxFFFFFFFF //get value of delivery (protection) ticker (months since most recent delivery)
    \2- 7C LOCAL_PROTECTION_OFFSET_STORE 20 \dxFFFFFFFF //subtract a stored pseudo-random offset - this is used to spread initial protection periods
    \2> 1A 00 \d00 //take maximum of (previous result, 0)...to avoid possible hassle with negative value  
  \b1 //range
     D3 00  THIS_NUM_MONTHS_CLOSURE_PROTECTED  THIS_NUM_MONTHS_CLOSURE_PROTECTED //we've reached the protection limit
  D2 00 //default

-1 * 0 02 0A D0 //industry counts: rotate storage for last count, get new count and put into storage  
  89
    7C LOCAL_INDUSTRY_COUNT_STORE_1 20  \dxFFFFFFFF //rotate stored counts so previous count is in LOCAL_INDUSTRY_COUNT_STORE_2
    10  1A 20 \dx04 // store in persistent storage
    0F 
    1A 20  \dxFFFFFFFF //create grfid (FFFFFFFF = 'this' grf)
    \2sto 1A 20  \dx100 //store grfid in register 100h
    0F
    67 THIS_INDUSTRY  30  FF 00 00 00 //get industry count for type THIS_INDUSTRY
    10  1A 00 \dx03 // store in persistent storage (LOCAL_INDUSTRY_COUNT_STORE_1)
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
    D1 00 \d00 \d00 //
  D1 00 //default	
  

// industry window texts	
-1 * 0 02 0A F0 //calculate number months remaining before closure protection ends - store for display  
    89 // 
      1A 20 THIS_NUM_MONTHS_CLOSURE_PROTECTED //create a value (constant 1A anded (20) with following bytes)
      \2sto 1A 20 \dx101 // store to text stack
      \2- 7C LOCAL_DELIVERY_TICKER_STORE 20  \dxFFFFFFFF //get value of ticker for months since most recent delivery
      \2sto 1a 00 \dx100 // store to text stack
    \b2 //range
       THIS_STR_INFO 80 \d02 THIS_NUM_MONTHS_CLOSURE_PROTECTED //branch as we need to put a different value on the text stack
       THIS_STR_INFO_WARN_1 80 \d01 \d01 //show warning text
     THIS_STR_INFO_WARN_2 80 //default



// handle cb 22 industry availability
#include "industry_availability_check.pnfo" 

//handle cb 38 additional text industry fund window
#include "industry_fundwindow_text.pnfo"



// handle callbacks
-1 * 0 02 0A C3
	85 0C //<type> <variable> word-access callback
	00 FF FF //no shift, no mask
	\b7 //number of ranges to check
    A0 00   00 00   00 00 //production cb
	  #ifdef THIS_HANDLE_CB28
    THIS_HANDLE_CB28   28 00   28 00 //can industry be built? ...relies on action 2 with correct ID defined in industry pnfo
    #endif
    #ifndef THIS_HANDLE_CB28
    00 84   28 00   28 00 //just allow the industry to be built (this industry doesn't care about location)
    #endif
    20 00   22 00   22 00 //industry availability cb 22
    22 00   38 00   38 00 //cb 38 display additional text in fund window
	  00 80   29 00   29 00 //random production change cb 29 // 00 80 = do nothing
	  D0 00   35 00   35 00 //monthly production change cb 35
    F0 00   3A 00   3A 00 //return text-ID D0xx if cb 3A (industry window)
	A1 00 //default to production cb (needed so that we fail unknown cbs with unintended results)

-1 * 0 02 0A C2 //use this to initialise variables // !! seems to blow up if industry not built
  89 // 
    1A 20 \d01 //create 01
    //1C 20 \dxFFFFFFFF //get result of previous varaction 2 // commented out for power station
    10  1A 00 \dx05 // store in persistent storage
  \b1 //range (stay out of the nvar=0 bear pit by checking one range, but returning same for both)
    C3 00  \d00  \d00  //
  C3 00 // default

// industry action 3 attach varaction2 chain to industry
-1 * 0 03 0A 01 THIS_INDUSTRY 00 \wxC3 //-1 * 0 03 09 <n-ids> <id> 00 <def-cid>	

#undef LOCAL_DELIVERY_TICKER_STORE
#undef LOCAL_CLOSURE_SPREAD_TICKER_STORE
#undef LOCAL_CLOSURE_SPREAD_LIMIT_STORE
#undef LOCAL_INDUSTRY_COUNT_STORE_1
#undef LOCAL_INDUSTRY_COUNT_STORE_2

#undef THIS_INDUSTRY
#undef THIS_STR_INFO_CB38
#undef THIS_HANDLE_CB28
#undef THIS_STR_INFO
#undef THIS_STR_INFO_WARN_1
#undef THIS_STR_INFO_WARN_2
#undef THIS_INTRO_DATE
#undef THIS_NUM_MONTHS_CLOSURE_PROTECTED