summaryrefslogtreecommitdiff
path: root/apps/plugins/doom/p_map.c
blob: 35194dec144385d0101834e3a6eba49fa79892ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
** Any non-GPL usage of this software or parts of this software is strictly
** forbidden.
**
** Commercial non-GPL licensing of this software is possible.
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
**
** $Id$
**/

#include "common.h"
#include "structs.h"

#include <stdlib.h>
#include <string.h>

#include "specrec.h"
#include "huffman.h"

/* ISO/IEC 14496-3/Amd.1 
 * 8.5.3.3: Huffman Codeword Reordering for AAC spectral data (HCR) 
 *
 * HCR devides the spectral data in known fixed size segments, and 
 * sorts it by the importance of the data. The importance is firstly 
 * the (lower) position in the spectrum, and secondly the largest 
 * value in the used codebook. 
 * The most important data is written at the start of each segment
 * (at known positions), the remaining data is interleaved inbetween, 
 * with the writing direction alternating.
 * Data length is not increased.
*/

#ifdef ERROR_RESILIENCE

/* 8.5.3.3.1 Pre-sorting */

#define NUM_CB      6
#define NUM_CB_ER   22
#define MAX_CB      32
#define VCB11_FIRST 16
#define VCB11_LAST  31

static const uint8_t PreSortCB_STD[NUM_CB] = 
    { 11, 9, 7, 5, 3, 1};

static const uint8_t PreSortCB_ER[NUM_CB_ER] = 
    { 11, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 9, 7, 5, 3, 1};

/* 8.5.3.3.2 Derivation of segment width */

static const uint8_t maxCwLen[MAX_CB] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
    0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};

#define segmentWidth(cb)    min(maxCwLen[cb], ics->length_of_longest_codeword)

/* bit-twiddling helpers */
static const uint8_t  S[] = {1, 2, 4, 8, 16};    
static const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};

typedef struct
{
    uint8_t     cb;
    uint8_t     decoded;
    uint16_t	sp_offset;
    bits_t      bits;
} codeword_t;

/* rewind and reverse */
/* 32 bit version */
static uint32_t rewrev_word(uint32_t v, const uint8_t len)
{  
    /* 32 bit reverse */
    v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]); 
    v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]); 
    v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]); 
    v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
    v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);

    /* shift off low bits */
    v >>= (32 - len);

    return v;
}

/* 64 bit version */
static void rewrev_lword(uint32_t *hi, uint32_t *lo, const uint8_t len)
{   
    if (len <= 32) {
        *hi = 0;
        *lo = rewrev_word(*lo, len);
    } else
    {
        uint32_t t = *hi, v = *lo;

        /* double 32 bit reverse */
        v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]); 
        t = ((t >> S[0]) & B[0]) | ((t << S[0]) & ~B[0]); 
        v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]); 
        t = ((t >> S[1]) & B[1]) | ((t << S[1]) & ~B[1]); 
        v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]); 
        t = ((t >> S[2]) & B[2]) | ((t << S[2]) & ~B[2]); 
        v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
        t = ((t >> S[3]) & B[3]) | ((t << S[3]) & ~B[3]);
        v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);                
        t = ((t >> S[4]) & B[4]) | ((t << S[4]) & ~B[4]);

        /* last 32<>32 bit swap is implicit below */
        
        /* shift off low bits (this is really only one 64 bit shift) */
        *lo = (t >> (64 - len)) | (v << (len - 32));
        *hi = v >> (64 - len);          
    }
}


/* bits_t version */
static void rewrev_bits(bits_t *bits)
{
    if (bits->len == 0) return;
    rewrev_lword(&bits->bufb, &bits->bufa,  bits->len);
}


/* merge bits of a to b */
static void concat_bits(bits_t *b, bits_t *a)
{
    uint32_t bl, bh, al, ah;

    if (a->len == 0) return;

    al = a->bufa;
    ah = a->bufb;
    
    if (b->len > 32)
    {
        /* maskoff superfluous high b bits */
        bl = b->bufa;
        bh = b->bufb & ((1 << (b->len-32)) - 1);
        /* left shift a b->len bits */
        ah = al << (b->len - 32);
        al = 0;
    } else {
        bl = b->bufa & ((1 << (b->len)) - 1);
        bh = 0;   
        ah = (ah << (b->len)) | (al >> (32 - b->len));
        al = al << b->len;
    }

    /* merge */
    b->bufa = bl | al;
    b->bufb = bh | ah;

    b->len += a->len;
}
     
uint8_t is_good_cb(uint8_t this_CB, uint8_t this_sec_CB)
{
    /* only want spectral data CB's */
    if ((this_sec_CB > ZERO_HCB && this_sec_CB <= ESC_HCB) || (this_sec_CB >= VCB11_FIRST && this_sec_CB <= VCB11_LAST))
    {
        if (this_CB < ESC_HCB)
        {
            /* normal codebook pairs */
            return ((this_sec_CB == this_CB) || (this_sec_CB == this_CB + 1));
        } else
        {
            /* escape codebook */
            return (this_sec_CB == this_CB);
        }
    }
    return 0;
}
                    
void read_segment(bits_t *segment, uint8_t segwidth, bitfile *ld)
{
    segment->len = segwidth;

     if (segwidth > 32)
     {
        segment->bufb = faad_getbits(ld, segwidth - 32);        
        segment->bufa = faad_getbits(ld, 32);        

    } else {
        segment->bufa = faad_getbits(ld, segwidth);
        segment->bufb = 0;        
    }    
}

void fill_in_codeword(codeword_t *codeword, uint16_t index, uint16_t sp, uint8_t cb)
{
    codeword[index].sp_offset = sp;
    codeword[index].cb = cb;
    codeword[index].decoded = 0;
    codeword[index].bits.len = 0;
}

uint8_t reordered_spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, 
                                bitfile *ld, int16_t *spectral_data)
{   
    uint16_t PCWs_done;
    uint16_t numberOfSegments, numberOfSets, numberOfCodewords;  

    static codeword_t codeword[512];
    static bits_t segment[512];

    uint16_t sp_offset[8];
    uint16_t g, i, sortloop, set, bitsread;
    uint8_t w_idx, sfb, this_CB, last_CB, this_sec_CB; 
    
    const uint16_t nshort = hDecoder->frameLength/8;
    const uint16_t sp_data_len = ics->length_of_reordered_spectral_data;
    
    const uint8_t *PreSortCb;

    /* no data (e.g. silence) */
    if (sp_data_len == 0)
        return 0;

    /* since there is spectral data, at least one codeword has nonzero length */
    if (ics->length_of_longest_codeword == 0)
        return 10;
    
    if (sp_data_len < ics->length_of_longest_codeword)
        return 10; 

    sp_offset[0] = 0;
    for (g = 1; g < ics->num_window_groups; g++)
    {
        sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
    }

    PCWs_done = 0;
    numberOfSegments = 0;
    numberOfCodewords = 0;
    bitsread = 0;

    /* VCB11 code books in use */
    if (hDecoder->aacSectionDataResilienceFlag)
    {
        PreSortCb = PreSortCB_ER;
        last_CB = NUM_CB_ER;
    } else
    {
        PreSortCb = PreSortCB_STD;
        last_CB = NUM_CB;
    }
 
    /* step 1: decode PCW's (set 0), and stuff data in easier-to-use format */
    for (sortloop = 0; sortloop < last_CB; sortloop++)
    {
        /* select codebook to process this pass */
        this_CB = PreSortCb[sortloop];
        
        /* loop over sfbs */
        for (sfb = 0; sfb < ics->max_sfb; sfb++)
        {
            /* loop over all in this sfb, 4 lines per loop */
            for (w_idx = 0; 4*w_idx < (ics->swb_offset[sfb+1] - ics->swb_offset[sfb]); w_idx++)
            {
                for(g = 0; g < ics->num_window_groups; g++)
                {
                    for (i = 0; i < ics->num_sec[g]; i++)
                    {
                        /* check whether sfb used here is the one we want to process */
                        if ((ics->sect_start[g][i] <= sfb) && (ics->sect_end[g][i] > sfb))
                        {                            
                            /* check whether codebook used here is the one we want to process */
                            this_sec_CB = ics->sect_cb[g][i];
                 
                            if (is_good_cb(this_CB, this_sec_CB))                              
                            {
                                /* precalculate some stuff */
                                uint16_t sect_sfb_size = ics->sect_sfb_offset[g][sfb+1] - ics->sect_sfb_offset[g][sfb];
                                uint8_t inc = (this_sec_CB < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
                                uint16_t group_cws_count = (4*ics->window_group_length[g])/inc;
                                uint8_t segwidth = segmentWidth(this_sec_CB);
                                uint16_t cws;                                

                                /* read codewords until end of sfb or end of window group (shouldn't only 1 trigger?) */                                 
                                for (cws = 0; (cws < group_cws_count) && ((cws + w_idx*group_cws_count) < sect_sfb_size); cws++)
                                {
                                    uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc * (cws + w_idx*group_cws_count);                                   

                                    /* read and decode PCW */
                                    if (!PCWs_done)
                                    {         
                                        /* read in normal segments */
                                        if (bitsread + segwidth <= sp_data_len)
                                        {                                            
                                            read_segment(&segment[numberOfSegments], segwidth, ld);                          
                                            bitsread += segwidth;
                                            
                                            huffman_spectral_data_2(this_sec_CB, &segment[numberOfSegments], &spectral_data[sp]);                                            

                                            /* keep leftover bits */
                                            rewrev_bits(&segment[numberOfSegments]);

                                            numberOfSegments++;
                                        } else {
                                            /* remaining stuff after last segment, we unfortunately couldn't read
                                               this in earlier because it might not fit in 64 bits. since we already
                                               decoded (and removed) the PCW it is now guaranteed to fit */
                                            if (bitsread < sp_data_len)
                                            {                                                
                                                const uint8_t additional_bits = sp_data_len - bitsread;                                               

                                                read_segment(&segment[numberOfSegments], additional_bits, ld);                                                
                                                segment[numberOfSegments].len += segment[numberOfSegments-1].len;
                                                rewrev_bits(&segment[numberOfSegments]);                                               

                                                if (segment[numberOfSegments-1].len > 32)
                                                {
                                                    segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb + 
                                                        showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len - 32);
                                                    segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa + 
                                                        showbits_hcr(&segment[numberOfSegments-1], 32);
                                                } else {
                                                    segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa + 
                                                        showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len);
                                                    segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb;
                                                }                                                
                                                segment[numberOfSegments-1].len += additional_bits;
                                            }
                                            bitsread = sp_data_len;
                                            PCWs_done = 1;

                                            fill_in_codeword(codeword, 0, sp, this_sec_CB);                                            
                                        }
                                    } else {    
                                        fill_in_codeword(codeword, numberOfCodewords - numberOfSegments, sp, this_sec_CB);                                         
                                    }
                                    numberOfCodewords++;
                                }                             
                            }
                        }
                    } 
                 } 
             }
         }
    }

    if (numberOfSegments == 0)
        return 10; 

    numberOfSets = numberOfCodewords / numberOfSegments;     

    /* step 2: decode nonPCWs */
    for (set = 1; set <= numberOfSets; set++)
    {
        uint16_t trial;

        for (trial = 0; trial < numberOfSegments; trial++)
        {
            uint16_t codewordBase;

            for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
            {
                const uint16_t segment_idx = (trial + codewordBase) % numberOfSegments;
                const uint16_t codeword_idx = codewordBase + set*numberOfSegments - numberOfSegments;

                /* data up */
                if (codeword_idx >= numberOfCodewords - numberOfSegments) break;

                if (!codeword[codeword_idx].decoded && segment[segment_idx].len > 0)
                {
                    uint8_t tmplen;

                    if (codeword[codeword_idx].bits.len != 0)                   
                        concat_bits(&segment[segment_idx], &codeword[codeword_idx].bits);                            
                    
                    tmplen = segment[segment_idx].len;

                    if (huffman_spectral_data_2(codeword[codeword_idx].cb, &segment[segment_idx],
                                               &spectral_data[codeword[codeword_idx].sp_offset]) >= 0)
                    {
                        codeword[codeword_idx].decoded = 1;
                    } else 
                    {   
                        codeword[codeword_idx].bits = segment[segment_idx];
                        codeword[codeword_idx].bits.len = tmplen;                        
                    }
                                            
                }
            }
        }
        for (i = 0; i < numberOfSegments; i++)
            rewrev_bits(&segment[i]);
    }

    return 0;
}
#endif
href='#n1988'>1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
/* Emacs style mode select   -*- C++ -*-
 *-----------------------------------------------------------------------------
 *
 *
 *  PrBoom a Doom port merged with LxDoom and LSDLDoom
 *  based on BOOM, a modified and improved DOOM engine
 *  Copyright (C) 1999 by
 *  id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
 *  Copyright (C) 1999-2000 by
 *  Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 *  02111-1307, USA.
 *
 * DESCRIPTION:
 *  Movement, collision handling.
 *  Shooting and aiming.
 *
 *-----------------------------------------------------------------------------*/

#include "doomstat.h"
#include "r_main.h"
#include "p_mobj.h"
#include "p_maputl.h"
#include "p_map.h"
#include "p_setup.h"
#include "p_spec.h"
#include "s_sound.h"
#include "sounds.h"
#include "p_inter.h"
#include "m_random.h"
#include "m_bbox.h"
#include "i_system.h"
#include "rockmacros.h"

static mobj_t    *tmthing;
static fixed_t   tmx;
static fixed_t   tmy;
static int pe_x; // Pain Elemental position for Lost Soul checks // phares
static int pe_y; // Pain Elemental position for Lost Soul checks // phares
static int ls_x; // Lost Soul position for Lost Soul checks      // phares
static int ls_y; // Lost Soul position for Lost Soul checks      // phares

// If "floatok" true, move would be ok
// if within "tmfloorz - tmceilingz".

boolean   floatok;

/* killough 11/98: if "felldown" true, object was pushed down ledge */
boolean   felldown;

// The tm* items are used to hold information globally, usually for
// line or object intersection checking

fixed_t   tmbbox[4];  // bounding box for line intersection checks
fixed_t   tmfloorz;   // floor you'd hit if free to fall
fixed_t   tmceilingz; // ceiling of sector you're in
fixed_t   tmdropoffz; // dropoff on other side of line you're crossing

// keep track of the line that lowers the ceiling,
// so missiles don't explode against sky hack walls

line_t    *ceilingline;
line_t        *blockline;    /* killough 8/11/98: blocking linedef */
line_t        *floorline;    /* killough 8/1/98: Highest touched floor */
static int    tmunstuck;     /* killough 8/1/98: whether to allow unsticking */

// keep track of special lines as they are hit,
// but don't process them until the move is proven valid

// 1/11/98 killough: removed limit on special lines crossed
line_t **spechit;                // new code -- killough
static int spechit_max;          // killough

int numspechit;

// Temporary holder for thing_sectorlist threads
msecnode_t* sector_list = NULL;                             // phares 3/16/98

//
// TELEPORT MOVE
//

//
// PIT_StompThing
//

static boolean telefrag;   /* killough 8/9/98: whether to telefrag at exit */

boolean PIT_StompThing (mobj_t* thing)
{
   fixed_t blockdist;

   // phares 9/10/98: moved this self-check to start of routine

   // don't clip against self

   if (thing == tmthing)
      return true;

   if (!(thing->flags & MF_SHOOTABLE)) // Can't shoot it? Can't stomp it!
      return true;

   blockdist = thing->radius + tmthing->radius;

   if (D_abs(thing->x - tmx) >= blockdist || D_abs(thing->y - tmy) >= blockdist)
      return true; // didn't hit it

   // monsters don't stomp things except on boss level
   if (!telefrag)  // killough 8/9/98: make consistent across all levels
      return false;

   P_DamageMobj (thing, tmthing, tmthing, 10000); // Stomp!

   return true;
}


/*
 * killough 8/28/98:
 *
 * P_GetFriction()
 *
 * Returns the friction associated with a particular mobj.
 */

int P_GetFriction(const mobj_t *mo, int *frictionfactor)
{
   int friction = ORIG_FRICTION;
   int movefactor = ORIG_FRICTION_FACTOR;
   const msecnode_t *m;
   const sector_t *sec;

   /* Assign the friction value to objects on the floor, non-floating,
    * and clipped. Normally the object's friction value is kept at
    * ORIG_FRICTION and this thinker changes it for icy or muddy floors.
    *
    * When the object is straddling sectors with the same
    * floorheight that have different frictions, use the lowest
    * friction value (muddy has precedence over icy).
    */

   if (!(mo->flags & (MF_NOCLIP|MF_NOGRAVITY))
         && (mbf_features || (mo->player && !compatibility)) &&
         variable_friction)
      for (m = mo->touching_sectorlist; m; m = m->m_tnext)
         if ((sec = m->m_sector)->special & FRICTION_MASK &&
               (sec->friction < friction || friction == ORIG_FRICTION) &&
               (mo->z <= sec->floorheight ||
                (sec->heightsec != -1 &&