// machinery.txt
// by Thralni
// v1.0.1

// Only for use within the scenario "HIM"
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//   Cell 1 - determines what machine this is (0 = memory, 1 = cake)

begincreaturescript;

variables;

short i,trgt,dmg,attack_tick;

body;

beginstate INIT_STATE;
	if (get_memory_cell(0) == 2)
		set_mobility(ME,0);
break;

beginstate DEAD_STATE;
	play_sound(102);

	set_character_pose(ME,11);
	force_instant_terrain_redraw();
	pause(1);

	set_character_pose(ME,12);
	force_instant_terrain_redraw();
	pause(1);

	set_character_pose(ME,13);
	force_instant_terrain_redraw();
	pause(1);

	set_character_pose(ME,14);
	force_instant_terrain_redraw();
	pause(1);

	set_terrain(my_loc_x(),my_loc_y(),138);

	if (get_memory_cell(1) == 0) { // memory databank
		set_flag(250,0,(get_flag(250,0) + 1));
		}

	if (get_memory_cell(1) == 1) { // cake
		set_flag(250,1,(get_flag(250,1) + 1));
		}

	erase_char(ME);
break;

beginstate START_STATE; 
	if (get_ran(1,0,6) > 4 && party_can_see_loc(my_loc_x(),my_loc_y())) { // display a lightning effect
			put_boom_on_char(ME,4,0);
			run_animation();
			}

	if (get_char_status(ME,15) < 2) // make sure that a constant level of martyr's shield is preserved
			set_char_status(ME,15,2,1,0);

	if (attack_tick == get_current_tick()) {
			end_combat_turn();
			}

	if (get_memory_cell(1) == 1 && get_flag(4,1) == 0) { // cake and not defeated HIM (otherwise, regenarting message is being displayed constantly)
			if (get_health(6) < get_max_health(6)) {
					change_char_health(6,(get_health(6) / 20));
					print_big_str("Feeder regenerates ",(get_health(6) / 20)," of HIM's health");
					}
			}

	// Attacking state starts here
	set_state(3);
	
break;

beginstate 3; // attacking
	trgt = 0;
	while (trgt != party_size()) { // makes an electrical field around the machinery, that damages a nearby party member
			set_target(ME,trgt);
			if (char_dist_to_loc(trgt,char_loc_x(ME),char_loc_y(ME)) < 2 && target_ok() == TRUE) {
					put_boom_on_char(trgt,4,0);
					run_animation_sound(54);
					dmg = get_ran(1,25,50);
					damage_char(trgt,dmg,3);							}
	
			trgt = trgt + 1;
			}

	attack_tick = get_current_tick();
	set_state(START_STATE);
break;

beginstate TALKING_STATE;
		print_str("Are you really trying to talk to machinery?");
break;