COPY_EXISTING ~AR0400.are~  ~override~
	PATCH_IF (SOURCE_SIZE > GW_are_min_size) THEN BEGIN
// New travel trigger.
// -------------------
		LPF fj_are_structure
		INT_VAR fj_type = 2					// the region type (0=trap, 1=info, 2=travel)
			fj_flags = (1 << 11)			// the bitwise region flags (blocked = connected to door)
			fj_box_left = 2995				// the leftmost X coordinate of the region’s bounding box
			fj_box_top = 1527				// the topmost Y coordinate of the region’s bounding box
			fj_box_right = 3139				// the rightmost X coordinate of the region’s bounding box
			fj_box_bottom = 1645			// the bottommost Y coordinate of the region’s bounding box
			fj_cursor_idx = 28				// the region’s mouse cursor index (from cursors.bam) : stairs
			fj_loc_x = 3072					// the trap launch X coordinate
			fj_loc_y = 1586					// the trap launch Y coordinate
//			fj_alt_x = 					// the activation point X coordinate
//			fj_alt_y = 					// the activation point Y coordinate
			fj_vertex_0 = (3073 + (1645 << 16))
			fj_vertex_1 = (3139 + (1605 << 16))
			fj_vertex_2 = (3061 + (1527 << 16))
			fj_vertex_3 = (2995 + (1567 << 16))
		STR_VAR fj_structure_type = region
			fj_destination_area = "L#NS01"	// the destination area resource reference (for travel regions);
			fj_destination_name = En1		// the entrance name in the destination area (for travel regions);
			fj_name = TranL#NS01			// the region’s name
		END

// New entrance.
// -------------
		LPF fj_are_structure
		INT_VAR fj_loc_x = 2941		// the X coordinate
				fj_loc_y = 1490		// the Y coordinate
				fj_orientation = 6	// the facing direction (northwest)
		STR_VAR fj_structure_type = entrance
				fj_name = L#NSEn	// the entrance’s name
		END

// We read the new area structure (because it has been modified).
// --------------------------------------------------------------
		LPF ~GW_READ_AREA_STRUCTURE~ RET GW_are_doors_num GW_are_doors_off GW_are_vertices_off GW_are_vertices_num 
		GW_are_wed GW_are_flag2 END

/* Control debug : useless when the function works fine !
LPF TO_HEX_NUMBER INT_VAR value = GW_are_doors_off minDigits = 1 prefix = 1 RET off_door = hexNumber END
LPF TO_HEX_NUMBER INT_VAR value = GW_are_vertices_off minDigits = 1 prefix = 1 RET off_vert = hexNumber END
PATCH_LOG ~CONTROL %SOURCE_FILE% :
%GW_are_doors_num% doors		- offset doors		= %GW_are_doors_off% (%off_door%)
%GW_are_vertices_num% vertices	- offset vertices	= %GW_are_vertices_off% (%off_vert%)~
*/
// We store the door settings and delete it.
// -----------------------------------------
		SPRINT  GW_Area "AR0400"
		SPRINT	GW_Door "Grate01"
		LPF ~GW_CLONE_AREA_DOOR~ INT_VAR GW_Delete = 1 STR_VAR GW_Area GW_Door 
		RET GW_match_door fj_flags fj_open_box_left fj_open_box_top fj_open_box_right fj_open_box_bottom fj_closed_box_left fj_closed_box_top fj_closed_box_right fj_closed_box_bottom
		fj_cursor_idx fj_trap_detect fj_trap_remove fj_trap_active fj_trap_status fj_trap_loc_x fj_trap_loc_y fj_detect_diff fj_locked_diff fj_open_loc_x fj_open_loc_y 
		fj_closed_loc_x fj_closed_loc_y fj_lockpick_strref fj_name fj_door_wed_id fj_door_open_wav fj_door_close_wav fj_key_resref fj_door_script fj_travel_trigger fj_dlg_strref fj_dlg_resref END

		PATCH_IF GW_match_door BEGIN
/* Control debug : useless when the function works fine !
PATCH_LOG "Door : %GW_Door% - %GW_Area%"
PATCH_LOG ~CONTROL 3 FUNCTION GW_CLONE_AREA_DOOR ==> settings of door %GW_Door% in %SOURCE_FILE% :
======================================================
- Name = %fj_name%	- Door ID (to link with WED) = %fj_door_wed_id%	- Flags = %fj_flags%
- Minimum bounding box of the door polygon (when open)	 = Left (%fj_open_box_left%)	- Right (%fj_open_box_right%)	- Top (%fj_open_box_top%)	- Bottom (%fj_open_box_bottom%)
- Minimum bounding box of the door polygon (when closed) = Left (%fj_closed_box_left%)	- Right (%fj_closed_box_right%)	- Top (%fj_closed_box_top%)	- Bottom (%fj_closed_box_bottom%)
- Door sounds = open (%fj_door_open_wav%) - close (%fj_door_close_wav%)
- Cursor index (cursors.bam) = %fj_cursor_idx%
- Trap difficulty = detection (%fj_trap_detect%) - removal (%fj_trap_remove%) - Door is trapped (0=No, 1=Yes) = %fj_trap_active%
- Trap detected (0=No, 1=Yes) = %fj_trap_status% - Trap launch target = X coordinate (%fj_trap_loc_x%) - Y coordinate (%fj_trap_loc_y%)
- Door script = %fj_door_script%
- Detection difficulty (secret doors) = %fj_detect_diff% - Lock difficulty (0-100) = %fj_locked_diff% - Key item = %fj_key_resref%
- Location to open the door : when open [X (%fj_open_loc_x%) - Y (%fj_open_loc_y%)]	--- when closed [X (%fj_closed_loc_x%) - Y (%fj_closed_loc_y%)].
- Lock-pick string = %fj_lockpick_strref%
- Travel trigger name = %fj_travel_trigger%
- Dialog speaker name = %fj_dlg_strref%	- Dialog resref = %fj_dlg_resref%~*/

// We adjust a few door settings.
// ------------------------------
			SET fj_flags = ((1 << 1) | (1 << 6) | (1 << 9))	//  | (1 << 10 [locked, located = linked, can be looked through] Uses key, 
/* useless --> auto-generate by the macro !
			SET fj_open_box_left		= 3011		// Minimum bounding box of the door polygon (when open)
			SET fj_open_box_top			= 1486		// Minimum bounding box of the door polygon (when open)
			SET fj_open_box_right		= 3137		// Minimum bounding box of the door polygon (when open)
			SET fj_open_box_bottom		= 1644		// Minimum bounding box of the door polygon (when open)
			SET fj_closed_box_left		= 2995		// Minimum bounding box of the door polygon (when closed)
			SET fj_closed_box_top		= 1527		// Minimum bounding box of the door polygon (when closed)
			SET fj_closed_box_right		= 3139		// Minimum bounding box of the door polygon (when closed)
			SET fj_closed_box_bottom	= 1645		// Minimum bounding box of the door polygon (when closed)
			SET	fj_trap_loc_x						// Trap launch target X coordinate
			SET	fj_trap_loc_y						// Trap launch target Y coordinate */
			SET fj_locked_diff			= 100		// Lock difficulty (0-100)
			SPRINT fj_key_resref "L#NSKey"			// Key item
//			SPRINT fj_door_script = 				// Door script
//			SET fj_lockpick_strref = 				// Lock-pick string
			SPRINT fj_travel_trigger "TranL#NS01"	// Travel trigger name

// We add the new door which replaces the original one.
// ----------------------------------------------------
			LPM ~GW_ADD_AREA_DOOR~ 
/* Control debug : useless when the function works fine !
PATCH_LOG ~CONTROL 4 ==> settings of door %GW_Door% in %SOURCE_FILE% :
======================================================
- Name = %fj_name%	- Door ID (to link with WED) = %fj_door_wed_id%	- Flags = %fj_flags%
- Minimum bounding box of the door polygon (when open)	 = Left (%fj_open_box_left%)	- Right (%fj_open_box_right%)	- Top (%fj_open_box_top%)	- Bottom (%fj_open_box_bottom%)
- Minimum bounding box of the door polygon (when closed) = Left (%fj_closed_box_left%)	- Right (%fj_closed_box_right%)	- Top (%fj_closed_box_top%)	- Bottom (%fj_closed_box_bottom%)
- Door sounds = open (%fj_door_open_wav%) - close (%fj_door_close_wav%)
- Cursor index (cursors.bam) = %fj_cursor_idx%
- Trap difficulty = detection (%fj_trap_detect%) - removal (%fj_trap_remove%) - Door is trapped (0=No, 1=Yes) = %fj_trap_active%
- Trap detected (0=No, 1=Yes) = %fj_trap_status% - Trap launch target = X coordinate (%fj_trap_loc_x%) - Y coordinate (%fj_trap_loc_y%)
- Door script = %fj_door_script%
- Detection difficulty (secret doors) = %fj_detect_diff% - Lock difficulty (0-100) = %fj_locked_diff% - Key item = %fj_key_resref%
- Location to open the door : when open [X (%fj_open_loc_x%) - Y (%fj_open_loc_y%)]	--- when closed [X (%fj_closed_loc_x%) - Y (%fj_closed_loc_y%)].
- Lock-pick string = %fj_lockpick_strref%
- Travel trigger name = %fj_travel_trigger%
- Dialog speaker name = %fj_dlg_strref%	- Dialog resref = %fj_dlg_resref%~
PATCH_LOG "Door : %GW_Door% - %GW_Area%" */

		END	// of PATCH_IF GW_match_door BEGIN

	END		// of PATCH_IF (SOURCE_SIZE > GW_are_min_size)
BUT_ONLY


COPY_EXISTING	~AR0400.wed~	~override/AR0400.wed~
				~AR0400N.wed~	~override/AR0400N.wed~
	LPF ~GW_READ_WED_STRUCTURE~ RET	GW_wed_overlays_num GW_wed_doors_num GW_wed_overlays_off GW_wed_2dheader_off GW_wed_doors_off GW_wed_door_tilecell_off	// Header
									GW_wed_wall_polygons_num GW_wed_polygons_off GW_wed_vertices_off GW_wed_wallgroups_off GW_wed_poly_lookup_table_off END	// Secondary header
		wed_size = SOURCE_SIZE
// 1. First, we read the overly structure and fix the wallgroups values.
// ---------------------------------------------------------------------
	LPF ~GW_READ_OVERLAY_STRUCTURE~ INT_VAR GW_overlay_num = 0 RET GW_wed_overlay_width GW_wed_overlay_height END
	LPM ~GW_DRAW_WED_WALLGROUPS~

// 2. Second, we add a new door in the WED file.
// ---------------------------------------------
	LPF ~GW_ADD_WED_DOOR~ STR_VAR GW_Wed = EVAL "%SOURCE_RES%" GW_Door = "Grate01" RET GW_output_wallgroups END

// 3. Third, we fix and update the wall groups and wall polygon index tables.
// ---------------------------------------------------------------------------
	LPF ~GW_READ_WED_STRUCTURE~ RET	GW_wed_overlays_num GW_wed_doors_num GW_wed_overlays_off GW_wed_2dheader_off GW_wed_doors_off GW_wed_door_tilecell_off	// Header
									GW_wed_wall_polygons_num GW_wed_polygons_off GW_wed_vertices_off GW_wed_wallgroups_off GW_wed_poly_lookup_table_off END	// Secondary header
	LPF ~GW_FIX_POLYGONS_IN_WALLGROUPS~ STR_VAR GW_array = EVAL "GW_fix_%SOURCE_RES%_wed_wallgroups" END
	LPF ~GW_FIX_POLYGONS_IN_WALLGROUPS~ INT_VAR GW_Add_new_polygon = 1 STR_VAR array = EVAL "%GW_output_wallgroups%" END
BUT_ONLY
