1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.jme3.bullet.control.ragdoll;
6
7import com.jme3.bullet.joints.SixDofJoint;
8import java.util.HashMap;
9import java.util.Map;
10import java.util.logging.Level;
11import java.util.logging.Logger;
12
13/**
14 *
15 * @author Nehon
16 */
17public abstract class RagdollPreset {
18
19    protected static final Logger logger = Logger.getLogger(RagdollPreset.class.getName());
20    protected Map<String, JointPreset> boneMap = new HashMap<String, JointPreset>();
21    protected Map<String, LexiconEntry> lexicon = new HashMap<String, LexiconEntry>();
22
23    protected abstract void initBoneMap();
24
25    protected abstract void initLexicon();
26
27    public void setupJointForBone(String boneName, SixDofJoint joint) {
28
29        if (boneMap.isEmpty()) {
30            initBoneMap();
31        }
32        if (lexicon.isEmpty()) {
33            initLexicon();
34        }
35        String resultName = "";
36        int resultScore = 0;
37
38        for (String key : lexicon.keySet()) {
39
40            int score = lexicon.get(key).getScore(boneName);
41            if (score > resultScore) {
42                resultScore = score;
43                resultName = key;
44            }
45
46        }
47
48        JointPreset preset = boneMap.get(resultName);
49
50        if (preset != null && resultScore >= 50) {
51            logger.log(Level.INFO, "Found matching joint for bone {0} : {1} with score {2}", new Object[]{boneName, resultName, resultScore});
52            preset.setupJoint(joint);
53        } else {
54            logger.log(Level.INFO, "No joint match found for bone {0}", boneName);
55            if (resultScore > 0) {
56                logger.log(Level.INFO, "Best match found is {0} with score {1}", new Object[]{resultName, resultScore});
57            }
58            new JointPreset().setupJoint(joint);
59        }
60
61    }
62
63    protected class JointPreset {
64
65        private float maxX, minX, maxY, minY, maxZ, minZ;
66
67        public JointPreset() {
68        }
69
70        public JointPreset(float maxX, float minX, float maxY, float minY, float maxZ, float minZ) {
71            this.maxX = maxX;
72            this.minX = minX;
73            this.maxY = maxY;
74            this.minY = minY;
75            this.maxZ = maxZ;
76            this.minZ = minZ;
77        }
78
79        public void setupJoint(SixDofJoint joint) {
80            joint.getRotationalLimitMotor(0).setHiLimit(maxX);
81            joint.getRotationalLimitMotor(0).setLoLimit(minX);
82            joint.getRotationalLimitMotor(1).setHiLimit(maxY);
83            joint.getRotationalLimitMotor(1).setLoLimit(minY);
84            joint.getRotationalLimitMotor(2).setHiLimit(maxZ);
85            joint.getRotationalLimitMotor(2).setLoLimit(minZ);
86        }
87    }
88
89    protected class LexiconEntry extends HashMap<String, Integer> {
90
91        public void addSynonym(String word, int score) {
92            put(word.toLowerCase(), score);
93        }
94
95        public int getScore(String word) {
96            int score = 0;
97            String searchWord = word.toLowerCase();
98            for (String key : this.keySet()) {
99                if (searchWord.indexOf(key) >= 0) {
100                    score += get(key);
101                }
102            }
103            return score;
104        }
105    }
106}
107