summaryrefslogtreecommitdiff
path: root/src/uqm/master.c
blob: 5c35aaba2e91b081675671366d99b465eddde2c0 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "master.h"

#include "build.h"
#include "resinst.h"
#include "displist.h"
#include "supermelee/melee.h"


QUEUE master_q;

void
LoadMasterShipList (void (* YieldProcessing)(void))
{
	COUNT num_entries;
	SPECIES_ID s_id = ARILOU_ID;
	num_entries = LAST_MELEE_ID - ARILOU_ID + 1;
	InitQueue (&master_q, num_entries, sizeof (MASTER_SHIP_INFO));
	while (num_entries--)
	{
		HMASTERSHIP hBuiltShip;
		char *builtName;
		HMASTERSHIP hStarShip, hNextShip;
		MASTER_SHIP_INFO *BuiltPtr;
		RACE_DESC *RDPtr;

		hBuiltShip = AllocLink (&master_q);
		if (!hBuiltShip)
			continue;

		// Allow other things to run
		//  supposedly, loading ship packages and data takes some time
		if (YieldProcessing)
			YieldProcessing ();

		BuiltPtr = LockMasterShip (&master_q, hBuiltShip);
		BuiltPtr->SpeciesID = s_id++;
		RDPtr = load_ship (BuiltPtr->SpeciesID, FALSE);
		if (!RDPtr)
		{
			UnlockMasterShip (&master_q, hBuiltShip);
			continue;
		}

		// Grab a copy of loaded icons, strings and info
		// XXX: SHIP_INFO implicitly referenced here
		BuiltPtr->ShipInfo = RDPtr->ship_info;
		BuiltPtr->Fleet = RDPtr->fleet;
		free_ship (RDPtr, FALSE, FALSE);

		builtName = GetStringAddress (SetAbsStringTableIndex (
				BuiltPtr->ShipInfo.race_strings, 2));
		UnlockMasterShip (&master_q, hBuiltShip);

		// Insert the ship in the master queue in the right location
		// to keep the list sorted on the name of the race.
		for (hStarShip = GetHeadLink (&master_q);
				hStarShip; hStarShip = hNextShip)
		{
			char *curName;
			MASTER_SHIP_INFO *MasterPtr;

			MasterPtr = LockMasterShip (&master_q, hStarShip);
			hNextShip = _GetSuccLink (MasterPtr);
			curName = GetStringAddress (SetAbsStringTableIndex (
					MasterPtr->ShipInfo.race_strings, 2));
			UnlockMasterShip (&master_q, hStarShip);

			if (strcmp (builtName, curName) < 0)
				break;
		}
		InsertQueue (&master_q, hBuiltShip, hStarShip);
	}
}

void
FreeMasterShipList (void)
{
	HMASTERSHIP hStarShip, hNextShip;

	for (hStarShip = GetHeadLink (&master_q);
			hStarShip != 0; hStarShip = hNextShip)
	{
		MASTER_SHIP_INFO *MasterPtr;

		MasterPtr = LockMasterShip (&master_q, hStarShip);
		hNextShip = _GetSuccLink (MasterPtr);

		DestroyDrawable (ReleaseDrawable (MasterPtr->ShipInfo.melee_icon));
		DestroyDrawable (ReleaseDrawable (MasterPtr->ShipInfo.icons));
		DestroyStringTable (ReleaseStringTable (
				MasterPtr->ShipInfo.race_strings));

		UnlockMasterShip (&master_q, hStarShip);
	}

	UninitQueue (&master_q);
}

HMASTERSHIP
FindMasterShip (SPECIES_ID ship_ref)
{
	HMASTERSHIP hStarShip, hNextShip;
	
	for (hStarShip = GetHeadLink (&master_q); hStarShip; hStarShip = hNextShip)
	{
		SPECIES_ID ref;
		MASTER_SHIP_INFO *MasterPtr;

		MasterPtr = LockMasterShip (&master_q, hStarShip);
		hNextShip = _GetSuccLink (MasterPtr);
		ref = MasterPtr->SpeciesID;
		UnlockMasterShip (&master_q, hStarShip);

		if (ref == ship_ref)
			break;
	}

	return (hStarShip);
}

int
FindMasterShipIndex (SPECIES_ID ship_ref)
{
	HMASTERSHIP hStarShip, hNextShip;
	int index;
	
	for (index = 0, hStarShip = GetHeadLink (&master_q); hStarShip;
			++index, hStarShip = hNextShip)
	{
		SPECIES_ID ref;
		MASTER_SHIP_INFO *MasterPtr;

		MasterPtr = LockMasterShip (&master_q, hStarShip);
		hNextShip = _GetSuccLink (MasterPtr);
		ref = MasterPtr->SpeciesID;
		UnlockMasterShip (&master_q, hStarShip);

		if (ref == ship_ref)
			break;
	}

	return hStarShip ? index : -1;
}

COUNT
GetShipCostFromIndex (unsigned Index)
{
	HMASTERSHIP hMasterShip;
	MASTER_SHIP_INFO *MasterPtr;
	COUNT val;

	hMasterShip = GetStarShipFromIndex (&master_q, Index);
	if (!hMasterShip)
		return 0;

	MasterPtr = LockMasterShip (&master_q, hMasterShip);
	val = MasterPtr->ShipInfo.ship_cost;
	UnlockMasterShip (&master_q, hMasterShip);

	return val;
}

FRAME
GetShipIconsFromIndex (unsigned Index)
{
	HMASTERSHIP hMasterShip;
	MASTER_SHIP_INFO *MasterPtr;
	FRAME val;

	hMasterShip = GetStarShipFromIndex (&master_q, Index);
	if (!hMasterShip)
		return 0;

	MasterPtr = LockMasterShip (&master_q, hMasterShip);
	val = MasterPtr->ShipInfo.icons;
	UnlockMasterShip (&master_q, hMasterShip);

	return val;
}

FRAME
GetShipMeleeIconsFromIndex (unsigned Index)
{
	HMASTERSHIP hMasterShip;
	MASTER_SHIP_INFO *MasterPtr;
	FRAME val;

	hMasterShip = GetStarShipFromIndex (&master_q, Index);
	if (!hMasterShip)
		return 0;

	MasterPtr = LockMasterShip (&master_q, hMasterShip);
	val = MasterPtr->ShipInfo.melee_icon;
	UnlockMasterShip (&master_q, hMasterShip);

	return val;
}