1   import java.awt.*;
2   import java.applet.*;
3   import javax.swing.*;
4   import java.awt.image.*;
5   import java.awt.geom.*;
6   import java.net.*;
7   import java.io.*;
8   /**
9    * Class to construct the Grid as specified in a mml file.
10   * 
11   * @see Game
12   * @see Grid
13   */
14  public class MapLoader
15  {
16      /**
17       * The Grid constructing
18       */
19      protected Grid g;
20      /**
21       * true iff finished Constructing the Grid
22       */
23      protected boolean fin;
24      /**
25       * The BufferedReader to read from
26       */
27      static BufferedReader br;
28      /**
29       * Counters
30       */
31      int quotes, bracket, element;
32      /**
33       * Char to read into
34       */
35      char c;
36      /**
37       * int to read into
38       */
39      int ch;
40      /**
41       * StringBuffer to test against
42       */
43      StringBuffer s;
44      /**
45       * The blocker, default MapObject for the grid
46       */
47      MapObject block, def;
48  
49      /**
50       * Construct a new MapLoader using the BufferedReader specified.
51       * 
52       * @param br the BufferedReader
53       * @exception InvalidFileFormatException iff the file is not properly formatted
54       */
55      public MapLoader(BufferedReader br) throws IOException, InvalidFileFormatException
56      {
57          g = new Grid();
58          this.br = br;
59          s = new StringBuffer();
60          quotes = 0;
61          bracket = 0;
62          element = 0;
63          startReading();
64      }
65      /**
66       * Sets the BufferedReader used to process the file, and begins processing.
67       * 
68       * @param br the BufferedReader
69       * @exception InvalidFileFormatException iff the file is not properly formatted
70       */
71      public boolean setFile(BufferedReader br) throws IOException, InvalidFileFormatException
72      {
73          if(fin && br != null)
74          {
75              this.br = br;
76              startReading();
77              return true;
78          }
79          return false;
80      }
81      /**
82       * Returns true iff c is between A and Z case insensitive
83       * 
84       * @return  true iff c is between A and Z case insensitive
85       */
86      protected boolean isChar(int c)
87      {
88          return ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') );
89      }
90      /**
91       * Get the Grid Constructed
92       * 
93       * @return the Grid Constructed
94       * @see Grid
95       */
96      public Grid getGrid() { return g; }
97      /**
98       * Gets the deafult MapObject for the GridConstructed
99       * 
100      * @return the deafult MapObject for the GridConstructed
101      */
102     public MapObject getDefault() { return def; }
103     /**
104      * Starts the processing of the file.
105      * 
106      * @exception InvalidFileFormatException iff the file is not properly formatted
107      */
108     protected void startReading() throws IOException, InvalidFileFormatException
109     {
110         
111         // Read until first keyword
112         s.setLength(0);
113         while((ch = br.read()) != ' ' && ch != -1)
114         {
115             if(isChar(ch))
116                 s.append((char)ch);
117         }
118         if(!s.toString().equalsIgnoreCase("map"))
119             throw new InvalidFileFormatException("Invalid File Format");
120         bracket++;
121         s.setLength(0);
122 
123         while((ch = br.read()) != -1)
124         {
125             if(ch == '\n')
126                 continue;
127             else if(ch == '<')
128             {
129                 s.setLength(0);
130                 bracket++;
131                 while((c = (char)br.read()) != '>' && c != ' ')
132                 {
133                     s.append(c);
134                 }
135                 String st = s.toString();
136                 if(st.equalsIgnoreCase("monstersAllowed"))
137                 {
138                     processMonstersAllowed(br);
139                 }
140                 else if(st.equalsIgnoreCase("person"))
141                 {
142                     processPerson(br, true);
143                 }
144                 else if(st.equalsIgnoreCase("trainer"))
145                 {
146                     processPerson(br, false);
147                 }
148                 else if(st.equalsIgnoreCase("player"))
149                 {
150                     processPlayer(br);
151                 }
152                 else if(st.equalsIgnoreCase("blocker"))
153                 {
154                     processBlocker(br);
155                 }
156                 else if(st.equalsIgnoreCase("building"))
157                 {
158                     processBuilding(br);
159                 }
160                 /*else if(st.equalsIgnoreCase("item"))
161                 {
162                     processItemMap(br);
163                 }*/
164                 else if(st.equalsIgnoreCase("redirect"))
165                 {
166                     processRedirect(br);
167                 }
168                 else if(st.indexOf("!") != -1) // Comments
169                 {
170                     continue;
171                 }
172                 else if(st.equalsIgnoreCase("/map"))
173                 {
174                     break;
175                 }
176                 else
177                 {
178                     throw new InvalidFileFormatException("Invalid File Format");
179                 }
180             }
181             else if(ch == '>')
182                 bracket--;
183             else if(ch == '=')
184             {
185                 if(s.toString().equalsIgnoreCase("default"))
186                 {
187                     s.setLength(0);
188                     while(br.read() != '\"'); // Skip first "
189                     while((c = (char)br.read()) != '\"')
190                     {
191                         s.append(c);
192                     }
193                     try
194                     {
195                         if(s.toString().equalsIgnoreCase("floor"))
196                         {
197                             def = new Floor();
198                             for(int i = 0; i < Grid.DEFAULTNUM; i++)
199                             {
200                                 for(int j = 0; j < Grid.DEFAULTNUM; j++)
201                                 {
202                                     g.setObjectAt(new Floor(), i, j);
203                                 }
204                             }
205                         }
206                         else
207                         {
208                             def = new Grass();
209                             for(int i = 0; i < Grid.DEFAULTNUM; i++)
210                             {
211                                 for(int j = 0; j < Grid.DEFAULTNUM; j++)
212                                 {
213                                     g.setObjectAt(new Grass(), i, j);
214                                 }
215                             }
216                         }
217                         g.setDefault(def);
218                     }
219                     catch(Exception e) { }
220                     s.setLength(0);
221                     continue;
222                 }
223                 else if(s.toString().equalsIgnoreCase("blocker"))
224                 {
225                     s.setLength(0);
226                     while(br.read() != '\"'); // Skip first "
227                     while((c = (char)br.read()) != '\"')
228                     {
229                         s.append(c);
230                     }
231                     if(s.toString().equalsIgnoreCase("Wall"))
232                     {
233                         block = new Wall();
234                     }
235                     else
236                     {
237                         block = new Rock();
238                     }
239                     s.setLength(0);
240                     continue;
241                 }
242             }
243             else if(ch == '\"')
244             {
245                 if(quotes > 0)
246                 {
247                     quotes--;
248                 }
249                 else
250                 {
251                     quotes++;
252                 }
253             }
254             else if(ch == ' ' && quotes == 0)
255                 continue;
256             else if(isChar(ch))
257             {
258                 s.append((char)ch);
259             }
260         }
261         fin = true;
262     }
263     /**
264      * Handles the Monsters Allowed
265      * 
266      * @param br the BufferedReader to use
267      * @exception InvalidFileFormatException iff the file is not properly formatted
268      */
269     protected void processMonstersAllowed(BufferedReader br) throws IOException, InvalidFileFormatException
270     {
271         int i = 0;
272         String st;
273         while(i!=-1)
274         {
275             i = br.read();
276             if(i == '>')
277             {
278                 bracket--;
279             }
280             else if(i == '<')
281             {
282                 i = br.read();
283                 if(i == '/')
284                 {
285                     s.setLength(0);
286                     while((i = br.read()) != '>')
287                     {
288                         s.append((char)i);
289                     }
290                     if(s.toString().equalsIgnoreCase("type"))
291                     {
292                         continue;
293                     }
294                     else if(s.toString().equalsIgnoreCase("monstersAllowed"))
295                     {
296                         s.setLength(0);
297                         return;
298                     }
299                     else
300                     {
301                         throw new InvalidFileFormatException("Invalid File Format");
302                     }
303                 }
304                 s.setLength(0);
305                 s.append((char)i);
306                 while((i = br.read()) != '>')
307                 {
308                     s.append((char)i);
309                 }
310                 bracket++;
311                 if(s.toString().equalsIgnoreCase("type"))
312                 {
313                     s.setLength(0);
314                     while((i = br.read()) != '<')
315                     {
316                         s.append((char)i);
317                     }
318                     bracket++;
319                     st = s.toString();
320                     if(st.equalsIgnoreCase("Electric"))
321                     {
322                         g.allowMonsterOnMap(Monster.ELECTRIC);
323                         continue;
324                     }
325                     else if(st.equalsIgnoreCase("Earth"))
326                     {
327                         g.allowMonsterOnMap(Monster.EARTH);
328                         continue;
329                     }
330                     else if(st.equalsIgnoreCase("Fire"))
331                     {
332                         g.allowMonsterOnMap(Monster.FIRE);
333                         continue;
334                     }
335                     else if(st.equalsIgnoreCase("Water"))
336                     {
337                         g.allowMonsterOnMap(Monster.WATER);
338                         continue;
339                     }
340                     else if(st.equalsIgnoreCase("Gas"))
341                     {
342                         g.allowMonsterOnMap(Monster.GAS);
343                         continue;
344                     }
345                     else
346                     {
347                         throw new InvalidFileFormatException("Invalid File Format");
348                     }
349                 }
350                 else if(s.toString().equalsIgnoreCase("level"))
351                 {
352                     s.setLength(0);
353                     while((i = br.read()) != '<')
354                     {
355                         s.append((char)i);
356                     }
357                     st = s.toString();
358                     try
359                     {
360                         int lev = Integer.parseInt(st);
361                         g.setMonsterLevel(lev);
362                     }
363                     catch(Exception e)
364                     { 
365                             throw new InvalidFileFormatException("Invalid File Format");
366                     }
367                 }
368             }
369         }
370     }
371 
372     /**
373      * Handle the Player
374      *   
375      * @param br the BufferedReader to use
376      * @exception InvalidFileFormatException iff the file is not properly formatted
377      */
378     protected void processPlayer(BufferedReader br) throws IOException, InvalidFileFormatException
379     {
380         int i, pi = 3, pj = 5;
381         String st;
382         Player p = new Player();
383 
384         while((i = br.read()) != '\n')
385         {
386             if(i == 'i' || i == 'I')
387             {
388                 while((i = br.read()) != '=');
389                 s.setLength(0);
390                 while((i = br.read()) != ' ')
391                 {
392                     if(i == '>')
393                         break;
394                     else
395                     {
396                         s.append((char)i);
397                     }
398                 }
399                 try
400                 {
401                     pi = Integer.parseInt(s.toString());
402                 }
403                 catch(Exception e)
404                 {
405                     throw new InvalidFileFormatException("Invalid Format");
406                 }
407             }
408             else if(i == 'j' || i == 'J')
409             {
410                 while((i = br.read()) != '=');
411                 s.setLength(0);
412                 while((i = br.read()) != ' ')
413                 {
414                     if(i == '>')
415                         break;
416                     else
417                     {
418                         s.append((char)i);
419                     }
420                 }
421                 try
422                 {
423                     pj = Integer.parseInt(s.toString());
424                 }
425                 catch(Exception e)
426                 {
427                     throw new InvalidFileFormatException("Invalid Format");
428                 }
429             }
430             if(i == '>')
431                 break;
432         }
433         try
434         {
435             g.setObjectAt(p, pi, pj);
436             p.setBehind(def);
437         }
438         catch(Exception e)
439         {
440             throw new InvalidFileFormatException("Invalid Format");
441         }
442         while((i = br.read()) != '>');
443     }
444 
445     /**
446      * Handle the Person
447      *   
448      * @param br the BufferedReader to use
449      * @param person pass true if a Person false if a Trainer
450      * @exception InvalidFileFormatException iff the file is not properly formatted
451      */
452     protected void processPerson(BufferedReader br, boolean person) throws IOException, InvalidFileFormatException
453     {
454         int i, pi = 3, pj = 3;
455         String st;
456         People p;
457         if(person)
458             p = new Person();
459         else
460             p = new Trainer();
461 
462         while((i = br.read()) != '\n')
463         {
464             if(i == 'i' || i == 'I')
465             {
466                 while((i = br.read()) != '=');
467                 s.setLength(0);
468                 while((i = br.read()) != ' ')
469                 {
470                     if(i == '>')
471                         break;
472                     else
473                     {
474                         s.append((char)i);
475                     }
476                 }
477                 try
478                 {
479                     pi = Integer.parseInt(s.toString());
480                 }
481                 catch(Exception e)
482                 {
483                     throw new InvalidFileFormatException("Invalid Format");
484                 }
485             }
486             else if(i == 'j' || i == 'J')
487             {
488                 while((i = br.read()) != '=');
489                 s.setLength(0);
490                 while((i = br.read()) != ' ')
491                 {
492                     if(i == '>')
493                         break;
494                     else
495                     {
496                         s.append((char)i);
497                     }
498                 }
499                 try
500                 {
501                     pj = Integer.parseInt(s.toString());
502                 }
503                 catch(Exception e)
504                 {
505                     throw new InvalidFileFormatException("Invalid Format");
506                 }
507             }
508             if(i == '>')
509                 break;
510         }
511         try
512         {
513             g.setObjectAt(p, pi, pj);
514             p.setBehind(def);
515         }
516         catch(Exception e)
517         {
518             throw new InvalidFileFormatException("Invalid Format");
519         }
520 
521         while(i!=-1)
522         {
523             i = br.read();
524             if(i == '>')
525                 bracket--;
526             else if(i == '<')
527             {
528                 i = br.read();
529                 if(i == '/')
530                 {
531                     s.setLength(0);
532                     while((i = br.read()) != '>')
533                     {
534                         s.append((char)i);
535                     }
536                     st = s.toString();
537                     if(st.equalsIgnoreCase("money"))
538                     {
539                         continue;
540                     }
541                     else if(st.equalsIgnoreCase("name"))
542                     {
543                         continue;
544                     }
545                     else if(st.equalsIgnoreCase("Direction"))
546                     {
547                         continue;
548                     }
549                     else if(st.equalsIgnoreCase("badge"))
550                     {
551                         continue;
552                     }
553                         else if(st.equalsIgnoreCase("dialogue"))
554                     {
555                         continue;
556                     }
557                     else if(st.equalsIgnoreCase("trainer"))
558                     {
559                         s.setLength(0);
560                         return;
561                     }
562                     else if(st.equalsIgnoreCase("person"))
563                     {
564                         s.setLength(0);
565                         return;
566                     }
567                     else
568                     {
569                         throw new InvalidFileFormatException("Invalid File Format");
570                     }
571 
572                 }
573                 s.setLength(0);
574                 s.append((char)i);
575                 while((i = br.read()) != '>')
576                 {
577                     s.append((char)i);
578                 }
579                 bracket++;
580                 st = s.toString();
581                 if(st.equalsIgnoreCase("money"))
582                 {
583                     s.setLength(0);
584                     while((i = br.read()) != '<')
585                     {
586                         s.append((char)i);
587                     }
588                     try
589                     {
590                         int mon = Integer.parseInt(s.toString());
591                         p.setMoney(mon);
592                     }
593                     catch(Exception e) 
594                     {
595                         throw new InvalidFileFormatException("Invalid File Format");
596                     }
597 
598                 }
599                 else if(st.equalsIgnoreCase("name"))
600                 {
601                     s.setLength(0);
602                     while((i = br.read()) != '<')
603                     {
604                         s.append((char)i);
605                     }
606                     p.setName(s.toString());
607                 }
608                 else if(st.equalsIgnoreCase("badge"))
609                 {
610                     if(p.getType() == MapObject.TRAINER)
611                     {
612                         s.setLength(0);
613                         while((i = br.read()) != '<')
614                         {
615                             s.append((char)i);
616                         }
617                         ((Trainer)p).setBadge(s.toString());
618                     }
619                     else
620                         continue;
621                 }
622                 else if(st.equalsIgnoreCase("direction"))
623                 {
624                     s.setLength(0);
625                     while((i = br.read()) != '<')
626                     {
627                         s.append((char)i);
628                     }
629                     st = s.toString();
630                     if(st.equalsIgnoreCase("UP"))
631                     {
632                         p.setDirection(Grid.UP);
633                         continue;
634                     }
635                     else if(st.equalsIgnoreCase("DOWN"))
636                     {
637                         p.setDirection(Grid.DOWN);
638                         continue;
639                     }
640                     else if(st.equalsIgnoreCase("LEFT"))
641                     {
642                         p.setDirection(Grid.LEFT);
643                         continue;
644                     }
645                     else if(st.equalsIgnoreCase("RIGHT"));
646                     {
647                         p.setDirection(Grid.RIGHT);
648                         continue;
649                     }
650                 }
651                 else if(st.equalsIgnoreCase("dialogue"))
652                 {
653                     s.setLength(0);
654                     while((i = br.read()) != '<')
655                     {
656                         s.append((char)i);
657                     }
658                     p.setDialogue(s.toString());
659                 }
660                 else if(st.equalsIgnoreCase("monsterVector"))
661                 {
662                     processMonsterVector(br, p);
663                 }
664                 else if(st.equalsIgnoreCase("itemVector"))
665                 {
666                     processItemVector(br, p);
667                 }
668                 else
669                 {
670                     throw new InvalidFileFormatException("Invalid File Format");
671                 }
672             }
673         }
674     }
675     /**
676      * Handle the Monster Vector
677      *   
678      * @param br the BufferedReader to use
679      * @param p the People to add the Monsters to
680      * @exception InvalidFileFormatException iff the file is not properly formatted
681      */
682     protected void processMonsterVector(BufferedReader br, People p) throws IOException, InvalidFileFormatException
683     {
684         int i = 0;
685         String st;
686         while(i!=-1)
687         {
688             i = br.read();
689             if(i == '>')
690             {
691                 bracket--;
692             }
693             else if(i == '<')
694             {
695                 i = br.read();
696                 if(i == '/')
697                 {
698                     s.setLength(0);
699                     while((i = br.read()) != '>')
700                     {
701                         s.append((char)i);
702                     }
703                     if(s.toString().equalsIgnoreCase("monsterVector"))
704                     {
705                         s.setLength(0);
706                         return;
707                     }
708                     else
709                     {
710                         throw new InvalidFileFormatException("Invalid File Format");
711                     }
712                 }
713                 s.setLength(0);
714                 s.append((char)i);
715                 while((i = br.read()) != '>')
716                 {
717                     s.append((char)i);
718                 }
719                 bracket--;
720                 if(s.toString().equalsIgnoreCase("monster"))
721                 {
722                     Monster m = processMonster(br);
723                     p.addMonster(m);
724                 }
725                 else
726                 {
727                     throw new InvalidFileFormatException("Invalid File Format");
728                 }
729             }
730         }
731     }
732     /**
733      * Handle the Monster
734      *   
735      * @param br the BufferedReader to use
736      * @return the Monster specified in th mml
737      * @exception InvalidFileFormatException iff the file is not properly formatted
738      */
739     protected Monster processMonster(BufferedReader br) throws IOException, InvalidFileFormatException
740     {
741         Monster m = null;
742         int i=0, pi, pj, lev= 20;
743         String st;
744         while(i!=-1)
745         {
746             i = br.read();
747             if(i == '>')
748                 bracket--;
749             else if(i == '<')
750             {
751                 i = br.read();
752                 if(i == '/')
753                 {
754                     s.setLength(0);
755                     while((i = br.read()) != '>')
756                     {
757                         s.append((char)i);
758                     }
759                     st = s.toString();
760                     if(st.equalsIgnoreCase("kind"))
761                     {
762                         continue;
763                     }
764                     else if(st.equalsIgnoreCase("hp"))
765                     {
766                         continue;
767                     }
768                     else if(st.equalsIgnoreCase("defp"))
769                     {
770                         continue;
771                     }
772                     else if(st.equalsIgnoreCase("attp"))
773                     {
774                         continue;
775                     }
776                     else if(st.equalsIgnoreCase("exp"))
777                     {
778                         continue;
779                     }
780                     else if(st.equalsIgnoreCase("level"))
781                     {
782                         continue;
783                     }
784                     else if(st.equalsIgnoreCase("attack"))
785                     {
786                         continue;
787                     }
788                     else if(st.equalsIgnoreCase("monster"))
789                     {
790                         s.setLength(0);
791                         return m;
792                     }
793                     else
794                     {
795                         throw new InvalidFileFormatException("Invalid File Format");
796                     }
797 
798                 }
799                 s.setLength(0);
800                 s.append((char)i);
801                 while((i = br.read()) != '>')
802                 {
803                     s.append((char)i);
804                 }
805                 bracket++;
806                 st = s.toString();
807                 if(st.equalsIgnoreCase("hp"))
808                 {
809                     s.setLength(0);
810                     while((i = br.read()) != '<')
811                     {
812                         s.append((char)i);
813                     }
814                     try
815                     {
816                         int hp = Integer.parseInt(s.toString());
817                         m.setMaxHP(hp);
818                         m.setHP(hp);
819                     }
820                     catch(Exception e) 
821                     { 
822                         throw new InvalidFileFormatException("Invalid File Format");
823                     }
824 
825                 }
826                 else if(st.equalsIgnoreCase("defp"))
827                 {
828                     s.setLength(0);
829                     while((i = br.read()) != '<')
830                     {
831                         s.append((char)i);
832                     }
833                     try
834                     {
835                         int def = Integer.parseInt(s.toString());
836                         m.setMaxDefense(def);
837                         m.setDefense(def);
838                     }
839                     catch(Exception e) 
840                     {
841                         throw new InvalidFileFormatException("Invalid File Format");
842                     }
843                 }
844                 else if(st.equalsIgnoreCase("attp"))
845                 {
846                     s.setLength(0);
847                     while((i = br.read()) != '<')
848                     {
849                         s.append((char)i);
850                     }
851                     try
852                     {
853                         int att = Integer.parseInt(s.toString());
854                         m.setMaxAttack(att);
855                         m.setAttack(att);
856                     }
857                     catch(Exception e) 
858                     { 
859                         throw new InvalidFileFormatException("Invalid File Format");
860                     }
861                 }
862                 else if(st.equalsIgnoreCase("exp"))
863                 {
864                     s.setLength(0);
865                     while((i = br.read()) != '<')
866                     {
867                         s.append((char)i);
868                     }
869                     try
870                     {
871                         int exp = Integer.parseInt(s.toString());
872                         m.setMaxExperience(10*lev);
873                         m.setExperience(exp);
874                     }
875                     catch(Exception e) 
876                     { 
877                         throw new InvalidFileFormatException("Invalid File Format");
878                     }
879                 }
880                 else if(st.equalsIgnoreCase("level"))
881                 {
882                     s.setLength(0);
883                     while((i = br.read()) != '<')
884                     {
885                         s.append((char)i);
886                     }
887                     try
888                     {
889                         lev = Integer.parseInt(s.toString());
890                         m.setLevel(lev);
891                     }
892                     catch(Exception e) 
893                     { 
894                         throw new InvalidFileFormatException("Invalid File Format");
895                     }
896                 }
897                 else if(st.equalsIgnoreCase("name"))
898                 {
899                     s.setLength(0);
900                     while((i = br.read()) != '<')
901                     {
902                         s.append((char)i);
903                     }
904                     m.setName(s.toString());
905                 }
906                 else if(st.equalsIgnoreCase("attack"))
907                 {
908                     s.setLength(0);
909                     while((i = br.read()) != '<')
910                     {
911                         s.append((char)i);
912                     }
913                     st = s.toString();
914                     if(m.getNumberOfAttacks() >= Monster.MAXATTACKS)
915                     {
916                         continue;
917                     }
918                     if(st.equalsIgnoreCase("A1"))
919                     {
920                         m.addAttack(m.getA1());
921                     }
922                     else if(st.equalsIgnoreCase("A2"))
923                     {
924                         m.addAttack(m.getA2());
925                     }
926                     else if(st.equalsIgnoreCase("A3"))
927                     {
928                         m.addAttack(m.getA3());
929                     }
930                     else if(st.equalsIgnoreCase("A4"))
931                     {
932                         m.addAttack(m.getA4());
933                     }
934                     else if(st.equalsIgnoreCase("A5"))
935                     {
936                         m.addAttack(m.getA5());
937                     }
938                     else if(st.equalsIgnoreCase("A6"))
939                     {
940                         m.addAttack(m.getA6());
941                     }
942                     else
943                     {
944                         throw new InvalidFileFormatException("Invalid File Format");
945                     }
946                 }
947                 else if(st.equalsIgnoreCase("kind"))
948                 {
949                     s.setLength(0);
950                     while((i = br.read()) != '<')
951                     {
952                         s.append((char)i);
953                     }
954                     st = s.toString();
955                     if(st.equalsIgnoreCase("Electric"))
956                     {
957                         m = new ElectricMon();
958                     }
959                     else if(st.equalsIgnoreCase("EARTH"))
960                     {
961                         m = new EarthMon();
962                     }
963                     else if(st.equalsIgnoreCase("Fire"))
964                     {
965                         m = new FireMon();
966                     }
967                     else if(st.equalsIgnoreCase("Water"))
968                     {
969                         m = new WaterMon();
970                     }
971                     else if(st.equalsIgnoreCase("Gas"))
972                     {
973                         m = new GasMon();
974                     }
975                     else
976                     {
977                         throw new InvalidFileFormatException("Invalid File Format");
978                     }
979                 }
980                 else
981                 {
982                     throw new InvalidFileFormatException("Invalid File Format");
983                 }
984             }
985         }
986         return m;
987     }
988 
989     /**
990      * Handle the Item Vector
991      *   
992      * @param br the BufferedReader to use
993      * @param p the People to add the Item to
994      * @exception InvalidFileFormatException iff the file is not properly formatted
995      */
996     protected void processItemVector(BufferedReader br, People p) throws IOException, InvalidFileFormatException
997     {
998         int i=0;
999         String st;
1000        Item m;
1001        while(i!=-1)
1002        {
1003            i = br.read();
1004            if(i == '>')
1005            {
1006                bracket--;
1007            }
1008            else if(i == '<')
1009            {
1010                i = br.read();
1011                if(i == '/')
1012                {
1013                    s.setLength(0);
1014                    while((i = br.read()) != '>')
1015                    {
1016                        s.append((char)i);
1017                    }
1018                    if(s.toString().equalsIgnoreCase("itemVector"))
1019                    {
1020                        s.setLength(0);
1021                        return;
1022                    }
1023                    else
1024                    {
1025                        throw new InvalidFileFormatException("Invalid File Format");
1026                    }
1027                }
1028                s.setLength(0);
1029                s.append((char)i);
1030                while((i = br.read()) != '>')
1031                {
1032                    s.append((char)i);
1033                }
1034                bracket--;
1035                if(s.toString().equalsIgnoreCase("item"))
1036                {
1037                    m = processItemForVector(br);
1038                    p.addItem(m);
1039                }
1040                else
1041                {
1042                    throw new InvalidFileFormatException("Invalid File Format");
1043                }
1044            }
1045        }
1046    }
1047    /**
1048     * Handle the Item
1049     *   
1050     * @param br the BufferedReader to use
1051     * @return the Item created
1052     * @exception InvalidFileFormatException iff the file is not properly formatted
1053     */
1054    protected Item processItemForVector(BufferedReader br) throws IOException, InvalidFileFormatException
1055    {
1056        int i=0, pi, pj;
1057        String st;
1058        Item it = null;
1059        while(i!=-1)
1060        {
1061            i = br.read();
1062            if(i == '>')
1063                bracket--;
1064            else if(i == '<')
1065            {
1066                i = br.read();
1067                if(i == '/')
1068                {
1069                    s.setLength(0);
1070                    while((i = br.read()) != '>')
1071                    {
1072                        s.append((char)i);
1073                    }
1074                    st = s.toString();
1075                    if(st.equalsIgnoreCase("potion"))
1076                    {
1077                        continue;
1078                    }
1079                    else if(st.equalsIgnoreCase("quantity"))
1080                    {
1081                        continue;
1082                    }
1083                    else if(st.equalsIgnoreCase("monsterBall"))
1084                    {
1085                        continue;
1086                    }
1087                    else if(st.equalsIgnoreCase("item"))
1088                    {
1089                        s.setLength(0);
1090                        return it;
1091                    }
1092                    else
1093                    {
1094                        throw new InvalidFileFormatException("Invalid File Format");
1095                    }
1096                }
1097                s.setLength(0);
1098                s.append((char)i);
1099                while((i = br.read()) != '>')
1100                {
1101                    s.append((char)i);
1102                }
1103                bracket++;
1104                st = s.toString();
1105                if(st.equalsIgnoreCase("potion"))
1106                {
1107                    s.setLength(0);
1108                    while((i = br.read()) != '<')
1109                    {
1110                        s.append((char)i);
1111                    }
1112                    st = s.toString();
1113                    if(st.equalsIgnoreCase("HPUPFULL"))
1114                    {
1115                        it = new PotionItem("HP Up Full", PotionItem.HPUPFULL);
1116                    }
1117                    else if(st.equalsIgnoreCase("HPUPHALF"))
1118                    {
1119                        it = new PotionItem("HP Up Half", PotionItem.HPUPHALF);
1120                    }
1121                    else if(st.equalsIgnoreCase("HPUPTHIRD"))
1122                    {
1123                        it = new PotionItem("HP Up Third", PotionItem.HPUPTHIRD);
1124                    }
1125                    else if(st.equalsIgnoreCase("DEFUP"))
1126                    {
1127                        it = new PotionItem("Defense Up", PotionItem.DEFUP);
1128                    }
1129                    else if(st.equalsIgnoreCase("ATTUP"))
1130                    {
1131                        it = new PotionItem("Attack Up", PotionItem.ATTUP);
1132                    }
1133                    else
1134                    {
1135                        throw new InvalidFileFormatException("Invalid File Format");
1136                    }
1137                }
1138                else if(st.equalsIgnoreCase("quantity"))
1139                {
1140                    s.setLength(0);
1141                    while((i = br.read()) != '<')
1142                    {
1143                        s.append((char)i);
1144                    }
1145                    st = s.toString();
1146                    try
1147                    {
1148                        int q = Integer.parseInt(st);
1149                        it.setItemCount(q);
1150                    }
1151                    catch(Exception e) 
1152                    { 
1153                        throw new InvalidFileFormatException("Invalid File Format");
1154                    }
1155                }
1156                else if(st.equalsIgnoreCase("monsterBall"))
1157                {
1158                    s.setLength(0);
1159                    while((i = br.read()) != '<')
1160                    {
1161                        s.append((char)i);
1162                    }
1163                    st = s.toString();
1164                    if(st.equalsIgnoreCase("NORMAL"))
1165                    {
1166                        System.out.println("Better");
1167                        it = new MonsterBallItem("Normal MonsterBall", MonsterBallItem.NORMAL);
1168                    }
1169                    else if(st.equalsIgnoreCase("MASTER"))
1170                    {
1171                        it = new MonsterBallItem("Master MonsterBall", MonsterBallItem.MASTER);
1172                    }
1173                    else
1174                    {
1175                        throw new InvalidFileFormatException("Invalid File Format");
1176                    }
1177                }
1178                else
1179                {
1180                    throw new InvalidFileFormatException("Invalid File Format");
1181                }
1182            }
1183        }
1184        return it;
1185    }
1186
1187    /**
1188     * Handle the Blocker
1189     *   
1190     * @param br the BufferedReader to use
1191     * @exception InvalidFileFormatException iff the file is not properly formatted
1192     */
1193    protected void processBlocker(BufferedReader br) throws IOException, InvalidFileFormatException
1194    {
1195        int i, pi = 3, pj = 0;
1196        String st;
1197        MapObject p;
1198        if(block.getType() == MapObject.ROCK)
1199            p = new Rock();
1200        else
1201            p = new Wall();
1202
1203        while((i = br.read()) != '\n')
1204        {
1205            if(i == 'i' || i == 'I')
1206            {
1207                while((i = br.read()) != '=');
1208                s.setLength(0);
1209                while((i = br.read()) != ' ')
1210                {
1211                    if(i == '>')
1212                        break;
1213                    else
1214                    {
1215                        s.append((char)i);
1216                    }
1217                }
1218                try
1219                {
1220                    pi = Integer.parseInt(s.toString());
1221                }
1222                catch(Exception e)
1223                {
1224                    throw new InvalidFileFormatException("Invalid Format");
1225                }
1226            }
1227            else if(i == 'j' || i == 'J')
1228            {
1229                while((i = br.read()) != '=');
1230                s.setLength(0);
1231                while((i = br.read()) != ' ')
1232                {
1233                    if(i == '>')
1234                        break;
1235                    else
1236                    {
1237                        s.append((char)i);
1238                    }
1239                }
1240                try
1241                {
1242                    pj = Integer.parseInt(s.toString());
1243                }
1244                catch(Exception e)
1245                {
1246                    throw new InvalidFileFormatException("Invalid Format");
1247                }
1248            }
1249            if(i == '>')
1250                break;
1251        }
1252        try
1253        {
1254            g.setObjectAt(p, pi, pj);
1255        }
1256        catch(Exception e)
1257        {
1258            throw new InvalidFileFormatException("Invalid Format");
1259        }
1260        while((i = br.read()) != '>');
1261    }
1262    /**
1263     * Handle the Building
1264     *   
1265     * @param br the BufferedReader to use
1266     * @exception InvalidFileFormatException iff the file is not properly formatted
1267     */
1268    protected void processBuilding(BufferedReader br) throws IOException, InvalidFileFormatException
1269    {
1270        int i, pi = 8, pj = 8;
1271        String st;
1272        while((i = br.read()) != '\n')
1273        {
1274            if(i == 'i' || i == 'I')
1275            {
1276                while((i = br.read()) != '=');
1277                s.setLength(0);
1278                while((i = br.read()) != ' ')
1279                {
1280                    if(i == '>')
1281                        break;
1282                    else
1283                    {
1284                        s.append((char)i);
1285                    }
1286                }
1287                try
1288                {
1289                    pi = Integer.parseInt(s.toString());
1290                }
1291                catch(Exception e)
1292                {
1293                    throw new InvalidFileFormatException("Invalid Format");
1294                }
1295            }
1296            else if(i == 'j' || i == 'J')
1297            {
1298                while((i = br.read()) != '=');
1299                s.setLength(0);
1300                while((i = br.read()) != ' ')
1301                {
1302                    if(i == '>')
1303                        break;
1304                    else
1305                    {
1306                        s.append((char)i);
1307                    }
1308                }
1309                try
1310                {
1311                    pj = Integer.parseInt(s.toString());
1312                }
1313                catch(Exception e)
1314                {
1315                    throw new InvalidFileFormatException("Invalid Format");
1316                }
1317            }
1318            if(i == '>')
1319                break;
1320        }
1321        try
1322        {
1323            g.setObjectAt(new Building(), pi, pj);
1324        }
1325        catch(Exception e)
1326        {
1327            throw new InvalidFileFormatException("Invalid Format");
1328        }
1329        while((i = br.read()) != '>');
1330    }
1331    /**
1332     * Handle the Redirect
1333     *   
1334     * @param br the BufferedReader to use
1335     * @exception InvalidFileFormatException iff the file is not properly formatted
1336     */
1337    protected void processRedirect(BufferedReader br) throws IOException, InvalidFileFormatException
1338    {
1339        int i, pi = 2, pj = 2;
1340        String st;
1341        Redirect r = new Redirect();
1342
1343        while((i = br.read()) != '\n')
1344        {
1345            if(i == 'i' || i == 'I')
1346            {
1347                while((i = br.read()) != '=');
1348                s.setLength(0);
1349                while((i = br.read()) != ' ')
1350                {
1351                    if(i == '>')
1352                        break;
1353                    else
1354                    {
1355                        s.append((char)i);
1356                    }
1357                }
1358                try
1359                {
1360                    pi = Integer.parseInt(s.toString());
1361                }
1362                catch(Exception e)
1363                {
1364                    throw new InvalidFileFormatException("Invalid Format");
1365                }
1366            }
1367            else if(i == 'j' || i == 'J')
1368            {
1369                while((i = br.read()) != '=');
1370                s.setLength(0);
1371                while((i = br.read()) != ' ')
1372                {
1373                    if(i == '>')
1374                        break;
1375                    else
1376                    {
1377                        s.append((char)i);
1378                    }
1379                }
1380                try
1381                {
1382                    pj = Integer.parseInt(s.toString());
1383                }
1384                catch(Exception e)
1385                {
1386                    throw new InvalidFileFormatException("Invalid Format");
1387                }
1388            }
1389            if(i == '>')
1390                break;
1391        }
1392        try
1393        {
1394            g.setObjectAt(r, pi, pj);
1395        }
1396        catch(Exception e)
1397        {
1398            throw new InvalidFileFormatException("Invalid Format");
1399        }
1400        while(i!=-1)
1401        {
1402            i = br.read();
1403            if(i == '>')
1404                bracket--;
1405            else if(i == '<')
1406            {
1407                i = br.read();
1408                if(i == '/')
1409                {
1410                    s.setLength(0);
1411                    while((i = br.read()) != '>')
1412                    {
1413                        s.append((char)i);
1414                    }
1415                    st = s.toString();
1416                    if(st.equalsIgnoreCase("to"))
1417                    {
1418                        continue;
1419                    }
1420                    else if(st.equalsIgnoreCase("requires"))
1421                    {
1422                        continue;
1423                    }
1424                    else if(st.equalsIgnoreCase("redirect"))
1425                    {
1426                        s.setLength(0);
1427                        return;
1428                    }
1429                    else
1430                    {
1431                        throw new InvalidFileFormatException("Invalid Format");
1432                    }
1433                }
1434                s.setLength(0);
1435                s.append((char)i);
1436                while((i = br.read()) != '>')
1437                {
1438                    s.append((char)i);
1439                }
1440                bracket++;
1441                st = s.toString();
1442                if(st.equalsIgnoreCase("to"))
1443                {
1444                    s.setLength(0);
1445                    while((i = br.read()) != '<')
1446                    {
1447                        s.append((char)i);
1448                    }
1449                    r.setMapTo(s.toString());
1450                    r.setBehind(def);
1451                }
1452                else if(st.equalsIgnoreCase("requires"))
1453                {
1454                    s.setLength(0);
1455                    while((i = br.read()) != '<')
1456                    {
1457                        s.append((char)i);
1458                    }
1459                    try
1460                    {
1461                        r.setNumberBadgesRequired(Integer.parseInt(s.toString()));
1462                    }
1463                    catch(Exception e)
1464                    {
1465                        throw new InvalidFileFormatException("Invalid Format");
1466                    }
1467                    r.setBehind(def);
1468                }
1469                else
1470                {
1471                    throw new InvalidFileFormatException("Invalid Format");
1472                }
1473            }
1474        }
1475    }
1476}