View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package spellcast.beings;
6   
7   import spellcast.model.Id;
8   import util.Nullable;
9   
10  /***
11   * @author Barrie Treloar
12   */
13  public abstract class Monster extends Being implements Nullable, IMonster {
14      private IWizard owner;
15  
16      private static final String[] KURUK_SYLLABLES = {"kur", "ak", "ral", "ki", "rel", "uk",
17      "kor", "kul", "kas", "lok", "luk", "las",
18      "mak", "mok", "mas", "mos", "ga",
19      "tha", "gul", "lug", "mag", "mog", "ug"};
20      private static final int KURUK_FIRST_NAME_MIN = 2;
21      private static final int KURUK_FIRST_NAME_EXTRA = 3;
22      private static final int KURUK_SECOND_NAME_MIN = 1;
23      private static final int KURUK_SECOND_NAME_EXTRA = 3;
24      private static final int KURUK_SECOND_NAME_PROBABILITY = 3; // 1 in 3 change of second namd
25      private static final int KURUK_SYLLABLES_NEEDED_BEFORE_SECOND_NAME_USED = 4;
26  
27      private static final String[] SNAFFI_SYLLABLES = {"sni", "sna", "fer", "fi", "fir",
28      "por", "per", "snu", "al", "an",
29      "erl", "lep", "fru", "fri", "ig",
30      "eg", "thi", "tha" };
31      private static final int SNAFFI_FIRST_NAME_MIN = 1;
32      private static final int SNAFFI_FIRST_NAME_EXTRA = 3;
33      private static final int SNAFFI_SECOND_NAME_MIN = 1;
34      private static final int SNAFFI_SECOND_NAME_EXTRA = 2;
35      private static final int SNAFFI_SECOND_NAME_PROBABILITY = 3; // 1 in 3 change of second namd
36      private static final int SNAFFI_SYLLABLES_NEEDED_BEFORE_SECOND_NAME_USED = 3;
37  
38      /*** determines whether this object is a <code>Nullable</code>. */
39      private boolean nullable;
40  
41      /***
42       * TODO: Leave this in until monsters are added properly.
43       */
44      public Monster() {
45          this( "Monster used wrong constructor.", Gender.FEMALE, 0 );
46      }
47  
48  
49      public Monster(String name, Gender gender, int maxHitPoints) {
50          super( Id.createId(), name, gender, maxHitPoints );
51      }
52  
53      String createKurukName() {
54          StringBuffer firstName = new StringBuffer();
55          StringBuffer secondName = new StringBuffer();
56  
57          int numberOfSyllablesInFirstName =
58          KURUK_FIRST_NAME_MIN + (int)(Math.random()*10)%KURUK_FIRST_NAME_EXTRA;
59  
60          // Generate First Name
61          for ( int i = 0; i < numberOfSyllablesInFirstName; i++ ) {
62              firstName.append( chooseKurukSyllable() );
63          }
64  
65          // Generate Second Name only if there weren't enough syllables in the first name
66          // and the probability of a second name succeeds.
67          if ( numberOfSyllablesInFirstName < KURUK_SYLLABLES_NEEDED_BEFORE_SECOND_NAME_USED &&
68          (int)(Math.random()*10)%KURUK_SECOND_NAME_PROBABILITY == 0 ) {
69              int numberOfSyllablesInSecondName =
70              KURUK_SECOND_NAME_MIN + (int)(Math.random()*10)%KURUK_SECOND_NAME_EXTRA;
71  
72              for ( int i = 0; i < numberOfSyllablesInSecondName; i++ ) {
73                  secondName.append( chooseKurukSyllable() );
74              }
75          }
76  
77          // Capitalize the names
78          firstName.setCharAt( 0, Character.toUpperCase( firstName.charAt( 0 ) ) );
79          if ( secondName.length() != 0 ) {
80              secondName.setCharAt( 0, Character.toUpperCase( secondName.charAt( 0 ) ) );
81              // If there is a second name append a space to separate first name from second name
82              firstName.append( ' ' );
83          }
84  
85          return firstName.toString() + secondName.toString();
86      }
87  
88      private String chooseKurukSyllable() {
89          return KURUK_SYLLABLES[ ((int)(Math.random()*100))%(KURUK_SYLLABLES.length) ];
90      }
91  
92      String createSnaffiName() {
93          StringBuffer firstName = new StringBuffer();
94          StringBuffer secondName = new StringBuffer();
95  
96          int numberOfSyllablesInFirstName =
97          SNAFFI_FIRST_NAME_MIN + (int)(Math.random()*10)%SNAFFI_FIRST_NAME_EXTRA;
98  
99          // Generate First Name
100         for ( int i = 0; i < numberOfSyllablesInFirstName; i++ ) {
101             firstName.append( chooseSnaffiSyllable() );
102         }
103 
104         // Generate Second Name only if there weren't enough syllables in the first name
105         // and the probability of a second name succeeds.
106         if ( numberOfSyllablesInFirstName == 1 ||
107         ( numberOfSyllablesInFirstName < SNAFFI_SYLLABLES_NEEDED_BEFORE_SECOND_NAME_USED &&
108         (int)(Math.random()*10)%SNAFFI_SECOND_NAME_PROBABILITY == 0 ) ) {
109             int numberOfSyllablesInSecondName =
110             SNAFFI_SECOND_NAME_MIN + (int)(Math.random()*10)%SNAFFI_SECOND_NAME_EXTRA;
111 
112             for ( int i = 0; i < numberOfSyllablesInSecondName; i++ ) {
113                 secondName.append( chooseSnaffiSyllable() );
114             }
115         }
116 
117         // Capitalize the names
118         firstName.setCharAt( 0, Character.toUpperCase( firstName.charAt( 0 ) ) );
119         if ( secondName.length() != 0 ) {
120             secondName.setCharAt( 0, Character.toUpperCase( secondName.charAt( 0 ) ) );
121             // If there is a second name append a space to separate first name from second name
122             firstName.append( ' ' );
123         }
124 
125         return firstName.toString() + secondName.toString();
126     }
127 
128     private String chooseSnaffiSyllable() {
129         return SNAFFI_SYLLABLES[ ((int)(Math.random()*100))%(SNAFFI_SYLLABLES.length) ];
130     }
131 
132     /***
133      * {@inheritDoc}
134      */
135     public IWizard getOwner() {
136         return owner;
137     }
138 
139     /***
140      * Return a Null <code>Monster</code>.
141      *
142      * @return an instance of the Null <code>Monster</code>.
143      */
144     public static Monster newNull() {
145         return new NullMonster();
146     }
147 
148 }