FLAG_ON = 1;
FLAG_OFF = 0;

PLAYER_RIGHT = 1;
PLAYER_LEFT = 0;

FLAG_SAVE_HELP_HEARD = 400;
FLAG_BURGER_HELP_HEARD = 401;
FLAG_LIFETOAST_HELP_HEARD = 402;
FLAG_MISSLEUP_HELP_HEARD = 403;

HARD_FLAG_HIGH_JUMP = 256
HARD_FLAG_BUDDY = 257

WEAPON_NONE		= 0;
WEAPON_STUNGUN	= 1;
WEAPON_STUNGUN2	= 2;
WEAPON_MACHINEGUN	= 3;
WEAPON_SHOTGUN		= 4;
WEAPON_BAZOOKA		= 5;
WEAPON_CHOMPER		= 6;
WEAPON_JOHNNYGUN	= 8;

ANIM_STAND_LEFT		= { 0, 0, 2, 50, 1 };
ANIM_STAND_RIGHT	= { 1, 0, 2, 50, 1 };
ANIM_WALK_LEFT	= { 2, 0, 4, 5, 1 };
ANIM_WALK_RIGHT	= { 3, 0, 4, 5, 1 };

ANIM_JUMP_LEFT	= { 2, 0, 1, 50, 1 };
ANIM_JUMP_RIGHT	= { 3, 0, 1, 50, 1 };

CHAN_PLAYER_MOVEMENT	= 1;
CHAN_WEAPONS			= 2;
CHAN_SCRIPTED			= 3;
CHAN_TEXTBOX			= 4;
CHAN_PLAYER_STATUS		= 5;
CHAN_EXPLOSIONS			= 6;
CHAN_WEAPON_IMPACTS		= 7;
CHAN_ENEMY_HURT			= 8;
CHAN_ENEMY_MOVEMENT		= 9;
CHAN_PLAYER_MOVEMENT_2	= 10;
CHAN_DOOR				= 11;
CHAN_ENEMY_ATTACKS		= 12;
CHAN_MISSLE				= 13;
CHAN_PLAYER_WATER		= 14;
CHAN_ENEMY_MOVEMENT_2	= 15;
CHAN_BUDDY_MOVEMENT		= 16;
CHAN_BUDDY_MOVEMENT_2	= 17;
CHAN_BUDDY_WEAPON		= 18;
CHAN_SPARK_MOVE			= 19;

function PauseScript()
	coroutine.yield(script);
end

function explode(x, y)
	DoCall("Explode", x, y);
end

function save()
	DoCall("Save");
end

function load()
	DoCall("Load");
end

function restartgame()
	DoCall("Restart");
end

function speaker(face)
	DoCall("Speaker", face);
end

function killenemies()
	DoCall("KillEnemies", image);
end

function killbullets()
	DoCall("KillBullets", image);
end

function fadein()
	DoCall("FadeIn");
	wait(10);
end

function fadeout()
	DoCall("FadeOut");
	wait(10);
end

function fadeininstant()
	DoCall("FadeInInstant");
end

function fadeoutinstant()
	DoCall("FadeOutInstant");
end

function setpos(x, y)
	DoCall("SetPos", x, y);
end

function maxmissiles(increase_amt)
	DoCall("MaxMissiles", increase_amt);
end

function fillmissiles()
	DoCall("FillMissiles");
end

function doscript(file)
	dofile(modfile("data/" .. file .. ".lua" ));
	runscript();
end

function graphic(image)
	DoCall("Graphic", image);
end

function create(name, x, y, param1, param2)
	DoCall("Create", name, x, y, param1, param2);
end

function createnpc(name, x, y)
	DoCall("CreateNPC", name, x, y);
end

function createnpcnamed(new_name, name, x, y)
	DoCall("CreateNPCNamed", name, new_name, x, y);
end

function destroy(name)
	DoCall("Destroy", name);
end

function sound(file, chan)
	DoCall("Sound", file, chan);
end

function loopsound(file, chan, numtimes)
	DoCall("LoopSound", file, chan, numtimes);
end

function createnpc_at_player(name)
	DoCall("CreateNPCatplayer", name);
end

function renamenpc(name, newName)
	DoCall("Rename", name, newName);
end

function setdir(dir)
	DoCall("SetDir", dir);
end

function setanim(name, row, start, numframes, delay, loop)
	DoCall("SetAnim", name, row, start, numframes, delay, loop);
end

function setanimtemplate(name, anim_array)
	DoCall("SetAnim", name, anim_array[1], anim_array[2], anim_array[3], anim_array[4], anim_array[5]);
end

function jump(name, val)
	DoCall("Jump", name, val);
end

function setgravity(name, val)
	DoCall("SetGravity", name, val);
end

function setuse(name, script)
	DoCall("SetUse", name, script);
end

function ResumeScript()
	DoCall("ResumeScript")
end

function movex(name, x)
	movexspeed(name, x, 1);
end

function movexspeed(name, x, speed)

	DoCall("LockControls", 1);
	DoCall("LockEnemies", 1);
	DoCall("LockBullets", 1);
	DoCall("LockControls", 1);
	DoCall("DisableInventory", 1);
	DoCall("RefreshGroups");
	DoCall("MoveX", name, x, speed);
	PauseScript();
end

function EndNPCMove()
	DoCall("LockControls", 0);
	DoCall("LockEnemies", 0);
	DoCall("LockBullets", 0);
	DoCall("LockControls", 0);
	DoCall("DisableInventory", 0);
	DoCall("RefreshGroups");
	ResumeScript();
end

function fallto(name, y)
	DoCall("LockControls", 1);
	DoCall("LockEnemies", 1);
	DoCall("LockBullets", 1);
	DoCall("LockControls", 1);
	DoCall("DisableInventory", 1);
	DoCall("RefreshGroups");
	DoCall("FallTo", name, y);
	PauseScript();
end

function toggleflag(num)
	DoCall("SetFlag", num, (1+DoCall("GetFlag", num))%2);
end

function setflag(num, val)
	DoCall("SetFlag", num, val);
end

function getflag(num)
	return tonumber(DoCall("GetFlag", num));
end

function getitem(num)
	return tonumber(DoCall("GetItem", num));
end

function getweapon(num)
	return tonumber(DoCall("GetWeapon", num));
end

function getplayerx()
	return tonumber(DoCall("GetPlayerX"));
end

function getplayery()
	return tonumber(DoCall("GetPlayerY"));
end

function getentx(name)
	return tonumber(DoCall("GetEntX", name));
end

function getenty(name)
	return tonumber(DoCall("GetEntY", name));
end

function setplayerx(x)
	DoCall("SetPlayerX", x);
end

function setplayery(y)
	DoCall("SetPlayerY", y);
end

function setentx(name, x)
	DoCall("SetEntX", name, x);
end

function setenty(name, y)
	DoCall("SetEntY", name, y);
end

function autotext(text_time)
	DoCall("AutoText", text_time);
end

function restorewindow()
	DoCall("RestoreWindow");
end

function storewindow()
	DoCall("StoreWindow");
end

function hideplayer()
	DoCall("HidePlayer");
end

function showplayer()
	DoCall("ShowPlayer");
end

function changelevel(level)
	DoCall("ChangeLevel", level);
end

function WaitEnd()
	DoCall("LockEnemies",		0);
	--DoCall("LockNPCs",			0);
	DoCall("LockBullets",		0);
	DoCall("LockControls",		0);
	DoCall("DisableInventory",	0);
	--DoCall("DisableTextBox", 	0);
	DoCall("LockEnemies", 0);

	DoCall("RefreshGroups");
	
	ResumeScript();
end

function wait(wait_time)
	DoCall("Wait", wait_time);
	
	DoCall("LockEnemies", 1);
	--DoCall("LockNPCs", 0);
	DoCall("LockBullets", 1);
	DoCall("LockControls", 1);
	DoCall("DisableInventory", 1);
	--DoCall("DisableTextBox", 1);
	DoCall("LockEnemies", 1);
	
	DoCall("RefreshGroups");

	PauseScript();
end

function say(text)
	-- Format the text
	-- Then for each line in the text, do the following stuff.
	
	DoCall("LockPlayer", 1);
	DoCall("LockEnemies", 1);
	DoCall("LockNPCs", 1);
	DoCall("LockBullets", 1);
	DoCall("LockParticles", 1);
	DoCall("DisableInventory", 1);
	
	DoCall("MessageBox", text)
	
	DoCall("RefreshGroups");

	PauseScript();
end

function ask(text)
	DoCall("YesNo");
	say(text);
	return (tonumber(DoCall("GetResponse")) == 1);
end

function timedsay(text, time_to_display)
	DoCall("TimedSay", time_to_display);
	say(text);
end


function heal(amt)
	DoCall("Heal", amt);
end

function mus(music)
	DoCall("Mus", music);
end

function musfadeout(fade_time)
	DoCall("MusFadeout", fade_time);
end

function musvol(vol)
	DoCall("MusVol", vol);
end

function musres()
	DoCall("MusRes");
end

function giveitem(num)
	DoCall("GiveItem", num);
end

function takeitem(num)
	DoCall("TakeItem", num);
end

function giveweapon(num)
	DoCall("GiveWeapon", num);
end

function takeweapon(num)
	DoCall("TakeWeapon", num);
end

function equip(num)
	DoCall("Equip", num);
end

function thunder()
	DoCall("Thunder");
end

function forcecam(x, y)
	DoCall("ForceCam", x, y);
end

function camera(x, y)
	DoCall("Camera", x, y);
end

function restartcamera()
	DoCall("RestartCamera");
end

function rumble(dur, amp)
	DoCall("Rumble", dur, amp);
end

function CloseMessageBox()
	DoCall("LockPlayer", 0);
	DoCall("LockEnemies", 0);
	DoCall("LockNPCs", 0);
	DoCall("LockBullets", 0);
	DoCall("LockParticles", 0);
	DoCall("DisableInventory", 0);

	DoCall("RefreshGroups");

	ResumeScript();
end

function runscript()
end

function walktox(name, x)

	local walkanim	= ANIM_WALK_LEFT;
	local standanim	= ANIM_STAND_LEFT;
	if (getentx(name) < x * 16) then
		walkanim	= ANIM_WALK_RIGHT;
		standanim	= ANIM_STAND_RIGHT;
	end
	
	setanimtemplate(name, walkanim);
	movex(name, x);
	setanimtemplate(name, standanim);
end

function walktoxbackwards(name, x)

	local walkanim	= ANIM_WALK_LEFT;
	local standanim	= ANIM_STAND_LEFT;
	if (getentx(name) >= x * 16) then
		walkanim	= ANIM_WALK_RIGHT;
		standanim	= ANIM_STAND_RIGHT;
	end
	
	setanimtemplate(name, walkanim);
	movex(name, x);
	setanimtemplate(name, standanim);
end

function walktoxspeed(name, x, speed)

	local walkanim	= ANIM_WALK_LEFT;
	local standanim	= ANIM_STAND_LEFT;
	if (getentx(name) < x * 16) then
		walkanim	= ANIM_WALK_RIGHT;
		standanim	= ANIM_STAND_RIGHT;
	end
	
	setanimtemplate(name, walkanim);
	movexspeed(name, x, speed);
	setanimtemplate(name, standanim);
end

function walktoxbackwardsspeed(name, x, speed)

	local walkanim	= ANIM_WALK_LEFT;
	local standanim	= ANIM_STAND_LEFT;
	if (getentx(name) >= x * 16) then
		walkanim	= ANIM_WALK_RIGHT;
		standanim	= ANIM_STAND_RIGHT;
	end
	
	setanimtemplate(name, walkanim);
	movexspeed(name, x, speed);
	setanimtemplate(name, standanim);
end

script = coroutine.create(runscript);
coroutine.resume(script);