Files @ r26123:a265ab51722a
Branch filter:

Location: cpp/openttd-patchpack/source/src/story.cpp

Michael Lutz
Codechange: Add support for additional command result values.
  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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * This file is part of OpenTTD.
 * OpenTTD 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, version 2.
 * OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file story.cpp Handling of stories. */

#include "stdafx.h"
#include "story_base.h"
#include "core/pool_func.hpp"
#include "cmd_helper.h"
#include "command_func.h"
#include "company_base.h"
#include "company_func.h"
#include "string_func.h"
#include "date_func.h"
#include "tile_map.h"
#include "goal_type.h"
#include "goal_base.h"
#include "window_func.h"
#include "gui.h"
#include "vehicle_base.h"
#include "game/game.hpp"
#include "script/api/script_story_page.hpp"
#include "script/api/script_event_types.hpp"
#include "story_cmd.h"

#include "safeguards.h"


StoryPageElementID _new_story_page_element_id;
StoryPageID _new_story_page_id;
uint32 _story_page_element_next_sort_value;
uint32 _story_page_next_sort_value;

StoryPageElementPool _story_page_element_pool("StoryPageElement");
StoryPagePool _story_page_pool("StoryPage");
INSTANTIATE_POOL_METHODS(StoryPageElement)
INSTANTIATE_POOL_METHODS(StoryPage)

/**
 * This helper for Create/Update PageElement Cmd procedure verifies if the page
 * element parameters are correct for the given page element type.
 * @param page_id The page id of the page which the page element (will) belong to
 * @param type The type of the page element to create/update
 * @param tile The tile parameter of the DoCommand proc
 * @param reference The reference parameter of the DoCommand proc (p2)
 * @param text The text parameter of the DoCommand proc
 * @return true, if and only if the given parameters are valid for the given page element type and page id.
 */
static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElementType type, TileIndex tile, uint32 reference, const char *text)
{
	StoryPageButtonData button_data{ reference };

	switch (type) {
		case SPET_TEXT:
			if (StrEmpty(text)) return false;
			break;
		case SPET_LOCATION:
			if (StrEmpty(text)) return false;
			if (!IsValidTile(tile)) return false;
			break;
		case SPET_GOAL:
			if (!Goal::IsValidID((GoalID)reference)) return false;
			/* Reject company specific goals on global pages */
			if (StoryPage::Get(page_id)->company == INVALID_COMPANY && Goal::Get((GoalID)reference)->company != INVALID_COMPANY) return false;
			break;
		case SPET_BUTTON_PUSH:
			if (!button_data.ValidateColour()) return false;
			return true;
		case SPET_BUTTON_TILE:
			if (!button_data.ValidateColour()) return false;
			if (!button_data.ValidateCursor()) return false;
			return true;
		case SPET_BUTTON_VEHICLE:
			if (!button_data.ValidateColour()) return false;
			if (!button_data.ValidateCursor()) return false;
			if (!button_data.ValidateVehicleType()) return false;
			return true;
		default:
			return false;
	}

	return true;
}

/**
 * This helper for Create/Update PageElement Cmd procedure updates a page
 * element with new content data.
 * @param pe The page element to update
 * @param tile The tile parameter of the DoCommand proc
 * @param reference The reference parameter of the DoCommand proc (p2)
 * @param text The text parameter of the DoCommand proc
 */
static void UpdateElement(StoryPageElement &pe, TileIndex tile, uint32 reference, const char *text)
{
	switch (pe.type) {
		case SPET_TEXT:
			pe.text = stredup(text);
			break;
		case SPET_LOCATION:
			pe.text = stredup(text);
			pe.referenced_id = tile;
			break;
		case SPET_GOAL:
			pe.referenced_id = (GoalID)reference;
			break;
		case SPET_BUTTON_PUSH:
		case SPET_BUTTON_TILE:
		case SPET_BUTTON_VEHICLE:
			pe.text = stredup(text);
			pe.referenced_id = reference;
			break;
		default: NOT_REACHED();
	}
}

/** Set the button background colour. */
void StoryPageButtonData::SetColour(Colours button_colour)
{
	assert(button_colour < COLOUR_END);
	SB(this->referenced_id, 0, 8, button_colour);
}

void StoryPageButtonData::SetFlags(StoryPageButtonFlags flags)
{
	SB(this->referenced_id, 24, 8, flags);
}

/** Set the mouse cursor used while waiting for input for the button. */
void StoryPageButtonData::SetCursor(StoryPageButtonCursor cursor)
{
	assert(cursor < SPBC_END);
	SB(this->referenced_id, 8, 8, cursor);
}

/** Set the type of vehicles that are accepted by the button */
void StoryPageButtonData::SetVehicleType(VehicleType vehtype)
{
	assert(vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END);
	SB(this->referenced_id, 16, 8, vehtype);
}

/** Get the button background colour. */
Colours StoryPageButtonData::GetColour() const
{
	return Extract<Colours, 0, 8>(this->referenced_id);
}

StoryPageButtonFlags StoryPageButtonData::GetFlags() const
{
	return (StoryPageButtonFlags)GB(this->referenced_id, 24, 8);
}

/** Get the mouse cursor used while waiting for input for the button. */
StoryPageButtonCursor StoryPageButtonData::GetCursor() const
{
	return Extract<StoryPageButtonCursor, 8, 8>(this->referenced_id);
}

/** Get the type of vehicles that are accepted by the button */
VehicleType StoryPageButtonData::GetVehicleType() const
{
	return (VehicleType)GB(this->referenced_id, 16, 8);
}

/** Verify that the data stored a valid Colour value */
bool StoryPageButtonData::ValidateColour() const
{
	return GB(this->referenced_id, 0, 8) < COLOUR_END;
}

bool StoryPageButtonData::ValidateFlags() const
{
	byte flags = GB(this->referenced_id, 24, 8);
	/* Don't allow float left and right together */
	if ((flags & SPBF_FLOAT_LEFT) && (flags & SPBF_FLOAT_RIGHT)) return false;
	/* Don't allow undefined flags */
	if (flags & ~(SPBF_FLOAT_LEFT | SPBF_FLOAT_RIGHT)) return false;
	return true;
}

/** Verify that the data stores a valid StoryPageButtonCursor value */
bool StoryPageButtonData::ValidateCursor() const
{
	return GB(this->referenced_id, 8, 8) < SPBC_END;
}

/** Verity that the data stored a valid VehicleType value */
bool StoryPageButtonData::ValidateVehicleType() const
{
	byte vehtype = GB(this->referenced_id, 16, 8);
	return vehtype == VEH_INVALID || vehtype < VEH_COMPANY_END;
}

/**
 * Create a new story page.
 * @param flags type of operation
 * @param company Company for which this story page belongs to.
 * @param text Title of the story page. Null is allowed in which case a generic page title is provided by OpenTTD.
 * @return the cost of this operation or an error
 */
CommandCost CmdCreateStoryPage(DoCommandFlag flags, CompanyID company, const std::string &text)
{
	if (!StoryPage::CanAllocateItem()) return CMD_ERROR;

	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		if (_story_page_pool.items == 0) {
			/* Initialize the next sort value variable. */
			_story_page_next_sort_value = 0;
		}

		StoryPage *s = new StoryPage();
		s->sort_value = _story_page_next_sort_value;
		s->date = _date;
		s->company = company;
		if (text.empty()) {
			s->title = nullptr;
		} else {
			s->title = stredup(text.c_str());
		}

		InvalidateWindowClassesData(WC_STORY_BOOK, -1);
		if (StoryPage::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);

		_new_story_page_id = s->index;
		_story_page_next_sort_value++;
	}

	return CommandCost();
}

/**
 * Create a new story page element.
 * @param flags type of operation
 * @param tile Tile location if it is a location page element, otherwise unused.
 * @param page_id The page which the element belongs to.
 * @param type Page element type
 * @param reference Id of referenced object
 * @param text Text content in case it is a text or location page element
 * @return the cost of this operation or an error
 */
CommandCost CmdCreateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32 reference, const std::string &text)
{
	if (!StoryPageElement::CanAllocateItem()) return CMD_ERROR;

	/* Allow at most 128 elements per page. */
	uint16 element_count = 0;
	for (StoryPageElement *iter : StoryPageElement::Iterate()) {
		if (iter->page == page_id) element_count++;
	}
	if (element_count >= 128) return CMD_ERROR;

	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
	if (!VerifyElementContentParameters(page_id, type, tile, reference, text.c_str())) return CMD_ERROR;

	if (flags & DC_EXEC) {
		if (_story_page_element_pool.items == 0) {
			/* Initialize the next sort value variable. */
			_story_page_element_next_sort_value = 0;
		}

		StoryPageElement *pe = new StoryPageElement();
		pe->sort_value = _story_page_element_next_sort_value;
		pe->type = type;
		pe->page = page_id;
		UpdateElement(*pe, tile, reference, text.c_str());

		InvalidateWindowClassesData(WC_STORY_BOOK, page_id);

		_new_story_page_element_id = pe->index;
		_story_page_element_next_sort_value++;
	}

	return CommandCost();
}

/**
 * Update a new story page element.
 * @param flags type of operation
 * @param tile Tile location if it is a location page element, otherwise unused.
 * @param page_element_id The page element to update.
 * @param reference Id of referenced object
 * @param text Text content in case it is a text or location page element
 * @return the cost of this operation or an error
 */
CommandCost CmdUpdateStoryPageElement(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, uint32 reference, const std::string &text)
{
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;

	StoryPageElement *pe = StoryPageElement::Get(page_element_id);
	StoryPageID page_id = pe->page;
	StoryPageElementType type = pe->type;

	if (!VerifyElementContentParameters(page_id, type, tile, reference, text.c_str())) return CMD_ERROR;

	if (flags & DC_EXEC) {
		UpdateElement(*pe, tile, reference, text.c_str());
		InvalidateWindowClassesData(WC_STORY_BOOK, pe->page);
	}

	return CommandCost();
}

/**
 * Update title of a story page.
 * @param flags type of operation
 * @param page_id StoryPageID to update.
 * @param text title text of the story page.
 * @return the cost of this operation or an error
 */
CommandCost CmdSetStoryPageTitle(DoCommandFlag flags, StoryPageID page_id, const std::string &text)
{
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		StoryPage *p = StoryPage::Get(page_id);
		free(p->title);
		if (text.empty()) {
			p->title = nullptr;
		} else {
			p->title = stredup(text.c_str());
		}

		InvalidateWindowClassesData(WC_STORY_BOOK, page_id);
	}

	return CommandCost();
}

/**
 * Update date of a story page.
 * @param flags type of operation
 * @param page_id StoryPageID to update.
 * @param date date
 * @return the cost of this operation or an error
 */
CommandCost CmdSetStoryPageDate(DoCommandFlag flags, StoryPageID page_id, Date date)
{
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		StoryPage *p = StoryPage::Get(page_id);
		p->date = date;

		InvalidateWindowClassesData(WC_STORY_BOOK, page_id);
	}

	return CommandCost();
}

/**
 * Display a story page for all clients that are allowed to
 * view the story page.
 * @param flags type of operation
 * @param page_id StoryPageID to show.
 * @return the cost of this operation or an error
 */
CommandCost CmdShowStoryPage(DoCommandFlag flags, StoryPageID page_id)
{
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		StoryPage *g = StoryPage::Get(page_id);
		if ((g->company != INVALID_COMPANY && g->company == _local_company) || (g->company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowStoryBook(_local_company, page_id);
	}

	return CommandCost();
}
/**
 * Remove a story page and associated story page elements.
 * @param flags type of operation
 * @param page_id StoryPageID to remove.
 * @return the cost of this operation or an error
 */
CommandCost CmdRemoveStoryPage(DoCommandFlag flags, StoryPageID page_id)
{
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		StoryPage *p = StoryPage::Get(page_id);

		for (StoryPageElement *pe : StoryPageElement::Iterate()) {
			if (pe->page == p->index) {
				delete pe;
			}
		}

		delete p;

		InvalidateWindowClassesData(WC_STORY_BOOK, -1);
		if (StoryPage::GetNumItems() == 0) InvalidateWindowData(WC_MAIN_TOOLBAR, 0);
	}

	return CommandCost();
}

/**
 * Remove a story page element
 * @param flags type of operation
 * @param page_element_id StoryPageElementID to remove.
 * @return the cost of this operation or an error
 */
CommandCost CmdRemoveStoryPageElement(DoCommandFlag flags, StoryPageElementID page_element_id)
{
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
	if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;

	if (flags & DC_EXEC) {
		StoryPageElement *pe = StoryPageElement::Get(page_element_id);
		StoryPageID page_id = pe->page;

		delete pe;

		InvalidateWindowClassesData(WC_STORY_BOOK, page_id);
	}

	return CommandCost();
}

/**
 * Clicked/used a button on a story page.
 * @param flags  Type of operation.
 * @param tile   Tile selected, for tile selection buttons, otherwise unused.
 * @param page_element_id story page element id of button.
 * @param reference ID of selected item for buttons that select an item (e.g. vehicle), otherwise unused.
 * @return The cost of the operation, or an error.
 */
CommandCost CmdStoryPageButton(DoCommandFlag flags, TileIndex tile, StoryPageElementID page_element_id, VehicleID reference)
{
	if (!StoryPageElement::IsValidID(page_element_id)) return CMD_ERROR;
	const StoryPageElement *const pe = StoryPageElement::Get(page_element_id);

	/* Check the player belongs to the company that owns the page. */
	const StoryPage *const sp = StoryPage::Get(pe->page);
	if (sp->company != INVALID_COMPANY && sp->company != _current_company) return CMD_ERROR;

	switch (pe->type) {
		case SPET_BUTTON_PUSH:
			/* No validation required */
			if (flags & DC_EXEC) Game::NewEvent(new ScriptEventStoryPageButtonClick(_current_company, pe->page, page_element_id));
			break;
		case SPET_BUTTON_TILE:
			if (!IsValidTile(tile)) return CMD_ERROR;
			if (flags & DC_EXEC) Game::NewEvent(new ScriptEventStoryPageTileSelect(_current_company, pe->page, page_element_id, tile));
			break;
		case SPET_BUTTON_VEHICLE:
			if (!Vehicle::IsValidID(reference)) return CMD_ERROR;
			if (flags & DC_EXEC) Game::NewEvent(new ScriptEventStoryPageVehicleSelect(_current_company, pe->page, page_element_id, reference));
			break;
		default:
			/* Invalid page element type, not a button. */
			return CMD_ERROR;
	}

	return CommandCost();
}