Commit c6a914a5c44e800c28ece5d7e421e59e55156b27

Authored by DarkGod
1 parent aec8f6c0

plop

  1 +-- TE4 - T-Engine 4
  2 +-- Copyright (C) 2009 - 2015 Nicolas Casalini
  3 +--
  4 +-- This program is free software: you can redistribute it and/or modify
  5 +-- it under the terms of the GNU General Public License as published by
  6 +-- the Free Software Foundation, either version 3 of the License, or
  7 +-- (at your option) any later version.
  8 +--
  9 +-- This program is distributed in the hope that it will be useful,
  10 +-- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +-- GNU General Public License for more details.
  13 +--
  14 +-- You should have received a copy of the GNU General Public License
  15 +-- along with this program. If not, see <http://www.gnu.org/licenses/>.
  16 +--
  17 +-- Nicolas Casalini "DarkGod"
  18 +-- darkgod@te4.org
  19 +
  20 +require "engine.class"
  21 +local ffi = require "ffi"
  22 +
  23 +ffi.cdef[[
  24 +void exit(int status);
  25 +void free(void *ptr);
  26 +]]
... ...
  1 +-- TE4 - T-Engine 4
  2 +-- Copyright (C) 2009 - 2015 Nicolas Casalini
  3 +--
  4 +-- This program is free software: you can redistribute it and/or modify
  5 +-- it under the terms of the GNU General Public License as published by
  6 +-- the Free Software Foundation, either version 3 of the License, or
  7 +-- (at your option) any later version.
  8 +--
  9 +-- This program is distributed in the hope that it will be useful,
  10 +-- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +-- GNU General Public License for more details.
  13 +--
  14 +-- You should have received a copy of the GNU General Public License
  15 +-- along with this program. If not, see <http://www.gnu.org/licenses/>.
  16 +--
  17 +-- Nicolas Casalini "DarkGod"
  18 +-- darkgod@te4.org
  19 +
  20 +--------------------------------------------------------------------------
  21 +--------------------------------------------------------------------------
  22 +-- WARNING about all that code
  23 +-- It makes things way faster but makes it non-reentrant:
  24 +-- Do NOT call a FOV calc from withing a FOV calc
  25 +-- not that you'd want that anyway
  26 +--------------------------------------------------------------------------
  27 +--------------------------------------------------------------------------
  28 +
  29 +require "engine.class"
  30 +local ffi = require "ffi"
  31 +local C = ffi.C
  32 +
  33 +ffi.cdef[[
  34 +struct lua_fovcache
  35 +{
  36 + bool *cache;
  37 + int w, h;
  38 +};
  39 +void ffi_fov_new_cache(struct lua_fovcache *cache, int x, int y);
  40 +void ffi_fov_cache_free(struct lua_fovcache *cache);
  41 +void ffi_fov_cache_set(struct lua_fovcache *cache, int x, int y, bool opaque);
  42 +bool ffi_fov_cache_get(struct lua_fovcache *cache, int x, int y);
  43 +
  44 +
  45 +typedef struct
  46 +{
  47 + int x, y, xy;
  48 + int dmap_value;
  49 + int sqdist, dist;
  50 +} seen_result_ffi;
  51 +void ffi_fov_calc_default_fov(int x, int y, int radius, struct lua_fovcache *cache, int w, int h);
  52 +bool ffi_fov_get_results(int *x, int *y, int *radius, int *dist);
  53 +]]
  54 +
  55 +local rx, ry, rradius, rdist = ffi.new("int[1]"), ffi.new("int[1]"), ffi.new("int[1]"), ffi.new("int[1]")
  56 +local ffi_fov = function(cache, x, y, radius, w, h, fct)
  57 + C.ffi_fov_calc_default_fov(x, y, radius, cache, w, h)
  58 + local ok = C.ffi_fov_get_results(rx, ry, rradius, rdist)
  59 + while ok do
  60 + fct(tonumber(rx[0]), tonumber(ry[0]), tonumber(rradius[0]), tonumber(rdist[0]))
  61 + ok = C.ffi_fov_get_results(rx, ry, rradius, rdist)
  62 + end
  63 +end
  64 +local ffi_fov_iterator = function(cache, x, y, radius, w, h, fct)
  65 + C.ffi_fov_calc_default_fov(x, y, radius, cache, w, h)
  66 + return function()
  67 + local ok = C.ffi_fov_get_results(rx, ry, rradius, rdist)
  68 + if not ok then return nil end
  69 + return tonumber(rx[0]), tonumber(ry[0]), tonumber(rradius[0]), tonumber(rdist[0])
  70 + end
  71 +end
  72 +
  73 +local fovcache
  74 +local fovcache_mt = {
  75 + __gc = C.ffi_fov_cache_free,
  76 + __index = {
  77 + get = C.ffi_fov_cache_get,
  78 + set = function(cache, x, y, v) C.ffi_fov_cache_set(cache, x, y, v and true or false) end,
  79 + fov = ffi_fov,
  80 + },
  81 +}
  82 +fovcache = ffi.metatype("struct lua_fovcache", fovcache_mt)
  83 +
  84 +--- FFI enabled FOV code
  85 +local fov = core.fov
  86 +fov.C = C
  87 +
  88 +fov.newCache2 = function(w, h)
  89 + local cache = ffi.new("struct lua_fovcache")
  90 + C.ffi_fov_new_cache(cache, w, h)
  91 + return cache
  92 +end
  93 +
  94 +fov.ffiFOV = ffi_fov
  95 +fov.ffiFOVIterator = ffi_fov_iterator
  96 +
  97 +local p = fov.newCache2(100,100)
  98 +p:set(50,50,true)
  99 +p:set(30,50,true)
  100 +for i = 1, 1 do
  101 +for x, y, radius, dist in core.fov.ffiFOVIterator(p, 40, 50, 2, 100, 100) do
  102 + -- print(x, y, radius, dist)
  103 +end
  104 +end
  105 +
  106 +-- C.exit(0)
... ...
  1 +-- TE4 - T-Engine 4
  2 +-- Copyright (C) 2009 - 2015 Nicolas Casalini
  3 +--
  4 +-- This program is free software: you can redistribute it and/or modify
  5 +-- it under the terms of the GNU General Public License as published by
  6 +-- the Free Software Foundation, either version 3 of the License, or
  7 +-- (at your option) any later version.
  8 +--
  9 +-- This program is distributed in the hope that it will be useful,
  10 +-- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +-- GNU General Public License for more details.
  13 +--
  14 +-- You should have received a copy of the GNU General Public License
  15 +-- along with this program. If not, see <http://www.gnu.org/licenses/>.
  16 +--
  17 +-- Nicolas Casalini "DarkGod"
  18 +-- darkgod@te4.org
  19 +
  20 +dofile("/engine/core/basic.lua")
  21 +dofile("/engine/core/fov.lua")
... ...