src/fsmblockmap.h
author richk
Tue, 17 Jun 2008 13:41:57 +0000
branchNewGRF_ports
changeset 10995 311b38c7f9a7
parent 6845 666878d09b80
permissions -rw-r--r--
(svn r13549) [NewGRF_ports] -Change: Make recolouring of groundtile (0x0f80) specific to NewGRF_ports only.
Also base groundsprite on airport_tile of station. This prevents mixed colour groundtiles in an airport.
6751
08176dfe64b7 (svn r10790) [NewGRF_ports] -Codechange: constify the fsmblockmaps where possible.
rubidium
parents: 6739
diff changeset
     1
/* $Id$ */
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     2
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     3
/** @file FSMblockmap.h Definition of FSMblockmap class for use in state machines*/
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     4
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     5
#ifndef FSMBLOCKMAP_H
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     6
#define FSMBLOCKMAP_H
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     7
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     8
#include "stdafx.h"
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
     9
6751
08176dfe64b7 (svn r10790) [NewGRF_ports] -Codechange: constify the fsmblockmaps where possible.
rubidium
parents: 6739
diff changeset
    10
uint64 inline GetBlockAsBits(byte blocknumber)
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    11
{
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    12
	if (blocknumber == 0) return 0ULL;
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    13
	if (blocknumber > 63) blocknumber -= 64;
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    14
	uint64 result = 1ULL << blocknumber;
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    15
	return result;
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    16
}
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    17
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    18
/**
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    19
 * This class provides functions and internal storage for blockmaps used in Finite State Machine
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    20
 *
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    21
*/
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    22
struct FSMblockmap {
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    23
public:
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    24
	void inline ResetAll()
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    25
	{
6783
cb68d9c92570 (svn r10854) [NewGRF_ports] -Fix: GCC wants that you specify that a constant is uint64 (unsigned long long).
rubidium
parents: 6782
diff changeset
    26
		blocks[0] = 0x0000000000000000ULL;
cb68d9c92570 (svn r10854) [NewGRF_ports] -Fix: GCC wants that you specify that a constant is uint64 (unsigned long long).
rubidium
parents: 6782
diff changeset
    27
		blocks[1] = 0x0000000000000000ULL;
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    28
	};
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    29
6782
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    30
	void inline SetAll()
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    31
	{
6783
cb68d9c92570 (svn r10854) [NewGRF_ports] -Fix: GCC wants that you specify that a constant is uint64 (unsigned long long).
rubidium
parents: 6782
diff changeset
    32
		blocks[0] = 0xFFFFFFFFFFFFFFFFULL;
cb68d9c92570 (svn r10854) [NewGRF_ports] -Fix: GCC wants that you specify that a constant is uint64 (unsigned long long).
rubidium
parents: 6782
diff changeset
    33
		blocks[1] = 0xFFFFFFFFFFFFFFFFULL;
6782
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    34
	};
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    35
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    36
	void Initialise(const byte *blocklist, int num_bytes)
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    37
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    38
		ResetAll();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    39
		for (int i = 0; i < num_bytes; i++) {
6782
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    40
			if (blocklist[i] == 0xFF) {
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    41
				SetAll();
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    42
				break;
1d546cb63611 (svn r10853) [NewGRF_ports] -Feature: Added heliport to airportsbasic.grf. Added Heli-EndTakeoff stage.
richk
parents: 6758
diff changeset
    43
			}
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    44
			blocks[(blocklist[i] < 64) ? 0 : 1] |= GetBlockAsBits(blocklist[i]);
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    45
		}
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    46
	}
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    47
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    48
	uint64 inline GetLower() const
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    49
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    50
		return blocks[0];
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    51
	}
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    52
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    53
	uint64 inline GetUpper() const
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    54
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    55
		return blocks[1];
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    56
	}
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    57
6819
67529db0fd74 (svn r10899) [NewGRF_ports] -Add: Added BlockCopy function to fsmblockmap.h.
richk
parents: 6810
diff changeset
    58
	void inline BlockCopy(const FSMblockmap *blockmap)
67529db0fd74 (svn r10899) [NewGRF_ports] -Add: Added BlockCopy function to fsmblockmap.h.
richk
parents: 6810
diff changeset
    59
	{
67529db0fd74 (svn r10899) [NewGRF_ports] -Add: Added BlockCopy function to fsmblockmap.h.
richk
parents: 6810
diff changeset
    60
		blocks[0] = blockmap->GetLower();
67529db0fd74 (svn r10899) [NewGRF_ports] -Add: Added BlockCopy function to fsmblockmap.h.
richk
parents: 6810
diff changeset
    61
		blocks[1] = blockmap->GetUpper();
67529db0fd74 (svn r10899) [NewGRF_ports] -Add: Added BlockCopy function to fsmblockmap.h.
richk
parents: 6810
diff changeset
    62
	}
67529db0fd74 (svn r10899) [NewGRF_ports] -Add: Added BlockCopy function to fsmblockmap.h.
richk
parents: 6810
diff changeset
    63
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    64
	void inline ReserveBlocks(const FSMblockmap *blockmap)
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    65
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    66
		/* OR with blockmap to reserve blocks */
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    67
		blocks[0] |= blockmap->GetLower();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    68
		blocks[1] |= blockmap->GetUpper();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    69
	}
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
    70
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    71
	void inline ReleaseBlocks(const FSMblockmap *blockmap)
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    72
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    73
		/* AND with ones-complement to clear set bits in blockmap from block_lower and block_upper */
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    74
		blocks[0] &= ~blockmap->GetLower();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    75
		blocks[1] &= ~blockmap->GetUpper();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    76
	}
6751
08176dfe64b7 (svn r10790) [NewGRF_ports] -Codechange: constify the fsmblockmaps where possible.
rubidium
parents: 6739
diff changeset
    77
6839
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    78
	void inline ReleaseOnlyOwnedBlocks(const FSMblockmap *blockmap, const FSMblockmap *owned_blockmap)
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    79
	{
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    80
		/* AND with ones-complement to clear set bits in blockmap from block_lower and block_upper */
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    81
		blocks[0] &= ~(blockmap->GetLower() & owned_blockmap->GetLower());
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    82
		blocks[1] &= ~(blockmap->GetUpper() & owned_blockmap->GetUpper());
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    83
	}
4884c39c4d66 (svn r10928) [NewGRF_ports] -Add: An aircraft can only release the blocks on an airport that it actually owns.
richk
parents: 6819
diff changeset
    84
6805
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
    85
	void inline ReleaseLowerBlocks(const FSMblockmap *blockmap)
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
    86
	{
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
    87
		/* used to forget any existing terminal reservations */
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
    88
		blocks[0] &= ~blockmap->GetLower();
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
    89
	}
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
    90
6845
666878d09b80 (svn r10943) [NewGRF_ports] -Change: Hangar terminal selection now checks for presence of a Choose Term/Helipad command. Usefully this can be *after* the TO_ALL, so ordinary processing will not pick up a secondary terminal reassignment.
richk
parents: 6839
diff changeset
    91
	void inline ReleaseUpperBlocks(const FSMblockmap *blockmap)
666878d09b80 (svn r10943) [NewGRF_ports] -Change: Hangar terminal selection now checks for presence of a Choose Term/Helipad command. Usefully this can be *after* the TO_ALL, so ordinary processing will not pick up a secondary terminal reassignment.
richk
parents: 6839
diff changeset
    92
	{
666878d09b80 (svn r10943) [NewGRF_ports] -Change: Hangar terminal selection now checks for presence of a Choose Term/Helipad command. Usefully this can be *after* the TO_ALL, so ordinary processing will not pick up a secondary terminal reassignment.
richk
parents: 6839
diff changeset
    93
		/* used to forget any normal movemnt reservations */
666878d09b80 (svn r10943) [NewGRF_ports] -Change: Hangar terminal selection now checks for presence of a Choose Term/Helipad command. Usefully this can be *after* the TO_ALL, so ordinary processing will not pick up a secondary terminal reassignment.
richk
parents: 6839
diff changeset
    94
		blocks[1] &= ~blockmap->GetUpper();
666878d09b80 (svn r10943) [NewGRF_ports] -Change: Hangar terminal selection now checks for presence of a Choose Term/Helipad command. Usefully this can be *after* the TO_ALL, so ordinary processing will not pick up a secondary terminal reassignment.
richk
parents: 6839
diff changeset
    95
	}
666878d09b80 (svn r10943) [NewGRF_ports] -Change: Hangar terminal selection now checks for presence of a Choose Term/Helipad command. Usefully this can be *after* the TO_ALL, so ordinary processing will not pick up a secondary terminal reassignment.
richk
parents: 6839
diff changeset
    96
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    97
	bool inline BlocksAreFree(const FSMblockmap *blockmap) const
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    98
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
    99
		uint64 test_lower = blockmap->GetLower();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   100
		uint64 test_upper = blockmap->GetUpper();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   101
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   102
		if ((~blocks[0] & test_lower) != test_lower)  return false;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   103
		if ((~blocks[1] & test_upper) != test_upper)  return false;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   104
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   105
		return true;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   106
	}
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   107
6810
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   108
	bool inline CanReserveBlocks(const FSMblockmap *wantedblockmap, const FSMblockmap *targetblockmap) const
6805
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   109
	{
6810
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   110
		/* HasBlocks is a strict "have I got all the blocks? Yes/No". While useful in places
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   111
		 * sometimes you want to ask "ive got some blocks. can I get the rest?"
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   112
		 * This function does that.
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   113
		 * The first assignment calculates which bits you need, but dont already have.
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   114
		 * Then it tests whether the remaining bits are free in targetblockmap.
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   115
		 * If they are, then return true.
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   116
		 */
6805
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   117
		uint64 test_lower = (blocks[0] ^ wantedblockmap->GetLower()) & wantedblockmap->GetLower();
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   118
		uint64 test_upper = (blocks[1] ^ wantedblockmap->GetUpper()) & wantedblockmap->GetUpper();
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   119
6810
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   120
		if ((~targetblockmap->GetLower() & test_lower) != test_lower)  return false;
ca073d196e33 (svn r10884) [NewGRF_ports] -Change: clarified CanReserveBlocks in comments.
richk
parents: 6805
diff changeset
   121
		if ((~targetblockmap->GetUpper() & test_upper) != test_upper)  return false;
6805
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   122
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   123
		return true;
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   124
	}
2b3ed8e47662 (svn r10879) [NewGRF_ports] -Feature: Added support for special orders, with addition of <data> byte in FSM command:
richk
parents: 6783
diff changeset
   125
6758
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   126
	bool inline HasBlocks(const FSMblockmap *blockmap) const
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   127
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   128
		uint64 test_lower = blockmap->GetLower();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   129
		uint64 test_upper = blockmap->GetUpper();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   130
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   131
		if ((blocks[0] & test_lower) != test_lower)  return false;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   132
		if ((blocks[1] & test_upper) != test_upper)  return false;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   133
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   134
		return true;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   135
	}
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   136
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   137
	bool inline HasBlock(byte blocknumber) const
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   138
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   139
		FSMblockmap test_blockmap;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   140
		test_blockmap.Initialise(&blocknumber, 1);
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   141
		return HasBlocks(&test_blockmap);
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   142
	}
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   143
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   144
	//bool inline HasBlock(byte blocknumber)
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   145
	//{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   146
	//	uint64 test_block = GetBlockAsBits(blocknumber);
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   147
	//	if (blocknumber < 64) return ((block_lower & test_block) == test_block);
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   148
	//	return ((block_upper & test_block) == test_block);
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   149
	//}
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   150
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   151
	bool inline IsNotZero(void) const
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   152
	{
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   153
		return ((blocks[0] != 0) || (blocks[1] != 0));
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   154
	}
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   155
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   156
	void PrintBlock(bool newline = true) const;
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   157
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   158
	/**
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   159
	 * Needed for an ugly hack:
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   160
	 *  - vehicles and stations need to store fsmblocks, so they use FSMBlock as container
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   161
	 *  - this internals of the container should be protected, e.g. private (or protected) by C++
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   162
	 *  - for saving/loading we need to pass pointer to objects
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   163
	 *  -> so *if* the pointer to the FSMBlock is the same as the pointer to the blocks array
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   164
	 *     encapsulated in the FSMBlock, we can just pass the FSMBlock as "offset".
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   165
	 *     Normally we would then just add the offset of the blocks variable within the class
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   166
	 *     but that is not possible because the variable is private. Furthermore we are not sure
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   167
	 *     that this works on all platforms, we need to check whether the offset is actually 0.
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   168
	 *     This cannot be done compile time, because the variable is private. So we need to write
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   169
	 *     a function that does actually check the offset runtime and call it somewhere where it
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   170
	 *     is always called but it should not be called often.
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   171
	 */
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   172
	static void AssertOnWrongBlockOffset();
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   173
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   174
private:
1545d187ab64 (svn r10800) [NewGRF_ports] -Add: saving of FSMblockmaps.
rubidium
parents: 6751
diff changeset
   175
	uint64 blocks[2];
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
   176
};
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
   177
6751
08176dfe64b7 (svn r10790) [NewGRF_ports] -Codechange: constify the fsmblockmaps where possible.
rubidium
parents: 6739
diff changeset
   178
FSMblockmap* SetFSMSingleBlock(const byte *blocklist);
6739
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
   179
3f2ca4d0abda (svn r10738) [NewGRF_ports] -Feature: FSMblockmap class added. Basic FSM import complete. Need to handle multiple blocks on import. Aircraft controller recoded to work with new 128 bit blocks. Still need to write saveload for new blocks.
richk
parents:
diff changeset
   180
#endif /* FSMBLOCKMAP_H */