@@ -6,17 +6,25 @@ procedure start;
66procedure map_enter_p_proc;
77procedure set_door_flag(variable state);
88procedure gamemode_handler;
9+ procedure autoclose;
10+ procedure close_door(variable door);
911
1012#define PORTAL (0)
1113#define DOOR_FLAGS (0x24)
14+
15+ /**
16+ * If there's any party member within this distance, door won't autoclose.
17+ */
18+ #define DOOR_CLOSE_DIST 5
19+
1220#define set_autodoors "autodoors"
1321variable only_once = 0;
1422variable enabled = 0;
1523
1624procedure start begin
1725 if game_loaded then begin
1826 enabled = fo2tweaks_setting(sec_main, set_autodoors);
19- if enabled == 1 then begin
27+ if enabled > 0 then begin
2028 register_hook_proc(HOOK_GAMEMODECHANGE, gamemode_handler);
2129 call map_enter_p_proc;
2230 ndebug("initialized");
@@ -25,7 +33,12 @@ procedure start begin
2533end
2634
2735procedure map_enter_p_proc begin
28- if enabled == 1 then call set_door_flag(FLAG_WALKTHRU);
36+ if enabled > 0 then call set_door_flag(FLAG_WALKTHRU);
37+ end
38+
39+ procedure map_update_p_proc begin
40+ if combat_is_initialized then return;
41+ if enabled > 1 then call autoclose;
2942end
3043
3144procedure set_door_flag(variable flag) begin
@@ -47,3 +60,26 @@ procedure gamemode_handler begin
4760 ndebug("combat started, disabled walking through doors");
4861 end
4962end
63+
64+ /**
65+ * Iterate over scenery, call close_door on each door.
66+ */
67+ procedure autoclose() begin
68+ variable obj;
69+ foreach (obj in list_as_array(LIST_SCENERY)) begin
70+ if is_door(obj) then call close_door(obj);
71+ end
72+ end
73+
74+ /**
75+ * Closes the door, if there are no nearby party members.
76+ */
77+ procedure close_door(variable door) begin
78+ variable who;
79+ if not (obj_is_open(door)) then return;
80+ if (tile_distance_objs(dude_obj, door) <= DOOR_CLOSE_DIST) then return;
81+ foreach who in party_member_list_critters begin
82+ if (tile_distance_objs(who, door) <= DOOR_CLOSE_DIST) then return;
83+ end
84+ obj_close(door);
85+ end
0 commit comments