summaryrefslogtreecommitdiff
path: root/src/uqm/planets/roster.c
blob: 663ac2836a476bcfcabc9f6486507bedc3a55f49 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//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 "../build.h"
#include "../colors.h"
#include "../controls.h"
#include "../races.h"
#include "../units.h"
#include "../sis.h"
#include "../shipcont.h"
#include "../setup.h"
#include "../sounds.h"
#include "port.h"
#include "libs/gfxlib.h"
#include "libs/tasklib.h"

#include <stdlib.h>

// Ship icon positions in status display around the flagship
static const POINT ship_pos[MAX_BUILT_SHIPS] =
{
	SUPPORT_SHIP_PTS
};

typedef struct
{
	// Ship icon positions split into (lower half) left and right (upper)
	// and sorted in the Y coord. These are used for navigation around the
	// escort positions.
	POINT shipPos[MAX_BUILT_SHIPS];
	COUNT count;
			// Number of ships
	
	POINT curShipPt;
			// Location of the currently selected escort
	FRAME curShipFrame;
			// Icon of the currently selected escort
	bool modifyingCrew;
			// true when in crew modification "sub-menu". This is simple
			// enough that it does not require a real sub-menu.
} ROSTER_STATE;

static SHIP_FRAGMENT* LockSupportShip (ROSTER_STATE *, HSHIPFRAG *phFrag);

static void
drawSupportShip (ROSTER_STATE *rosterState, bool filled)
{
	STAMP s;

	if (!rosterState->curShipFrame)
		return;

	s.origin = rosterState->curShipPt;
	s.frame = rosterState->curShipFrame;
	
	if (filled)
		DrawFilledStamp (&s);
	else
		DrawStamp (&s);
}

static void
getSupportShipIcon (ROSTER_STATE *rosterState)
{
	HSHIPFRAG hShipFrag;
	SHIP_FRAGMENT *ShipFragPtr;

	rosterState->curShipFrame = NULL;
	ShipFragPtr = LockSupportShip (rosterState, &hShipFrag);
	if (!ShipFragPtr)
		return;

	rosterState->curShipFrame = ShipFragPtr->icons;
	UnlockShipFrag (&GLOBAL (built_ship_q), hShipFrag);
}

static void
flashSupportShip (ROSTER_STATE *rosterState)
{
	static Color c = BUILD_COLOR (MAKE_RGB15_INIT (0x1F, 0x00, 0x00), 0x24);
	static TimeCount NextTime = 0;

	if (GetTimeCounter () >= NextTime)
	{
		NextTime = GetTimeCounter () + (ONE_SECOND / 15);
		
		/* The commented code out code is the old code before the switch
		 * to 24-bits colors. The current code produces very slightly
		 * different colors due to rounding errors, but the old code wasn't
		 * original anyhow, and you can't tell the difference visually.
		 * - SvdB
		if (c >= BUILD_COLOR (MAKE_RGB15 (0x1F, 0x19, 0x19), 0x24))
			c = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x00, 0x00), 0x24);
		else
			c += BUILD_COLOR (MAKE_RGB15 (0x00, 0x02, 0x02), 0x00);
		*/

		if (c.g >= CC5TO8 (0x19))
		{
			c = BUILD_COLOR (MAKE_RGB15 (0x1F, 0x00, 0x00), 0x24);
		}
		else
		{
			c.g += CC5TO8 (0x02);
			c.b += CC5TO8 (0x02);
		}
		SetContextForeGroundColor (c);

		drawSupportShip (rosterState, TRUE);
	}
}

static SHIP_FRAGMENT *
LockSupportShip (ROSTER_STATE *rosterState, HSHIPFRAG *phFrag)
{
	const POINT *pship_pos;
	HSHIPFRAG hStarShip, hNextShip;

	// Lookup the current escort's location in the unsorted points list
	// to find the original escort index
	for (hStarShip = GetHeadLink (&GLOBAL (built_ship_q)),
			pship_pos = ship_pos;
			hStarShip; hStarShip = hNextShip, ++pship_pos)
	{
		SHIP_FRAGMENT *StarShipPtr;

		StarShipPtr = LockShipFrag (&GLOBAL (built_ship_q), hStarShip);

		if (pointsEqual (*pship_pos, rosterState->curShipPt))
		{
			*phFrag = hStarShip;
			return StarShipPtr;
		}

		hNextShip = _GetSuccLink (StarShipPtr);
		UnlockShipFrag (&GLOBAL (built_ship_q), hStarShip);
	}

	return NULL;
}

static void
flashSupportShipCrew (void)
{
	RECT r;

	SetContext (StatusContext);
	GetStatusMessageRect (&r);
	SetFlashRect (&r);
}

static BOOLEAN
DeltaSupportCrew (ROSTER_STATE *rosterState, SIZE crew_delta)
{
	BOOLEAN ret = FALSE;
	UNICODE buf[40];
	HFLEETINFO hTemplate;
	HSHIPFRAG hShipFrag;
	SHIP_FRAGMENT *StarShipPtr;
	FLEET_INFO *TemplatePtr;

	StarShipPtr = LockSupportShip (rosterState, &hShipFrag);
	if (!StarShipPtr)
		return FALSE;

	hTemplate = GetStarShipFromIndex (&GLOBAL (avail_race_q),
			StarShipPtr->race_id);
	TemplatePtr = LockFleetInfo (&GLOBAL (avail_race_q), hTemplate);

	StarShipPtr->crew_level += crew_delta;

	if (StarShipPtr->crew_level == 0)
		StarShipPtr->crew_level = 1;
	else if (StarShipPtr->crew_level > TemplatePtr->crew_level &&
			crew_delta > 0)
		StarShipPtr->crew_level -= crew_delta;
	else
	{
		if (StarShipPtr->crew_level >= TemplatePtr->crew_level)
			sprintf (buf, "%u", StarShipPtr->crew_level);
		else
			sprintf (buf, "%u/%u",
					StarShipPtr->crew_level,
					TemplatePtr->crew_level);

		PreUpdateFlashRect ();
		DrawStatusMessage (buf);
		PostUpdateFlashRect ();
		DeltaSISGauges (-crew_delta, 0, 0);
		if (crew_delta)
		{
			flashSupportShipCrew ();
		}
		ret = TRUE;
	}

	UnlockFleetInfo (&GLOBAL (avail_race_q), hTemplate);
	UnlockShipFrag (&GLOBAL (built_ship_q), hShipFrag);

	return ret;
}

static void
drawModifiedSupportShip (ROSTER_STATE *rosterState)
{
	SetContext (StatusContext);
	SetContextForeGroundColor (ROSTER_MODIFY_SHIP_COLOR);
	drawSupportShip (rosterState, TRUE);
}

static void
selectSupportShip (ROSTER_STATE *rosterState, COUNT shipIndex)
{
	rosterState->curShipPt = rosterState->shipPos[shipIndex];
	getSupportShipIcon (rosterState);
	DeltaSupportCrew (rosterState, 0);
}

static BOOLEAN
DoModifyRoster (MENU_STATE *pMS)
{
	ROSTER_STATE *rosterState = pMS->privData;
	BOOLEAN select, cancel, up, down, horiz;

	if (GLOBAL (CurrentActivity) & CHECK_ABORT)
		return FALSE;

	select = PulsedInputState.menu[KEY_MENU_SELECT];
	cancel = PulsedInputState.menu[KEY_MENU_CANCEL];
	up = PulsedInputState.menu[KEY_MENU_UP];
	down = PulsedInputState.menu[KEY_MENU_DOWN];
	// Left or right produces the same effect because there are 2 columns
	horiz = PulsedInputState.menu[KEY_MENU_LEFT] ||
			PulsedInputState.menu[KEY_MENU_RIGHT];

	if (cancel && !rosterState->modifyingCrew)
	{
		return FALSE;
	}
	else if (select || cancel)
	{
		rosterState->modifyingCrew ^= true;
		if (!rosterState->modifyingCrew)
		{
			SetFlashRect (NULL);
			SetMenuSounds (MENU_SOUND_ARROWS, MENU_SOUND_SELECT);
		}
		else
		{
			drawModifiedSupportShip (rosterState);
			flashSupportShipCrew ();
			SetMenuSounds (MENU_SOUND_UP | MENU_SOUND_DOWN,
					MENU_SOUND_SELECT | MENU_SOUND_CANCEL);
		}
	}
	else if (rosterState->modifyingCrew)
	{
		SIZE delta = 0;
		BOOLEAN failed = FALSE;

		if (up)
		{
			if (GLOBAL_SIS (CrewEnlisted))
				delta = 1;
			else
				failed = TRUE;
		}
		else if (down)
		{
			if (GLOBAL_SIS (CrewEnlisted) < GetCrewPodCapacity ())
				delta = -1;
			else
				failed = TRUE;
		}
		
		if (delta != 0)
		{
			failed = !DeltaSupportCrew (rosterState, delta);
		}

		if (failed)
		{	// not enough room or crew
			PlayMenuSound (MENU_SOUND_FAILURE);
		}
	}
	else
	{
		COUNT NewState;
		POINT *pship_pos = rosterState->shipPos;
		COUNT top_right = (rosterState->count + 1) >> 1;

		NewState = pMS->CurState;
		
		if (rosterState->count < 2)
		{
			// no navigation allowed
		}
		else if (horiz)
		{
			if (NewState == top_right - 1)
				NewState = rosterState->count - 1;
			else if (NewState >= top_right)
			{
				NewState -= top_right;
				if (pship_pos[NewState].y < pship_pos[pMS->CurState].y)
					++NewState;
			}
			else
			{
				NewState += top_right;
				if (NewState != top_right
						&& pship_pos[NewState].y > pship_pos[pMS->CurState].y)
					--NewState;
			}
		}
		else if (down)
		{
			++NewState;
			if (NewState == rosterState->count)
				NewState = top_right;
			else if (NewState == top_right)
				NewState = 0;
		}
		else if (up)
		{
			if (NewState == 0)
				NewState = top_right - 1;
			else if (NewState == top_right)
				NewState = rosterState->count - 1;
			else
				--NewState;
		}

		BatchGraphics ();
		SetContext (StatusContext);

		if (NewState != pMS->CurState)
		{
			// Draw the previous escort in unselected state
			drawSupportShip (rosterState, FALSE);
			// Select the new one
			selectSupportShip (rosterState, NewState);
			pMS->CurState = NewState;
		}

		flashSupportShip (rosterState);

		UnbatchGraphics ();
	}

	SleepThread (ONE_SECOND / 30);

	return TRUE;
}

static int
compShipPos (const void *ptr1, const void *ptr2)
{
	const POINT *pt1 = (const POINT *) ptr1;
	const POINT *pt2 = (const POINT *) ptr2;

	// Ships on the left in the lower half
	if (pt1->x < pt2->x)
		return -1;
	else if (pt1->x > pt2->x)
		return 1;

	// and ordered on Y
	if (pt1->y < pt2->y)
		return -1;
	else if (pt1->y > pt2->y)
		return 1;
	else
		return 0;
}

BOOLEAN
RosterMenu (void)
{
	MENU_STATE MenuState;
	ROSTER_STATE RosterState;

	memset (&MenuState, 0, sizeof MenuState);
	MenuState.privData = &RosterState;

	memset (&RosterState, 0, sizeof RosterState);
	
	RosterState.count = CountLinks (&GLOBAL (built_ship_q));
	if (!RosterState.count)
		return FALSE;

	// Get the escort positions we will use and sort on X then Y
	assert (sizeof (RosterState.shipPos) == sizeof (ship_pos));
	memcpy (RosterState.shipPos, ship_pos, sizeof (ship_pos));
	qsort (RosterState.shipPos, RosterState.count,
			sizeof (RosterState.shipPos[0]), compShipPos);

	SetContext (StatusContext);
	selectSupportShip (&RosterState, MenuState.CurState);

	SetMenuSounds (MENU_SOUND_ARROWS, MENU_SOUND_SELECT);

	MenuState.InputFunc = DoModifyRoster;
	DoInput (&MenuState, TRUE);

	SetContext (StatusContext);
	// unselect the last ship
	drawSupportShip (&RosterState, FALSE);
	DrawStatusMessage (NULL);

	return TRUE;
}