1 /*
2  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 #include "dialog-script.h"
10 
11 class Actor;
12 
13 #include "Log.h"
14 #include "Path.h"
15 #include "FishDialog.h"
16 #include "DialogStack.h"
17 #include "SubTitleAgent.h"
18 #include "SoundAgent.h"
19 #include "Planner.h"
20 #include "Color.h"
21 
22 #include "def-script.h"
23 
24 //-----------------------------------------------------------------
25     inline Planner *
getPlanner(lua_State * L)26 getPlanner(lua_State *L)
27 {
28     return dynamic_cast<Planner*>(script_getLeader(L));
29 }
30 //-----------------------------------------------------------------
31     inline DialogStack *
getDialogs(lua_State * L)32 getDialogs(lua_State *L)
33 {
34     return getPlanner(L)->dialogs();
35 }
36 
37 //-----------------------------------------------------------------
38 /**
39  * void game_planAction(func)
40  */
41     int
script_game_planAction(lua_State * L)42 script_game_planAction(lua_State *L) throw()
43 {
44     BEGIN_NOEXCEPTION;
45     luaL_checktype(L, 1, LUA_TFUNCTION);
46     int funcRef = luaL_ref(L, LUA_REGISTRYINDEX);
47 
48     getPlanner(L)->planAction(funcRef);
49     END_NOEXCEPTION;
50     return 0;
51 }
52 //-----------------------------------------------------------------
53 /**
54  * bool game_isPlanning()
55  */
56     int
script_game_isPlanning(lua_State * L)57 script_game_isPlanning(lua_State *L) throw()
58 {
59     BEGIN_NOEXCEPTION;
60     bool planning = getPlanner(L)->isPlanning();
61     lua_pushboolean(L, planning);
62     END_NOEXCEPTION;
63     //NOTE: return planning
64     return 1;
65 }
66 //-----------------------------------------------------------------
67 /**
68  * bool game_killPlan()
69  */
70     int
script_game_killPlan(lua_State * L)71 script_game_killPlan(lua_State *L) throw()
72 {
73     BEGIN_NOEXCEPTION;
74     getPlanner(L)->interruptPlan();
75     END_NOEXCEPTION;
76     return 0;
77 }
78 
79 //-----------------------------------------------------------------
80 /**
81  * bool dialog_isDialog()
82  */
83     int
script_dialog_isDialog(lua_State * L)84 script_dialog_isDialog(lua_State *L) throw()
85 {
86     BEGIN_NOEXCEPTION;
87     bool isDialog = getDialogs(L)->isDialog();
88     lua_pushboolean(L, isDialog);
89     END_NOEXCEPTION;
90     //NOTE: return isDialog
91     return 1;
92 }
93 //-----------------------------------------------------------------
94 /**
95  * void dialog_addFont(fontname, red, green, blue)
96  */
97     int
script_dialog_addFont(lua_State * L)98 script_dialog_addFont(lua_State *L) throw()
99 {
100     BEGIN_NOEXCEPTION;
101     const char *name = luaL_checkstring(L, 1);
102     int red = luaL_checkint(L, 2);
103     int green = luaL_checkint(L, 3);
104     int blue = luaL_checkint(L, 4);
105 
106     SubTitleAgent::agent()->addFont(name, new Color(red, green, blue));
107 
108     END_NOEXCEPTION;
109     return 0;
110 }
111 //-----------------------------------------------------------------
112 /**
113  * void dialog_addDialog(name, lang, soundfile, fontname="", subtitle="")
114  */
115     int
script_dialog_addDialog(lua_State * L)116 script_dialog_addDialog(lua_State *L) throw()
117 {
118     BEGIN_NOEXCEPTION;
119     const char *name = luaL_checkstring(L, 1);
120     const char *lang = luaL_checkstring(L, 2);
121     const char *soundfile = luaL_checkstring(L, 3);
122     const char *fontname = luaL_optstring(L, 4, "");
123     const char *subtitle = luaL_optstring(L, 5, "");
124 
125     FishDialog *dialog =
126         new FishDialog(lang, soundfile, subtitle, fontname);
127     getDialogs(L)->addDialog(name, dialog);
128 
129     END_NOEXCEPTION;
130     return 0;
131 }
132 //-----------------------------------------------------------------
133 /**
134  * bool model_isTalking(model_index)
135  */
136     int
script_model_isTalking(lua_State * L)137 script_model_isTalking(lua_State *L) throw()
138 {
139     BEGIN_NOEXCEPTION;
140     int model_index = luaL_checkint(L, 1);
141 
142     bool talking = getDialogs(L)->isTalking(model_index);
143     lua_pushboolean(L, talking);
144     END_NOEXCEPTION;
145     //NOTE: return talking
146     return 1;
147 }
148 //-----------------------------------------------------------------
149 /**
150  * void model_talk(model_index, name, volume, loops=0, dialogFlag=false)
151  */
152     int
script_model_talk(lua_State * L)153 script_model_talk(lua_State *L) throw()
154 {
155     BEGIN_NOEXCEPTION;
156     int model_index = luaL_checkint(L, 1);
157     const char *name = luaL_checkstring(L, 2);
158     int volume = luaL_optint(L, 3, 75);
159     int loops = luaL_optint(L, 4, 0);
160     bool dialogFlag = lua_toboolean(L, 5);
161 
162     getDialogs(L)->actorTalk(model_index, name, volume, loops, dialogFlag);
163     END_NOEXCEPTION;
164     return 0;
165 }
166 //-----------------------------------------------------------------
167 /**
168  * void model_killSound(model_index)
169  */
170     int
script_model_killSound(lua_State * L)171 script_model_killSound(lua_State *L) throw()
172 {
173     BEGIN_NOEXCEPTION;
174     int model_index = luaL_checkint(L, 1);
175 
176     getDialogs(L)->killSound(model_index);
177     END_NOEXCEPTION;
178     return 0;
179 }
180 
181 //-----------------------------------------------------------------
182 /**
183  * void sound_playMusic(music_name)
184  */
185     int
script_sound_playMusic(lua_State * L)186 script_sound_playMusic(lua_State *L) throw()
187 {
188     BEGIN_NOEXCEPTION;
189     const char *music_name = luaL_checkstring(L, 1);
190 
191     SoundAgent::agent()->playMusic(Path::dataReadPath(music_name), NULL);
192     END_NOEXCEPTION;
193     return 0;
194 }
195 //-----------------------------------------------------------------
196 /**
197  * void sound_stopMusic()
198  */
199     int
script_sound_stopMusic(lua_State * L)200 script_sound_stopMusic(lua_State *L) throw()
201 {
202     BEGIN_NOEXCEPTION;
203     SoundAgent::agent()->stopMusic();
204     END_NOEXCEPTION;
205     return 0;
206 }
207 
208