aboutsummaryrefslogtreecommitdiff
path: root/modules/mod_path/mod_path.c
blob: ded80f64f44e53563696a12b6fd6f8226dd06441 (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
/*
 *  Copyright � 2006-2016 SplinterGU (Fenix/Bennugd)
 *  Copyright � 2002-2006 Fenix Team (Fenix)
 *  Copyright � 1999-2002 Jos� Luis Cebri�n Pag�e (Fenix)
 *
 *  This file is part of Bennu - Game Development
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

/* --------------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>

#include "bgddl.h"
#include "bgdrtm.h"

#include "libgrbase.h"

/* --------------------------------------------------------------------------- */

#include "mod_path.h"

/* --------------------------------------------------------------------------- */

typedef struct _node
{
    unsigned int x, y ;
    double f, g, h ;
    struct _node * parent ;
    struct _node * next ;
    struct _node * inext ;
}
node ;

/* --------------------------------------------------------------------------- */

static int  * path_result = NULL ;
static int  * path_result_pointer = NULL ;

static node * pf_all = NULL;

static node * pf_open = NULL ;
static node * pf_closed = NULL ;
static node * found = NULL ;

static int destination_x, destination_y ;
static int startup_x, startup_y ;

static GRAPH * map ;

static int block_if = 1 ;

/* --------------------------------------------------------------------------- */

static double heuristic( int x, int y )
{
    int dx, dy ;
    uint8_t block = (( uint8_t* )map->data )[map->pitch*y+x] ;

    if ( x == destination_x && y == destination_y ) return 0 ;
    if ( block >= block_if ) return 1073741824.0 ;
    if ( x < 0 || y < 0 || x >= ( int )map->width || y >= ( int )map->height ) return 1073741824.0 ;

    dx = abs( destination_x - x ) ;
    dy = abs( destination_y - y ) ;
    return ( double )block + ( double )dx*dx + ( double )dy*dy ;
}

/* --------------------------------------------------------------------------- */

/* Uso: pf_open = add(pf_open, this) ; */
/* La lista permanecer� ordenada */
static node * node_add( node * list, node * this )
{
    node * curr = list ;
    if ( !curr )
    {
        this->next = NULL ;
        return this ;
    }
    if ( curr->f > this->f )
    {
        this->next = curr ;
        return this ;
    }

    for ( ;; )
    {
        if ( !curr->next )
        {
            curr->next = this ;
            this->next = NULL ;
            return list ;
        }
        if ( curr->next->f > this->f )
        {
            this->next = curr->next ;
            curr->next = this ;
            return list ;
        }
        curr = curr->next ;
    }
}

/* --------------------------------------------------------------------------- */

/* Uso: pf_open = remove(pf_open, this) ; */
static node * node_remove( node * list, node * this )
{
    node * curr = list ;
    if ( curr == this ) return this->next ;
    while ( curr )
    {
        if ( curr->next == this )
        {
            curr->next = this->next ;
            return list ;
        }
        curr = curr->next ;
    }
    return list ;
}

/* --------------------------------------------------------------------------- */

static node * node_find( node * list, int x, int y )
{
    while ( list )
    {
        if ( list->x == ( unsigned )x && list->y == ( unsigned )y ) return list ;
        list = list->next ;
    }
    return NULL ;
}

/* --------------------------------------------------------------------------- */
/*
static void node_reset( node * list )
{
    node * next ;
    while ( list )
    {
        next = list->next ;
        free( list ) ;
        list = next ;
    }
}
*/
/* --------------------------------------------------------------------------- */

static node * node_new( node * parent, int x, int y, int cost_inc )
{
    node * curr ;

    curr = ( node * ) malloc( sizeof( node ) ) ;
    if ( !curr ) return NULL ;

    curr->x = x ;
    curr->y = y ;
    curr->g = ( parent ? parent->g : 0 ) + cost_inc ;
    curr->h = heuristic( x, y ) ;
    curr->f = curr->g + curr->h ;

    curr->parent = parent ;
    curr->next   = NULL ;
    return curr ;
}

/* --------------------------------------------------------------------------- */

static void node_push_succesor( node * parent, int ix, int iy, int cost )
{
    node * curr, * f_op, * f_cl ;

    curr = node_new( parent, parent->x + ix, parent->y + iy, cost ) ;
    if ( curr->h > 131072 )
    {
        free( curr ); return ;
    }

    f_cl = node_find( pf_closed, curr->x, curr->y ) ;
    if ( f_cl )
    {
        free( curr ); return ;
    }

    f_op = node_find( pf_open, curr->x, curr->y ) ;
    if ( f_op && f_op->f <= curr->f )
    {
        free( curr ); return ;
    }

    /* Add to general list (used for free resources)*/
    curr->inext = pf_all; pf_all = curr;

    if ( f_op ) { pf_open = node_remove( pf_open, f_op ); } /* this node is removed but childs that referent this node as parent will be wrong */
/* this can't be possible, previous "if ( f_cl )" abort this code ->   if ( f_cl ) { pf_closed = node_remove( pf_closed, f_cl ); }*/ /* this node is removed but childs that referent this node as parent will be wrong */

    pf_open = node_add( pf_open, curr ) ;
}

/* --------------------------------------------------------------------------- */

static void node_push_succesors( node * parent, int options )
{
    node * prior = parent->parent ;
    if ( !prior ) prior = parent ;

    node_push_succesor( parent, 1, 0, prior->x < parent->x ? 9 : 10 ) ;
    node_push_succesor( parent, 0, 1, prior->x > parent->x ? 9 : 10 ) ;
    node_push_succesor( parent, -1, 0, prior->y < parent->y ? 9 : 10 ) ;
    node_push_succesor( parent, 0, -1, prior->y > parent->y ? 9 : 10 ) ;

    if ( !( options & PF_NODIAG ) )
    {
        node_push_succesor( parent, 1, 1, 12 ) ;
        node_push_succesor( parent, -1, -1, 12 ) ;
        node_push_succesor( parent, -1, 1, 12 ) ;
        node_push_succesor( parent, 1, -1, 12 ) ;
    }
}

/* --------------------------------------------------------------------------- */

static int path_find( GRAPH * bitmap, int sx, int sy, int dx, int dy, int options )
{
    node * curr, * inext ;

    startup_x = sx ;
    startup_y = sy ;
    map = bitmap ;
    destination_x = dx ;
    destination_y = dy ;
/*
    node_reset( pf_open ) ;
    node_reset( pf_closed ) ;
*/
    /* Release all resources */

    curr = pf_all;
    while ( curr )
    {
        inext = curr->inext ;
        free( curr ) ;
        curr = inext ;
    }
    pf_all = NULL;

    pf_open = NULL;
    pf_closed = NULL;

    if ( path_result ) { free ( path_result ); path_result = NULL; }
    path_result_pointer = NULL;

    curr = node_new( NULL, startup_x, startup_y, 0 ) ;

    /* Add to general list (used for free resources)*/
    curr->inext = pf_all; pf_all = curr;

    curr->f = curr->h = 1 ;
    pf_open = node_add( pf_open, curr ) ;

    while ( pf_open )
    {
        curr = pf_open ;
        pf_open = node_remove( pf_open, curr ) ;

        if ( curr->x == ( unsigned )destination_x && curr->y == ( unsigned )destination_y )
        {
            int count = 1 ;

            found = curr ;
            while ( curr->parent )
            {
                count++ ;
                curr = curr->parent ;
            }

            path_result = malloc( sizeof( int ) * 2 * ( count + 4 ) ) ;
            if ( !( options & PF_REVERSE ) )
            {
                path_result_pointer = path_result + count * 2 + 1;
                *path_result_pointer-- = -1 ;
                *path_result_pointer-- = -1 ;
                while ( found )
                {
                    *path_result_pointer-- = found->y ;
                    *path_result_pointer-- = found->x ;
                    found = found->parent ;
                }

                if ( path_result_pointer != path_result - 1 )
                {
                    path_result_pointer = NULL;
                    return 0;
                }
            }
            else
            {
                path_result_pointer = path_result ;
                while ( found )
                {
                    *path_result_pointer++ = found->x ;
                    *path_result_pointer++ = found->y ;
                    found = found->parent ;
                }
                *path_result_pointer++ = -1 ;
                *path_result_pointer++ = -1 ;
            }

            path_result_pointer = path_result ;
            return 1 ;
        }

        node_push_succesors( curr, options ) ;

        pf_closed = node_add( pf_closed, curr ) ;
    }

    return 0 ;
}

/* --------------------------------------------------------------------------- */

static int path_get( int * x, int * y )
{
    if ( path_result_pointer )
    {
        ( *x ) = *path_result_pointer++ ;
        ( *y ) = *path_result_pointer++ ;
        if ( *path_result_pointer == -1 ) path_result_pointer = NULL ;
        return 1 ;
    }
    return 0 ;
}

/* --------------------------------------------------------------------------- */

static int path_set_wall( int n )
{
    if ( n >= 1 ) block_if = n ;
    return block_if ;
}

/* --------------------------------------------------------------------------- */
/* Funciones de b�squeda de caminos */

static int modpathfind_path_find( INSTANCE * my, int * params )
{
    GRAPH * gpath = bitmap_get( params[0], params[1] ) ;
    if ( !gpath || !gpath->format || gpath->format->depth != 8 ) return 0;
    return path_find( gpath, params[2], params[3], params[4], params[5], params[6] ) ;
}

/* --------------------------------------------------------------------------- */

static int modpathfind_path_getxy( INSTANCE * my, int * params )
{
    return path_get(( int * )params[0], ( int * )params[1] ) ;
}

/* --------------------------------------------------------------------------- */

static int modpathfind_path_wall( INSTANCE * my, int * params )
{
    return path_set_wall( params[0] ) ;
}

/* ----------------------------------------------------------------- */
/* exports                                                           */
/* ----------------------------------------------------------------- */

#include "mod_path_exports.h"

/* ----------------------------------------------------------------- */