1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef NUVIE_SCRIPT_SCRIPT_H
24 #define NUVIE_SCRIPT_SCRIPT_H
25 
26 #include "common/lua/lua.h"
27 
28 #include "ultima/shared/std/string.h"
29 #include "ultima/shared/std/containers.h"
30 #include "ultima/nuvie/gui/gui.h"
31 #include "ultima/nuvie/misc/u6_misc.h"
32 #include "ultima/nuvie/usecode/usecode.h"
33 
34 namespace Ultima {
35 namespace Nuvie {
36 
37 class Configuration;
38 class MapCoord;
39 class Spell;
40 class Obj;
41 class Actor;
42 class NuvieIO;
43 class SoundManager;
44 
45 #define NUVIE_SCRIPT_NOT_STARTED     255
46 #define NUVIE_SCRIPT_ERROR             0
47 #define NUVIE_SCRIPT_FINISHED          1
48 #define NUVIE_SCRIPT_GET_TARGET        2
49 #define NUVIE_SCRIPT_GET_DIRECTION     3
50 #define NUVIE_SCRIPT_GET_INV_OBJ       4
51 #define NUVIE_SCRIPT_ADVANCE_GAME_TIME 5
52 #define NUVIE_SCRIPT_ADVANCE_REAL_TIME 6
53 #define NUVIE_SCRIPT_TALK_TO_ACTOR     7
54 #define NUVIE_SCRIPT_GET_SPELL         8
55 #define NUVIE_SCRIPT_GET_OBJ           9
56 #define NUVIE_SCRIPT_GET_PLAYER_OBJ   10 //can get an object immediately surrounding the player or from their inventory.
57 
58 #define NUVIE_SCRIPT_CB_ADV_GAME_TIME  1
59 
60 class ScriptThread {
61 	lua_State *L;
62 	int start_nargs;
63 	uint32 data;
64 	uint8 state;
65 
66 public:
67 
ScriptThread(lua_State * l,int nargs)68 	ScriptThread(lua_State *l, int nargs) {
69 		L = l;
70 		start_nargs = nargs;
71 		data = 0;
72 		state = NUVIE_SCRIPT_NOT_STARTED;
73 	}
~ScriptThread()74 	~ScriptThread() {  }
get_data()75 	uint32 get_data() {
76 		return data;
77 	}
start()78 	uint8 start() {
79 		return resume(start_nargs);
80 	}
81 	uint8 resume_with_location(MapCoord loc);
82 	uint8 resume_with_direction(uint8 dir);
83 	uint8 resume_with_spell_num(uint8 spell_num);
84 	uint8 resume_with_obj(Obj *obj);
85 	uint8 resume_with_nil();
86 	uint8 resume(int narg = 0);
finished()87 	bool finished() {
88 		return lua_status(L) != LUA_YIELD ? true : false;
89 	}
get_error()90 	int get_error() {
91 		return lua_status(L);
92 	}
93 
is_running()94 	bool is_running() {
95 		return !finished();
96 	}
get_state()97 	uint8 get_state() {
98 		return state;
99 	}
100 };
101 
102 #define SCRIPT_DISPLAY_HIT_MSG true
103 class Script {
104 	static Script *script;
105 	Configuration *config;
106 	nuvie_game_t gametype; // what game is being played?
107 	SoundManager *soundManager;
108 	lua_State *L;
109 
110 public:
111 
112 	Script(Configuration *cfg, GUI *gui, SoundManager *sm, nuvie_game_t type);
113 	~Script();
114 
115 	bool init();
116 
117 	/* Return instance of self */
get_script()118 	static Script *get_script()           {
119 		return (script);
120 	}
get_config()121 	Configuration *get_config() {
122 		return (config);
123 	}
get_sound_manager()124 	SoundManager *get_sound_manager() {
125 		return soundManager;
126 	}
127 
128 	bool run_script(const char *script);
129 	bool call_load_game(NuvieIO *objlist);
130 	bool call_save_game(NuvieIO *objlist);
131 
132 	bool play_cutscene(const char *script_file);
133 	MovementStatus call_player_before_move_action(sint16 *rel_x, sint16 *rel_y);
134 	bool call_player_post_move_action(bool didMove);
135 	bool call_player_pass();
136 	bool call_actor_update_all();
137 	bool call_actor_init(Actor *actor, uint8 alignment);
138 	bool call_actor_attack(Actor *actor, MapCoord location, Obj *weapon, Actor *foe);
139 	bool call_actor_map_dmg(Actor *actor, MapCoord location);
140 	bool call_actor_tile_dmg(Actor *actor, uint16 tile_num);
141 	bool call_actor_hit(Actor *actor, uint8 dmg, bool display_hit_msg = false);
142 	uint8 call_actor_str_adj(Actor *actor);
143 	uint8 call_actor_dex_adj(Actor *actor);
144 	uint8 call_actor_int_adj(Actor *actor);
145 	bool call_look_obj(Obj *obj);
146 	int call_obj_get_readiable_location(Obj *obj);
147 	uint8 actor_get_max_magic_points(Actor *actor);
148 	bool call_actor_get_obj(Actor *actor, Obj *obj, Obj *container = NULL);
149 	bool call_actor_subtract_movement_points(Actor *actor, uint8 points);
150 	bool call_actor_resurrect(Actor *actor);
151 	bool call_use_keg(Obj *obj); //we need this until we move all usecode into script.
152 	bool call_has_usecode(Obj *obj, UseCodeEvent usecode_type);
153 	ScriptThread *call_use_obj(Obj *obj, Actor *actor);
154 	bool call_ready_obj(Obj *obj, Actor *actor);
155 	bool call_move_obj(Obj *obj, sint16 rel_x, sint16 rel_y);
156 	bool call_handle_alt_code(uint16 altcode);
157 
158 	bool call_magic_get_spell_list(Spell **spell_list);
159 	bool call_actor_use_effect(Obj *effect_obj, Actor *actor);
160 	bool call_function(const char *func_name, int num_args, int num_return, bool print_stacktrace = true);
161 	ScriptThread *call_function_in_thread(const char *function_name);
162 	bool run_lua_file(const char *filename);
163 	bool call_moonstone_set_loc(uint8 phase, MapCoord location); //this is a hack until we have 'use' moonstone in script.
164 	bool call_advance_time(uint16 minutes);
165 	bool call_can_get_obj_override(Obj *obj);
166 	bool call_out_of_ammo(Actor *attacker, Obj *weapon, bool print_message);
167 	bool call_is_avatar_dead();
168 	bool call_is_ranged_select(UseCodeType operation);
169 	bool call_set_g_show_stealing(bool stealing);
170 	uint8 call_get_combat_range(uint16 absx, uint16 absy);
171 	uint8 call_get_weapon_range(uint16 obj_n);
172 
173 	MapCoord call_moonstone_get_loc(uint8 phase);
174 	bool call_update_moongates(bool visible);
175 
176 	uint8 call_play_midgame_sequence(uint16 seq_num);
177 	bool call_talk_script(uint8 script_number);
178 	bool call_talk_to_obj(Obj *obj);
179 	bool call_talk_to_actor(Actor *actor);
180 	bool call_is_container_obj(uint16 obj_n);
181 	uint8 call_get_portrait_number(Actor *actor);
182 	bool call_player_attack();
183 
184 	uint16 call_get_tile_to_object_mapping(uint16 tile_n);
185 	bool call_is_tile_object(uint16 obj_n);
186 
187 	ScriptThread *new_thread(const char *scriptfile);
188 	ScriptThread *new_thread_from_string(const char *script);
189 
190 protected:
191 	bool call_loadsave_game(const char *function, NuvieIO *objlist);
192 	void seed_random();
193 };
194 
195 } // End of namespace Nuvie
196 } // End of namespace Ultima
197 
198 #endif
199