MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / CmdCreateStoryPageElement

Function CmdCreateStoryPageElement

src/story.cpp:253–285  ·  view source on GitHub ↗

* 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 a

Source from the content-addressed store, hash-verified

251 * @return the cost of this operation or an error
252 */
253std::tuple<CommandCost, StoryPageElementID> CmdCreateStoryPageElement(DoCommandFlags flags, TileIndex tile, StoryPageID page_id, StoryPageElementType type, uint32_t reference, const EncodedString &text)
254{
255 if (!StoryPageElement::CanAllocateItem()) return { CMD_ERROR, StoryPageElementID::Invalid() };
256
257 /* Allow at most 128 elements per page. */
258 uint16_t element_count = 0;
259 for (StoryPageElement *iter : StoryPageElement::Iterate()) {
260 if (iter->page == page_id) element_count++;
261 }
262 if (element_count >= 128) return { CMD_ERROR, StoryPageElementID::Invalid() };
263
264 if (_current_company != OWNER_DEITY) return { CMD_ERROR, StoryPageElementID::Invalid() };
265 if (!StoryPage::IsValidID(page_id)) return { CMD_ERROR, StoryPageElementID::Invalid() };
266 if (!VerifyElementContentParameters(page_id, type, tile, reference, text)) return { CMD_ERROR, StoryPageElementID::Invalid() };
267
268
269 if (flags.Test(DoCommandFlag::Execute)) {
270 if (StoryPageElement::GetNumItems() == 0) {
271 /* Initialize the next sort value variable. */
272 _story_page_element_next_sort_value = 0;
273 }
274
275 StoryPageElement *pe = new StoryPageElement(_story_page_element_next_sort_value, type, page_id);
276 UpdateElement(*pe, tile, reference, text);
277
278 InvalidateWindowClassesData(WC_STORY_BOOK, page_id);
279
280 _story_page_element_next_sort_value++;
281 return { CommandCost(), pe->index };
282 }
283
284 return { CommandCost(), StoryPageElementID::Invalid() };
285}
286
287/**
288 * Update a new story page element.

Callers

nothing calls this directly

Calls 6

InvalidFunction · 0.85
UpdateElementFunction · 0.85
CommandCostClass · 0.85
TestMethod · 0.80

Tested by

no test coverage detected