summaryrefslogtreecommitdiff
path: root/src/subsidy_base.h
blob: 4750605bc64112299afba957975f2e1a86feaa8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* $Id$ */

/** @file subsidy_base.h Subsidy base class. */

#ifndef SUBSIDY_BASE_H
#define SUBSIDY_BASE_H

#include "cargo_type.h"
#include "company_type.h"
#include "subsidy_type.h"

/** Struct about subsidies, offered and awarded */
struct Subsidy {
	CargoID cargo_type;      ///< Cargo type involved in this subsidy, CT_INVALID for invalid subsidy
	byte remaining;          ///< Remaining months when this subsidy is valid
	CompanyByte awarded;     ///< Subsidy is awarded to this company; INVALID_COMPANY if it's not awarded to anyone
	SourceTypeByte src_type; ///< Source of subsidised path (ST_INDUSTRY or ST_TOWN)
	SourceTypeByte dst_type; ///< Destination of subsidised path (ST_INDUSTRY or ST_TOWN)
	SourceID src;            ///< Index of source. Either TownID or IndustryID
	SourceID dst;            ///< Index of destination. Either TownID or IndustryID

	/**
	 * Tests whether this subsidy has been awarded to someone
	 * @return is this subsidy awarded?
	 */
	FORCEINLINE bool IsAwarded() const
	{
		return this->awarded != INVALID_COMPANY;
	}

	void AwardTo(CompanyID company);

	/**
	 * Determines index of this subsidy
	 * @return index (in the Subsidy::array array)
	 */
	FORCEINLINE SubsidyID Index() const
	{
		return this - Subsidy::array;
	}

	/**
	 * Tests for validity of this subsidy
	 * @return is this subsidy valid?
	 */
	FORCEINLINE bool IsValid() const
	{
		return this->cargo_type != CT_INVALID;
	}


	static Subsidy array[MAX_COMPANIES]; ///< Array holding all subsidies

	/**
	 * Total number of subsidies, both valid and invalid
	 * @return length of Subsidy::array
	 */
	static FORCEINLINE size_t GetArraySize()
	{
		return lengthof(Subsidy::array);
	}

	/**
	 * Tests whether given index is an index of valid subsidy
	 * @param index index to check
	 * @return can this index be used to access a valid subsidy?
	 */
	static FORCEINLINE bool IsValidID(size_t index)
	{
		return index < Subsidy::GetArraySize() && Subsidy::Get(index)->IsValid();
	}

	/**
	 * Returns pointer to subsidy with given index
	 * @param index index of subsidy
	 * @return pointer to subsidy with given index
	 */
	static FORCEINLINE Subsidy *Get(size_t index)
	{
		assert(index < Subsidy::GetArraySize());
		return &Subsidy::array[index];
	}

	static Subsidy *AllocateItem();
	static void Clean();
};

#define FOR_ALL_SUBSIDIES_FROM(var, start) for (size_t subsidy_index = start; var = NULL, subsidy_index < Subsidy::GetArraySize(); subsidy_index++) \
		if ((var = Subsidy::Get(subsidy_index))->IsValid())
#define FOR_ALL_SUBSIDIES(var) FOR_ALL_SUBSIDIES_FROM(var, 0)

#endif /* SUBSIDY_BASE_H */