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 "LevelScript.h"
10 
11 #include "V2.h"
12 #include "Room.h"
13 #include "ScriptState.h"
14 
15 #include "ScriptCmd.h"
16 #include "LogicException.h"
17 #include "Cube.h"
18 #include "Unit.h"
19 
20 #include "game-script.h"
21 #include "level-script.h"
22 
23 #include <memory> // for auto_ptr
24 
25 //-----------------------------------------------------------------
26 /**
27  * Create new plan holder.
28  */
LevelScript(Level * aLevel)29 LevelScript::LevelScript(Level *aLevel)
30 {
31     m_level = aLevel;
32     registerGameFuncs();
33 }
34 //-----------------------------------------------------------------
35 /**
36  * Create ScriptCmd for show.
37  * Command will be executed in this script context.
38  * @param funcRef index of lua function
39  * @return new command
40  */
41 Command *
createCommand(int funcRef)42 LevelScript::createCommand(int funcRef)
43 {
44     return new ScriptCmd(m_script, funcRef);
45 }
46 
47 //-----------------------------------------------------------------
48 /**
49  * Update level (plan dialogs, do anim, ...).
50  */
51     void
updateScript()52 LevelScript::updateScript()
53 {
54     m_script->doString("script_update()");
55     satisfyPlan();
56 }
57 //-----------------------------------------------------------------
58 void
interruptPlan()59 LevelScript::interruptPlan()
60 {
61     Planner::interruptPlan();
62     //NOTE: checkActive is before unBusyUnits to allow script
63     // make busy unwanted fishes
64     room()->checkActive();
65     room()->unBusyUnits();
66 }
67 //-----------------------------------------------------------------
68 /**
69  * Add model at scene.
70  * @param new_model new object
71  * @param new_unit driver for the object or NULL
72  * @return model index
73  * @throws LogicException when room is not created yet
74  */
75     int
addModel(Cube * new_model,Unit * new_unit)76 LevelScript::addModel(Cube *new_model, Unit *new_unit)
77 {
78     std::auto_ptr<Cube> ptr_model(new_model);
79     std::auto_ptr<Unit> ptr_unit(new_unit);
80 
81     ptr_model->takeDialogs(dialogs());
82     return room()->addModel(ptr_model.release(), ptr_unit.release());
83 }
84 //-----------------------------------------------------------------
85     Cube *
getModel(int model_index)86 LevelScript::getModel(int model_index)
87 {
88     return room()->getModel(model_index);
89 }
90 //-----------------------------------------------------------------
91 /**
92  * Returns model at location.
93  */
94     Cube *
askField(const V2 & loc)95 LevelScript::askField(const V2 &loc)
96 {
97     return room()->askField(loc);
98 }
99 
100 //-----------------------------------------------------------------
101     void
addSound(const std::string & name,const Path & file)102 LevelScript::addSound(const std::string &name, const Path &file)
103 {
104     room()->addSound(name, file);
105 }
106 //-----------------------------------------------------------------
107     void
playSound(const std::string & name,int volume)108 LevelScript::playSound(const std::string &name, int volume)
109 {
110     room()->playSound(name, volume);
111 }
112 //-----------------------------------------------------------------
113 /**
114  * Register functions usable from script.
115  */
116     void
registerGameFuncs()117 LevelScript::registerGameFuncs()
118 {
119     m_script->registerFunc("game_setRoomWaves", script_game_setRoomWaves);
120     m_script->registerFunc("game_addModel", script_game_addModel);
121     m_script->registerFunc("game_getCycles", script_game_getCycles);
122     m_script->registerFunc("game_addDecor", script_game_addDecor);
123     m_script->registerFunc("game_setScreenShift", script_game_setScreenShift);
124     m_script->registerFunc("game_changeBg", script_game_changeBg);
125     m_script->registerFunc("game_getBg", script_game_getBg);
126     m_script->registerFunc("game_checkActive", script_game_checkActive);
127     m_script->registerFunc("game_setFastFalling", script_game_setFastFalling);
128 
129     m_script->registerFunc("model_addAnim", script_model_addAnim);
130     m_script->registerFunc("model_runAnim", script_model_runAnim);
131     m_script->registerFunc("model_setAnim", script_model_setAnim);
132     m_script->registerFunc("model_useSpecialAnim", script_model_useSpecialAnim);
133     m_script->registerFunc("model_countAnims", script_model_countAnims);
134     m_script->registerFunc("model_setEffect", script_model_setEffect);
135     m_script->registerFunc("model_getLoc", script_model_getLoc);
136     m_script->registerFunc("model_getAction", script_model_getAction);
137     m_script->registerFunc("model_getState", script_model_getState);
138     m_script->registerFunc("model_getDir", script_model_getDir);
139     m_script->registerFunc("model_getTouchDir", script_model_getTouchDir);
140     m_script->registerFunc("model_isAlive", script_model_isAlive);
141     m_script->registerFunc("model_isOut", script_model_isOut);
142     m_script->registerFunc("model_isLeft", script_model_isLeft);
143     m_script->registerFunc("model_isAtBorder", script_model_isAtBorder);
144     m_script->registerFunc("model_getW", script_model_getW);
145     m_script->registerFunc("model_getH", script_model_getH);
146     m_script->registerFunc("model_setGoal", script_model_setGoal);
147     m_script->registerFunc("model_change_turnSide",
148             script_model_change_turnSide);
149     m_script->registerFunc("model_change_setLocation",
150             script_model_change_setLocation);
151     m_script->registerFunc("model_setViewShift",
152             script_model_setViewShift);
153     m_script->registerFunc("model_getViewShift",
154             script_model_getViewShift);
155     m_script->registerFunc("model_setBusy", script_model_setBusy);
156     m_script->registerFunc("model_getExtraParams", script_model_getExtraParams);
157     m_script->registerFunc("model_change_setExtraParams",
158             script_model_change_setExtraParams);
159     m_script->registerFunc("model_equals", script_model_equals);
160 
161     m_script->registerFunc("sound_addSound", script_sound_addSound);
162     m_script->registerFunc("sound_playSound", script_sound_playSound);
163 
164     registerLevelFuncs();
165 }
166 //-----------------------------------------------------------------
167 void
registerLevelFuncs()168 LevelScript::registerLevelFuncs()
169 {
170     m_script->registerFunc("level_save", script_level_save);
171     m_script->registerFunc("level_load", script_level_load);
172 
173     m_script->registerFunc("level_action_move", script_level_action_move);
174     m_script->registerFunc("level_action_save", script_level_action_save);
175     m_script->registerFunc("level_action_load", script_level_action_load);
176     m_script->registerFunc("level_action_restart", script_level_action_restart);
177 
178     m_script->registerFunc("level_createRoom", script_level_createRoom);
179     m_script->registerFunc("level_getRestartCounter",
180             script_level_getRestartCounter);
181     m_script->registerFunc("level_getDepth", script_level_getDepth);
182     m_script->registerFunc("level_isNewRound", script_level_isNewRound);
183     m_script->registerFunc("level_isSolved", script_level_isSolved);
184     m_script->registerFunc("level_newDemo", script_level_newDemo);
185     m_script->registerFunc("level_planShow", script_level_planShow);
186     m_script->registerFunc("level_isShowing", script_level_isShowing);
187 }
188 
189