! Melus v0.1
! Copyright 2026 Kopye
!
! Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
! the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the 
! following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the 
! following disclaimer in the documentation and/or other materials provided with the distribution.
!
! 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote 
! products derived from this software without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANT
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
! DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
! SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
! USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
!
program melus
implicit none
integer :: watchnum = 1, game = 0, creator = 0, chance = 0, attack = 0, hp = 0, maxhp = 0, whereami = 0, level = 0, &
xp = 0, spfireball = 0, fireballpower = 0,enemyhp = 0, enemymaxhp = 0, enemyattack = 0, &
enemyspawn = 0, battle = 0, turn = 0, regen = 0, startmenu = 0, skillmenu = 0 
character(len=15) :: playername, location, fireball, input, class, enemy 
fireball = 'ready'
do while(watchnum == 1)
fireballpower = attack + attack
	if (input == 'start' .and. game == 0 .and. creator == 0) then
		creator = 1
		print *, ' '
		print *, 'Enter your name.'
		print *, ' '
	else if (input == '1' .and. game == 0 .and. creator == 2) then
		class = 'Caster'
		attack = 5
		hp = 30
		maxhp = 30
		level = 1	
		location = 'village'
		game = 1
		whereami = 1
	else if (input == 'stats' .and. game == 1) then
		print *, ' '
		print *, playername
		print *, 'Class : ', class
		print *, hp, ' hp'
		print *, maxhp, ' maxhp'
		print *, attack, ' attack'
		print *, ' '
	else if (input == 'location' .and. game == 1) then
		whereami = 1
	else if (input == 'help' .and. game == 1) then
		print *, ' '
		print *, 'stats : See a list of your character''s stats.'
		print *, 'skills : See a list of your character''s skills.'
		print *, 'location : See a description of your current location.'
		print *, 'fight : Find an enemy to fight if you are in a location that has them.'
		print *, 'attack : Use a basic attack against an enemy.'
		print *, '(spell name) : Cast a spell if it is ready.'
		print *, 'quit : Close the game.'
		print *, ' '
	else if (input == 'fight' .and. game == 1 .and. location == 'village' .and. battle == 0) then
		battle = 1
	else if (input == 'attack' .and. game == 1 .and. battle == 3. .and. turn == 0 .and. class == 'Caster') then
		enemyhp = enemyhp - attack
		turn = 1
		regen = 1
		print *, ' '
		print *, '-----------------------------'
		print *, ' You throw a bolt of magic at the ', enemy
		print *, ' ' 
	else if (input == 'fireball' .and. game == 1 .and. battle == 3 .and. turn == 0 .and. spfireball == 1 .and. fireball == &
'ready') then
		enemyhp = enemyhp - fireballpower
		turn = 1
		regen = 1
		fireball = 'used'
		print *, ' '
		print *, '-----------------------------'
		print *,  ' You throw a fireball at the ', enemy
		print *, ' '
	else if (input == 'fireball' .and. game == 1 .and. battle == 3 .and. turn == 0 .and. spfireball == 1 .and. fireball == &
'used') then
		print *, ' '
		print *, 'That spell is on cooldown.'
		print *, ' '
	else if (input == 'fireball' .and. game == 1 .and. battle /= 3 .and. spfireball == 1) then
		print *, ' '
		print *, 'You can only use that spell in combat.'
		print *, ' '
	else if (input == 'tester' .and. game == 1) then
		xp = xp + 200
		hp = hp + 50
	else if (input == 'skills' .and. game == 1) then
		skillmenu = 1
	end if
	if (battle == 1 .and. game == 1 .and. enemyspawn == 0 .and. location == 'village') then
		enemy = 'coyote'
		enemyspawn = 1
	end if
	if (battle == 1 .and. game == 1 .and. enemyspawn == 1 .and. enemy == 'coyote') then
		enemyhp = 15
		enemymaxhp = 15
		enemyattack = 2
		battle = 2
		enemyspawn = 0
	end if
	if (battle == 2 .and. game == 1) then
		print *, ' '
		print *, '------------------------------'
		print *,  ' An enemy ', enemy, ' appears!'
		print *, '------------------------------'
		print *, ' '
		battle = 3
	end if
	if (hp > maxhp) then
		hp = maxhp
	end if
	if (game == 1 .and. regen == 1 .and. fireball == 'used') then
		fireball = '2'
		regen = 0
	else if (game == 1 .and. regen == 1 .and. fireball == '2') then
		fireball = '1'
		regen = 0
	else if (game == 1 .and. regen ==1 .and. fireball == '1') then
		fireball = 'ready'
		regen = 0
	end if
	if (battle == 3 .and. turn == 1 .and. game == 1 .and. enemyhp > 0 .and. enemy == 'coyote') then
		hp = hp - enemyattack
		turn = 0
		print *, 'The coyote bites you!'
		print *, enemyattack, ' Damage!'
		print *, '------------------------------'
		print *, ' '
	else if (battle == 3 .and. turn == 1 .and. game == 1 .and. enemyhp < 1 .and. enemy == 'coyote' .and. level < 4) then
		xp = xp + 40
		turn = 0
		battle = 0
		print *, 'You slew the coyote!'
		print *, 'you gain 20 xp.'
		print *, '------------------------------'
		print *, ' '
	end if
	if (enemyhp > enemymaxhp) then
		enemyhp = enemymaxhp
	end if
	if (level == 1 .and. xp >= 100 .and. game == 1 .and. class == 'Caster') then
		level = 2
		hp = hp + 5
		maxhp = maxhp + 1
		xp = 0
		print *, ' '
		print *, '------------------------------'
		print *, 'You gained a level!'
		print *, 'You are now level 2.'
		print *, 'Your hp has increased by one.'
		print *, '------------------------------'
		print *, ' '
	else if (level == 2 .and. xp >= 120 .and. game == 1 .and. class == 'Caster') then
		level = 3
		spfireball = 1
		xp = 0
		hp = hp + 7
		print *, ' '
		print *, '------------------------------'
		print *, 'You gained a level!'
		print *, 'You are now level 3.'
		print *, 'You gained a new skill: fireball'
		print *, '------------------------------'
		print *, ' '
	end if
	if (skillmenu == 1) then
		print *, ' '
	end if
	if (skillmenu == 1 .and. spfireball == 1) then
		print *, 'Fireball ', fireball
	end if
	if (skillmenu == 1) then
		print *, ' '
		skillmenu = 0
	end if
	if (whereami == 1 .and. location == 'village') then
		print *, ' '
		print *, '------------------------------'
		print *, 'You are in a rural unnammed village.  The small village is made up of'
		print *, 'a few houses, a general store, and some produce stalls.'
		print *, '------------------------------'
		print *, ' '
		whereami = 0
	end if
	if (game == 0 .and. creator == 0 .and. startmenu == 0) then
	startmenu = 1
		print *, ' '
		print *, '--------------------'
		print *, '|                  |'
		print *, '|      MELUS       |'
		print *, '|                  |'
		print *, '--------------------'
		print *, ' '
		print *, 'Type start to begin.'
		print *, 'Type help to see a list of commands once you''ve made your character.'
		print *, ' '
	else if (game == 0 .and. creator == 2) then
		print *, ' '
		print *, 'Choose your class.'
		print *, ' '
		print *, '1.  Caster'
		print *, ' '
		print *, 'The caster class gives multitudes of spells to pick from.'
		print *, 'Some of those include summoning, healing, and damaging spells.'
		print *, 'This class however has lower attack and hp'
		print *, ' '
		print *, 'Type the number of the class you want to pick'
		print *, ' '
	end if
	read (*,*) input
	if (game == 0 .and. creator == 1) then
		playername = input
		creator = 2
	end if	
	if (input == 'quit') then
		watchnum = 0
	end if
end do
end program melus
