File: init.lua

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (574 lines) | stat: -rw-r--r-- 13,035 bytes parent folder | download | duplicates (4)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
local lua_file = nil

--load common functions
lua_file = nuvie_load("common/common.lua"); lua_file();


SFX_BLOCKED = 0
SFX_HIT = 1
SFX_FOUNTAIN = 2
SFX_DEATH = 3
SFX_RUBBER_DUCK = 4
SFX_BROKEN_GLASS = 5
SFX_BELL = 6
SFX_FIRE = 7
SFX_CLOCK = 8
SFX_PROTECTION_FIELD = 9
SFX_WATER_WHEEL = 10
SFX_MISSLE = 11
SFX_EXPLOSION = 12
SFX_ATTACK_SWING = 13
SFX_SUCCESS = 14
SFX_FAILURE = 15
SFX_CORPSER_DRAGGED_UNDER = 16
SFX_CORPSER_REGURGITATE = 17
SFX_CASTING_MAGIC_P1 = 18
SFX_CASTING_MAGIC_P1_2 = 19
SFX_CASTING_MAGIC_P1_3 = 20
SFX_CASTING_MAGIC_P1_4 = 21
SFX_CASTING_MAGIC_P1_5 = 22
SFX_CASTING_MAGIC_P1_6 = 23
SFX_CASTING_MAGIC_P1_7 = 24
SFX_CASTING_MAGIC_P1_8 = 25
SFX_CASTING_MAGIC_P2 = 26
SFX_CASTING_MAGIC_P2_2 = 27
SFX_CASTING_MAGIC_P2_3 = 28
SFX_CASTING_MAGIC_P2_4 = 29
SFX_CASTING_MAGIC_P2_5 = 30
SFX_CASTING_MAGIC_P2_6 = 31
SFX_CASTING_MAGIC_P2_7 = 32
SFX_CASTING_MAGIC_P2_8 = 33
SFX_AVATAR_DEATH = 34
SFX_KAL_LOR = 35
SFX_SLUG_DISSOLVE = 36
SFX_HAIL_STONE = 37

SFX_EARTH_QUAKE = 39

FADE_COLOR_RED = 12
FADE_COLOR_BLUE = 9

TIMER_LIGHT = 0
TIMER_INFRAVISION = 1
TIMER_STORM = 13
TIMER_TIME_STOP = 14
TIMER_ECLIPSE = 15

OBJLIST_OFFSET_VANISH_OBJ = 0x1c13
OBJLIST_OFFSET_MOONSTONES = 0x1c1b
OBJLIST_OFFSET_KEG_TIMER  = 0x1c4b

g_vanish_obj = {["obj_n"] = 0, ["frame_n"] = 0}

g_keg_timer = 0
g_armageddon = false
g_avatar_died = false -- used so we don't keep casting once Avatar is dead
function is_avatar_dead()
	return g_avatar_died
end

--used with triple crossbow and magic wind spells.
g_projectile_offset_tbl =
{
 {
  4,5,5,5,5,6,6,6,6,6,6,
  4,4,5,5,5,6,6,6,6,6,7,
  4,4,4,5,5,6,6,6,6,7,7,
  4,4,4,4,5,6,6,6,7,7,7,
  4,4,4,4,4,6,6,7,7,7,7,
  4,4,4,4,4,0,0,0,0,0,0,
  3,3,3,3,2,2,0,0,0,0,0,
  3,3,3,2,2,2,1,0,0,0,0,
  3,3,2,2,2,2,1,1,0,0,0,
  3,2,2,2,2,2,1,1,1,0,0,
  2,2,2,2,2,2,1,1,1,1,0
 },
 {
  2,2,2,2,2,2,3,3,3,3,4,
  1,2,2,2,2,2,3,3,3,4,4,
  1,1,2,2,2,2,3,3,4,4,4,
  1,1,1,2,2,2,3,4,4,4,4,
  1,1,1,1,2,2,4,4,4,4,4,
  0,0,0,0,0,0,4,4,4,4,4,
  0,0,0,0,0,6,6,5,5,5,5,
  0,0,0,0,7,6,6,6,5,5,5,
  0,0,0,7,7,6,6,6,6,5,5,
  0,0,7,7,7,6,6,6,6,6,5,
  0,7,7,7,7,6,6,6,6,6,6
 }
}

--moonstone data is loaded from objlist in load_game()
g_moonstone_loc_tbl =
{
 {x=0x3A7, y=0x106, z=0},
 {x=0x1F7, y=0x166, z=0},
 {x=0x9F,  y=0x3AE, z=0},
 {x=0x127, y=0x26,  z=0},
 {x=0x33F, y=0x0A6, z=0},
 {x=0x147, y=0x336, z=0},
 {x=0x17,  y=0x16,  z=1},
 {x=0x397, y=0x3A6, z=0}
}

g_show_stealing = config_get_boolean_value("config/ultima6/show_stealing")

-- some common functions

function set_g_show_stealing(stealing)
	g_show_stealing = stealing
end

function dbg(msg_string)
	--io.stderr:write(msg_string)
end

function alignment_is_evil(align)
   if align == ALIGNMENT_EVIL or align == ALIGNMENT_CHAOTIC then return true end

   return false
end

function advance_game_time(nturns)
 if nturns == 0 then return end

 coroutine.yield("adv_game_time", nturns);
end

function get_obj_from_inventory(actor)
	local obj = coroutine.yield("inv_obj", actor)
	return obj
end

function get_obj()
	local obj = coroutine.yield("obj")
	return obj
end

function actor_talk(actor)
	coroutine.yield("talk", actor)
	return
end

function get_spell()
	local spell_num = coroutine.yield("spell")
	return spell_num
end

function obj_new(obj_n, frame_n, status, qty, quality, x, y, z)
  local obj = {}

  obj["obj_n"] = obj_n or 0
  obj["frame_n"] = frame_n or 0
  obj["status"] = status or 0
  obj["qty"] = qty or 0
  obj["quality"] = quality or 0

  obj["x"] = x or 0
  obj["y"] = y or 0
  obj["z"] = z or 0

  return obj
end

--FIXME need a better way of doing this. Remove need for deprecated setfenv() function.
function run_script(script)
  local t = {};
  setmetatable(t, {__index = _G});
  local body = nuvie_load(script);
  setfenv(body, t);
  body();
end

function look_obj(obj)
   print("Thou dost see " .. obj.look_string);
   local weight = obj.weight; --FIXME this could be a problem if we want to change Lua_number type to int.
   if weight ~= 0 then
      if obj.qty > 1 and obj.stackable then
         print(". They weigh");
      else
         print(". It weighs");
      end

      print(string.format(" %.1f", weight).." stones");
   end

   --FIXME usecode look description should be lua code.
   if usecode_look(obj) then
      print("\n")
      return false
   end

   local dmg = weapon_dmg_tbl[obj.obj_n];
   if dmg ~= nil then
      if weight ~= 0 then
         print(" and")
      else
         print(". It")
      end

      print(" can do "..dmg.." point")
      if dmg > 1 then print("s") end
      print(" of damage")

   end

   local ac = armour_tbl[obj.obj_n]
   if ac ~= nil then
      if weight ~= 0 or dmg ~= 0 then
         print(" and")
      else
         print(". It")
      end

      print(" can absorb "..ac.." point")
      if ac > 1 then print("s") end
      print(" of damage")
   end

   print(".\n");
   local player_loc = player_get_location();
   if g_show_stealing == true and obj.getable == true and player_loc.z == 0 and obj.ok_to_take == false then
      if math.abs(player_loc.x - obj.x) > 1 or math.abs(player_loc.y - obj.y) > 1 then
        print("PRIVATE PROPERTY\n")
      else
        print("PRIVATE PROPERTY")
      end
   end

   return true
end

function player_subtract_karma(k)
	local karma = player_get_karma() - k
	if karma < 0 then karma = 0 end

	player_set_karma(karma)
end

function player_add_karma(k)
local karma = player_get_karma() + k
if karma >= 100 then karma = 99 end

player_set_karma(karma)
end

function party_heal()
	for actor in party_members() do
		actor.asleep = false
		actor.poisoned = false
		actor.paralyzed = false
		actor_remove_charm(actor)
		actor.hp = actor.max_hp
	end
end

function explosion(tile_num, x, y)
	play_sfx(SFX_EXPLOSION)
	return explosion_start(tile_num, x, y)
end

function projectile(tile_num, start_x, start_y, end_x, end_y, speed, spin)

	if spin == nil then spin = 0 end

	local rotate_offset = 0
	local src_tile_y_offset = 0

	if tile_num == 547 then --spear
		rotate_offset = 45
	elseif tile_num == 566 then --bow
		rotate_offset = 90
		src_tile_y_offset = 4
	elseif tile_num == 567 then --crossbow
		rotate_offset = 90
		src_tile_y_offset = 3
	end

	play_sfx(SFX_MISSLE)
	projectile_anim(tile_num, start_x, start_y, end_x, end_y, speed, false, rotate_offset, spin, src_tile_y_offset)
end

function fade_obj_blue(obj)
  fade_obj(obj, FADE_COLOR_BLUE, 20)
end

function fade_actor_blue(actor)
	Actor.black_fade_effect(actor, FADE_COLOR_BLUE, 20)
end

function fade_obj_out(obj)
	obj.invisible = true
	fade_tile(obj.x, obj.y, obj.z, obj.tile_num)
	obj.invisible = false
end

function fade_obj_in(obj)
	obj.invisible = true
	fade_tile(obj.x, obj.y, obj.z, nil, obj.tile_num)
	obj.invisible = false
end

function fade_actor_in(actor)
	local new_tile_num = actor.tile_num
	actor.visible = false
	fade_tile(actor.x, actor.y, actor.z, nil, new_tile_num)
	actor.visible = true
end

--tile_num, readied location
local g_readiable_objs_tbl = {
[0x200] = 0, [0x201] = 0, [0x202] = 0, [0x203] = 0, [0x204] = 0, [0x205] = 0, [0x206] = 0, [0x207] = 0,

[0x219] = 1, [0x250] = 1, [0x251] = 1, [0x252] = 1, [0x217] = 1, [0x101] = 1,

[0x220] = 2, [0x221] = 2, [0x223] = 2, [0x224] = 2, [0x225] = 2, [0x226] = 2, [0x227] = 2, [0x22A] = 2,
[0x22F] = 2, [0x230] = 2, [0x238] = 2, [0x254] = 2, [0x256] = 2, [0x255] = 2, [0x259] = 2, [0x262] = 2,
[0x263] = 2, [0x264] = 2, [0x270] = 2, [0x271] = 2, [0x272] = 2, [0x273] = 2, [0x274] = 2, [0x275] = 2,
[0x279] = 2, [0x27D] = 2, [0x27E] = 2, [0x27F] = 2, [0x280] = 2, [0x281] = 2, [0x28C] = 2, [0x28E] = 2,
[0x29D] = 2, [0x2A2] = 2, [0x2A3] = 2, [0x2B9] = 2,

[0x210] = 4, [0x211] = 4, [0x212] = 4, [0x213] = 4, [0x214] = 4, [0x215] = 4, [0x216] = 4, [0x218] = 4,
[0x219] = 4, [0x28c] = 4, [0x28e] = 4, [0x29d] = 4, [0x257] = 4,
[0x208] = 5, [0x209] = 5, [0x20a] = 5, [0x20b] = 5, [0x20c] = 5, [0x20d] = 5, [0x20e] = 5, [0x20f] = 5,
[0x222] = 5,

[0x21a] = 7, [0x21b] = 7,

[0x228] = 8, [0x229] = 8, [0x231] = 8, [0x235] = 8, [0x22b] = 8, [0x22c] = 8, [0x22d] = 8, [0x22e] = 8,

[0x258] = 9, [0x37d] = 9, [0x37e] = 9, [0x37f] = 9
}

function actor_is_readiable_obj(actor)
	if g_readiable_objs_tbl[actor.tile_num] ~= nil then
		return true
	end

	return false
end

function obj_is_readiable(obj)
	if g_readiable_objs_tbl[obj.tile_num] ~= nil then
		return true
	end

	return false
end

function is_time_stopped()
	if timer_get(TIMER_TIME_STOP) ~= 0 then return true end

	return false
end

function load_game()
	objlist_seek(OBJLIST_OFFSET_VANISH_OBJ)
	local tmp_obj_dat = objlist_read2()

	local obj_n = tmp_obj_dat
	local frame_n = 0
	if tmp_obj_dat > 1023 then
		frame_n = tmp_obj_dat - 1023
		obj_n = tmp_obj_dat - frame_n
	end

	g_vanish_obj.obj_n = obj_n
	g_vanish_obj.frame_n = frame_n

	--Load moonstone locations.
	objlist_seek(OBJLIST_OFFSET_MOONSTONES)

	for i=1,8 do

		local x = objlist_read2()
		local y = objlist_read2()
		local z = objlist_read2()
		dbg("moonstone["..i.."] at ("..x..","..y..","..z..")\n")
		g_moonstone_loc_tbl[i] = {x=x, y=y, z=z}
	end

	objlist_seek(OBJLIST_OFFSET_KEG_TIMER)
	g_keg_timer = objlist_read2()
	set_g_armageddon(false)
	g_avatar_died = false
end

function save_game()
	objlist_seek(OBJLIST_OFFSET_VANISH_OBJ)

	local tmp_obj_dat = g_vanish_obj.obj_n
	local frame_n = g_vanish_obj.frame_n

	if frame_n > 0 then
		tmp_obj_dat = tmp_obj_dat + (frame_n + 1023)
	end

	objlist_write2(tmp_obj_dat)

	--Save moonstone locations
	objlist_seek(OBJLIST_OFFSET_MOONSTONES)

	for i=1,8 do
		local loc = g_moonstone_loc_tbl[i]
		objlist_write2(loc.x)
		objlist_write2(loc.y)
		objlist_write2(loc.z)
	end

	objlist_seek(OBJLIST_OFFSET_KEG_TIMER)
	objlist_write2(g_keg_timer)
end

function moonstone_set_loc(phase, x, y, z)
	if phase < 1 or phase > 8 then return false end

	g_moonstone_loc_tbl[phase] = {x=x, y=y, z=z}
	return true
end

function moonstone_get_loc(phase)
	if phase < 1 or phase > 8 then return nil end

	return g_moonstone_loc_tbl[phase]
end

function update_moongates(show_moongates)
	local i, loc
	for i,loc in ipairs(g_moonstone_loc_tbl) do
		local moongate = map_get_obj(loc.x, loc.y, loc.z, 0x55) --moongate
		if show_moongates == true then
			if moongate == nil and loc.x ~= 0 then
				moongate = Obj.new(0x55, 1)
				Obj.moveToMap(moongate, loc)
			end
		else --hide moongate
			if moongate ~= nil then
				map_remove_obj(moongate)
			end
		end
	end
end

function use_keg(obj)
	if obj.frame_n ~= 0 then
		print("\nNo effect\n")
		return
	end

	if g_keg_timer > 0 then
		print("\nNot now\n")
	else
		obj.frame_n = 1
		print("\nPowder lit!\n")
		g_keg_timer = 3
	end
end

function explode_keg()
	--try to find lit keg on the current game map.
	local loc = player_get_location()
	for obj in find_obj(loc.z, 223, 1) do --keg obj, frame_n = 1
		if obj ~= nil then
			explode_obj(obj)
		end
	end

	--try to explode lit kegs in the party's inventory
	local party_actor
	for party_actor in party_members() do
		local obj = Actor.inv_get_obj_n(party_actor, 223, 1) --keg with frame_n = 1
		if obj ~= nil then
			explode_obj(obj, party_actor)
		end
	end
end

function explode_obj(obj, actor)
	dbg("Exploding "..obj.name.."\n")
	local x, y, z

	if actor ~= nil then
		x = actor.x
		y = actor.y
		z = actor.z
	else
		x = obj.x
		y = obj.y
		z = obj.z
	end

	Obj.removeFromEngine(obj)

	local hit_items = explosion(0x189, x, y)
	local random = math.random
	local k, v
	for k,v in pairs(hit_items) do
		if v.luatype == "actor" then
			actor_hit(v, random(1, 0x3c))
		end
		if g_avatar_died == true then
			explode_surrounding_objects(x, y, z)
			actor_avatar_death(Actor.get(1))
			return -- don't keep exploding once Avatar is dead
		end
	end

	explode_surrounding_objects(x, y, z)
end

function explode_surrounding_objects(x, y, z)
	--blow up doors and other kegs
	for x = x - 2,x + 2 do
		for y = y - 2,y + 2 do
			local map_obj = map_get_obj(x, y, z, 223)
			if map_obj ~= nil then
				explode_obj(map_obj)
			end

			map_obj = map_get_obj(x, y, z, 0x12c) --steel door
			if map_obj == nil or map_obj.frame_n == 0xc then
				map_obj = map_get_obj(x, y, z, 0x129) --oaken door
				if map_obj == nil or map_obj.frame_n == 0xc then
					map_obj = map_get_obj(x, y, z, 0x12a) --windowed door
					if map_obj == nil or map_obj.frame_n == 0xc then
						map_obj = map_get_obj(x, y, z, 0x12b) --cedar door
					end
				end
			end

			if map_obj ~= nil and map_obj.frame_n <= 0xc then
				Obj.removeFromEngine(map_obj)
				print("\nThe door is blown up!\n")
			end
		end
	end
end

function set_g_armageddon(val)
	g_armageddon = val
	set_armageddon(val)
end

function create_object_needs_quan(obj_n)
-- obj.stackable is already checked
	return false
end

--load actor functions
actor_load = nuvie_load("u6/actor.lua");
if type(actor_load) == "function" then
   actor_load()
else
   if type(actor_load) == "string" then
      io.stderr:write(actor_load);
   end
end

-- init magic
magic_init = nuvie_load("u6/magic.lua"); magic_init();

-- init usecode
usecode_init = nuvie_load("u6/usecode.lua"); usecode_init();

player_init = nuvie_load("u6/player.lua"); player_init();