00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <string.h>
00038
00039 #include "stratagus.h"
00040 #include "video.h"
00041 #include "map.h"
00042 #include "construct.h"
00043 #include "script.h"
00044
00045
00046
00047
00048
00052 static std::vector<CConstruction *> Constructions;
00053
00054
00055
00056
00057
00061 void InitConstructions(void)
00062 {
00063 }
00064
00068 void LoadConstructions(void)
00069 {
00070 const char *file;
00071 std::vector<CConstruction *>::iterator i;
00072
00073 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
00074 if ((*i)->Ident.empty()) {
00075 continue;
00076 }
00077 file = (*i)->File.File.c_str();
00078 (*i)->Width = (*i)->File.Width;
00079 (*i)->Height = (*i)->File.Height;
00080 if (file && *file) {
00081 ShowLoadProgress("Construction %s", file);
00082 (*i)->Sprite = CPlayerColorGraphic::New(file, (*i)->Width, (*i)->Height);
00083 (*i)->Sprite->Load();
00084 }
00085 file = (*i)->ShadowFile.File.c_str();
00086 (*i)->ShadowWidth = (*i)->ShadowFile.Width;
00087 (*i)->ShadowHeight = (*i)->ShadowFile.Height;
00088 if (file && *file) {
00089 ShowLoadProgress("Construction %s", file);
00090 (*i)->ShadowSprite = CGraphic::ForceNew(file,
00091 (*i)->ShadowWidth, (*i)->ShadowHeight);
00092 (*i)->ShadowSprite->Load();
00093 (*i)->ShadowSprite->MakeShadow();
00094 }
00095 }
00096 }
00097
00101 void CleanConstructions(void)
00102 {
00103 CConstructionFrame *cframe;
00104 CConstructionFrame *tmp;
00105 std::vector<CConstruction *>::iterator i;
00106
00107
00108
00109
00110 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
00111 CGraphic::Free((*i)->Sprite);
00112 CGraphic::Free((*i)->ShadowSprite);
00113 cframe = (*i)->Frames;
00114 while (cframe) {
00115 tmp = cframe->Next;
00116 delete cframe;
00117 cframe = tmp;
00118 }
00119 delete *i;
00120 }
00121 Constructions.clear();
00122 }
00123
00131 CConstruction *ConstructionByIdent(const std::string &ident)
00132 {
00133 std::vector<CConstruction *>::iterator i;
00134
00135 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
00136 if (ident == (*i)->Ident) {
00137 return *i;
00138 }
00139 }
00140 DebugPrint("Construction `%s' not found.\n" _C_ ident.c_str());
00141 return NULL;
00142 }
00143
00144
00145
00153 static int CclDefineConstruction(lua_State *l)
00154 {
00155 const char *value;
00156 std::string str;
00157 CConstruction *construction;
00158 std::vector<CConstruction *>::iterator i;
00159 int subargs;
00160 int k;
00161
00162 LuaCheckArgs(l, 2);
00163 if (!lua_istable(l, 2)) {
00164 LuaError(l, "incorrect argument");
00165 }
00166
00167
00168
00169 str = LuaToString(l, 1);
00170
00171 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
00172 if ((*i)->Ident == str) {
00173
00174 construction = *i;
00175 break;
00176 }
00177 }
00178 if (i == Constructions.end()) {
00179 construction = new CConstruction;
00180 Constructions.push_back(construction);
00181 }
00182 construction->Ident = str;
00183
00184
00185
00186
00187 lua_pushnil(l);
00188 while (lua_next(l, 2)) {
00189 int files;
00190
00191 value = LuaToString(l, -2);
00192
00193 if ((files = !strcmp(value, "Files")) ||
00194 !strcmp(value, "ShadowFiles")) {
00195 const char *file = NULL;
00196 int w = 0;
00197 int h = 0;
00198
00199 if (!lua_istable(l, -1)) {
00200 LuaError(l, "incorrect argument");
00201 }
00202 lua_pushnil(l);
00203 while (lua_next(l, -2)) {
00204 value = LuaToString(l, -2);
00205
00206 if (!strcmp(value, "File")) {
00207 file = LuaToString(l, -1);
00208 } else if (!strcmp(value, "Size")) {
00209 if (!lua_istable(l, -1) || lua_objlen(l, -1) != 2) {
00210 LuaError(l, "incorrect argument");
00211 }
00212 lua_rawgeti(l, -1, 1);
00213 w = LuaToNumber(l, -1);
00214 lua_pop(l, 1);
00215 lua_rawgeti(l, -1, 2);
00216 h = LuaToNumber(l, -1);
00217 lua_pop(l, 1);
00218 } else {
00219 LuaError(l, "Unsupported tag: %s" _C_ value);
00220 }
00221 lua_pop(l, 1);
00222 }
00223 if (files) {
00224 construction->File.File = file;
00225 construction->File.Width = w;
00226 construction->File.Height = h;
00227 } else {
00228 construction->ShadowFile.File = file;
00229 construction->ShadowFile.Width = w;
00230 construction->ShadowFile.Height = h;
00231 }
00232 } else if (!strcmp(value, "Constructions")) {
00233 subargs = lua_objlen(l, -1);
00234 for (k = 0; k < subargs; ++k) {
00235 int percent;
00236 ConstructionFileType file;
00237 int frame;
00238 CConstructionFrame **cframe;
00239
00240 percent = 0;
00241 file = ConstructionFileConstruction;
00242 frame = 0;
00243
00244 lua_rawgeti(l, -1, k + 1);
00245 if (!lua_istable(l, -1)) {
00246 LuaError(l, "incorrect argument");
00247 }
00248 lua_pushnil(l);
00249 while (lua_next(l, -2)) {
00250 value = LuaToString(l, -2);
00251
00252 if (!strcmp(value, "Percent")) {
00253 percent = LuaToNumber(l, -1);
00254 } else if (!strcmp(value, "File")) {
00255 value = LuaToString(l, -1);
00256 if (!strcmp(value, "construction")) {
00257 file = ConstructionFileConstruction;
00258 } else if (!strcmp(value, "main")) {
00259 file = ConstructionFileMain;
00260 } else {
00261 LuaError(l, "Unsupported tag: %s" _C_ value);
00262 }
00263 } else if (!strcmp(value, "Frame")) {
00264 frame = LuaToNumber(l, -1);
00265 } else {
00266 LuaError(l, "Unsupported tag: %s" _C_ value);
00267 }
00268 lua_pop(l, 1);
00269 }
00270 lua_pop(l, 1);
00271 cframe = &construction->Frames;
00272 while (*cframe) {
00273 cframe = &((*cframe)->Next);
00274 }
00275 (*cframe) = new CConstructionFrame;
00276 (*cframe)->Percent = percent;
00277 (*cframe)->File = file;
00278 (*cframe)->Frame = frame;
00279 (*cframe)->Next = NULL;
00280 }
00281 } else {
00282 LuaError(l, "Unsupported tag: %s" _C_ value);
00283 }
00284 lua_pop(l, 1);
00285 }
00286
00287 return 0;
00288 }
00289
00290
00291
00295 void ConstructionCclRegister(void)
00296 {
00297 lua_register(Lua, "DefineConstruction", CclDefineConstruction);
00298 }
00299