Added my own flapping. :)
[supertux.git] / src / badguy_specs.cpp
1 /***************************************************************************
2                badguy_specs.cpp  -  badguys properties table
3                      -------------------
4     begin                : Oct, 11 2004
5     copyright            : (C) 2004 by Ricardo Cruz
6     email                : rick2@aeiou.pt
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include <iostream>
19
20 #include "special/sprite_manager.h"
21 #include "resources.h"
22
23 #include "badguy_specs.h"
24
25 BadGuySpecsManager* badguyspecs_manager = 0;
26
27 BadGuySpecsManager::BadGuySpecsManager(const std::string& filename)
28 {
29   load_resfile(filename);
30 }
31
32 BadGuySpecsManager::~BadGuySpecsManager()
33 {
34   for(std::map<std::string, BadGuySpecs*>::iterator i =
35      badguys_specs.begin(); i != badguys_specs.end(); ++i)
36     delete i->second;
37 }
38
39 void
40 BadGuySpecsManager::load_resfile(const std::string& filename)
41 {
42   lisp_object_t* root_obj = lisp_read_from_file(filename);
43   if (!root_obj)
44     {
45       std::cout << "BadGuySpecsManager: Couldn't load: " << filename << std::endl;
46       return;
47     }
48
49   lisp_object_t* cur = root_obj;
50
51   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-badguys-specifications") != 0)
52     return;
53   cur = lisp_cdr(cur);
54
55   while(cur)
56     {
57       lisp_object_t* el = lisp_car(cur);
58
59       if (strcmp(lisp_symbol(lisp_car(el)), "badguy") == 0)
60         {
61           LispReader reader(lisp_cdr(lisp_car(cur)));
62           BadGuySpecs* badguy_specs = new BadGuySpecs(reader);
63
64           BadGuysSpecs::iterator i = badguys_specs.find(badguy_specs->get_name());
65           if (i == badguys_specs.end())
66             {
67               badguys_specs[badguy_specs->get_name()] = badguy_specs;
68             }
69           else
70             {
71               delete i->second;
72               i->second = badguy_specs;
73               std::cerr << "Warning: dulpicate entry: '" << badguy_specs->get_name() << "'" << std::endl;
74             }
75         }
76       else
77         {
78           std::cout << "BadGuySpecsManager: Unknown tag" << std::endl;
79         }
80
81       cur = lisp_cdr(cur);
82     }
83
84   lisp_free(root_obj);
85 }
86
87 BadGuySpecs*
88 BadGuySpecsManager::load(const std::string& name)
89 {
90   BadGuysSpecs::iterator i = badguys_specs.find(name);
91   if (i == badguys_specs.end())
92     {
93       std::cerr << "Warning: BadGuy specification '" << name << "' not found" << std::endl;
94       return 0;
95     }
96   return i->second;
97 }
98
99 BadGuySpecs::BadGuySpecs(LispReader& reader)
100 {
101   reset();
102
103   std::string str;
104   reader.read_string("kind", str);
105   kind = str;
106
107   str.clear();
108   reader.read_string("inherits", str);
109   if(!str.empty())
110     {
111     BadGuySpecs* bgspecs = badguyspecs_manager->load(str);
112     if(bgspecs)
113       {
114       sprite = bgspecs->sprite;
115       }
116     else
117       std::cerr << "Warning: inherited '" << str
118                 << "was not found.\nProbably, it was declared after"
119                    "this entry '" << kind << std::endl;
120     }
121
122   str.clear();
123   reader.read_string("sprite", str);
124   if(str.empty())
125     std::cerr << "Warning: No sprite has been set to badguy " << kind << std::endl;
126   else
127     sprite = sprite_manager->load(str);
128
129   if(!sprite)
130     {
131     std::cerr << "Warning: Sprite '" << str << "' could not be loaded.\n";
132     }
133 }
134
135 BadGuySpecs::BadGuySpecs(std::string& kind_)
136 {
137   reset();
138   kind = kind_;
139 }
140
141 BadGuySpecs::~BadGuySpecs()
142 {
143 }
144
145 void
146 BadGuySpecs::reset()
147 {
148   kind.clear();
149 }
150
151 std::string
152 BadGuySpecs::get_name()
153 {
154 return kind;
155 }
156
157 // EOF //