// Never Forget: Comments can "lie", but code not so much ;P
//----------------------------------------------------------//
// Contains self defined 2DA files related functionalities  //
//----------------------------------------------------------//


// Purpose: Adds a calculated new item type into the itemtype.2DA
// --------------------------------------------------------------
// Parameters:|
// ============
// inventorySlotType 	->	Defines which slot type in inventory an item has.This is an integer value.
//							see: https://github.com/Incrementis/Infinity-Engine-Modding-Wiki-Content-/blob/main/misc/Itemslot%20Types/Itemslot%20types.pdf
// soundGrab			-> 	Defines the sound file name when the item is grabed. 
//							This has to be a string value with max 8 characters. 
//							No file extension is needed(e.g. "mySound" and not "mySound.wav").
// soundDrop			->  Defines the sound file name when the item is dropped(released). 
//							This has to be a string value with max 8 characters.
// 							No file extension is needed(e.g. "mySound" and not "mySound.wav").
// typeNumber			->	Returns a calculated value of the new type. 
//							This value is needed to reference the new item type to an item. 
DEFINE_DIMORPHIC_FUNCTION ~ADD_ITEMTYPE_IN_2DA~
	INT_VAR
		inventorySlotType 	= 0
	STR_VAR
		soundGrab			= ~0~
		soundDrop			= ~0~
	RET
		typeNumber
BEGIN
	// Checks if grab sound name has more than 8 characters
	ACTION_IF (STRING_LENGTH ~%soundGrab%~ > 8) THEN
	BEGIN
		OUTER_SET numbers = STRING_LENGTH ~%soundGrab%~
		FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%soundGrab%' for sound is %numbers% characters long. The maximum allowed length for this argument is 8 characters~
	END
	// Checks if drop sound name has more than 8 characters
	ACTION_IF (STRING_LENGTH ~%soundDrop%~ > 8) THEN
	BEGIN
		OUTER_SET numbers = STRING_LENGTH ~%soundDrop%~
		FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%soundDrop%' for sound is %numbers% characters long. The maximum allowed length for this argument is 8 characters~
	END
	// Checks that inventorySlotType is not a negative number, because there are no negative slot numbers.
	ACTION_IF (~%inventorySlotType%~ < 0) THEN
	BEGIN
		FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The argument '%inventorySlotType%' for inventorySlotType is too small. The minimum allowed number is 0, because there are no negative slot numbers in game. Please adjust the inventorySlotType value to be within the allowed range.~
	END

	COPY_EXISTING ~ITEMTYPE.2DA~ ~override~
		// Gets the number of columns and rows in the file.
		COUNT_2DA_COLS numCols		
		// PSTEE itemtype.2da has more cloumns than the other EE games,
		// but will result in wrong item type number if not set to 4 columns.
		PATCH_IF (GAME_IS ~pstee~) THEN
		BEGIN
			SET numCols = 4
		END	
		// Reads the entire 2DA content into memory.
		READ_2DA_ENTRIES_NOW ~itemtypes~ ~%numCols%~
		
		FOR (rowIndex = 0; rowIndex < itemtypes; ++rowIndex) BEGIN
			// Gets the unique identifier for the item type from the list of item types.
			READ_2DA_ENTRY_FORMER itemtypes rowIndex 0 itemTypeId
			// Creates an array of item type reference numbers from the first column.
			SPRINT $itemtypes("%rowIndex%") "%itemTypeId%"
		END
		
		// Finds the highest item type reference number. 
		SET ~maxTypeNumber~ = 0
		PHP_EACH itemtypes AS key => value BEGIN			
			PATCH_IF (~%maxTypeNumber%~ < ~%value%~) THEN
			BEGIN
				SET ~maxTypeNumber~ = ~%value%~
			END
		END		
		
		// Calculates the new item type by incrementing the existing value.
		// This works because hex value begins by 0x00.
		// See: https://gibberlings3.github.io/iesdp/file_formats/ie_formats/itm_v1.htm#Header_ItemType 
		SET typeNumber = ~%maxTypeNumber%~ + 1
		// Checks if typeNumber is within 2-byte unsigned integer value
		ACTION_IF (~%typeNumber%~ > 65535) THEN
		BEGIN
			FAIL ~DEFINE_DIMORPHIC_FUNCTION ADD_ITEMTYPE_IN_2DA -> The value '%typeNumber%' for typeNumber exceeds 2MB. The maximum number of allowed item types is 65536~
		END	
		// Adds a new row to the bottom of the file with the calculated type, sounds, and slot.
		APPEND ~ITEMTYPE.2DA~ "%typeNumber%         %soundGrab%    %soundDrop%    %inventorySlotType%"
	BUT_ONLY // End of "COPY_EXISTING"

END // End of "ADD_ITEMTYPE"