/*
 * Copyright 2016 Lukas Tenbrink
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ivorius.ivtoolkit.random;

import ivorius.ivtoolkit.math.IvVecMathHelper;
import ivorius.ivtoolkit.tools.NBTCompoundObject;
import ivorius.ivtoolkit.tools.NBTCompoundObjects;
import net.minecraft.nbt.NBTTagCompound;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by lukas on 12.10.14.
 */
public class BlurredValueField implements NBTCompoundObject
{
    private final List<Value> values = new ArrayList<>();

    private int[] size;

    public BlurredValueField()
    {
    }

    public BlurredValueField(int... size)
    {
        this.size = size;
    }

    public int[] getSize()
    {
        return size.clone();
    }

    public void addValue(Value value)
    {
        if (value.pos.length != size.length)
            throw new IllegalArgumentException();

        values.add(value);
    }

    public void addValue(double value, Random random)
    {
        int[] pos = new int[size.length];
        for (int i = 0; i < pos.length; i++)
            pos[i] = random.nextInt(size[i]);

        addValue(new Value(value, pos));
    }

    public double getValue(int... position)
    {
        double[] pos = new double[position.length];
        for (int i = 0; i < pos.length; i++)
            pos[i] = position[i];

        double total = 0;
        double[] inf = new double[values.size()];
        for (int i = 0; i < inf.length; i++)
        {
            Value value = values.get(i);

            double[] valPos = new double[size.length];
            for (int j = 0; j < valPos.length; j++)
                valPos[j] = value.pos[j];

            double dist = IvVecMathHelper.distanceSQ(pos, valPos);
            dist = dist * dist;
            dist = dist * dist;

            if (dist <= 0.0001)
                return value.value;

            inf[i] = (double) (1.0 / dist);
            total += inf[i];
        }

        double retVal = 0;

        for (int i = 0; i < inf.length; i++)
             retVal += values.get(i).value * (inf[i] / total);

        return retVal;
    }

    @Override
    public void readFromNBT(NBTTagCompound compound)
    {
        size = compound.getIntArray("size");
        values.addAll(NBTCompoundObjects.readListFrom(compound, "values", Value.class));
    }

    @Override
    public void writeToNBT(NBTTagCompound compound)
    {
        compound.setIntArray("size", size);
        NBTCompoundObjects.writeListTo(compound, "values", values);
    }

    public static class Value implements NBTCompoundObject
    {
        private double value;
        private int[] pos;

        public Value()
        {
        }

        public Value(double value, int[] pos)
        {
            this.value = value;
            this.pos = pos;
        }

        @Override
        public void readFromNBT(NBTTagCompound compound)
        {
            value = compound.getDouble("value");
            pos = compound.getIntArray("pos");
        }

        @Override
        public void writeToNBT(NBTTagCompound compound)
        {
            compound.setDouble("value", value);
            compound.setIntArray("pos", pos);
        }
    }
}

@@ -658,55 +658,8 @@
 m;%0A%0A
-import ivorius.ivtoolkit.math.IvVecMathHelper;%0A
 impo
@@ -1725,140 +1725,8 @@
   %7B%0A
-        double%5B%5D pos = new double%5Bposition.length%5D;%0A        for (int i = 0; i %3C pos.length; i++)%0A            pos%5Bi%5D = position%5Bi%5D;%0A%0A
     
@@ -1766,17 +1766,21 @@
 ble%5B%5D in
-f
+vDist
  = new d
@@ -1823,33 +1823,37 @@
 nt i = 0; i %3C in
-f
+vDist
 .length; i++)%0A  
@@ -1924,44 +1924,58 @@
 uble
-%5B%5D valPos = new double%5Bsize.length%5D;
+ dist = 0.0;%0A%0A            // Get distance to point
 %0A   
@@ -2003,22 +2003,24 @@
  0; j %3C 
-valPos
+position
 .length;
@@ -2023,32 +2023,40 @@
 gth; j++)%0A      
+      %7B%0A
           valPos
@@ -2053,19 +2053,38 @@
     
-valPos
+      double d = position
 %5Bj%5D 
-=
+-
  val
@@ -2094,17 +2094,16 @@
 pos%5Bj%5D;%0A
-%0A
         
@@ -2110,62 +2110,66 @@
     
-double
+   
  dist 
++
 = 
-IvVecMathHelper.distanceSQ(pos, valPos);
+d * d;%0A            %7D%0A%0A            // Extremify
 %0A   
@@ -2317,25 +2317,19 @@
   in
-f
+vDist
 %5Bi%5D = 
-(double) (
 1.0 
@@ -2334,17 +2334,16 @@
 0 / dist
-)
 ;%0A      
@@ -2359,17 +2359,21 @@
 al += in
-f
+vDist
 %5Bi%5D;%0A   
@@ -2404,24 +2404,90 @@
 etVal = 0;%0A%0A
+        total = 1D / total; // Do this just once for performance%0A%0A
         for 
@@ -2504,17 +2504,21 @@
 ; i %3C in
-f
+vDist
 .length;
@@ -2572,24 +2572,26 @@
 e * 
-(
 in
-f
+vDist
 %5Bi%5D 
-/
+*
  total
-)
 ;%0A%0A 

Improve BlurredValueField's performance a bit by not creating new arrays for each value for each call, as well as doing less divisions.