OUTER_SPRINT ~slash~ ~/~
OUTER_SPRINT ~star~ ~*~
OUTER_SET minus = 0 - 1
ACTION_IF GAME_IS ~totsc~ BEGIN
	OUTER_SPRINT kagain_sn ~KAGAIN~
	OUTER_SPRINT kagain_scr ~KAGAIN~
	OUTER_SPRINT kagain_jd ~KAGAIJ~
	OUTER_SPRINT montaron_sn ~MONTARON~
	OUTER_SPRINT montaron_scr ~MONTARON~
	OUTER_SPRINT montaron_jd ~MONTAJ~
	OUTER_SPRINT dorn_sn ~SHNO_SCR_NAME~
	OUTER_SPRINT dorn_scr ~SHNOSCRP~
	OUTER_SPRINT dorn_jd ~SHNODIAL~
	OUTER_SPRINT garrick_sn ~GARRICK~
	OUTER_SPRINT garrick_scr ~GARRICK~
	OUTER_SPRINT garrick_jd ~GARRIJ~
	OUTER_SPRINT coran_sn ~CORAN~
	OUTER_SPRINT coran_scr ~CORAN~
	OUTER_SPRINT coran_jd ~CORANJ~
	OUTER_SPRINT eldoth_sn ~ELDOTH~
	OUTER_SPRINT eldoth_scr ~ELDOTH~
	OUTER_SPRINT eldoth_jd ~ELDOTJ~
	OUTER_SPRINT branwen_sn ~BRANWEN~
	OUTER_SPRINT branwen_scr ~BRANWEN~
	OUTER_SPRINT branwen_jd ~BRANWJ~
	OUTER_SPRINT sharteel_sn ~SHARTEEL~
	OUTER_SPRINT sharteel_scr ~SHARTEEL~
	OUTER_SPRINT sharteel_jd ~SHARTJ~
	OUTER_SPRINT beregost_area ~AR3300~
	OUTER_SPRINT baldur_west ~AR0600~
	OUTER_SPRINT baldur_east ~AR0800~
	OUTER_SPRINT garrick_check_class ~!Class(Player1,PALADIN_ALL)~
	OUTER_SPRINT com_ee_open ~%slash%%star%~
	OUTER_SPRINT com_ee_close ~%star%%slash%~
END
ACTION_IF GAME_IS ~bgt~ BEGIN
	OUTER_SPRINT kagain_sn ~KAGAIN~
	OUTER_SPRINT kagain_scr ~KAGAIN~
	OUTER_SPRINT kagain_jd ~KAGAIJ~
	OUTER_SPRINT montaron_sn ~MONTARON~
	OUTER_SPRINT montaron_scr ~MONTARON~
	OUTER_SPRINT montaron_jd ~MONTAJ~
	OUTER_SPRINT dorn_sn ~SHNO_SCR_NAME~
	OUTER_SPRINT dorn_scr ~SHNOSCRP~
	OUTER_SPRINT dorn_jd ~SHNODIAL~
	OUTER_SPRINT garrick_sn ~GARRICK~
	OUTER_SPRINT garrick_scr ~BGGARRIK~
	OUTER_SPRINT garrick_jd ~GARRIJ~
	OUTER_SPRINT coran_sn ~CORAN~
	OUTER_SPRINT coran_scr ~CORAN~
	OUTER_SPRINT coran_jd ~CORANJ~
	OUTER_SPRINT eldoth_sn ~ELDOTH~
	OUTER_SPRINT eldoth_scr ~ELDOTH~
	OUTER_SPRINT eldoth_jd ~ELDOTJ~
	OUTER_SPRINT branwen_sn ~BRANWEN~
	OUTER_SPRINT branwen_scr ~BGBRANW~
	OUTER_SPRINT branwen_jd ~BRANWJ~
	OUTER_SPRINT sharteel_sn ~SHARTEEL~
	OUTER_SPRINT sharteel_scr ~SHARTEEL~
	OUTER_SPRINT sharteel_jd ~SHARTJ~
	OUTER_SPRINT beregost_area ~AR6700~
	OUTER_SPRINT baldur_west ~AR7600~
	OUTER_SPRINT baldur_east ~AR7800~
	OUTER_SPRINT garrick_check_class ~!Class(Player1,PALADIN_ALL)~
	OUTER_SPRINT com_ee_open ~%slash%%star%~
	OUTER_SPRINT com_ee_close ~%star%%slash%~
END
ACTION_IF GAME_IS ~bgee~ BEGIN
	OUTER_SPRINT kagain_sn ~KAGAIN~
	OUTER_SPRINT kagain_scr ~KAGAIN~
	OUTER_SPRINT kagain_jd ~KAGAIJ~
	OUTER_SPRINT montaron_sn ~MONTARON~
	OUTER_SPRINT montaron_scr ~MONTARON~
	OUTER_SPRINT montaron_jd ~MONTAJ~
	OUTER_SPRINT dorn_sn ~DORN~
	OUTER_SPRINT dorn_scr ~DORN~
	OUTER_SPRINT dorn_jd ~DORNJ~
	OUTER_SPRINT garrick_sn ~GARRICK~
	OUTER_SPRINT garrick_scr ~GARRICK~
	OUTER_SPRINT garrick_jd ~GARRIJ~
	OUTER_SPRINT coran_sn ~CORAN~
	OUTER_SPRINT coran_scr ~CORAN~
	OUTER_SPRINT coran_jd ~CORANJ~
	OUTER_SPRINT eldoth_sn ~ELDOTH~
	OUTER_SPRINT eldoth_scr ~ELDOTH~
	OUTER_SPRINT eldoth_jd ~ELDOTJ~
	OUTER_SPRINT branwen_sn ~BRANWEN~
	OUTER_SPRINT branwen_scr ~BRANWEN~
	OUTER_SPRINT branwen_jd ~BRANWJ~
	OUTER_SPRINT sharteel_sn ~SHARTEEL~
	OUTER_SPRINT sharteel_scr ~SHARTEEL~
	OUTER_SPRINT sharteel_jd ~SHARTJ~
	OUTER_SPRINT beregost_area ~AR3300~
	OUTER_SPRINT baldur_west ~AR0600~
	OUTER_SPRINT baldur_east ~AR0800~
	OUTER_SPRINT garrick_check_class ~OR(2) !Class(Player1,PALADIN_ALL) Kit(Player1,BLACKGUARD)~
	OUTER_SPRINT com_ee_open ~~
	OUTER_SPRINT com_ee_close ~~
END
ACTION_IF GAME_IS ~eet~ BEGIN
	OUTER_SPRINT kagain_sn ~KAGAIN~
	OUTER_SPRINT kagain_scr ~KAGAIN~
	OUTER_SPRINT kagain_jd ~KAGAIJ~
	OUTER_SPRINT montaron_sn ~MONTARON~
	OUTER_SPRINT montaron_scr ~MONTARON~
	OUTER_SPRINT montaron_jd ~MONTAJ~
	OUTER_SPRINT dorn_sn ~DORN~
	OUTER_SPRINT dorn_scr ~DORN_~
	OUTER_SPRINT dorn_jd ~DORNJ_~
	OUTER_SPRINT garrick_sn ~GARRICK~
	OUTER_SPRINT garrick_scr ~GARRICK_~
	OUTER_SPRINT garrick_jd ~GARRIJ~
	OUTER_SPRINT coran_sn ~CORAN~
	OUTER_SPRINT coran_scr ~CORAN~
	OUTER_SPRINT coran_jd ~CORANJ~
	OUTER_SPRINT eldoth_sn ~ELDOTH~
	OUTER_SPRINT eldoth_scr ~ELDOTH~
	OUTER_SPRINT eldoth_jd ~ELDOTJ~
	OUTER_SPRINT branwen_sn ~BRANWEN~
	OUTER_SPRINT branwen_scr ~BRANWEN_~
	OUTER_SPRINT branwen_jd ~BRANWJ~
	OUTER_SPRINT sharteel_sn ~SHARTEEL~
	OUTER_SPRINT sharteel_scr ~SHARTEEL~
	OUTER_SPRINT sharteel_jd ~SHARTJ~
	OUTER_SPRINT beregost_area ~BG3300~
	OUTER_SPRINT baldur_west ~BG0600~
	OUTER_SPRINT baldur_east ~BG0800~
	OUTER_SPRINT garrick_check_class ~OR(2) !Class(Player1,PALADIN_ALL) Kit(Player1,BLACKGUARD)~
	OUTER_SPRINT com_ee_open ~~
	OUTER_SPRINT com_ee_close ~~
END
ACTION_IF FILE_EXISTS_IN_GAME ~garri99.wav~ BEGIN
	OUTER_SPRINT garric_song ~PlaySong(0) PlaySound("garri99")~
END ELSE BEGIN
	OUTER_SPRINT garric_song ~~
END



DEFINE_PATCH_FUNCTION GET_BIT
INT_VAR
	number = 0
	nth_bit = 0
RET
	bit_value
BEGIN
	SET bit_value = (number >> nth_bit) & 1
END

//BLOCK ALTER_AREA_ACTOR_EX
DEFINE_PATCH_FUNCTION MATCH_ALTER_ACTOR
	INT_VAR
		match_pos_x = "-1"
		match_pos_y = "-1"
		match_dest_x = "-1"
		match_dest_y = "-1"
		match_bit_unattached = "-1"
		match_bit_has_seen = "-1"
		match_bit_invulnerable = "-1"
		match_bit_override_script = "-1"
		match_spawned = "-1"
		match_animation = "-1"
		match_orientation = "-1"
		match_expiry = "-2"
		match_wander = "-1"
		match_follow = "-1"
		match_talked_to = "-1"
		
		pos_x = "-1"
		pos_y = "-1"
		dest_x = "-1"
		dest_y = "-1"
		bit_unattached = 1
		bit_has_seen = 0
		bit_invulnerable = 0
		bit_override_script = 0
		spawned = "-1"
		animation = "-1"
		orientation = "-1"
		expiry = "-2"
		wander = "-1"
		follow = "-1"
		talked_to = "-1"
	STR_VAR
		match_name = "same"
		match_dialogue = "same"
		match_override_script = "same"
		match_general_script = "same"
		match_class_script = "same"
		match_race_script = "same"
		match_default_script = "same"
		match_specifics_script = "same"
		match_cre_file = "same"
		
		name = "same"
		dialogue = "same"
		override_script = "same"
		general_script = "same"
		class_script = "same"
		race_script = "same"
		default_script = "same"
		specifics_script = "same"
		cre_file = "same"
	RET
		file_txt
BEGIN
	SET minus_one = ( 0 - 1 )
	SET minus_two = ( 0 - 2 )
	SPRINT file_txt ~~
	READ_LONG  0x54 act_off
	READ_SHORT 0x58 act_num
	PATCH_IF ( act_num > 0 ) BEGIN
		FOR ( i = 0 ; i < act_num ; ++i ) BEGIN
			SET match = 0
			SET offset = act_off + i * 0x110
			READ_ASCII offset + 0x00 data ( 32 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_name%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_name%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x20 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_pos_x%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_pos_x%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x22 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_pos_y%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_pos_y%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x24 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_dest_x%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_dest_x%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x26 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_dest_y%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_dest_y%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_BYTE  offset + 0x28 flags
				LPF GET_BIT INT_VAR nth_bit = 0 number = flags RET data = bit_value END
				PATCH_IF ( match >= 0 ) AND ( ~%match_bit_unattached%~ != minus_one ) BEGIN
					PATCH_IF ( ~%match_bit_unattached%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
				END
				LPF GET_BIT INT_VAR nth_bit = 1 number = flags RET data = bit_value END
				PATCH_IF ( match >= 0 ) AND ( ~%match_bit_has_seen%~ != minus_one ) BEGIN
					PATCH_IF ( ~%match_bit_has_seen%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
				END
				LPF GET_BIT INT_VAR nth_bit = 2 number = flags RET data = bit_value END
				PATCH_IF ( match >= 0 ) AND ( ~%match_bit_invulnerable%~ != minus_one ) BEGIN
					PATCH_IF ( ~%match_bit_invulnerable%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
				END
				LPF GET_BIT INT_VAR nth_bit = 3 number = flags RET data = bit_value END
				PATCH_IF ( match >= 0 ) AND ( ~%match_bit_override_script%~ != minus_one ) BEGIN
					PATCH_IF ( ~%match_bit_override_script%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
				END
			READ_SHORT offset + 0x2c data
			PATCH_IF ( match >= 0 ) AND ( ~%match_spawned%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_spawned%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_LONG  offset + 0x30 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_animation%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_animation%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x34 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_orientation%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_orientation%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_LONG  offset + 0x38 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_expiry%~ != minus_two ) BEGIN
				PATCH_IF ( ~%match_expiry%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x3c data
			PATCH_IF ( match >= 0 ) AND ( ~%match_wander%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_wander%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_SHORT offset + 0x3e data
			PATCH_IF ( match >= 0 ) AND ( ~%match_follow%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_follow%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_LONG  offset + 0x44 data
			PATCH_IF ( match >= 0 ) AND ( ~%match_talked_to%~ != minus_one ) BEGIN
				PATCH_IF ( ~%match_talked_to%~ = ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x48 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_dialogue%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_dialogue%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x50 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_override_script%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_override_script%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x58 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_general_script%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_general_script%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x60 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_class_script%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_class_script%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x68 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_race_script%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_race_script%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x70 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_default_script%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_default_script%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x78 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_specifics_script%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_specifics_script%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			READ_ASCII offset + 0x80 data ( 8 ) NULL
			PATCH_IF ( match >= 0 ) AND ( ~%match_cre_file%~ STR_CMP ~same~ ) BEGIN
				PATCH_IF ( ~%match_cre_file%~ STR_EQ ~%data%~ ) BEGIN SET match = 1 END ELSE BEGIN SET match = minus_one END
			END
			PATCH_IF ( match = 1 ) BEGIN
				SET idx = i + 1
				SPRINT file_txt ~%file_txt%%idx%  %offset%  zzz%LNL%~
			END
		END
	END	
END
//BLOCK 
DEFINE_PATCH_FUNCTION ALTER_AREA_ACTOR_EX //0x110
	INT_VAR
		numbers = 2 //0 - first, 1 - last, 2 - all, 3 - position (start =  0)
		position = 10
		
		match_pos_x = "-1"
		match_pos_y = "-1"
		match_dest_x = "-1"
		match_dest_y = "-1"
		match_bit_unattached = "-1"
		match_bit_has_seen = "-1"
		match_bit_invulnerable = "-1"
		match_bit_override_script = "-1"
		match_spawned = "-1"
		match_animation = "-1"
		match_orientation = "-1"
		match_expiry = "-2"
		match_wander = "-1"
		match_follow = "-1"
		match_talked_to = "-1"
		
		pos_x = "-1"
		pos_y = "-1"
		dest_x = "-1"
		dest_y = "-1"
		bit_unattached = 1
		bit_has_seen = 0
		bit_invulnerable = 0
		bit_override_script = 0
		spawned = "-1"
		animation = "-1"
		orientation = "-1"
		expiry = "-2"
		wander = "-1"
		follow = "-1"
		talked_to = "-1"
	STR_VAR
		match_name = "same"
		match_dialogue = "same"
		match_override_script = "same"
		match_general_script = "same"
		match_class_script = "same"
		match_race_script = "same"
		match_default_script = "same"
		match_specifics_script = "same"
		match_cre_file = "same"
		
		name = "same"
		dialogue = "same"
		override_script = "same"
		general_script = "same"
		class_script = "same"
		race_script = "same"
		default_script = "same"
		specifics_script = "same"
		cre_file = "same"
BEGIN
	PATCH_IF ( numbers < 0 ) OR ( numbers > 3 ) BEGIN SET numbers = 2 END
	PATCH_IF ( numbers != 3 ) BEGIN SET position = ( 0 - 1 ) END
	LPF MATCH_ALTER_ACTOR
		INT_VAR match_pos_x match_pos_y match_dest_x match_dest_y match_bit_unattached match_bit_has_seen match_bit_invulnerable match_bit_override_script match_spawned match_animation match_orientation match_expiry match_wander match_follow match_talked_to pos_x pos_y dest_x dest_y bit_unattached bit_has_seen bit_invulnerable bit_override_script spawned animation orientation expiry wander follow talked_to
		STR_VAR match_name match_dialogue match_override_script match_general_script match_class_script match_race_script match_default_script match_specifics_script match_cre_file name dialogue override_script general_script class_script race_script default_script specifics_script cre_file
		RET file_txt
	END
	CLEAR_ARRAY num_items_array
	INNER_ACTION BEGIN
		OUTER_INNER_PATCH_SAVE ~file_txt~ ~%file_txt%~ BEGIN
			COUNT_2DA_ROWS 3 num_items
			FOR ( i = 0 ; i < num_items ; ++i ) BEGIN
				READ_2DA_ENTRY i 0 3 numx
				READ_2DA_ENTRY i 1 3 offset
				SET EVAL ~num_items_array_%i%~ = ~%offset%~
			END
		END
	END
	SET minus_one = ( 0 - 1 )
	SET minus_two = ( 0 - 2 )
	PATCH_IF ( num_items > 0 ) BEGIN
		PATCH_MATCH numbers WITH
			0 BEGIN
				SET num_items = 1
			END
			1 BEGIN
				SET idx = num_items - 1
				SET num_items = 1
				SET num_items_array_0 = EVAL ~num_items_array_%idx%~
			END
			2 BEGIN
			END
			3 BEGIN
				PATCH_IF ( position > num_items ) BEGIN SET position = num_items - 1 END
				SET num_items = 1
				SET num_items_array_0 = EVAL ~num_items_array_%position%~
			END
			DEFAULT
		END
		FOR ( i = 0 ; i < num_items ; ++i ) BEGIN
			SET offset = EVAL ~num_items_array_%i%~
			PATCH_IF ( ~%name%~             STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x00 ~%name%~ (32)            END
			PATCH_IF ( pos_x != minus_one )                  BEGIN WRITE_SHORT           offset + 0x20 pos_x                    END
			PATCH_IF ( pos_y != minus_one )                  BEGIN WRITE_SHORT           offset + 0x22 pos_y                    END
			PATCH_IF ( dest_x != minus_one )                 BEGIN WRITE_SHORT           offset + 0x24 dest_x                   END
			PATCH_IF ( dest_y != minus_one )                 BEGIN WRITE_SHORT           offset + 0x26 dest_y                   END
			READ_BYTE 0x28 flags
			//BLOCK 
			SET set_flags = flags
			PATCH_IF ( bit_unattached != minus_one ) BEGIN
				LPF GET_BIT INT_VAR nth_bit = 0 number = flags RET bit_value END
				PATCH_IF ( bit_value ) BEGIN
					PATCH_IF ( bit_unattached = 0 ) BEGIN SET set_flags = set_flags - 1       END
				END ELSE BEGIN
					PATCH_IF ( bit_unattached != 0 ) BEGIN SET set_flags = set_flags + 1      END
				END
			END
			PATCH_IF ( bit_has_seen != minus_one ) BEGIN
				LPF GET_BIT INT_VAR nth_bit = 1 number = flags RET bit_value END
				PATCH_IF ( bit_value ) BEGIN
					PATCH_IF ( bit_has_seen = 0 ) BEGIN SET set_flags = set_flags - 2         END
				END ELSE BEGIN
					PATCH_IF ( bit_has_seen != 0 ) BEGIN SET set_flags = set_flags + 2        END
				END
			END
			PATCH_IF ( bit_invulnerable != minus_one ) BEGIN
				LPF GET_BIT INT_VAR nth_bit = 2 number = flags RET bit_value END
				PATCH_IF ( bit_value ) BEGIN
					PATCH_IF ( bit_invulnerable = 0 ) BEGIN SET set_flags = set_flags - 4     END
				END ELSE BEGIN
					PATCH_IF ( bit_invulnerable != 0 ) BEGIN SET set_flags = set_flags + 4    END
				END
			END
			PATCH_IF ( bit_override_script != minus_one ) BEGIN
				LPF GET_BIT INT_VAR nth_bit = 3 number = flags RET bit_value END
				PATCH_IF ( bit_value ) BEGIN
					PATCH_IF ( bit_override_script = 0 ) BEGIN SET set_flags = set_flags - 8  END
				END ELSE BEGIN
					PATCH_IF ( bit_override_script != 0 ) BEGIN SET set_flags = set_flags + 8 END
				END
			END
			//BLOCKEND
			WRITE_BYTE 0x28 set_flags
			PATCH_IF ( spawned != minus_one )                BEGIN WRITE_SHORT           offset + 0x2c spawned                  END
			PATCH_IF ( animation != minus_one )              BEGIN WRITE_LONG            offset + 0x30 animation                END
			PATCH_IF ( orientation != minus_one )            BEGIN WRITE_SHORT           offset + 0x34 orientation              END
			PATCH_IF ( expiry != minus_two )                 BEGIN WRITE_LONG            offset + 0x38 expiry                   END
			PATCH_IF ( wander != minus_one )                 BEGIN WRITE_SHORT           offset + 0x3c wander                   END
			PATCH_IF ( follow != minus_one )                 BEGIN WRITE_SHORT           offset + 0x3e follow                   END
			PATCH_IF ( talked_to != minus_one )              BEGIN WRITE_LONG            offset + 0x44 talked_to                END
			PATCH_IF ( ~%dialogue%~         STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x48 ~%dialogue%~ (8)         END
			PATCH_IF ( ~%override_script%~  STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x50 ~%override_script%~ (8)  END
			PATCH_IF ( ~%general_script%~   STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x58 ~%general_script%~ (8)   END
			PATCH_IF ( ~%class_script%~     STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x60 ~%class_script%~ (8)     END
			PATCH_IF ( ~%race_script%~      STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x68 ~%race_script%~ (8)      END
			PATCH_IF ( ~%default_script%~   STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x70 ~%default_script%~ (8)   END
			PATCH_IF ( ~%specifics_script%~ STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x78 ~%specifics_script%~ (8) END
			PATCH_IF ( ~%cre_file%~         STR_CMP ~same~ ) BEGIN WRITE_EVALUATED_ASCII offset + 0x80 ~%cre_file%~ (8)         END
		END
	END
END
//BLOCKEND
//BLOCKEND
//BLOCK  GET_SCRIPT_BLOCK
DEFINE_PATCH_FUNCTION GET_SCRIPT_BLOCK
INT_VAR
  start_offset  = 0
  reverse       = 0
STR_VAR
  search_regexp = ~~
RET
  script_block
  start_offset
  end_offset
BEGIN
  SET offset = start_offset
  PATCH_IF (offset < 0) BEGIN SET offset = 0 END
  PATCH_IF (offset > BUFFER_LENGTH) BEGIN SET offset = BUFFER_LENGTH END

  TEXT_SPRINT script_block ~~
  SET start_offset = "-1"
  SET end_offset = "-1"

  PATCH_IF (NOT ~%search_regexp%~ STR_EQ ~~) BEGIN
    PATCH_IF (reverse) BEGIN
      SET offset = RINDEX_BUFFER(~%search_regexp%~ offset)
    END ELSE BEGIN
      SET offset = INDEX_BUFFER(~%search_regexp%~ offset)
    END

    PATCH_IF (offset >= 0) BEGIN
      SET ofsStart = RINDEX_BUFFER(~^IF$~ offset)
      PATCH_IF (ofsStart >= 0) BEGIN
        SET ofsEnd = INDEX_BUFFER(~^END$~ offset)
        PATCH_IF (ofsEnd >= 0) BEGIN
          SET ofsEnd += 3
          READ_ASCII ofsStart script_block (ofsEnd - ofsStart)
          SET start_offset = ofsStart
          SET end_offset = ofsEnd
        END
      END
    END
  END
END
//BLOCKEND


