// tilechest.txt - This script is used to power chests which require that a puzzle be solved
// before the chest can be opened.

// When you want to put a trap on a section of floor or container, you need to
// use the create terrain script button. Then you need to set the memory cells
// for the sort of trap.

// Memory Cells - 
//   0 - Puzzle difficulty. This is the intelligence needed to solve the puzzle.
//   2,3 - Coordinates for a stuff done flag. The stuff done flag is set to 1 when the puzzle is
//   solved.

beginterrainscript; 

variables;
	
short choice,r1;
body;

beginstate INIT_STATE;
break;

beginstate START_STATE;
break;

beginstate SEARCH_STATE;
        if (get_flag(get_memory_cell(2),get_memory_cell(3)) == 0) {
                reset_dialog();
                add_dialog_str(0,"This chest is held shut by a large, slightly glowing, padlock. You won't be able to break it open. On top of the chest are two grids and 32 multicolored tiles.",0);
                add_dialog_str(1,"One of the grids has 16 of the tiles in it. The other is empty. You guess that the lock has something to do with the other 16 tiles.",0);
                add_dialog_choice(0,"Play with the tiles.");
                add_dialog_choice(1,"Leave.");
                choice = run_dialog(1);
                if (choice == 1) {
                        if (get_highest_skill(2) < get_memory_cell(0)) {
                                add_dialog_str(0,"You start playing with the tiles. None of them match anything in the original 16, so the meaning of the first grid is more obscure. Unfortunately, you can't figure out the pattern. After about 20 failed tries, you give up and leave.",0);
                                block_entry(1);
                        end();
                        }
                        if (get_highest_skill(2) >= get_memory_cell(0)) {
                                add_dialog_str(0,"You start playing with the tiles. None of them match anything in the original 16, so the meaning of the first grid is more obscure. Fortunately, you have enough intelligence to figure out the pattern.",0);
                                add_dialog_str(1,"The puzzle is complicated, but you figure it out after a couple of attempts. The lock opens once you've figured out the puzzle.",0);
                               set_flag(get_memory_cell(2),get_memory_cell(3),1);
                               end();
                        }
                }
                if (choice == 2) {
                        end();
                }
        }
break;

beginstate UNLOCK_SPELL_STATE;
        print_str("Unlock: This spell doesn't help solve puzzles.");
break;