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 <string.h>
00036 #include <stdio.h>
00037
00038 #include "stratagus.h"
00039
00040 #include <stdlib.h>
00041
00042 #include "script.h"
00043 #include "sound.h"
00044 #include "sound_server.h"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00061 static int CclSoundForName(lua_State *l)
00062 {
00063 CSound *id;
00064 const char *sound_name;
00065 LuaUserData *data;
00066
00067 sound_name = LuaToString(l, -1);
00068 id = SoundForName(sound_name);
00069
00070 data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
00071 data->Type = LuaSoundType;
00072 data->Data = id;
00073 return 1;
00074 }
00075
00083 static CSound *CclGetSound(lua_State *l)
00084 {
00085 LuaUserData *data;
00086 int pop;
00087
00088 pop = 0;
00089 if (lua_isstring(l, -1)) {
00090 CclSoundForName(l);
00091 pop = 1;
00092 }
00093 if (lua_isuserdata(l, -1)) {
00094 data = (LuaUserData *)lua_touserdata(l, -1);
00095 if (data->Type == LuaSoundType) {
00096 if (pop) {
00097 lua_pop(l, 1);
00098 }
00099 return (CSound *)data->Data;
00100 }
00101 }
00102 LuaError(l, "CclGetSound: not a sound");
00103 return NULL;
00104 }
00105
00117 static int CclMakeSound(lua_State *l)
00118 {
00119 CSound *id;
00120 std::string c_name;
00121 const char *c_file;
00122 char **c_files;
00123 int args;
00124 int j;
00125 LuaUserData *data;
00126
00127 LuaCheckArgs(l, 2);
00128
00129 c_name = LuaToString(l, 1);
00130 if (lua_isstring(l, 2)) {
00131
00132 c_file = LuaToString(l, 2);
00133 id = MakeSound(c_name, &c_file, 1);
00134 } else if (lua_istable(l, 2)) {
00135
00136 args = lua_objlen(l, 2);
00137 c_files = new char *[args];
00138 for (j = 0; j < args; ++j) {
00139 lua_rawgeti(l, 2, j + 1);
00140 c_files[j] = new_strdup(LuaToString(l, -1));
00141 lua_pop(l, 1);
00142 }
00143 id = MakeSound(c_name, (const char **)c_files, args);
00144 for (j = 0; j < args; ++j) {
00145 delete[] c_files[j];
00146 }
00147 delete[] c_files;
00148 } else {
00149 LuaError(l, "string or table expected");
00150 return 0;
00151 }
00152 data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
00153 data->Type = LuaSoundType;
00154 data->Data = id;
00155 return 1;
00156 }
00157
00166 static int CclMakeSoundGroup(lua_State *l)
00167 {
00168 CSound *id;
00169 std::string c_name;
00170 CSound *first;
00171 CSound *second;
00172 LuaUserData *data;
00173
00174 LuaCheckArgs(l, 3);
00175
00176 c_name = LuaToString(l, 1);
00177
00178 lua_pushvalue(l, 2);
00179 first = CclGetSound(l);
00180 lua_pop(l, 1);
00181 second = CclGetSound(l);
00182 id = MakeSoundGroup(c_name, first, second);
00183 data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
00184 data->Type = LuaSoundType;
00185 data->Data = id;
00186 return 1;
00187 }
00188
00197 static int CclMapSound(lua_State *l)
00198 {
00199 const char *sound_name;
00200
00201 LuaCheckArgs(l, 2);
00202 sound_name = LuaToString(l, 1);
00203 MapSound(sound_name, CclGetSound(l));
00204 lua_pushvalue(l, 2);
00205 return 1;
00206 }
00207
00213 static int CclPlaySound(lua_State *l)
00214 {
00215 CSound *id;
00216
00217 LuaCheckArgs(l, 1);
00218
00219 id = CclGetSound(l);
00220 PlayGameSound(id, MaxSampleVolume);
00221 return 0;
00222 }
00223
00230 static int CclDefineGameSounds(lua_State *l)
00231 {
00232 const char *value;
00233 int args;
00234 int j;
00235 LuaUserData *data;
00236
00237 args = lua_gettop(l);
00238 data = NULL;
00239 for (j = 0; j < args; ++j) {
00240 value = LuaToString(l, j + 1);
00241 ++j;
00242
00243
00244 if (!strcmp(value, "click")) {
00245 if (!lua_isuserdata(l, j + 1) ||
00246 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
00247 LuaError(l, "Sound id expected");
00248 }
00249 GameSounds.Click.Sound = (CSound *)data->Data;
00250 } else if (!strcmp(value, "placement-error")) {
00251 if (!lua_isuserdata(l, j + 1) ||
00252 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
00253 LuaError(l, "Sound id expected");
00254 }
00255 GameSounds.PlacementError.Sound = (CSound *)data->Data;
00256 } else if (!strcmp(value, "placement-success")) {
00257 if (!lua_isuserdata(l, j + 1) ||
00258 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
00259 LuaError(l, "Sound id expected");
00260 }
00261 GameSounds.PlacementSuccess.Sound = (CSound *)data->Data;
00262 } else if (!strcmp(value, "work-complete")) {
00263 if (!lua_isuserdata(l, j + 1) ||
00264 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
00265 LuaError(l, "Sound id expected");
00266 }
00267 GameSounds.WorkComplete.Sound = (CSound *)data->Data;
00268 } else if (!strcmp(value, "rescue")) {
00269 if (!lua_isuserdata(l, j + 1) ||
00270 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
00271 LuaError(l, "Sound id expected");
00272 }
00273 GameSounds.Rescue.Sound = (CSound *)data->Data;
00274 } else if (!strcmp(value, "chat-message")) {
00275 if (!lua_isuserdata(l, j + 1) ||
00276 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
00277 LuaError(l, "Sound id expected");
00278 }
00279 GameSounds.ChatMessage.Sound = (CSound *)data->Data;
00280 } else {
00281 LuaError(l, "Unsupported tag: %s" _C_ value);
00282 }
00283 }
00284 return 0;
00285 }
00286
00292 static int CclSetGlobalSoundRange(lua_State *l)
00293 {
00294 int d;
00295
00296 LuaCheckArgs(l, 1);
00297
00298 d = LuaToNumber(l, 1);
00299 if (d > 0) {
00300 DistanceSilent = d;
00301 }
00302 return 0;
00303 }
00304
00310 static int CclSetSoundRange(lua_State *l) {
00311 unsigned char theRange;
00312 int tmp;
00313 CSound *id;
00314
00315 LuaCheckArgs(l, 2);
00316
00317 tmp = LuaToNumber(l, 2);
00318 if (tmp < 0) {
00319 theRange = 0;
00320 } else if (tmp > 255) {
00321 theRange = 255;
00322 } else {
00323 theRange = (unsigned char)tmp;
00324 }
00325 lua_pushvalue(l, 1);
00326 id = CclGetSound(l);
00327 SetSoundRange(id, theRange);
00328 return 1;
00329 }
00330
00334 void SoundCclRegister(void)
00335 {
00336 lua_register(Lua, "SetGlobalSoundRange", CclSetGlobalSoundRange);
00337 lua_register(Lua, "DefineGameSounds", CclDefineGameSounds);
00338 lua_register(Lua, "MapSound", CclMapSound);
00339 lua_register(Lua, "SoundForName", CclSoundForName);
00340 lua_register(Lua, "SetSoundRange", CclSetSoundRange);
00341 lua_register(Lua, "MakeSound", CclMakeSound);
00342 lua_register(Lua, "MakeSoundGroup", CclMakeSoundGroup);
00343 lua_register(Lua, "PlaySound", CclPlaySound);
00344 }
00345