Subversion Repositories Games.Descent

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 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
#ifdef __cplusplus
33
#include "pack.h"
34
#include "fwd-object.h"
35
#include "fwd-segment.h"
36
#include "fwd-valptridx.h"
37
#include "valptridx.h"
38
#include "dsx-ns.h"
39
#include "fwd-player.h"
40
#include "fwd-window.h"
41
#include <array>
42
 
43
namespace dcx {
44
constexpr std::integral_constant<std::size_t, 100> MAX_TRIGGERS{};
45
constexpr std::integral_constant<unsigned, 10> MAX_WALLS_PER_LINK{};
46
}
47
 
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 defined(DXX_BUILD_DESCENT_II)
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 defined(DXX_BUILD_DESCENT_II)
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
};
86
 
87
static constexpr trigger_behavior_flag_mask operator~(const trigger_behavior_flags value)
88
{
89
        return static_cast<trigger_behavior_flag_mask>(~static_cast<uint8_t>(value));
90
}
91
 
92
static constexpr uint8_t operator&(const trigger_behavior_flags value, const trigger_behavior_flags mask)
93
{
94
        return static_cast<uint8_t>(value) & static_cast<uint8_t>(mask);
95
}
96
 
97
static constexpr trigger_behavior_flags &operator|=(trigger_behavior_flags &value, const trigger_behavior_flags mask)
98
{
99
        value = static_cast<trigger_behavior_flags>(static_cast<uint8_t>(value) | static_cast<uint8_t>(mask));
100
        return value;
101
}
102
 
103
static constexpr trigger_behavior_flags &operator&=(trigger_behavior_flags &value, const trigger_behavior_flag_mask mask)
104
{
105
        value = static_cast<trigger_behavior_flags>(static_cast<uint8_t>(value) & static_cast<uint8_t>(mask));
106
        return value;
107
}
108
 
109
//old trigger structs
110
 
111
struct v29_trigger
112
{
113
        sbyte   type;
114
        short   flags;
115
        fix     value;
116
        fix     time;
117
        sbyte   link_num;
118
        short   num_links;
119
        std::array<segnum_t, MAX_WALLS_PER_LINK>   seg;
120
        std::array<short, MAX_WALLS_PER_LINK>   side;
121
} __pack__;
122
 
123
struct v30_trigger
124
{
125
        short   flags;
126
        sbyte   num_links;
127
        sbyte   pad;                        //keep alignment
128
        fix     value;
129
        fix     time;
130
        std::array<segnum_t, MAX_WALLS_PER_LINK>   seg;
131
        std::array<short, MAX_WALLS_PER_LINK>   side;
132
} __pack__;
133
#endif
134
 
135
enum TRIGGER_FLAG : uint16_t
136
{
137
//flags for V30 & below triggers
138
        CONTROL_DOORS =      1,    // Control Trigger
139
        SHIELD_DAMAGE =      2,    // Shield Damage Trigger
140
        ENERGY_DRAIN =       4,    // Energy Drain Trigger
141
        EXIT =               8,    // End of level Trigger
142
        ONE_SHOT =          32,    // If Trigger can only be triggered once
143
        MATCEN =            64,    // Trigger for materialization centers
144
        ILLUSION_OFF =     128,    // Switch Illusion OFF trigger
145
        SECRET_EXIT =      256,    // Exit to secret level
146
        ILLUSION_ON =      512,    // Switch Illusion ON trigger
147
#if defined(DXX_BUILD_DESCENT_II)
148
        UNLOCK_DOORS =    1024,    // Unlocks a door
149
        OPEN_WALL =       2048,    // Makes a wall open
150
        CLOSE_WALL =      4096,    // Makes a wall closed
151
        ILLUSORY_WALL =   8192,    // Makes a wall illusory
152
#endif
153
};
154
 
155
#define TRIGGER_CONTROL_DOORS TRIGGER_FLAG::CONTROL_DOORS
156
#define TRIGGER_SHIELD_DAMAGE TRIGGER_FLAG::SHIELD_DAMAGE
157
#define TRIGGER_ENERGY_DRAIN TRIGGER_FLAG::ENERGY_DRAIN
158
#define TRIGGER_EXIT TRIGGER_FLAG::EXIT
159
#define TRIGGER_ONE_SHOT TRIGGER_FLAG::ONE_SHOT
160
#define TRIGGER_MATCEN TRIGGER_FLAG::MATCEN
161
#define TRIGGER_ILLUSION_OFF TRIGGER_FLAG::ILLUSION_OFF
162
#define TRIGGER_SECRET_EXIT TRIGGER_FLAG::SECRET_EXIT
163
#define TRIGGER_ILLUSION_ON TRIGGER_FLAG::ILLUSION_ON
164
#if defined(DXX_BUILD_DESCENT_II)
165
#define TRIGGER_UNLOCK_DOORS TRIGGER_FLAG::UNLOCK_DOORS
166
#define TRIGGER_OPEN_WALL TRIGGER_FLAG::OPEN_WALL
167
#define TRIGGER_CLOSE_WALL TRIGGER_FLAG::CLOSE_WALL
168
#define TRIGGER_ILLUSORY_WALL TRIGGER_FLAG::ILLUSORY_WALL
169
#endif
170
 
171
//the trigger really should have both a type & a flags, since most of the
172
//flags bits are exclusive of the others.
173
#if defined(DXX_BUILD_DESCENT_I) || defined(DXX_BUILD_DESCENT_II)
174
typedef uint8_t trgnum_t;
175
 
176
namespace dsx {
177
 
178
struct trigger : public prohibit_void_ptr<trigger>
179
{
180
#if defined(DXX_BUILD_DESCENT_I)
181
        uint16_t flags;
182
        uint16_t num_links;
183
#elif defined(DXX_BUILD_DESCENT_II)
184
        trigger_action type;       //what this trigger does
185
        trigger_behavior_flags flags;
186
        uint8_t   num_links;  //how many doors, etc. linked to this
187
#endif
188
        fix     value;
189
        std::array<segnum_t, MAX_WALLS_PER_LINK>   seg;
190
        std::array<short, MAX_WALLS_PER_LINK>   side;
191
};
192
 
193
}
194
DXX_VALPTRIDX_DECLARE_SUBTYPE(dsx::, trigger, trgnum_t, MAX_TRIGGERS);
195
namespace dsx {
196
DXX_VALPTRIDX_DEFINE_SUBTYPE_TYPEDEFS(trigger, trg);
197
 
198
struct d_level_unique_trigger_state
199
{
200
        valptridx<trigger>::array_managed_type Triggers;
201
};
202
}
203
 
204
constexpr std::integral_constant<uint8_t, 0xff> trigger_none{};
205
 
206
extern void trigger_init();
207
namespace dsx {
208
window_event_result check_trigger(vcsegptridx_t seg, unsigned side, object &plrobj, vcobjptridx_t objnum, int shot);
209
window_event_result check_trigger_sub(object &, trgnum_t trigger_num, playernum_t player_num, unsigned shot);
210
 
211
static inline int trigger_is_exit(const trigger *t)
212
{
213
#if defined(DXX_BUILD_DESCENT_I)
214
        return t->flags & TRIGGER_EXIT;
215
#elif defined(DXX_BUILD_DESCENT_II)
216
        return t->type == trigger_action::normal_exit;
217
#endif
218
}
219
 
220
static inline int trigger_is_matcen(const trigger &t)
221
{
222
#if defined(DXX_BUILD_DESCENT_I)
223
        return t.flags & TRIGGER_MATCEN;
224
#elif defined(DXX_BUILD_DESCENT_II)
225
        return t.type == trigger_action::matcen;
226
#endif
227
}
228
 
229
#if defined(DXX_BUILD_DESCENT_I)
230
void v25_trigger_read(PHYSFS_File *fp, trigger *);
231
void v26_trigger_read(PHYSFS_File *fp, trigger &);
232
#endif
233
 
234
#if defined(DXX_BUILD_DESCENT_II)
235
/*
236
 * reads a v29_trigger structure from a PHYSFS_File
237
 */
238
extern void v29_trigger_read(v29_trigger *t, PHYSFS_File *fp);
239
 
240
/*
241
 * reads a v30_trigger structure from a PHYSFS_File
242
 */
243
extern void v30_trigger_read(v30_trigger *t, PHYSFS_File *fp);
244
#endif
245
 
246
/*
247
 * reads a trigger structure from a PHYSFS_File
248
 */
249
extern void trigger_read(trigger *t, PHYSFS_File *fp);
250
void v29_trigger_read_as_v31(PHYSFS_File *fp, trigger &t);
251
void v30_trigger_read_as_v31(PHYSFS_File *fp, trigger &t);
252
}
253
 
254
/*
255
 * reads n trigger structs from a PHYSFS_File and swaps if specified
256
 */
257
void trigger_read(PHYSFS_File *fp, trigger &t);
258
void trigger_write(PHYSFS_File *fp, const trigger &t);
259
 
260
#ifdef dsx
261
namespace dsx {
262
void v29_trigger_write(PHYSFS_File *fp, const trigger &t);
263
void v30_trigger_write(PHYSFS_File *fp, const trigger &t);
264
void v31_trigger_write(PHYSFS_File *fp, const trigger &t);
265
}
266
#endif
267
#endif
268
 
269
#endif