aboutsummaryrefslogtreecommitdiff
path: root/engines/tony/inventory.cpp
blob: 5fb375da8182eabfe58fa9629669a495a3748a61 (plain)
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on original Tony Tough source code
 *
 * Copyright (c) 1997-2003 Nayma Software
 */

#include "common/textconsole.h"
#include "tony/mpal/mpalutils.h"
#include "tony/inventory.h"
#include "tony/game.h"
#include "tony/tony.h"

namespace Tony {


/****************************************************************************\
*           RMInventory Methods
\****************************************************************************/

RMInventory::RMInventory() {
	m_items = NULL;
	m_state = CLOSED;
	m_bCombining = false;
	m_csModifyInterface = g_system->createMutex();
	m_nItems = 0;

	Common::fill(m_inv, m_inv + 256, 0);
	m_nInv = 0;
	m_curPutY = 0;
	m_curPutTime = 0;
	m_curPos = 0;
	m_bHasFocus = false;
	m_nSelectObj = 0;
	m_nCombine = 0;
	m_bBlinkingRight = false;
	m_bBlinkingLeft = false;
	miniAction = 0;
}

RMInventory::~RMInventory() {
	Close();
	g_system->deleteMutex(m_csModifyInterface);
}

bool RMInventory::CheckPointInside(const RMPoint &pt) {
	if (!GLOBALS.bCfgInvUp)
		return pt.y > RM_SY - 70;
	else
		return pt.y < 70;
}



void RMInventory::Init(void) {
	int i, j;
	int curres;

	// Create the main buffer
	Create(RM_SX, 68);
	SetPriority(185);

	// Setup the inventory
	m_nInv = 0;
	m_curPos = 0;
	m_bCombining = false;

	// New items
	m_nItems = 78;  // @@@ Number of takeable items
	m_items = new RMInventoryItem[m_nItems + 1];

	curres = 10500;

	// Loop through the items
	for (i = 0; i <= m_nItems; i++) {
		// Load the items from the resource
		RMRes res(curres);
		RMDataStream ds;

		assert(res.IsValid());

		// Initialise the MPAL inventory item by reading it in.
		m_items[i].icon.SetInitCurPattern(false);
		ds.OpenBuffer(res);
		ds >> m_items[i].icon;
		ds.Close();

		// Puts in the default pattern 1
		m_items[i].pointer = NULL;
		m_items[i].status = 1;
		m_items[i].icon.SetPattern(1);
		m_items[i].icon.DoFrame(this, false);

		curres++;
		if (i == 0 || i == 28 || i == 29) continue;

		m_items[i].pointer = new RMGfxSourceBuffer8RLEByteAA[m_items[i].icon.NumPattern()];

		for (j = 0; j < m_items[i].icon.NumPattern(); j++) {
			RMResRaw raw(curres);

			assert(raw.IsValid());

			m_items[i].pointer[j].Init((const byte *)raw, raw.Width(), raw.Height(), true);
			curres++;
		}
	}

	m_items[28].icon.SetPattern(1);
	m_items[29].icon.SetPattern(1);

	// Download interface
	RMDataStream ds;
	RMRes res(RES_I_MINIINTER);
	assert(res.IsValid());
	ds.OpenBuffer(res);
	ds >> miniInterface;
	miniInterface.SetPattern(1);
	ds.Close();

	// Create the text for hints on the mini interface
	m_hints[0].SetAlignType(RMText::HCENTER, RMText::VTOP);
	m_hints[1].SetAlignType(RMText::HCENTER, RMText::VTOP);
	m_hints[2].SetAlignType(RMText::HCENTER, RMText::VTOP);

	// The text is taken from MPAL for translation
	RMMessage msg1(15);
	RMMessage msg2(13);
	RMMessage msg3(14);

	m_hints[0].WriteText(msg1[0], 1);       // Examine
	m_hints[1].WriteText(msg2[0], 1);       // Take
	m_hints[2].WriteText(msg3[0], 1);       // Use


	// Prepare initial inventory
	Prepare();
	DrawOT(Common::nullContext);
	ClearOT();
}

void RMInventory::Close(void) {
	// Has memory
	if (m_items != NULL) {
		// Delete the item pointers
		for (int i = 0; i <= m_nItems; i++)
			delete[] m_items[i].pointer;

		// Delete the items array
		delete[] m_items;
		m_items = NULL;
	}

	Destroy();
}

void RMInventory::Reset(void) {
	m_state = CLOSED;
	EndCombine();
}

void RMInventory::Draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
	CORO_BEGIN_CONTEXT;
	RMPoint pos;
	RMPoint pos2;
	RMGfxPrimitive *p;
	RMGfxPrimitive *p2;
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	if (m_state == OPENING || m_state == CLOSING)
		prim->SetDst(RMPoint(0, m_curPutY));
	else
		prim->SetDst(RMPoint(0, m_curPutY));

	g_system->lockMutex(m_csModifyInterface);
	CORO_INVOKE_2(RMGfxWoodyBuffer::Draw, bigBuf, prim);
	g_system->unlockMutex(m_csModifyInterface);

	if (m_state == SELECTING) {

		if (!GLOBALS.bCfgInvUp) {
			_ctx->pos.Set((m_nSelectObj + 1) * 64 - 20, RM_SY - 113);
			_ctx->pos2.Set((m_nSelectObj + 1) * 64 + 34, RM_SY - 150);
		} else {
			_ctx->pos.Set((m_nSelectObj + 1) * 64 - 20, 72 - 4); // The brown part is at the top :(
			_ctx->pos2.Set((m_nSelectObj + 1) * 64 + 34, 119 - 4);
		}

		_ctx->p = new RMGfxPrimitive(prim->m_task, _ctx->pos);
		_ctx->p2 = new RMGfxPrimitive(prim->m_task, _ctx->pos2);

		// Draw the mini interface
		CORO_INVOKE_2(miniInterface.Draw, bigBuf, _ctx->p);

		if (GLOBALS.bCfgInterTips) {
			if (miniAction == 1) // Examine
				CORO_INVOKE_2(m_hints[0].Draw, bigBuf, _ctx->p2);
			else if (miniAction == 2) // Talk
				CORO_INVOKE_2(m_hints[1].Draw, bigBuf, _ctx->p2);
			else if (miniAction == 3) // Use
				CORO_INVOKE_2(m_hints[2].Draw, bigBuf, _ctx->p2);
		}

		delete _ctx->p;
		delete _ctx->p2;
	}

	CORO_END_CODE;
}

void RMInventory::RemoveThis(CORO_PARAM, bool &result) {
	if (m_state == CLOSED)
		result = true;
	else
		result = false;
}

void RMInventory::RemoveItem(int code) {
	int i;

	for (i = 0; i < m_nInv; i++)
		if (m_inv[i] == code - 10000) {
			g_system->lockMutex(m_csModifyInterface);

			Common::copy(&m_inv[i + 1], &m_inv[i + 1] + (m_nInv - i), &m_inv[i]);
			m_nInv--;

			Prepare();
			DrawOT(Common::nullContext);
			ClearOT();
			g_system->unlockMutex(m_csModifyInterface);
			return;
		}
}

void RMInventory::AddItem(int code) {
	if (code <= 10000 && code >= 10101) {
		// If we are here, it means that we are adding an item that should not be in the inventory
		warning("Cannot find a valid icon for this item, and then it will not be added to the inventory");
	} else {
		g_system->lockMutex(m_csModifyInterface);
		if (m_curPos + 8 == m_nInv) {
			// Break through the inventory! On the flashing pattern
			m_items[28].icon.SetPattern(2);
		}

		m_inv[m_nInv++] = code - 10000;

		Prepare();
		DrawOT(Common::nullContext);
		ClearOT();
		g_system->unlockMutex(m_csModifyInterface);
	}
}

void RMInventory::ChangeItemStatus(uint32 code, uint32 dwStatus) {
	if (code <= 10000 && code >= 10101) {
		error("Specified object code is not valid");
	} else {
		g_system->lockMutex(m_csModifyInterface);
		m_items[code - 10000].icon.SetPattern(dwStatus);
		m_items[code - 10000].status = dwStatus;

		Prepare();
		DrawOT(Common::nullContext);
		ClearOT();
		g_system->unlockMutex(m_csModifyInterface);
	}
}


void RMInventory::Prepare(void) {
	int i;

	for (i = 1; i < RM_SX / 64 - 1; i++) {
		if (i - 1 + m_curPos < m_nInv)
			AddPrim(new RMGfxPrimitive(&m_items[m_inv[i - 1 + m_curPos]].icon, RMPoint(i * 64, 0)));
		else
			AddPrim(new RMGfxPrimitive(&m_items[0].icon, RMPoint(i * 64, 0)));
	}

	// Frecce
	AddPrim(new RMGfxPrimitive(&m_items[29].icon, RMPoint(0, 0)));
	AddPrim(new RMGfxPrimitive(&m_items[28].icon, RMPoint(640 - 64, 0)));
}

bool RMInventory::MiniActive(void) {
	return m_state == SELECTING;
}

bool RMInventory::HaveFocus(const RMPoint &mpos) {
	// When we combine, have the focus only if we are on an arrow (to scroll)
	if (m_state == OPENED && m_bCombining && CheckPointInside(mpos) && (mpos.x < 64 || mpos.x > RM_SX - 64))
		return true;

	// If the inventory is open, focus we we go over it
	if (m_state == OPENED && !m_bCombining && CheckPointInside(mpos))
		return true;

	// If we are selecting a verb (and then right down), we always focus
	if (m_state == SELECTING)
		return true;

	return false;
}

void RMInventory::EndCombine(void) {
	m_bCombining = false;
}

bool RMInventory::LeftClick(const RMPoint &mpos, int &nCombineObj) {
	int n;

	// The left click picks an item from your inventory to use it with the background
	n = mpos.x / 64;

	if (m_state == OPENED) {
		if (n > 0 && n < RM_SX / 64 - 1 && m_inv[n - 1 + m_curPos] != 0) {
			m_bCombining = true; //m_state = COMBINING;
			m_nCombine = m_inv[n - 1 + m_curPos];
			nCombineObj = m_nCombine + 10000;

			_vm->playUtilSFX(1);
			return true;
		}
	}

	// Click the right arrow
	if ((m_state == OPENED) && m_bBlinkingRight) {
		g_system->lockMutex(m_csModifyInterface);
		m_curPos++;

		if (m_curPos + 8 >= m_nInv) {
			m_bBlinkingRight = false;
			m_items[28].icon.SetPattern(1);
		}

		if (m_curPos > 0) {
			m_bBlinkingLeft = true;
			m_items[29].icon.SetPattern(2);
		}

		Prepare();
		DrawOT(Common::nullContext);
		ClearOT();
		g_system->unlockMutex(m_csModifyInterface);
	}
	// Click the left arrow
	else if ((m_state == OPENED) && m_bBlinkingLeft) {
		assert(m_curPos > 0);
		g_system->lockMutex(m_csModifyInterface);
		m_curPos--;

		if (m_curPos == 0) {
			m_bBlinkingLeft = false;
			m_items[29].icon.SetPattern(1);
		}

		if (m_curPos + 8 < m_nInv) {
			m_bBlinkingRight = true;
			m_items[28].icon.SetPattern(2);
		}

		Prepare();
		DrawOT(Common::nullContext);
		ClearOT();
		g_system->unlockMutex(m_csModifyInterface);
	}


	return false;
}


void RMInventory::RightClick(const RMPoint &mpos) {
	int n;

	assert(CheckPointInside(mpos));

	if (m_state == OPENED && !m_bCombining) {
		// Open the context interface
		n = mpos.x / 64;

		if (n > 0 && n < RM_SX / 64 - 1 && m_inv[n - 1 + m_curPos] != 0) {
			m_state = SELECTING;
			miniAction = 0;
			m_nSelectObj = n - 1;

			_vm->playUtilSFX(0);
		}
	}

	if ((m_state == OPENED) && m_bBlinkingRight) {
		g_system->lockMutex(m_csModifyInterface);
		m_curPos += 7;
		if (m_curPos + 8 > m_nInv)
			m_curPos = m_nInv - 8;

		if (m_curPos + 8 <= m_nInv) {
			m_bBlinkingRight = false;
			m_items[28].icon.SetPattern(1);
		}

		if (m_curPos > 0) {
			m_bBlinkingLeft = true;
			m_items[29].icon.SetPattern(2);
		}

		Prepare();
		DrawOT(Common::nullContext);
		ClearOT();
		g_system->unlockMutex(m_csModifyInterface);
	} else if ((m_state == OPENED) && m_bBlinkingLeft) {
		assert(m_curPos > 0);
		g_system->lockMutex(m_csModifyInterface);
		m_curPos -= 7;
		if (m_curPos < 0) m_curPos = 0;

		if (m_curPos == 0) {
			m_bBlinkingLeft = false;
			m_items[29].icon.SetPattern(1);
		}

		if (m_curPos + 8 < m_nInv) {
			m_bBlinkingRight = true;
			m_items[28].icon.SetPattern(2);
		}

		Prepare();
		DrawOT(Common::nullContext);
		ClearOT();
		g_system->unlockMutex(m_csModifyInterface);
	}
}

bool RMInventory::RightRelease(const RMPoint &mpos, RMTonyAction &curAction) {
	if (m_state == SELECTING) {
		m_state = OPENED;

		if (miniAction == 1) { // Esamina
			curAction = TA_EXAMINE;
			return true;
		} else if (miniAction == 2) { // Parla
			curAction = TA_TALK;
			return true;
		} else if (miniAction == 3) { // Usa
			curAction = TA_USE;
			return true;
		}
	}

	return false;
}

#define INVSPEED    20

void RMInventory::DoFrame(RMGfxTargetBuffer &bigBuf, RMPointer &ptr, RMPoint mpos, bool bCanOpen) {
	int i;
	bool bNeedRedraw = false;

	if (m_state != CLOSED) {
		// Clean up the OT list
		g_system->lockMutex(m_csModifyInterface);
		ClearOT();

		// DoFrame makes all the objects currently in the inventory be displayed
		// @@@ Maybe we should do all takeable objects? Please does not help
		for (i = 0; i < m_nInv; i++)
			if (m_items[m_inv[i]].icon.DoFrame(this, false) && (i >= m_curPos && i <= m_curPos + 7))
				bNeedRedraw = true;

		if ((m_state == CLOSING || m_state == OPENING || m_state == OPENED) && CheckPointInside(mpos)) {
			if (mpos.x > RM_SX - 64) {
				if (m_curPos + 8 < m_nInv && !m_bBlinkingRight) {
					m_items[28].icon.SetPattern(3);
					m_bBlinkingRight = true;
					bNeedRedraw = true;
				}
			} else if (m_bBlinkingRight) {
				m_items[28].icon.SetPattern(2);
				m_bBlinkingRight = false;
				bNeedRedraw = true;
			}

			if (mpos.x < 64) {
				if (m_curPos > 0 && !m_bBlinkingLeft) {
					m_items[29].icon.SetPattern(3);
					m_bBlinkingLeft = true;
					bNeedRedraw = true;
				}
			} else if (m_bBlinkingLeft) {
				m_items[29].icon.SetPattern(2);
				m_bBlinkingLeft = false;
				bNeedRedraw = true;
			}
		}

		if (m_items[28].icon.DoFrame(this, false))
			bNeedRedraw = true;

		if (m_items[29].icon.DoFrame(this, false))
			bNeedRedraw = true;

		if (bNeedRedraw)
			Prepare();

		g_system->unlockMutex(m_csModifyInterface);
	}

	if (_vm->getEngine()->getInput().getAsyncKeyState(Common::KEYCODE_i)) {
		GLOBALS.bCfgInvLocked = !GLOBALS.bCfgInvLocked;
	}

	if (m_bCombining) {//m_state == COMBINING)
		ptr.SetCustomPointer(&m_items[m_nCombine].pointer[m_items[m_nCombine].status - 1]);
		ptr.SetSpecialPointer(RMPointer::PTR_CUSTOM);
	}

	if (!GLOBALS.bCfgInvUp) {
		if ((m_state == CLOSED) && (mpos.y > RM_SY - 10 || GLOBALS.bCfgInvLocked) && bCanOpen) {
			if (!GLOBALS.bCfgInvNoScroll) {
				m_state = OPENING;
				m_curPutY = RM_SY - 1;
				m_curPutTime = _vm->getTime();
			} else {
				m_state = OPENED;
				m_curPutY = RM_SY - 68;
			}
		} else if (m_state == OPENED) {
			if ((mpos.y < RM_SY - 70 && !GLOBALS.bCfgInvLocked) || !bCanOpen) {
				if (!GLOBALS.bCfgInvNoScroll) {
					m_state = CLOSING;
					m_curPutY = RM_SY - 68;
					m_curPutTime = _vm->getTime();
				} else {
					m_state = CLOSED;
				}
			}
		} else if (m_state == OPENING) {
			while (m_curPutTime + INVSPEED < _vm->getTime()) {
				m_curPutY -= 3;
				m_curPutTime += INVSPEED;
			}

			if (m_curPutY <= RM_SY - 68) {
				m_state = OPENED;
				m_curPutY = RM_SY - 68;
			}

		} else if (m_state == CLOSING) {
			while (m_curPutTime + INVSPEED < _vm->getTime()) {
				m_curPutY += 3;
				m_curPutTime += INVSPEED;
			}

			if (m_curPutY > 480)
				m_state = CLOSED;
		}
	} else {
		if ((m_state == CLOSED) && (mpos.y < 10 || GLOBALS.bCfgInvLocked) && bCanOpen) {
			if (!GLOBALS.bCfgInvNoScroll) {
				m_state = OPENING;
				m_curPutY = - 68;
				m_curPutTime = _vm->getTime();
			} else {
				m_state = OPENED;
				m_curPutY = 0;
			}
		} else if (m_state == OPENED) {
			if ((mpos.y > 70 && !GLOBALS.bCfgInvLocked) || !bCanOpen) {
				if (!GLOBALS.bCfgInvNoScroll) {
					m_state = CLOSING;
					m_curPutY = -2;
					m_curPutTime = _vm->getTime();
				} else {
					m_state = CLOSED;
				}
			}
		} else if (m_state == OPENING) {
			while (m_curPutTime + INVSPEED < _vm->getTime()) {
				m_curPutY += 3;
				m_curPutTime += INVSPEED;
			}

			if (m_curPutY >= 0) {
				m_state = OPENED;
				m_curPutY = 0;
			}
		} else if (m_state == CLOSING) {
			while (m_curPutTime + INVSPEED < _vm->getTime()) {
				m_curPutY -= 3;
				m_curPutTime += INVSPEED;
			}

			if (m_curPutY < -68)
				m_state = CLOSED;
		}
	}

	if (m_state == SELECTING) {
		int startx = (m_nSelectObj + 1) * 64 - 20;
		int starty;

		if (!GLOBALS.bCfgInvUp)
			starty = RM_SY - 109;
		else
			starty = 70;

		// Make sure it is on one of the verbs
		if (mpos.y > starty && mpos.y < starty + 45) {
			if (mpos.x > startx && mpos.x < startx + 40) {
				if (miniAction != 1) {
					miniInterface.SetPattern(2);
					miniAction = 1;
					_vm->playUtilSFX(1);
				}
			} else if (mpos.x >= startx + 40 && mpos.x < startx + 80) {
				if (miniAction != 2) {
					miniInterface.SetPattern(3);
					miniAction = 2;
					_vm->playUtilSFX(1);
				}
			} else if (mpos.x >= startx + 80 && mpos.x < startx + 108) {
				if (miniAction != 3) {
					miniInterface.SetPattern(4);
					miniAction = 3;
					_vm->playUtilSFX(1);
				}
			} else {
				miniInterface.SetPattern(1);
				miniAction = 0;
			}
		} else  {
			miniInterface.SetPattern(1);
			miniAction = 0;
		}

		// Update the mini-interface
		miniInterface.DoFrame(&bigBuf, false);
	}

	if ((m_state != CLOSED) && !m_nInList) {
		bigBuf.AddPrim(new RMGfxPrimitive(this));
	}
}


bool RMInventory::ItemInFocus(const RMPoint &mpt) {
	if ((m_state == OPENED || m_state == OPENING) && CheckPointInside(mpt))
		return true;
	else
		return false;
}

RMItem *RMInventory::WhichItemIsIn(const RMPoint &mpt) {
	int n;

	if (m_state == OPENED) {
		if (CheckPointInside(mpt)) {
			n = mpt.x / 64;
			if (n > 0 && n < RM_SX / 64 - 1 && m_inv[n - 1 + m_curPos] != 0 && (!m_bCombining || m_inv[n - 1 + m_curPos] != m_nCombine))
				return &m_items[m_inv[n - 1 + m_curPos]].icon;
		}
	}

	return NULL;
}



int RMInventory::GetSaveStateSize(void) {
	//     m_inv   pattern   m_nInv
	return 256 * 4 + 256 * 4   +  4     ;
}

void RMInventory::SaveState(byte *state) {
	int i, x;

	WRITE_LE_UINT32(state, m_nInv);
	state += 4;
	Common::copy(m_inv, m_inv + 256, (uint32 *)state);
	state += 256 * 4;

	for (i = 0; i < 256; i++) {
		if (i < m_nItems)
			x = m_items[i].status;
		else
			x = 0;

		WRITE_LE_UINT32(state, x);
		state += 4;
	}
}

int RMInventory::LoadState(byte *state) {
	int i, x;

	m_nInv = READ_LE_UINT32(state);
	state += 4;
	Common::copy((uint32 *)state, (uint32 *)state + 256, m_inv);
	state += 256 * 4;

	for (i = 0; i < 256; i++) {
		x = READ_LE_UINT32(state);
		state += 4;

		if (i < m_nItems) {
			m_items[i].status = x;
			m_items[i].icon.SetPattern(x);
		}
	}

	m_curPos = 0;
	m_bCombining = false;

	m_items[29].icon.SetPattern(1);

	if (m_nInv > 8)
		m_items[28].icon.SetPattern(2);
	else
		m_items[28].icon.SetPattern(1);

	Prepare();
	DrawOT(Common::nullContext);
	ClearOT();

	return GetSaveStateSize();
}


/****************************************************************************\
*           RMInterface methods
\****************************************************************************/

RMInterface::~RMInterface() {

}

bool RMInterface::Active() {
	return m_bActive;
}

int RMInterface::OnWhichBox(RMPoint pt) {
	int max, i;

	pt -= m_openStart;

	// Check how many verbs you have to consider
	max = 4;
	if (m_bPalesati) max = 5;

	// Find the verb
	for (i = 0; i < max; i++)
		if (m_hotbbox[i].PtInRect(pt))
			return i;

	// Found no verb
	return -1;
}

void RMInterface::Draw(CORO_PARAM, RMGfxTargetBuffer &bigBuf, RMGfxPrimitive *prim) {
	CORO_BEGIN_CONTEXT;
	int h;
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	prim->Dst().TopLeft() = m_openStart;
	CORO_INVOKE_2(RMGfxSourceBuffer8RLEByte::Draw, bigBuf, prim);

	// Check if there is a draw hot zone
	_ctx->h = OnWhichBox(m_mpos);
	if (_ctx->h != -1) {
		prim->Dst().TopLeft() = m_openStart;
		CORO_INVOKE_2(m_hotzone[_ctx->h].Draw, bigBuf, prim);

		if (m_lastHotZone != _ctx->h) {
			m_lastHotZone = _ctx->h;
			_vm->playUtilSFX(1);
		}

		if (GLOBALS.bCfgInterTips) {
			prim->Dst().TopLeft() = m_openStart + RMPoint(70, 177);
			CORO_INVOKE_2(m_hints[_ctx->h].Draw, bigBuf, prim);
		}
	} else
		m_lastHotZone = -1;

	CORO_END_CODE;
}


void RMInterface::DoFrame(RMGfxTargetBuffer &bigBuf, RMPoint mousepos) {
	// If needed, add to the OT schedule list
	if (!m_nInList && m_bActive)
		bigBuf.AddPrim(new RMGfxPrimitive(this));

	m_mpos = mousepos;
}

void RMInterface::Clicked(const RMPoint &mousepos) {
	m_bActive = true;
	m_openPos = mousepos;

	// Calculate the top left corner of the interface
	m_openStart = m_openPos - RMPoint(m_dimx / 2, m_dimy / 2);
	m_lastHotZone = -1;

	// Keep it inside the screen
	if (m_openStart.x < 0) m_openStart.x = 0;
	if (m_openStart.y < 0) m_openStart.y = 0;
	if (m_openStart.x + m_dimx > RM_SX) m_openStart.x = RM_SX - m_dimx;
	if (m_openStart.y + m_dimy > RM_SY) m_openStart.y = RM_SY - m_dimy;

	// Play the sound effect
	_vm->playUtilSFX(0);
}

bool RMInterface::Released(const RMPoint &mousepos, RMTonyAction &action) {
	if (!m_bActive)
		return false;

	m_bActive = false;

	switch (OnWhichBox(mousepos)) {
	case 0:
		action = TA_TAKE;
		break;

	case 1:
		action = TA_TALK;
		break;

	case 2:
		action = TA_USE;
		break;

	case 3:
		action = TA_EXAMINE;
		break;

	case 4:
		action = TA_PALESATI;
		break;

	default:        // No verb
		return false;
	}

	return true;
}

void RMInterface::Reset(void) {
	m_bActive = false;
}

void RMInterface::SetPalesati(bool bOn) {
	m_bPalesati = bOn;
}

bool RMInterface::GetPalesati(void) {
	return m_bPalesati;
}

void RMInterface::Init(void) {
	int i;
	RMResRaw inter(RES_I_INTERFACE);
	RMRes pal(RES_I_INTERPPAL);

	SetPriority(191);

	RMGfxSourceBuffer::Init(inter, inter.Width(), inter.Height());
	LoadPaletteWA(RES_I_INTERPAL);

	for (i = 0; i < 5; i++) {
		RMResRaw part(RES_I_INTERP1 + i);

		m_hotzone[i].Init(part, part.Width(), part.Height());
		m_hotzone[i].LoadPaletteWA(pal);
	}

	m_hotbbox[0].SetRect(126, 123, 159, 208);   // Take
	m_hotbbox[1].SetRect(90, 130, 125, 186);    // About
	m_hotbbox[2].SetRect(110, 60, 152, 125);
	m_hotbbox[3].SetRect(56, 51, 93, 99);
	m_hotbbox[4].SetRect(51, 105, 82, 172);

	m_hints[0].SetAlignType(RMText::HRIGHT, RMText::VTOP);
	m_hints[1].SetAlignType(RMText::HRIGHT, RMText::VTOP);
	m_hints[2].SetAlignType(RMText::HRIGHT, RMText::VTOP);
	m_hints[3].SetAlignType(RMText::HRIGHT, RMText::VTOP);
	m_hints[4].SetAlignType(RMText::HRIGHT, RMText::VTOP);

	// The text is taken from MPAL for translation
	RMMessage msg0(12);
	RMMessage msg1(13);
	RMMessage msg2(14);
	RMMessage msg3(15);
	RMMessage msg4(16);

	m_hints[0].WriteText(msg0[0], 1);   // Take
	m_hints[1].WriteText(msg1[0], 1);   // Talk
	m_hints[2].WriteText(msg2[0], 1);   // Use
	m_hints[3].WriteText(msg3[0], 1);   // Examine
	m_hints[4].WriteText(msg4[0], 1);   // Show Yourself

	m_bActive = false;
	m_bPalesati = false;
	m_lastHotZone = 0;
}

void RMInterface::Close(void) {
	int i;

	Destroy();

	for (i = 0; i < 5; i++)
		m_hotzone[i].Destroy();
}

} // End of namespace Tony