0

我的目标:

  • 让它更加规范化。(更相似的等待时间 - 线性增长)
  • 扩大到 XXX 甚至可能是 XXXX 数量的怪物和 npc。
  • 有没有你们所有的 Java 大师给我一些想法:D

我的问题如下:

  • 它永远不会达到第二次迭代循环(npc)
  • “浪费”的时间太随意了;将有数百个(如果不是更多)生物/NPC 进行迭代,这个解决方案根本无法扩展
  • 我的服务器将在主循环中执行许多其他“事件”,其中一些使用相同的 HashMap,因此使用 ConcurrentHashMap(计算命中损坏/等)

代码:我希望这已经是 SSCCE 了。我试图尽可能地减少脂肪......

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class WaitTime {

    static ConcurrentHashMap<String, Integer> mobInstanceMap = new ConcurrentHashMap<String, Integer>();
    static ConcurrentHashMap<String, Integer> npcInstanceMap = new ConcurrentHashMap<String, Integer>();

    public static void main(String[] args){
        mobInstanceMap.put("mob1", 0);
        mobInstanceMap.put("mob2", 0);
        mobInstanceMap.put("mob3", 0);
        npcInstanceMap.put("npc1", 0);
        npcInstanceMap.put("npc2", 0);
        npcInstanceMap.put("npc3", 0);
        while(true){
            updateEntityLocations();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static void updateEntityLocations() {
        long entityMovementLoopStartTime = System.nanoTime();
        Iterator<Entry<String, Integer>> it = mobInstanceMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Integer> mobEntity = it.next();
            String mobName = mobEntity.getKey();
            int lastWalkTime = mobEntity.getValue();
            int mobWalkSpeed = 4000;
            long walkWaitTime = lastWalkTime;
            long elapsedTime = (long) ((System.nanoTime() - entityMovementLoopStartTime) / 100.0);
            walkWaitTime += elapsedTime;

            if (walkWaitTime >= mobWalkSpeed){
                System.out.println("Wasted time(walking)(" + mobName + "): " + (walkWaitTime - mobWalkSpeed));

                //mobInstanceMap.put(mobName, 0);
                mobInstanceMap.replace(mobName, 0);
            } else {  //!(walkWaitTime >= walkSpeed)
                //mobInstanceMap.put(mobName, (int) walkWaitTime);
                mobInstanceMap.replace(mobName, (int) walkWaitTime);
            }
        }

        Iterator<Entry<String, Integer>> it1 = npcInstanceMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Integer> npcEntity = it1.next();
            String npcCoords = npcEntity.getKey();
            int lastWalkTime = npcEntity.getValue();
            int npcWalkSpeed = 4000;
            long walkWaitTime = lastWalkTime;
            long elapsedTime = (long) ((System.nanoTime() - entityMovementLoopStartTime) / 100.0);
            walkWaitTime += elapsedTime;

            if (walkWaitTime >= npcWalkSpeed){
                System.out.println("Wasted time(walking)(" + npcCoords + "): " + (walkWaitTime - npcWalkSpeed));

                npcInstanceMap.put(npcCoords, 0);
            } else {  //!(walkWaitTime >= walkSpeed)
                npcInstanceMap.put(npcCoords, (int) walkWaitTime);
            }
        }   
    }
}

安慰:

Wasted time(walking)(mob2): 58
Wasted time(walking)(mob1): 1983
Wasted time(walking)(mob3): 2288
Wasted time(walking)(mob3): 266
Wasted time(walking)(mob1): 122
Wasted time(walking)(mob3): 232
Wasted time(walking)(mob2): 23
Wasted time(walking)(mob1): 674
Wasted time(walking)(mob3): 27
Wasted time(walking)(mob1): 159
Wasted time(walking)(mob3): 1723
Wasted time(walking)(mob2): 119
Wasted time(walking)(mob1): 676
Wasted time(walking)(mob3): 1698
Wasted time(walking)(mob3): 3983
Wasted time(walking)(mob1): 182

正如您所看到的,如果您使用 put(commented out) 而不是 replace 运行它,它的运行速度会稍慢且更不稳定。

4

1 回答 1

2

你会踢自己,但第二个循环引用第一个迭代器。

Iterator<Entry<String, Integer>> it1 = npcInstanceMap.entrySet().iterator();
while (it.hasNext()) {
    ...
}

另外,正如这个答案所暗示的,我建议不要使用迭代器。

for(String mob : mobInstanceMap.keySet()){
    String mobName = mob;
    int lastWalkTime = mobInstanceMap.get(mob);
    int mobWalkSpeed = 4000;
    long walkWaitTime = lastWalkTime;
    long elapsedTime = (long) ((System.nanoTime() - entityMovementLoopStartTime) / 100.0);
    walkWaitTime += elapsedTime;

    if (walkWaitTime >= mobWalkSpeed){
        System.out.println("Wasted time(walking)(" + mobName + "): " + (walkWaitTime - mobWalkSpeed));

        mobInstanceMap.put(mobName, 0);
    } else {  //!(walkWaitTime >= walkSpeed)
        mobInstanceMap.put(mobName, (int) walkWaitTime);
    }
}
于 2013-05-29T17:37:04.953 回答