| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Triggers and Switches. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <type_traits> |
| 29 |
#include <physfs.h> |
| 30 |
#include "maths.h" |
| 31 |
|
| 32 |
#include "pack.h" |
| 33 |
#include "fwd-object.h" |
| 34 |
#include "fwd-segment.h" |
| 35 |
#include "fwd-valptridx.h" |
| 36 |
#include "valptridx.h" |
| 37 |
#include "dsx-ns.h" |
| 38 |
#include "fwd-player.h" |
| 39 |
#include "fwd-window.h" |
| 40 |
#include <array> |
| 41 |
|
| 42 |
namespace dcx { |
| 43 |
constexpr std::integral_constant<std::size_t, 100> MAX_TRIGGERS{}; |
| 44 |
constexpr std::integral_constant<unsigned, 10> MAX_WALLS_PER_LINK{}; |
| 45 |
} |
| 46 |
|
| 47 |
#ifdef DXX_BUILD_DESCENT |
| 48 |
// Trigger types |
| 49 |
enum class trigger_action : uint8_t |
| 50 |
{ |
| 51 |
open_door = 0, // Open a door |
| 52 |
matcen = 2, // Activate a matcen |
| 53 |
normal_exit = 3, // End the level |
| 54 |
secret_exit = 4, // Go to secret level |
| 55 |
illusion_off = 5, // Turn an illusion off |
| 56 |
illusion_on = 6, // Turn an illusion on |
| 57 |
#if DXX_BUILD_DESCENT == 2 |
| 58 |
close_door = 1, // Close a door |
| 59 |
unlock_door = 7, // Unlock a door |
| 60 |
lock_door = 8, // Lock a door |
| 61 |
open_wall = 9, // Makes a wall open |
| 62 |
close_wall = 10, // Makes a wall closed |
| 63 |
illusory_wall = 11, // Makes a wall illusory |
| 64 |
light_off = 12, // Turn a light off |
| 65 |
light_on = 13, // Turn a light on |
| 66 |
#endif |
| 67 |
}; |
| 68 |
|
| 69 |
#if DXX_BUILD_DESCENT == 2 |
| 70 |
#define NUM_TRIGGER_TYPES 14 |
| 71 |
|
| 72 |
// Trigger flags |
| 73 |
|
| 74 |
//could also use flags for one-shots |
| 75 |
|
| 76 |
enum class trigger_behavior_flags : uint8_t |
| 77 |
{ |
| 78 |
no_message = 1, // Don't show a message when triggered |
| 79 |
one_shot = 2, // Only trigger once |
| 80 |
disabled = 4, // Set after one-shot fires |
| 81 |
}; |
| 82 |
|
| 83 |
enum class trigger_behavior_flag_mask : uint8_t; |
| 84 |
|
| 85 |
static constexpr trigger_behavior_flag_mask operator~(const trigger_behavior_flags value) |
| 86 |
{ |
| 87 |
return static_cast<trigger_behavior_flag_mask>(~static_cast<uint8_t>(value)); |
| 88 |
} |
| 89 |
|
| 90 |
static constexpr uint8_t operator&(const trigger_behavior_flags value, const trigger_behavior_flags mask) |
| 91 |
{ |
| 92 |
return static_cast<uint8_t>(value) & static_cast<uint8_t>(mask); |
| 93 |
} |
| 94 |
|
| 95 |
static constexpr trigger_behavior_flags &operator|=(trigger_behavior_flags &value, const trigger_behavior_flags mask) |
| 96 |
{ |
| 97 |
value = static_cast<trigger_behavior_flags>(static_cast<uint8_t>(value) | static_cast<uint8_t>(mask)); |
| 98 |
return value; |
| 99 |
} |
| 100 |
|
| 101 |
static constexpr trigger_behavior_flags &operator&=(trigger_behavior_flags &value, const trigger_behavior_flag_mask mask) |
| 102 |
{ |
| 103 |
value = static_cast<trigger_behavior_flags>(static_cast<uint8_t>(value) & static_cast<uint8_t>(mask)); |
| 104 |
return value; |
| 105 |
} |
| 106 |
|
| 107 |
//old trigger structs |
| 108 |
|
| 109 |
struct v29_trigger : prohibit_void_ptr<v29_trigger> |
| 110 |
{ |
| 111 |
sbyte type; |
| 112 |
short flags; |
| 113 |
fix value; |
| 114 |
sbyte link_num; |
| 115 |
short num_links; |
| 116 |
std::array<segnum_t, MAX_WALLS_PER_LINK> seg; |
| 117 |
std::array<sidenum_t, MAX_WALLS_PER_LINK> side; |
| 118 |
}; |
| 119 |
|
| 120 |
struct v30_trigger : prohibit_void_ptr<v30_trigger> |
| 121 |
{ |
| 122 |
short flags; |
| 123 |
sbyte num_links; |
| 124 |
sbyte pad; //keep alignment |
| 125 |
fix value; |
| 126 |
std::array<segnum_t, MAX_WALLS_PER_LINK> seg; |
| 127 |
std::array<sidenum_t, MAX_WALLS_PER_LINK> side; |
| 128 |
}; |
| 129 |
#endif |
| 130 |
|
| 131 |
enum TRIGGER_FLAG : uint16_t |
| 132 |
{ |
| 133 |
//flags for V30 & below triggers |
| 134 |
CONTROL_DOORS = 1, // Control Trigger |
| 135 |
SHIELD_DAMAGE = 2, // Shield Damage Trigger |
| 136 |
ENERGY_DRAIN = 4, // Energy Drain Trigger |
| 137 |
EXIT = 8, // End of level Trigger |
| 138 |
ONE_SHOT = 32, // If Trigger can only be triggered once |
| 139 |
MATCEN = 64, // Trigger for materialization centers |
| 140 |
ILLUSION_OFF = 128, // Switch Illusion OFF trigger |
| 141 |
SECRET_EXIT = 256, // Exit to secret level |
| 142 |
ILLUSION_ON = 512, // Switch Illusion ON trigger |
| 143 |
#if DXX_BUILD_DESCENT == 2 |
| 144 |
UNLOCK_DOORS = 1024, // Unlocks a door |
| 145 |
OPEN_WALL = 2048, // Makes a wall open |
| 146 |
CLOSE_WALL = 4096, // Makes a wall closed |
| 147 |
ILLUSORY_WALL = 8192, // Makes a wall illusory |
| 148 |
#endif |
| 149 |
}; |
| 150 |
|
| 151 |
#define TRIGGER_CONTROL_DOORS TRIGGER_FLAG::CONTROL_DOORS |
| 152 |
#define TRIGGER_SHIELD_DAMAGE TRIGGER_FLAG::SHIELD_DAMAGE |
| 153 |
#define TRIGGER_ENERGY_DRAIN TRIGGER_FLAG::ENERGY_DRAIN |
| 154 |
#define TRIGGER_EXIT TRIGGER_FLAG::EXIT |
| 155 |
#define TRIGGER_ONE_SHOT TRIGGER_FLAG::ONE_SHOT |
| 156 |
#define TRIGGER_MATCEN TRIGGER_FLAG::MATCEN |
| 157 |
#define TRIGGER_ILLUSION_OFF TRIGGER_FLAG::ILLUSION_OFF |
| 158 |
#define TRIGGER_SECRET_EXIT TRIGGER_FLAG::SECRET_EXIT |
| 159 |
#define TRIGGER_ILLUSION_ON TRIGGER_FLAG::ILLUSION_ON |
| 160 |
#if DXX_BUILD_DESCENT == 2 |
| 161 |
#define TRIGGER_UNLOCK_DOORS TRIGGER_FLAG::UNLOCK_DOORS |
| 162 |
#define TRIGGER_OPEN_WALL TRIGGER_FLAG::OPEN_WALL |
| 163 |
#define TRIGGER_CLOSE_WALL TRIGGER_FLAG::CLOSE_WALL |
| 164 |
#define TRIGGER_ILLUSORY_WALL TRIGGER_FLAG::ILLUSORY_WALL |
| 165 |
#endif |
| 166 |
|
| 167 |
//the trigger really should have both a type & a flags, since most of the |
| 168 |
//flags bits are exclusive of the others. |
| 169 |
enum class trgnum_t : uint8_t |
| 170 |
{ |
| 171 |
None = UINT8_MAX |
| 172 |
}; |
| 173 |
|
| 174 |
namespace dsx { |
| 175 |
|
| 176 |
struct trigger : public prohibit_void_ptr<trigger> |
| 177 |
{ |
| 178 |
#if DXX_BUILD_DESCENT == 1 |
| 179 |
uint16_t flags; |
| 180 |
#elif DXX_BUILD_DESCENT == 2 |
| 181 |
trigger_action type; //what this trigger does |
| 182 |
trigger_behavior_flags flags; |
| 183 |
#endif |
| 184 |
uint8_t num_links; //how many doors, etc. linked to this |
| 185 |
fix value; |
| 186 |
std::array<segnum_t, MAX_WALLS_PER_LINK> seg; |
| 187 |
std::array<sidenum_t, MAX_WALLS_PER_LINK> side; |
| 188 |
}; |
| 189 |
|
| 190 |
} |
| 191 |
DXX_VALPTRIDX_DECLARE_SUBTYPE(dsx::, trigger, trgnum_t, MAX_TRIGGERS); |
| 192 |
namespace dsx { |
| 193 |
DXX_VALPTRIDX_DEFINE_SUBTYPE_TYPEDEFS(trigger, trg); |
| 194 |
|
| 195 |
struct d_level_unique_trigger_state |
| 196 |
{ |
| 197 |
valptridx<trigger>::array_managed_type Triggers; |
| 198 |
}; |
| 199 |
} |
| 200 |
|
| 201 |
constexpr std::integral_constant<trgnum_t, trgnum_t::None> trigger_none{}; |
| 202 |
|
| 203 |
namespace dsx { |
| 204 |
window_event_result check_trigger(vcsegptridx_t seg, sidenum_t side, object &plrobj, vcobjptridx_t objnum, int shot); |
| 205 |
window_event_result check_trigger_sub(object &, trgnum_t trigger_num, playernum_t player_num, unsigned shot); |
| 206 |
|
| 207 |
static inline int trigger_is_exit(const trigger *t) |
| 208 |
{ |
| 209 |
#if DXX_BUILD_DESCENT == 1 |
| 210 |
return t->flags & TRIGGER_EXIT; |
| 211 |
#elif DXX_BUILD_DESCENT == 2 |
| 212 |
return t->type == trigger_action::normal_exit; |
| 213 |
#endif |
| 214 |
} |
| 215 |
|
| 216 |
static inline int trigger_is_matcen(const trigger &t) |
| 217 |
{ |
| 218 |
#if DXX_BUILD_DESCENT == 1 |
| 219 |
return t.flags & TRIGGER_MATCEN; |
| 220 |
#elif DXX_BUILD_DESCENT == 2 |
| 221 |
return t.type == trigger_action::matcen; |
| 222 |
#endif |
| 223 |
} |
| 224 |
|
| 225 |
#if DXX_BUILD_DESCENT == 1 |
| 226 |
void v25_trigger_read(NamedPHYSFS_File fp, trigger *); |
| 227 |
void v26_trigger_read(NamedPHYSFS_File fp, trigger &); |
| 228 |
#endif |
| 229 |
|
| 230 |
#if DXX_BUILD_DESCENT == 2 |
| 231 |
/* |
| 232 |
* reads a v29_trigger structure from a PHYSFS_File |
| 233 |
*/ |
| 234 |
void v29_trigger_read(v29_trigger *t, NamedPHYSFS_File fp); |
| 235 |
|
| 236 |
/* |
| 237 |
* reads a v30_trigger structure from a PHYSFS_File |
| 238 |
*/ |
| 239 |
void v30_trigger_read(v30_trigger *t, NamedPHYSFS_File fp); |
| 240 |
|
| 241 |
/* |
| 242 |
* reads a trigger structure from a PHYSFS_File |
| 243 |
*/ |
| 244 |
void v29_trigger_read_as_v31(NamedPHYSFS_File fp, trigger &t); |
| 245 |
void v30_trigger_read_as_v31(NamedPHYSFS_File fp, trigger &t); |
| 246 |
#endif |
| 247 |
|
| 248 |
void trigger_read(NamedPHYSFS_File fp, trigger &t); |
| 249 |
void trigger_write(PHYSFS_File *fp, const trigger &t); |
| 250 |
|
| 251 |
void v29_trigger_write(PHYSFS_File *fp, const trigger &t); |
| 252 |
void v30_trigger_write(PHYSFS_File *fp, const trigger &t); |
| 253 |
void v31_trigger_write(PHYSFS_File *fp, const trigger &t); |
| 254 |
} |
| 255 |
#endif |