-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ml
315 lines (292 loc) · 11.6 KB
/
main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
(* Main - The Main File
################################################################################
# #
# oooooo oooooo oooo o8o A project by students EPITA #
# `888. `888. .8' `"' in infosup 2017 #
# `888. .8888. .8' oooo ooo. .oo. .oooo.o #
# `888 .8'`888. .8' `888 `888P"Y88b d88( "8 #
# Wins `888.8' `888.8' 888 888 888 `"Y88b. #
# is not `888' `888' 888 888 888 o. )88b #
# Scumm ! `8' `8' o888o o888o o888o 8""888P' #
# Programmed by #
# Philémon "Phil Gekni" Gardet [[email protected]] #
# Rafael "Yayg" Gozlan [[email protected]] #
# with help #
# Lorry "Bardaf" Guedj [[email protected]] #
# Alexandre "Stawayld" Starck [[email protected]] #
# #
################################################################################
# Wins is a "Point and Click" Game Motor written with OCaml #
# Copyright (C) 2013 Philémon Gardet [[email protected]] #
# Rafael Gozlan [[email protected]] #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
################################################################################
*)
open Wally
open Zak
open Tool
open Lua_api
let usage_msg = "Usage : wins [gameFolder]\n";;
(* Main function **************************************************************)
let main () =
let _ =
let w =
Jed.initW ();
Jed.getWindow ()
in
w#changeRoom (envString#get "firstRoom") (envString#get "firstNode") ()
in
Woop.bigLoop();
exit 0
;;
(* Registration function Lua **************************************************)
let registerStaticFuncLua () =
(** print_debug (string) : print a debug message in the console **)
let printdebug state =
match (Lua.tostring state 1) with
| Some s -> print_debug ("Script : "^s)
| _ -> print_debug "script <"
and getenvironementvar state =
let value = match (Lua.tostring state 1) with
| Some name -> getEnvString name
| _ -> "Invalid call in lua at get_environement_var"
in Lua.pushstring state value
and setglobalint state =
match (Lua.tostring state 1),(Lua.tointeger state 2) with
| Some name,value -> setGlobalInt name value
| _ -> failwith "Invalid call in lua at set_global_int"
and getglobalint state =
let value = match (Lua.tostring state 1) with
| Some name ->
(try getGlobalInt name
with _ -> 0)
| _ -> failwith "Invalid call in lua at get_global_int"
in Lua.pushinteger state value
and removeglobalint state =
match (Lua.tostring state 1) with
| Some name -> removeGlobalInt name
| _ -> failwith "Invalid call in lua at remove_global_int"
and setglobalstring state =
match (Lua.tostring state 1),(Lua.tostring state 2) with
| Some name,Some value -> setGlobalString name value
| _ -> failwith "Invalid call in lua at set_global_string"
and getglobalstring state =
let value = match (Lua.tostring state 1) with
| Some name ->
(try getGlobalString name
with _ -> "")
| _ -> failwith "Invalid call in lua at get_global_string"
in Lua.pushstring state value
and removeglobalstring state =
match (Lua.tostring state 1) with
| Some name -> removeGlobalString name
| _ -> failwith "Invalid call in lua at remove_global_string"
in
registerStaticFunction "print_debug" printdebug;
registerStaticFunction "get_environement_var" getenvironementvar ~output:1;
registerStaticFunction "set_global_int" setglobalint;
registerStaticFunction "get_global_int" getglobalint ~output:1;
registerStaticFunction "remove_global_int" removeglobalint;
registerStaticFunction "set_global_string" setglobalstring;
registerStaticFunction "get_global_string" getglobalstring ~output:1;
registerStaticFunction "remove_global_string" removeglobalstring;
;;
let registerDynamicFuncLua () =
let changeroom state =
match (Lua.tostring state 1),(Lua.tostring state 2) with
| Some name,Some node ->
let w = Jed.getWindow () in
w#changeRoom name node ()
| _ -> failwith "Invalid call in lua at change_room"
and giveitem state =
match (Lua.tostring state 1) with
| Some name ->
if not(invCheckItem name) then
invAddItem name
| _ -> failwith "Invalid call in lua at give_item"
and dropitem state =
match (Lua.tostring state 1) with
| Some name ->
if invCheckItem name then
invDropItem name
| _ -> failwith "Invalid call in lua at drop_item"
and addcharacter state =
match (Lua.tostring state 1),(Lua.tostring state 2),
(Lua.tointeger state 3),(Lua.tointeger state 4)
with
| (Some name,Some node,ox,oy) ->
let w = Jed.getWindow () in
w#addCharacterToDisplay name node;
begin if (ox <> 0 || oy <> 0) then
w#placeOffsetNode name node (ox,oy)
end
| _ -> failwith "Invalid call in lua at add_character"
and placeitem state =
match (Lua.tostring state 1),(Lua.tointeger state 2),(Lua.tointeger state 3)
with
| (Some name,x,y) ->
let w = Jed.getWindow () in
w#addItemToDisplay name (x,y)
| _ -> failwith "Invalid call in lua at place_item"
and setanimation state =
match (Lua.tostring state 1),(Lua.tostring state 2) with
| (Some obj,Some anim) ->
let w = Jed.getWindow () in
w#setAnimation obj anim
| _ -> failwith "Invalid call in lua at set_animation"
and removeobject state =
match (Lua.tostring state 1) with
| Some name ->
let w = Jed.getWindow () in
w#removeDisplayElement name
| _ -> failwith "Invalid call in lua at remove_object"
and setdialog state =
match (Lua.tostring state 1) with
| (Some name) ->
let w = Jed.getWindow () in
w#setDialog name
| _ -> failwith "Invalid call in lua at set_dialog"
in
registerDynamicFunction "change_room" changeroom;
registerDynamicFunction "give_item" giveitem;
registerDynamicFunction "drop_item" dropitem;
registerDynamicFunction "add_character" addcharacter;
registerDynamicFunction "place_item" placeitem;
registerDynamicFunction "set_animation" setanimation;
registerDynamicFunction "remove_object" removeobject;
registerDynamicFunction "set_dialog" setdialog
;;
let initLua () =
let _ = registerStaticFuncLua () in
let _ = loadGlobalScripts (envString#get "scriptDir") in
registerDynamicFuncLua ();
print_string "┝┅ Scripts loaded.\n";
;;
(* Setup **********************************************************************)
let setup execDir =
(* motd *)
print_string "╒═════════════════════════════╕\n";
print_string "╎ Wins Version 233rd ╎\n";
print_string "╎ The Point and Click Motor ╎\n";
print_string "╞═════════════════════════════╛\n";
(* Open Xml gobal file *)
let xmlPath = execDir//"game.xml" in
let xmlGame =
if not (Sys.file_exists xmlPath) then
raise Not_found
else
(new treeXml xmlPath)#getFirstByName "game"
in
(* Setup Environement variables *)
let dimension = (xmlGame#getFirstByName "dimension")
in
envString#set "name" (xmlGame#getAttr "name");
envString#set "icon" (xmlGame#getAttr "icon");
envString#set "xScreen" (dimension#getAttr "x");
envString#set "yScreen" (dimension#getAttr "y");
envString#set "dir" execDir;
envString#set "itemDir" (execDir//(xmlGame#getFirstByName "itemDir")
#getAttr "href");
envString#set "characterDir" (execDir//(xmlGame#getFirstByName "characterDir")
#getAttr "href");
envString#set "roomDir" (execDir//(xmlGame#getFirstByName "roomDir")
#getAttr "href");
envString#set "scriptDir" (execDir//(xmlGame#getFirstByName "scriptDir")
#getAttr "href");
envString#set "fontDir" (execDir//(xmlGame#getFirstByName "fontDir")
#getAttr "href");
envString#set "firstRoom" ((xmlGame#getFirstByName "firstRoom")
#getAttr "name");
envString#set "firstNode" ((xmlGame#getFirstByName "firstRoom")
#getAttr "node");
envString#set "player" ((xmlGame#getFirstByName "player")
#getAttr "name");
print_string "┝┅ Runtime variables loaded.\n";
(* Setup game's objects *)
ignore(try
print_string "┝┅ Load game's objects...\n";
loadItems ();
loadCharacters ();
loadRooms ();
print_string "┝┅ Game's objects loaded.\n";
Jed.loadFonts (envString#get "fontDir");
initLua ();
with
| Failure e ->
print_string ("☢ Error during loading data game : "^e^" \n");
Printexc.print_backtrace stdout;
exit 2
| e ->
let str = Printexc.to_string e in
print_string ("☢ Error during loading data game of kind "^str^"\n");
Printexc.print_backtrace stdout;
exit 2
); print_string "┝┅ game data loaded.\n";
(* End Setup ! *)
print_string ("╘══ "^(envString#get "name")
^" is loaded in "^envString#get "dir"^"\n")
;;
(* Initialization *************************************************************)
let initialization execDir =
try
if not (Sys.is_directory execDir) then (
print_string "The specified path is not a folder.\n";
Printexc.print_backtrace stdout;
exit 2
)
else
let _ =
try
setup execDir
with
| Not_found ->
print_string
"☢ There is no file 'game.xml' in the directory of the game.\n";
Printexc.print_backtrace stdout;
exit 2
| _ ->
print_string "☢ The file 'game.xml' is invalid.\n";
Printexc.print_backtrace stdout;
exit 2
in flush stdout;
try
main ()
with
| Failure e ->
print_string ("☢ Error during game : "^e^" \n");
Printexc.print_backtrace stdout;
exit 2
| e ->
let str = Printexc.to_string e in
print_string ("☢ Error during game of kind "^str^"\n");
Printexc.print_backtrace stdout;
exit 2
with _ -> (
if execDir = "" then
print_string usage_msg
else
print_string "The specified path is not valid.\n";
Printexc.print_backtrace stdout;
); exit 2
;;
let get_arguments () =
let arg = ref "" in
Arg.parse ([]:((Arg.key * Arg.spec * Arg.doc) list))
(function str -> arg := str) usage_msg;
!arg
;;
(* Run !!! ********************************************************************)
Printexc.record_backtrace true; (*Debug trace*)
initialization (get_arguments ())