Proof read my % algorithm

Discussion in 'Plugin Development' started by Tster, Feb 20, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Tster

    Code:
                    event.getBlock().setType(Material.STONE);
                    oremap.put(OreGen.coal, Material.COAL_ORE);
                    oremap.put(OreGen.iron, Material.IRON_ORE);
                    oremap.put(OreGen.lapis, Material.LAPIS_ORE);
                    oremap.put(OreGen.redstone, Material.REDSTONE_ORE);
                    oremap.put(OreGen.gold, Material.GOLD_ORE);
                    oremap.put(OreGen.diamond, Material.DIAMOND_ORE);   
                    oremap.put(OreGen.stone, Material.STONE);
                    final TreeMap sortedoremap = new TreeMap(oremap);
                    //If so then it generates a random number between 0 and 99,
                    //this is easiest as the user can configure the chance as a percentage
                    randomGen = new Random();
                    percentOre = randomGen.nextInt(100);
                    //Add one so we are dealing with 1 to 100 (easier concept to understand)
                    percentOre = percentOre + 1;
                    //Find the number that the percentage must be greater than for each ore
                    if (percentOre <= ((Double) sortedoremap.keySet().toArray()[0])){
                        event.getBlock().setType((Material) sortedoremap.values().toArray()[0]);
                    } else if (percentOre <= ((Double) sortedoremap.keySet().toArray()[1])){
                        event.getBlock().setType((Material) sortedoremap.values().toArray()[1]);
                    } else if (percentOre <= ((Double) sortedoremap.keySet().toArray()[2]) + ((Double) sortedoremap.keySet().toArray()[1]) + (Double) sortedoremap.keySet().toArray()[0]){
                        event.getBlock().setType((Material) sortedoremap.values().toArray()[2]);
                    } else if (percentOre <= ((Double) sortedoremap.keySet().toArray()[3]) + ((Double) sortedoremap.keySet().toArray()[2]) + ((Double) sortedoremap.keySet().toArray()[1] + (Double) sortedoremap.keySet().toArray()[0])){
                        event.getBlock().setType((Material) sortedoremap.values().toArray()[3]);
                    } else if (percentOre <= (((Double) sortedoremap.keySet().toArray()[4]) + (Double) sortedoremap.keySet().toArray()[3]) + ((Double) sortedoremap.keySet().toArray()[2]) + ((Double) sortedoremap.keySet().toArray()[1] + (Double) sortedoremap.keySet().toArray()[0])){
                        event.getBlock().setType((Material) sortedoremap.values().toArray()[4]);
                    } else if (percentOre <= (((Double) sortedoremap.keySet().toArray()[5]) + ((Double) sortedoremap.keySet().toArray()[4]) + (Double) sortedoremap.keySet().toArray()[3]) + ((Double) sortedoremap.keySet().toArray()[2]) + ((Double) sortedoremap.keySet().toArray()[1] + (Double) sortedoremap.keySet().toArray()[0])){
                        event.getBlock().setType((Material) sortedoremap.values().toArray()[5]);
                    } else {
                        event.getBlock().setType(Material.STONE);
                    }
    I will optimise it, but does it make sense?
     
  2. Offline

    ItsHarry

    Code is really confusing o-o
     
  3. Offline

    Tster

    I shall hence do the optimisation first.
     
  4. Offline

    Acrobot

    I assume you may want to achieve something like this:

    http://pastebin.com/6E27Q0fm

    Written in Notepad++, so I didn't have any code-correction, written only from head.
    (Of course, do an event.getBlock().setType(type) after the code executes)
     
  5. Offline

    Tster

    And this works? (as in, is it a valid algorithm
     
  6. Offline

    Acrobot

    Tster
    I don't know, I didn't test it.
    But looking at the code now, it won't work - you just don't have to add
    ((Double) sortedoremap.keySet().toArray()[5]) + ((Double) sortedoremap.keySet().toArray()[4])
    etc.

    Just getting one value should be enough.
     
  7. Offline

    Tster

    Lets assume case 1 has 10% chance
    and case 2 has 15%
    if rnd(100) + 1 is less than or equal to 10 then case 1
    but if we then do if rnd (100) +1 is less than or equal to 15 then case 2
    there is only a 5% chance so:
    in the 2nd test we need to add 10+15, which gives a correct outcome
     
  8. Offline

    Acrobot

    Tster

    Well, that all depends on how you store things in your sortedoremap.
    If you store them as a range (so first number is 15, that means that it works for 1-15. Second would be 30, so that would work for 16-30 etc) it will work fine.
     
  9. Offline

    Tster

    mhm..
     
Thread Status:
Not open for further replies.

Share This Page