File: main.qc

package info (click to toggle)
nexuiz-data 2.5.2-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,294,288 kB
  • sloc: ansic: 10,523; perl: 6,845; sh: 2,188; java: 1,417; xml: 969; lisp: 519; ruby: 136; makefile: 125
file content (534 lines) | stat: -rw-r--r-- 13,365 bytes parent folder | download | duplicates (6)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
void pathlib_deletepath(entity start)
{
    entity e;

    e = findchainentity(owner, start);
    while(e)
    {
        e.think = SUB_Remove;
        e.nextthink = time;
        e = e.chain;
    }
}

//#define PATHLIB_NODEEXPIRE 0.05
#define PATHLIB_NODEEXPIRE 20

void dumpnode(entity n)
{
    n.is_path_node = FALSE;
    n.think        = SUB_Remove;
    n.nextthink    = time;
}

entity pathlib_mknode(vector where,entity parent)
{
    entity node;

    node = pathlib_nodeatpoint(where);
    if(node)
    {
        mark_error(where,60);
        return node;
    }

    node = spawn();

    node.think        = SUB_Remove;
    node.nextthink    = time + PATHLIB_NODEEXPIRE;
    node.is_path_node = TRUE;
    node.owner        = openlist;
    node.path_prev    = parent;

    setmodel(node,"models/pathlib/square.md3");
    setsize(node,'0 0 0','0 0 0');
    node.colormod = randomvec() * 2;
    node.alpha = 0.25;
    node.scale     = pathlib_gridsize / 512.001;

    //pathlib_showsquare2(node,'1 1 1',0);//(node.medium & CONTENT_EMPTY));
    setorigin(node, where);
    node.medium = pointcontents(where);

    mark_info(where,60);
    //pathlib_showsquare(where,1,30);


    ++pathlib_made_cnt;
    ++pathlib_open_cnt;

    return node;
}

float pathlib_makenode_adaptive(entity parent,vector start, vector to, vector goal,float cost)
{
    entity node;
    float h,g,f,doedge;
    vector where;

    ++pathlib_searched_cnt;

    if(inwater(parent.origin))
    {
        pathlib_expandnode = pathlib_expandnode_box;
        pathlib_movenode   = pathlib_swimnode;
    }
    else
    {
        if(inwater(to))
        {
            pathlib_expandnode = pathlib_expandnode_box;
            pathlib_movenode   = pathlib_walknode;
        }
        else
        {
            //if(edge_check(parent.origin))
            //    return 0;

            pathlib_expandnode = pathlib_expandnode_star;
            pathlib_movenode   = pathlib_walknode;
            doedge = 1;
        }
    }

    node = pathlib_nodeatpoint(to);
    if(node)
    {
        ++pathlib_merge_cnt;

        if(node.owner == openlist)
        {
            h = pathlib_heuristic(node.origin,goal);
            g = pathlib_cost(parent,node.origin,cost);
            f = g + h;

            if(node.pathlib_node_g > g)
            {
                node.pathlib_node_h = h;
                node.pathlib_node_g = g;
                node.pathlib_node_f = f;

                node.path_prev = parent;
            }

            if not (best_open_node)
                best_open_node = node;
            else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
                best_open_node = node;
        }

        return 1;
    }

    where = pathlib_movenode(parent.origin,to,0);
    if not(pathlib_movenode_goodnode)
        return 0;

    if(pathlib_nodeatpoint(where))
    {
        dprint("NAP WHERE :",vtos(where),"\n");
        dprint("not NAP TO:",vtos(to),"\n");
        dprint("NAP-NNAP:",ftos(vlen(to-where)),"\n\n");
        return 0;
    }

    if(doedge)
        if not (tile_check(where))
            return 0;

    h = pathlib_heuristic(where,goal);
    g = pathlib_cost(parent,where,cost);
    f = g + h;


    /*
    node = findradius(where,pathlib_gridsize * 0.5);
    while(node)
    {
        if(node.is_path_node == TRUE)
        {
            ++pathlib_merge_cnt;
            if(node.owner == openlist)
            {
                if(node.pathlib_node_g > g)
                {
                    //pathlib_movenode(where,node.origin,0);
                    //if(pathlib_movenode_goodnode)
                    //{
                        //mark_error(node.origin + '0 0 128',30);
                        node.pathlib_node_h = h;
                        node.pathlib_node_g = g;
                        node.pathlib_node_f = f;
                        node.path_prev = parent;
                    //}
                }

                if not (best_open_node)
                    best_open_node = node;
                else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
                    best_open_node = node;
            }

            return 1;
        }
        node = node.chain;
    }
    */

    node = pathlib_mknode(where,parent);
    node.pathlib_node_h = h;
    node.pathlib_node_g = g;
    node.pathlib_node_f = f;

    if not (best_open_node)
        best_open_node = node;
    else if(best_open_node.pathlib_node_f > node.pathlib_node_f)
        best_open_node = node;

    return 1;
}

entity pathlib_getbestopen()
{
    entity node;
    entity bestnode;

    if(best_open_node)
    {
        ++pathlib_bestcash_hits;
        pathlib_bestcash_saved += pathlib_open_cnt;

        return best_open_node;
    }

    node = findchainentity(owner,openlist);
    if(!node)
        return world;

    bestnode = node;
    while(node)
    {
        ++pathlib_bestopen_seached;
        if(node.pathlib_node_f < bestnode.pathlib_node_f)
            bestnode = node;

        node = node.chain;
    }

    return bestnode;
}

void pathlib_close_node(entity node,vector goal)
{

    if(node.owner == closedlist)
    {
        dprint("Pathlib: Tried to close a closed node!\n");
        return;
    }

    if(node == best_open_node)
        best_open_node = world;

    ++pathlib_closed_cnt;
    --pathlib_open_cnt;

    node.owner = closedlist;

    if(vlen(node.origin - goal) <= pathlib_gridsize)
    {
        vector goalmove;

        goalmove = pathlib_walknode(node.origin,goal,1);
        if(pathlib_movenode_goodnode)
        {
            goal_node         = node;
            pathlib_foundgoal = TRUE;
        }
    }
}

void pathlib_cleanup()
{
    best_open_node = world;

    //return;

    entity node;

    node = findfloat(world,is_path_node, TRUE);
    while(node)
    {
        /*
        node.owner = openlist;
        node.pathlib_node_g = 0;
        node.pathlib_node_h = 0;
        node.pathlib_node_f = 0;
        node.path_prev = world;
        */

        dumpnode(node);
        node = findfloat(node,is_path_node, TRUE);
    }

    if(openlist)
        remove(openlist);

    if(closedlist)
        remove(closedlist);

    openlist       = world;
    closedlist     = world;

}

float Cosine_Interpolate(float a, float b, float x)
{
    float ft,f;

	ft = x * 3.1415927;
	f = (1 - cos(ft)) * 0.5;

	return  a*(1-f) + b*f;
}

float buildpath_nodefilter_directional(vector n,vector c,vector p)
{
    vector d1,d2;

    d2 = normalize(p - c);
    d1 = normalize(c - n);

    if(vlen(d1-d2) < 0.25)
    {
        //mark_error(c,30);
        return 1;
    }
    //mark_info(c,30);
    return 0;
}

float buildpath_nodefilter_moveskip(vector n,vector c,vector p)
{
    pathlib_walknode(p,n,1);
    if(pathlib_movenode_goodnode)
        return 1;

    return 0;
}

entity path_build(entity next, vector where, entity prev, entity start)
{
    entity path;

    if(prev && next)
        if(buildpath_nodefilter)
            if(buildpath_nodefilter(next.origin,where,prev.origin))
                return next;


    path           = spawn();
    path.owner     = start;
    path.path_next = next;

    setorigin(path,where);

    if(!next)
        path.classname = "path_end";
    else
    {
        if(!prev)
            path.classname = "path_start";
        else
            path.classname = "path_node";
    }

    return path;
}

entity pathlib_astar(vector from,vector to)
{
    entity path, start, end, open, n, ln;
    float ptime, ftime, ctime;

    ptime = gettime(GETTIME_REALTIME);
    pathlib_starttime = ptime;

    pathlib_cleanup();

    // Select water<->land capable node make/link
    pathlib_makenode     = pathlib_makenode_adaptive;
    // Select XYZ cost estimate
    //pathlib_heuristic    = pathlib_h_diagonal3;
    pathlib_heuristic    = pathlib_h_diagonal;
    // Select distance + waterfactor cost
    pathlib_cost         = pathlib_g_euclidean_water;
    // Select star expander
    pathlib_expandnode   = pathlib_expandnode_star;
    // Select walk simulation movement test
    pathlib_movenode     = pathlib_walknode;
    // Filter final nodes by direction
    buildpath_nodefilter = buildpath_nodefilter_directional;
    // Filter tiles with cross pattern
    tile_check = tile_check_cross;

    // If the start is in water we need diffrent settings
    if(inwater(from))
    {
        // Select volumetric node expaner
        pathlib_expandnode = pathlib_expandnode_box;

        // Water movement test
        pathlib_movenode   = pathlib_swimnode;
    }

    if not(openlist)
        openlist       = spawn();

    if not(closedlist)
        closedlist     = spawn();

    pathlib_closed_cnt       = 0;
    pathlib_open_cnt         = 0;
    pathlib_made_cnt         = 0;
    pathlib_merge_cnt        = 0;
    pathlib_searched_cnt     = 0;
    pathlib_bestopen_seached = 0;
    pathlib_bestcash_hits    = 0;
    pathlib_bestcash_saved   = 0;

    pathlib_gridsize       = 128;
    pathlib_movecost       = pathlib_gridsize;
    pathlib_movecost_diag  = vlen(('1 1 0' * pathlib_gridsize));
    pathlib_movecost_waterfactor = 1;
    pathlib_foundgoal      = 0;

    movenode_boxmax   = self.maxs * 1.25;
    movenode_boxmin   = self.mins * 1.25;

    movenode_stepsize = 32;

    tile_check_size = 65;
    tile_check_up   = '0 0 128';
    tile_check_down = '0 0 128';

    movenode_stepup   = '0 0 36';
    movenode_maxdrop  = '0 0 128';
    movenode_boxup    = '0 0 72';

    from_x = fsnap(from_x,pathlib_gridsize);
    from_y = fsnap(from_y,pathlib_gridsize);

    to_x = fsnap(to_x,pathlib_gridsize);
    to_y = fsnap(to_y,pathlib_gridsize);

    dprint("AStar init\n");
    path = pathlib_mknode(from,world);
    pathlib_close_node(path,to);
    if(pathlib_foundgoal)
    {
        dprint("AStar: Goal found on first node!\n");

        open           = spawn();
        open.owner     = open;
        open.classname = "path_end";
        setorigin(open,path.origin);

        pathlib_cleanup();

        return open;
    }

    if(pathlib_expandnode(path,from,to) <= 0)
    {
        dprint("AStar path fail.\n");
        pathlib_cleanup();

        return world;
    }

    best_open_node = pathlib_getbestopen();
    n = best_open_node;
    pathlib_close_node(best_open_node,to);
    if(inwater(n.origin))
        pathlib_expandnode_box(n,from,to);
    else
        pathlib_expandnode_star(n,from,to);

    while(pathlib_open_cnt)
    {
        if((gettime(GETTIME_REALTIME) - pathlib_starttime) > pathlib_maxtime)
        {
            dprint("Path took to long to compute!\n");
            dprint("Nodes - created: ", ftos(pathlib_made_cnt),"\n");
            dprint("Nodes -    open: ", ftos(pathlib_open_cnt),"\n");
            dprint("Nodes -  merged: ", ftos(pathlib_merge_cnt),"\n");
            dprint("Nodes -  closed: ", ftos(pathlib_closed_cnt),"\n");

            pathlib_cleanup();
            return world;
        }

        best_open_node = pathlib_getbestopen();
        n = best_open_node;
        pathlib_close_node(best_open_node,to);

        if(inwater(n.origin))
            pathlib_expandnode_box(n,from,to);
        else
            pathlib_expandnode(n,from,to);

        if(pathlib_foundgoal)
        {
            dprint("Target found. Rebuilding and filtering path...\n");
            ftime = gettime(GETTIME_REALTIME);
            ptime = ftime - ptime;

            start = path_build(world,path.origin,world,world);
            end   = path_build(world,goal_node.origin,world,start);
            ln    = end;

            open = goal_node;
            for(open = goal_node; open.path_prev != path; open = open.path_prev)
            {
                n    = path_build(ln,open.origin,open.path_prev,start);
                ln.path_prev = n;
                ln = n;
            }
            start.path_next = n;
            n.path_prev = start;
            ftime = gettime(GETTIME_REALTIME) - ftime;

            ctime = gettime(GETTIME_REALTIME);
            pathlib_cleanup();
            ctime = gettime(GETTIME_REALTIME) - ctime;


#ifdef DEBUGPATHING
            pathlib_showpath2(start);

            dprint("Time used -      pathfinding: ", ftos(ptime),"\n");
            dprint("Time used - rebuild & filter: ", ftos(ftime),"\n");
            dprint("Time used -          cleanup: ", ftos(ctime),"\n");
            dprint("Time used -            total: ", ftos(ptime + ftime + ctime),"\n");
            dprint("Time used -         # frames: ", ftos(ceil((ptime + ftime + ctime) / sys_ticrate)),"\n\n");
            dprint("Nodes -         created: ", ftos(pathlib_made_cnt),"\n");
            dprint("Nodes -            open: ", ftos(pathlib_open_cnt),"\n");
            dprint("Nodes -          merged: ", ftos(pathlib_merge_cnt),"\n");
            dprint("Nodes -          closed: ", ftos(pathlib_closed_cnt),"\n");
            dprint("Nodes -        searched: ", ftos(pathlib_searched_cnt),"\n");
            dprint("Nodes bestopen searched: ", ftos(pathlib_bestopen_seached),"\n");
            dprint("Nodes bestcash -   hits: ", ftos(pathlib_bestcash_hits),"\n");
            dprint("Nodes bestcash -   save: ", ftos(pathlib_bestcash_saved),"\n");
            dprint("AStar done.\n");
#endif
            return start;
        }
    }

    dprint("A* Faild to find a path! Try a smaller gridsize.\n");

    pathlib_cleanup();

    return world;
}