Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 961 lines (780 sloc) 27.483 kb
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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <stdio.h>
#include <math.h>

#include "vectorlib.c"
#include "world.c"
#include "world_drawing.c"
#include "render.c"
#include "volume_draw.c"
#include "light.c"

double region_get_sector_value (void *reg, int x, int y, int z)
{
  if (!reg)
     return 0;

  vec3_init (secpos, x, y, z);
  double l = fabs (vec3_len (secpos));
  if (l < 1)
    return 1.85; // the core
  else if (l < 200
           && ((abs (x) < 1 && abs (y) < 1)
               || (abs (z) < 1 && abs (y) < 1)
               || (abs (x) < 1 && abs (z) < 1)))
    return 1.65; // the axis, going from center to the outer construct connection
  else if (l < 30 // here we check the void: void is in the inner shell surface
           || (l > 130 // and beyond the outer shell surface, but only if inside the
                       // huge construct cube which spans 200 in each direction
               && (abs (x) < 200 && abs (y) < 200 && abs (z) < 200)))
    return 1.55; // the void
  else // we are in the sphere shell around that
    {
      double *region = reg;
      int reg_size = region[0];
      region++;

      if (x < 0) x = -x;
      if (y < 0) y = -y;
      if (z < 0) z = -z;
      x %= reg_size;
      y %= reg_size;
      z %= reg_size;

      return region[x + y * reg_size + z * reg_size * reg_size];
    }
}

unsigned int vox_cone_sphere_intersect (double cam_x, double cam_y, double cam_z, double cam_v_x, double cam_v_y, double cam_v_z, double cam_fov, double sphere_x, double sphere_y, double sphere_z, double sphere_rad)
{
  vec3_init(cam, cam_x, cam_y, cam_z);
  vec3_init(cam_v, cam_v_x, cam_v_y, cam_v_z);
  vec3_init(sphere, sphere_x, sphere_y, sphere_z);
  vec3_clone(u, cam);
  vec3_clone(uv, cam_v);
  vec3_clone(d, sphere);

  vec3_s_mul (uv, sphere_rad / sinl (cam_fov));
  vec3_sub (u, uv);
  vec3_sub (d, u);

  double l = vec3_len (d);

  if (vec3_dot (cam_v, d) >= l * cosl (cam_fov))
    {
       vec3_assign (d, sphere);
       vec3_sub (d, cam);
       l = vec3_len (d);

       if (-vec3_dot (cam_v, d) >= l * sinl (cam_fov))
         return (l <= sphere_rad);
       else
         return 1;
    }
  else
    return 0;
}

MODULE = Games::VoxEngine PACKAGE = Games::VoxEngine::Math PREFIX = vox_

unsigned int vox_cone_sphere_intersect (double cam_x, double cam_y, double cam_z, double cam_v_x, double cam_v_y, double cam_v_z, double cam_fov, double sphere_x, double sphere_y, double sphere_z, double sphere_rad);

AV *
vox_point_aabb_distance (double pt_x, double pt_y, double pt_z, double box_min_x, double box_min_y, double box_min_z, double box_max_x, double box_max_y, double box_max_z)
  CODE:
    vec3_init (pt, pt_x, pt_y, pt_z);
    vec3_init (bmin, box_min_x, box_min_y, box_min_z);
    vec3_init (bmax, box_max_x, box_max_y, box_max_z);
    unsigned int i;

    double out[3];
    for (i = 0; i < 3; i++)
      {
        out[i] = pt[i];

        if (bmin[i] > bmax[i])
          {
            double swp = bmin[i];
            bmin[i] = bmax[i];
            bmax[i] = swp;
          }

        if (out[i] < bmin[i])
          out[i] = bmin[i];
        if (out[i] > bmax[i])
          out[i] = bmax[i];
      }

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    for (i = 0; i < 3; i++)
      av_push (RETVAL, newSVnv (out[i]));

  OUTPUT:
    RETVAL



AV *
vox_calc_visible_chunks_at_in_cone (double pt_x, double pt_y, double pt_z, double rad, double cam_x, double cam_y, double cam_z, double cam_v_x, double cam_v_y, double cam_v_z, double cam_fov, double sphere_rad)
  CODE:
    int r = rad;

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    vec3_init (pt, pt_x, pt_y, pt_z);
    vec3_s_div (pt, CHUNK_SIZE);
    vec3_floor (pt);

    int x, y, z;
    for (x = -r; x <= r; x++)
      for (y = -r; y <= r; y++)
        for (z = -r; z <= r; z++)
          {
            vec3_init (chnk, x, y, z);
            vec3_add (chnk, pt);
            vec3_clone (chnk_p, chnk);

            vec3_sub (chnk, pt);
            if (vec3_len (chnk) < rad)
              {
                vec3_clone (sphere_pos, chnk_p);
                vec3_s_mul (sphere_pos, CHUNK_SIZE);
                sphere_pos[0] += CHUNK_SIZE / 2;
                sphere_pos[1] += CHUNK_SIZE / 2;
                sphere_pos[2] += CHUNK_SIZE / 2;

                if (vox_cone_sphere_intersect (
                      cam_x, cam_y, cam_z, cam_v_x, cam_v_y, cam_v_z,
                      cam_fov, sphere_pos[0], sphere_pos[1], sphere_pos[2],
                      sphere_rad))
                  {
                    int i;
                    for (i = 0; i < 3; i++)
                      av_push (RETVAL, newSVnv (chnk_p[i]));
                  }
              }
          }

  OUTPUT:
    RETVAL

AV *
vox_calc_visible_chunks_at (double pt_x, double pt_y, double pt_z, double rad)
  CODE:
    int r = rad;

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    vec3_init (pt, pt_x, pt_y, pt_z);
    vec3_s_div (pt, CHUNK_SIZE);
    vec3_floor (pt);

    int x, y, z;
    for (x = -r; x <= r; x++)
      for (y = -r; y <= r; y++)
        for (z = -r; z <= r; z++)
          {
            vec3_init (chnk, x, y, z);
            vec3_add (chnk, pt);
            vec3_clone (chnk_p, chnk);
            vec3_sub (chnk, pt);
            if (vec3_len (chnk) < rad)
              {
                int i;
                for (i = 0; i < 3; i++)
                  av_push (RETVAL, newSVnv (chnk_p[i]));
              }
          }

  OUTPUT:
    RETVAL

MODULE = Games::VoxEngine PACKAGE = Games::VoxEngine::Renderer PREFIX = vox_render_

void *vox_render_new_geom ();

void vox_render_clear_geom (void *c);

void vox_render_draw_geom (void *c);

void vox_render_free_geom (void *c);

int vox_render_chunk (int x, int y, int z, void *geom)
  CODE:
    vox_render_clear_geom (geom);
    RETVAL = vox_render_chunk (x, y, z, geom);
  OUTPUT:
    RETVAL

void
vox_render_model (unsigned int type, unsigned short color, double light, unsigned int xo, unsigned int yo, unsigned int zo, void *geom, int skip, int force_model)
  CODE:
     vox_render_clear_geom (geom);
     vox_render_model (type, color, light, xo, yo, zo, geom, skip, force_model, 1);
     vox_render_compile_geom (geom);

void vox_render_init ();

void vox_render_set_ambient_light (double l)
  CODE:
     vox_ambient_light = l;

MODULE = Games::VoxEngine PACKAGE = Games::VoxEngine::World PREFIX = vox_world_

void vox_world_init (SV *change_cb, SV *cell_change_cb)
  CODE:
     vox_world_init ();
     SvREFCNT_inc (change_cb);
     WORLD.chunk_change_cb = change_cb;
     SvREFCNT_inc (cell_change_cb);
     WORLD.active_cell_change_cb = cell_change_cb;


int
vox_world_has_chunk (int x, int y, int z)
  CODE:
    vox_chunk *chnk = vox_world_chunk (x, y, z, 0);
    RETVAL = chnk ? 1 : 0;
  OUTPUT:
    RETVAL

SV *
vox_world_get_chunk_data (int x, int y, int z)
  CODE:
    vox_chunk *chnk = vox_world_chunk (x, y, z, 0);
    if (!chnk)
      {
        XSRETURN_UNDEF;
      }

    int len = CHUNK_ALEN * 4;
    unsigned char *data = malloc (sizeof (unsigned char) * len);
    vox_world_get_chunk_data (chnk, data);

    RETVAL = newSVpv (data, len);
    free (data);
  OUTPUT:
    RETVAL


int vox_world_set_chunk_data (int x, int y, int z, unsigned char *data, unsigned int len)
  CODE:
    vox_chunk *chnk = vox_world_chunk (x, y, z, 1);
    assert (chnk);
    RETVAL = vox_world_set_chunk_from_data (chnk, data, len);
    int lenc = (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) * 4;
    if (lenc != len)
      {
        printf ("CHUNK DATA LEN DOES NOT FIT! %d vs %d\n", len, lenc);
        exit (1);
      }

    // FIXME: this needs to be done for neighborss where whe changed too!!!
    vox_world_chunk_calc_visibility (chnk);

    vox_world_emit_chunk_change (x, y, z);

    //d// vox_world_dump ();
  OUTPUT:
    RETVAL

void vox_world_purge_chunk (int x, int y, int z);

int vox_world_is_solid_at (double x, double y, double z)
  CODE:
    RETVAL = 1;

    vox_chunk *chnk = vox_world_chunk_at (x, y, z, 0);
    if (chnk)
      {
        vox_cell *c = vox_chunk_cell_at_abs (chnk, x, y, z);
        vox_obj_attr *attr = vox_world_get_attr (c->type);
        RETVAL = attr ? attr->blocking : 0;
      }
  OUTPUT:
    RETVAL

void vox_world_set_object_type (unsigned int type, unsigned int transparent, unsigned int blocking, unsigned int has_txt, unsigned int active, double uv0, double uv1, double uv2, double uv3);

void vox_world_set_object_model (unsigned int type, unsigned int dim, AV *blocks);

AV *
vox_world_at (double x, double y, double z)
  CODE:
    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    vox_chunk *chnk = vox_world_chunk_at (x, y, z, 0);
    if (chnk)
      {
        vox_cell *c = vox_chunk_cell_at_abs (chnk, x, y, z);
        av_push (RETVAL, newSViv (c->type));
        av_push (RETVAL, newSViv (c->light));
        av_push (RETVAL, newSViv (c->meta));
        av_push (RETVAL, newSViv (c->add));
        av_push (RETVAL, newSViv (c->visible));
      }

  OUTPUT:
    RETVAL

AV *
vox_world_chunk_visible_faces (int x, int y, int z)
  CODE:
    vox_chunk *chnk = vox_world_chunk (x, y, z, 0);

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    for (z = 0; z < CHUNK_SIZE; z++)
      for (y = 0; y < CHUNK_SIZE; y++)
        for (x = 0; x < CHUNK_SIZE; x++)
          {
            if (chnk->cells[REL_POS2OFFS(x, y, z)].visible)
              {
                av_push (RETVAL, newSViv (chnk->cells[REL_POS2OFFS(x, y, z)].type));
                av_push (RETVAL, newSVnv (x));
                av_push (RETVAL, newSVnv (y));
                av_push (RETVAL, newSVnv (z));
              }
          }

  OUTPUT:
    RETVAL

void vox_world_query_load_chunks (int alloc = 0);

void vox_world_query_set_at (unsigned int rel_x, unsigned int rel_y, unsigned int rel_z, AV *cell)
  CODE:
    vox_world_query_set_at_pl (rel_x, rel_y, rel_z, cell);

void vox_world_query_set_at_abs (unsigned int rel_x, unsigned int rel_y, unsigned int rel_z, AV *cell)
  CODE:
    vox_world_query_abs2rel (&rel_x, &rel_y, &rel_z);
    vox_world_query_set_at_pl (rel_x, rel_y, rel_z, cell);

void vox_world_query_setup (int x, int y, int z, int ex, int ey, int ez);

int vox_world_query_desetup (int no_update = 0);

AV *vox_world_query_possible_light_positions ()
  CODE:
    int xw = QUERY_CONTEXT.x_w * CHUNK_SIZE,
        yw = QUERY_CONTEXT.y_w * CHUNK_SIZE,
        zw = QUERY_CONTEXT.z_w * CHUNK_SIZE;

    static int offsets[6][3] = {
        { 0, 0, 1 },
        { 0, 0, -1 },
        { 0, 1, 0 },
        { 0, -1, 0 },
        { 1, 0, 0 },
        { -1, 0, 0 },
    };

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    int x, y, z;
    for (x = 2; x < (xw - 2); x += 6)
      for (y = 2; y < (yw - 2); y += 6)
        for (z = 2; z < (zw - 2); z += 6)
           {
             int ix, iy, iz;
             int rad, found = 0;
             int i;
             int fnd = 0;
             for (i = 0; !fnd && i < 6; i++)
               {
                 int m;
                 for (m = 1; m <= 2; m++)
                   {
                     int px = x + offsets[i][0] * (m - 1),
                         py = y + offsets[i][1] * (m - 1),
                         pz = z + offsets[i][2] * (m - 1);

                     vox_cell *cur = vox_world_query_cell_at (px, py, pz, 0);
                     if (!cur || cur->type != 0)
                       continue;

                     cur = vox_world_query_cell_at (
                       x + offsets[i][0] * m,
                       y + offsets[i][1] * m,
                       z + offsets[i][2] * m,
                       0);
                     if (cur && cur->type != 0)
                       {
                         av_push (RETVAL,
                                  newSViv (px + QUERY_CONTEXT.chnk_x * CHUNK_SIZE));
                         av_push (RETVAL,
                                  newSViv (py + QUERY_CONTEXT.chnk_y * CHUNK_SIZE));
                         av_push (RETVAL,
                                  newSViv (pz + QUERY_CONTEXT.chnk_z * CHUNK_SIZE));
                         fnd = 1;
                         break;
                       }
                   }
               }
           }
  OUTPUT:
    RETVAL

AV *vox_world_find_free_spot (int x, int y, int z, int with_floor)
  CODE:
    vec3_init (pos, x, y, z);
    vec3_s_div (pos, CHUNK_SIZE);
    vec3_floor (pos);
    int chnk_x = pos[0],
        chnk_y = pos[1],
        chnk_z = pos[2];

    vox_world_query_setup (
      chnk_x - 2, chnk_y - 2, chnk_z - 2,
      chnk_x + 2, chnk_y + 2, chnk_z + 2
    );

    vox_world_query_load_chunks (0);

    int cx = x, cy = y, cz = z;
    vox_world_query_abs2rel (&cx, &cy, &cz);

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    int rad;
    int ix, iy, iz;
    int found = 0;
    for (rad = 0; !found && rad < ((CHUNK_SIZE * 2) - 3); rad++) // -3 safetymargin
      for (ix = -rad; !found && ix <= rad; ix++)
        for (iy = -rad; !found && iy <= rad; iy++)
          for (iz = -rad; !found && iz <= rad; iz++)
            {
              int dx = ix + cx,
                  dy = iy + cy,
                  dz = iz + cz;

              vox_cell *cur = vox_world_query_cell_at (dx, dy, dz, 0);
              if (!cur)
                continue;
              vox_obj_attr *attr = vox_world_get_attr (cur->type);
              if (attr->blocking)
                continue;

              cur = vox_world_query_cell_at (dx, dy + 1, dz, 0);
              if (!cur)
                continue;
              attr = vox_world_get_attr (cur->type);
              if (attr->blocking)
                continue;

              cur = vox_world_query_cell_at (dx, dy - 1, dz, 0);
              if (!cur)
                continue;
              attr = vox_world_get_attr (cur->type);
              if (with_floor && !attr->blocking)
                continue;

              av_push (RETVAL, newSViv (x + ix));
              av_push (RETVAL, newSViv (y + iy));
              av_push (RETVAL, newSViv (z + iz));
              found = 1;
            }

  OUTPUT:
    RETVAL

AV *vox_world_get_types_in_cube (int x, int y, int z, int size, int type_match = -1)
  CODE:
    vec3_init (pos1, x, y, z);
    vec3_s_div (pos1, CHUNK_SIZE);
    vec3_floor (pos1);

    vec3_init (pos2, x + size, y + size, z + size);
    vec3_s_div (pos2, CHUNK_SIZE);
    vec3_floor (pos2);

    vox_world_query_setup (
      (int) pos1[0], (int) pos1[1], (int) pos1[2],
      (int) pos2[0], (int) pos2[1], (int) pos2[2]
    );

    vox_world_query_load_chunks (0);

    int cx = x, cy = y, cz = z;
    vox_world_query_abs2rel (&cx, &cy, &cz);

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    int dx, dy, dz;
    for (dx = 0; dx < size; dx++)
      for (dy = 0; dy < size; dy++)
        for (dz = 0; dz < size; dz++)
          {
            vox_cell *cur = vox_world_query_cell_at (cx + dx, cy + dy, cz + dz, 0);
            if (!cur)
              continue;

            if (type_match >= 0)
              {
                if (cur->type == type_match)
                  {
                    av_push (RETVAL, newSViv (x + dx));
                    av_push (RETVAL, newSViv (y + dy));
                    av_push (RETVAL, newSViv (z + dz));
                    av_push (RETVAL, newSViv (cur->type));
                  }
              }
            else
              av_push (RETVAL, newSViv (cur->type));
          }

    vox_world_query_desetup (1);

  OUTPUT:
    RETVAL

AV *vox_world_get_pattern (int x, int y, int z, int mutate)
  CODE:
    vec3_init (pos, x, y, z);
    vec3_s_div (pos, CHUNK_SIZE);
    vec3_floor (pos);
    int chnk_x = pos[0],
        chnk_y = pos[1],
        chnk_z = pos[2];

    vox_world_query_setup (
      chnk_x - 1, chnk_y - 1, chnk_z - 1,
      chnk_x + 1, chnk_y + 1, chnk_z + 1
    );

    vox_world_query_load_chunks (0);

    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    // calc relative size inside chunks:
    int cx = x, cy = y, cz = z;
    vox_world_query_abs2rel (&cx, &cy, &cz);

    //d// printf ("QUERY AT %d %d %d\n", cx, cy, cz);

    // find lowest cx/cz coord with constr. floor
    vox_cell *cur = vox_world_query_cell_at (cx, cy, cz, 0);
    while (cur && cur->type == 36)
      {
        cx--;
        printf ("CX %d\n", cx);
        cur = vox_world_query_cell_at (cx, cy, cz, 0);
      }

    cx++;
    cur = vox_world_query_cell_at (cx, cy, cz, 0);
    while (cur && cur->type == 36)
      {
        cz--;
        cur = vox_world_query_cell_at (cx, cy, cz, 0);
      }
    cz++;

    //d// printf ("MINX FOUND %d %d\n", cx, cz);

    // find out how large the floor is
    int dim;
    for (dim = 5; dim >= 1; dim--)
      {
        int no_floor = 0;

        int dx, dz;
        for (dx = 0; dx < dim; dx++)
          for (dz = 0; dz < dim; dz++)
            {
              vox_cell *cur = vox_world_query_cell_at (cx + dx, cy, cz + dz, 0);
              //d// printf ("TXT[%d] %d %d %d: %d\n", dim, cx + dx, cy, cz + dz, cur->type);
              if (!cur || cur->type != 36)
                no_floor = 1;
            }
        if (!no_floor)
          break;
      }

    if (dim <= 0)
      {
        vox_world_query_desetup (1);
        XSRETURN_UNDEF;
      }

    //d// printf ("floor dimension: %d\n", dim);
    // next: search first x/z coord with a block over it
    int min_x = 100, min_y = 100, min_z = 100, max_x = 0, max_y = 0, max_z = 0;
    int dx, dy, dz;
    int fnd = 0;
    for (dy = 1; dy <= dim; dy++)
      for (dz = 0; dz < dim; dz++)
        for (dx = 0; dx < dim; dx++)
          {
            int ix = dx + cx,
                iy = dy + cy,
                iz = dz + cz;
            cur = vox_world_query_cell_at (ix, iy, iz, 0);
            if (cur && cur->type != 0)
              {
                if (min_x > ix) min_x = ix;
                if (min_y > iy) min_y = iy;
                if (min_z > iz) min_z = iz;
              }
          }

    for (dy = 1; dy <= dim; dy++)
      for (dz = 0; dz < dim; dz++)
        for (dx = 0; dx < dim; dx++)
          {
            int ix = dx + cx,
                iy = dy + cy,
                iz = dz + cz;
            cur = vox_world_query_cell_at (ix, iy, iz, 0);
            if (cur && cur->type != 0)
              {
                if (max_x < ix) max_x = ix;
                if (max_y < iy) max_y = iy;
                if (max_z < iz) max_z = iz;
              }
          }

    dim = 0;
    if (((max_x - min_x) + 1) > dim)
      dim = (max_x - min_x) + 1;
    if (((max_y - min_y) + 1) > dim)
      dim = (max_y - min_y) + 1;
    if (((max_z - min_z) + 1) > dim)
      dim = (max_z - min_z) + 1;

    //d// printf ("FOUND MIN MAX %d %d %d, %d %d %d, dimension: %d\n", min_x, min_y, min_z, max_x, max_y, max_z, dim);

    if (!mutate)
      av_push (RETVAL, newSViv (dim));

    int blk_nr = 1;
    for (dy = 0; dy < dim; dy++)
      for (dz = 0; dz < dim; dz++)
        for (dx = 0; dx < dim; dx++)
          {
            int ix = min_x + dx,
                iy = min_y + dy,
                iz = min_z + dz;

            // outside construction pad:
            if (ix > max_x || iy > max_y || iz > max_z)
              {
                blk_nr++; // but is just empty space of pattern
                continue;
              }

            cur = vox_world_query_cell_at (ix, iy, iz, 0);
            if (cur && cur->type != 0)
              {
                if (mutate == 1)
                  {
                    av_push (RETVAL, newSViv ((chnk_x - 1) * CHUNK_SIZE + ix));
                    av_push (RETVAL, newSViv ((chnk_y - 1) * CHUNK_SIZE + iy));
                    av_push (RETVAL, newSViv ((chnk_z - 1) * CHUNK_SIZE + iz));
                  }
                else
                  {
                    av_push (RETVAL, newSViv (blk_nr));
                    av_push (RETVAL, newSViv (cur->type));
                  }
              }

            blk_nr++;
          }

    vox_world_query_desetup (1);

  OUTPUT:
    RETVAL


#define DEBUG_LIGHT 0

void vox_world_flow_light_query_setup (int minx, int miny, int minz, int maxx, int maxy, int maxz)
  CODE:
    vec3_init (min_pos, minx, miny, minz);
    vec3_s_div (min_pos, CHUNK_SIZE);
    vec3_floor (min_pos);
    vec3_init (max_pos, maxx, maxy, maxz);
    vec3_s_div (max_pos, CHUNK_SIZE);
    vec3_floor (max_pos);

    vox_world_query_setup (
      min_pos[0] - 2, min_pos[1] - 2, min_pos[2] - 2,
      max_pos[0] + 2, max_pos[1] + 2, max_pos[2] + 2
    );

    vox_world_query_load_chunks (0);


AV *vox_world_query_search_types (int t1, int t2, int t3)
  CODE:
    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);

    int xw = QUERY_CONTEXT.x_w * CHUNK_SIZE,
        yw = QUERY_CONTEXT.y_w * CHUNK_SIZE,
        zw = QUERY_CONTEXT.z_w * CHUNK_SIZE;
    int x, y, z;
    for (x = 0; x < xw; x++)
      for (y = 0; y < yw; y++)
        for (z = 0; z < zw; z++)
           {
             vox_cell *cur = vox_world_query_cell_at (x, y, z, 0);
             if (cur && (cur->type == t1 || cur->type == t2 || cur->type == t3))
               {
                  int rx = x, ry = y, rz = z;
                  vox_world_query_rel2abs (&rx, &ry, &rz);
                  av_push (RETVAL, newSViv (rx));
                  av_push (RETVAL, newSViv (ry));
                  av_push (RETVAL, newSViv (rz));
               }
           }

  OUTPUT:
    RETVAL

void vox_world_query_reflow_every_light ()
  CODE:
    int xw = QUERY_CONTEXT.x_w * CHUNK_SIZE,
        yw = QUERY_CONTEXT.y_w * CHUNK_SIZE,
        zw = QUERY_CONTEXT.z_w * CHUNK_SIZE;
    int x, y, z;
    for (x = 0; x < xw; x++)
      for (y = 0; y < yw; y++)
        for (z = 0; z < zw; z++)
           {
             vox_cell *cur = vox_world_query_cell_at (x, y, z, 0);
             if (cur && (cur->type == 35 || cur->type == 40 || cur->type == 41))
               vox_world_query_reflow_light (x, y, z);
           }

void vox_world_flow_light_at (int x, int y, int z)
  CODE:
    vox_world_query_abs2rel (&x, &y, &z);
    vox_world_query_reflow_light (x, y, z);


MODULE = Games::VoxEngine PACKAGE = Games::VoxEngine::VolDraw PREFIX = vol_draw_

void vol_draw_init ();

void vol_draw_alloc (unsigned int size);

void vol_draw_set_op (unsigned int op);

void vol_draw_set_dst (unsigned int i);

void vol_draw_set_src (unsigned int i);

void vol_draw_set_dst_range (double a, double b);

void vol_draw_set_src_range (double a, double b);

void vol_draw_set_src_blend (double r);

void vol_draw_val (double val);

void vol_draw_dst_self ();

void vol_draw_subdiv (int type, float x, float y, float z, float size, float shrink_fact, int lvl);

void vol_draw_fill_simple_noise_octaves (unsigned int seed, unsigned int octaves, double factor, double persistence);

void vol_draw_mandel_box (double xc, double yc, double zc, double xsc, double ysc, double zsc, double s, double r, double f, int it, double cfact);

void vol_draw_menger_sponge_box (float x, float y, float z, float size, unsigned short lvl);

void vol_draw_cantor_dust_box (float x, float y, float z, float size, unsigned short lvl);

void vol_draw_sierpinski_pyramid (float x, float y, float z, float size, unsigned short lvl);

void vol_draw_self_sim_cubes_hash_seed (float x, float y, float z, float size, unsigned int corners, unsigned int seed, unsigned short lvl);

void vol_draw_map_range (float a, float b, float x, float y);

void vol_draw_copy (void *dst_arr);

void vol_draw_histogram_equalize (int buckets, double a, double b);

int vol_draw_count_in_range (double a, double b)
  CODE:
    int c = 0;
    int x, y, z;
    for (x = 0; x < DRAW_CTX.size; x++)
      for (y = 0; y < DRAW_CTX.size; y++)
        for (z = 0; z < DRAW_CTX.size; z++)
          {
            double v = DRAW_DST (x, y, z);
            if (v >= a && v < b)
              c++;
          }
    RETVAL = c;
  OUTPUT:
    RETVAL


AV *vol_draw_to_perl ()
  CODE:
    RETVAL = newAV ();
    sv_2mortal ((SV *)RETVAL);
    av_push (RETVAL, newSViv (DRAW_CTX.size));

    int x, y, z;
    for (x = 0; x < DRAW_CTX.size; x++)
      for (y = 0; y < DRAW_CTX.size; y++)
        for (z = 0; z < DRAW_CTX.size; z++)
          av_push (RETVAL, newSVnv (DRAW_DST (x, y, z)));

  OUTPUT:
    RETVAL

void vol_draw_dst_to_world (int sector_x, int sector_y, int sector_z, AV *range_map)
  CODE:
    int cx = sector_x * CHUNKS_P_SECTOR,
        cy = sector_y * CHUNKS_P_SECTOR,
        cz = sector_z * CHUNKS_P_SECTOR;

    vox_world_query_setup (
      cx, cy, cz,
      cx + (CHUNKS_P_SECTOR - 1),
      cy + (CHUNKS_P_SECTOR - 1),
      cz + (CHUNKS_P_SECTOR - 1)
    );

    vox_world_query_load_chunks (1);
    int x, y, z;
    for (x = 0; x < DRAW_CTX.size; x++)
      for (y = 0; y < DRAW_CTX.size; y++)
        for (z = 0; z < DRAW_CTX.size; z++)
          {
            vox_cell *cur = vox_world_query_cell_at (x, y, z, 1);
            assert (cur);
            double v = DRAW_DST(x, y, z);

            int al = av_len (range_map);
            int i;
            for (i = 0; i <= al; i += 3)
              {
                SV **a = av_fetch (range_map, i, 0);
                SV **b = av_fetch (range_map, i + 1, 0);
                SV **t = av_fetch (range_map, i + 2, 0);
                if (!a || !b || !v)
                  continue;

                double av = SvNV (*a),
                       bv = SvNV (*b);
                if (v >= av && v < bv)
                  {
                    cur->type = SvIV (*t);
                    if (vox_world_is_active (cur->type))
                      vox_world_emit_active_cell_change (x, y, z, cur, 0);
                  }
              }
          }

MODULE = Games::VoxEngine PACKAGE = Games::VoxEngine::Random PREFIX = random_

unsigned int rnd_xor (unsigned int x);

double rnd_float (unsigned int x)
  CODE:
    double val = (double) x / (double) 0xFFFFFFFF;
    RETVAL = val;
  OUTPUT:
    RETVAL

MODULE = Games::VoxEngine PACKAGE = Games::VoxEngine::Region PREFIX = region_

void *region_new_from_vol_draw_dst ()
  CODE:
    double *region =
       malloc ((sizeof (double) * DRAW_CTX.size * DRAW_CTX.size * DRAW_CTX.size) + 1);
    RETVAL = region;

    region[0] = DRAW_CTX.size;
    region++;
    vol_draw_copy (region);

  OUTPUT:
    RETVAL

unsigned int region_get_sector_seed (int x, int y, int z)
  CODE:
    RETVAL = map_coord2int (x, y, z);
  OUTPUT:
    RETVAL

AV *region_get_nearest_sector_in_range (void *reg, int x, int y, int z, double a, double b)
  CODE:
     double *region = reg;
     int reg_size = region[0];
     region++;

     RETVAL = newAV ();
     sv_2mortal ((SV *)RETVAL);

     int rad;
     for (rad = 1; rad < 200; rad++)
       {
         int fnd = 0;
         int dx, dy, dz;
         for (dx = -rad; dx <= rad; dx++)
           for (dy = -rad; dy <= rad; dy++)
             for (dz = -rad; dz <= rad; dz++)
               {
                 int ox = x + dx,
                     oy = y + dy,
                     oz = z + dz;

                 double v = region_get_sector_value (reg, ox, oy, oz);
                 if (v < a || v >= b)
                   continue;

                 av_push (RETVAL, newSViv (x + dx));
                 av_push (RETVAL, newSViv (y + dy));
                 av_push (RETVAL, newSViv (z + dz));
                 fnd = 1;
               }

         if (fnd)
           break;
       }

  OUTPUT:
    RETVAL

double region_get_sector_value (void *reg, int x, int y, int z);
Something went wrong with that request. Please try again.