-- mf_help
--- Code
-- Author: Astrobe
-- License: LGLP 2.1+
--- Media
-- Author: Robin Lamb
-- License: CC0
-- Part of https://opengameart.org/content/ui-sound-effects-button-clicks-user-feedback-notifications

local storage=minetest.get_mod_storage()

local function givetip(name)
	if not minetest.get_player_by_name(name) then return end
	local tips=
	{
		"Coal in stone can catch fire and explode.",
		"When you get your crafting guide, make a copy before you make your crafting sign.",
		"Bored at night? Keep the crafting and furnace work for night time.",
		"The compass can detect nearby ores.",
		"Get the news with /news.",
		"Keep track of how many lamps you have. You don't want to forget one somewhere.",
		"Catch butterflies with a net and place them in a hive to get honey. Place flowers next to the hive.",
		"You can catch fish with a net.",
		'Check who is online with /status.',
		"Storms bring bad news. Take cover.",
		"Danger often comes from above. Look *up*.",
		"One day is one hour.",
		"Beware: seawater floods.", 
		"Whales appear during the day, near snow above 60 meters.",
		"Different woods have different burning times.",
		"Most mobs regenerate HP over time. Finish them off quickly and be careful when you chase one.",
		"Know when to run away.",
		"The rock crab, the ice spider, the red and green giant wasps can drop colored Mese.",
		"The giant beetle and the scorpionoid drop seeds.",
		"Don't spam the sword. Wait until it comes back to rest position for another hit.",
		"The hammer is slow but has a great stun effect.",
		"You can set your home beacon by hitting a beacon with a green mese crystal. The HUD X mark will be updated accordingly.",
		"Beware not to hit your allies.",
		"There are two clans: men and women. Players can only use the beacons and void chests of their clan.",
		"Beginners: don't assume, check or ask and listen. Regulars: just because you got yourself good equipment doesn't mean you are invincible.",
		"Placing loot boxes is the only way to get RP.",
		"You can check if a block is under your protection with the screwdriver.",
		"You can open a loot box for 0 RP with a green mese crystal fragment.",
		"You can pick up most blocks (including ruins walls) with an obsidian shard.",
		"To mount/unmount whales, use right-click. To change its direction while riding, target the whale and left-click.",
		"You can right-click a block with the crafting guide to see its description.",
		"Keys can be labeled by punching a sign.",
		"Use the radar mode of the minimap to spot dungeons and caves.",
		"The light, furniture and protection attributes can be searched in the crafting guide",
	}
	minetest.sound_play("ding", {gain=1, to_player=name}, true)
	minetest.chat_send_player(name, minetest.colorize("lightgreen",tips[math.random(1,#tips)]))
	minetest.after(3600, givetip, name)
end

local help_msg=
{
	"#1 priority: stay alive. Collect food for healing. Look for apples, mushrooms or blueberries.\nStay in green grass areas and just run away if you are attacked.\nHide (e.g. in a ruin) if you need to stop to do something.",
	"Look for lootboxes (see inventory screen) in ruins. Dig them until you get 2 crafting tables and 6 bronze ingots.",
	"Note that eating refills your mana bar first, and then your HP bar.",
	"Digging lootboxes costs Reputation Points (RP), which are used to level up. You start with 30. You lose 5 RP if you die."..
	"You won't be able to dig lootboxes if you have no RP. One can earn RPs by placing lootboxes, but we are not here yet.",
	"Collect those red luminous mushrooms - no cheap torches in this game.",
	"With your first 6 bronze ingots, make a pick and an axe, which can be used as a weapon.\nFor \"sticks\", use bush trunks (do collect the leaves, they'll be very useful).",
	"The axe is a second-rate weapon. It is slow, so if your attacks miss often, make sure it has returned to rest position before attacking again.",
	"For better melee weapons, there are swords and spears. Spears have the same durability as swords for the cost, deal less damage, but are faster and have longer reach.",
	"Nights are not more dangerous here. The big moon even gives good light when the sky is clear.",
	"Look for ores and coal. They can be found at any altitude. Good spots are cliffs.",
	"Ores can be found in red and grey stones.They drop a random metal lump.",
	"With a weapon, you can venture in savanna or pine forests, but still be careful. Often look around and *up* because attacks can come from the sky too.",
	"The compass, that you may have gotten from lootboxes, has a metal ore detection feature.\nYou can try it near one you have already spotted to test it.",
	"Look for cobble in ruins; you will one block to make a furnace.",
	"Note that you only need 3 copper and 1 tin to make bronze",
	"Making ingots from ores takes longer and more fuel than in most other games. Some trees are better fuels than other, checkout the descriptions in the crafting guide.",
	"Try to get 9 coal lumps to make a coal block, that's the best fuel.",
	"Start to look for a place where to build your town. The ideal place would be ruins in good shape near apple trees and water (for farming).\n",
	"Also look for coral reefs, as they are good fishing spots. Fish is important for trade.",
	"Ruins or surface dungeons are cheap first homes because repairing them costs less material than building from the ground up. Dungeons might already be \"occupied\", though.",
	"Most ruin blocks cannot be dug with normal tools, you'll need obsidian for that; but we are not here yet.",
	"If your future town is a bit far from the beacon, you can make a Scroll Of Return to set your second waypoint here.",
	"About lighting: the basic light source are red mushrooms. But you can use them to craft small lamps, which are more convenient.",
	"Start a mushroom farm. Mushrooms grow in nearly complete darkness, on tree logs.",
	"A mushroom farm will secure a reliable source of food (and/or red mushrooms) and allow you to make sandwiches, which can be traded with NPC traders for lootboxes.",
	"When you have all the basic bronze tools and a weapon, you can go hunting for seeds and mese.",
	"Giant beetles drop wheat seeds, while scorpionoids drop cotton seeds.\nPrioritize cotton for now.",
	"Drops are not garanteed; sometimes you get nothing.",
	"Beetles lurk in pine woods and apple tree forests; scorpionoids lurk in savannas and jungles.",
	"Your first 9 mese shards should be used to make a mese wand, a ranged weapon that will make things a bit easier.",
	"Plants are sensitive to climate. They take more time to grow when outside of their usual biome.",
	"Thunderstorms can bring more trouble than you need, so if you see lightnings, look for a shelter.",
	"We are in spring now, so the rainy winter is behind us; the weather should get better until summer.",
	"When you have a bunch of seeds, start a farming plot.\nPlace dirt in a very well lit location next to water, and then work it with a right-click of the shovel in order to be able to plant.",
	"Harvesting does not give back seeds. So keep hunting while things grow.",
	"NPC traders accept trades that are the only way to get important items you cannot craft. Checkout the Help tab.",
	"In order to attract traders, you need a properly lit house, with a roof, furniture, and cloth. Cloth is made from cotton.",
	"You can for instance put cloth blocks as 'carpets' right next to a bookshelf with a lamp. The \"furniture\" attribute is searchable in the crafting guide.",
	"Traders don't like crowds, they won't appear if you or another trader are near (~10 blocks).",
	"While waiting for traders to come, go fishing. Fish is the second ingredient (with bread) to make tradable sandwishes.",
	"The alternative is candy apples, for which you need to make beehives to get honey. In both case you'll need a net to catch fishes or butterflies.",
	"Yet another option to get honey is to hunt giant yellow wasps.",
	"Speaking of wasps, you should try to hunt the green ones too, for the green mese they drop.",
	"One use of green mese is to set your \"home\" beacon. You can use the initial beacon for that.",
	"Another use of green mese is to open lootboxes without losing RPs.",
	"So if you place a lootbox and open it with a green mese shard, you get 1 RP and the content of the lootbox.",
	"Accumulate RPs until you have 30-40, so you can level up (this will take a while).",
	"Leveling up itself costs only 20, but fatal mistakes will cost you 10 RP after leveling up.",
	"The upside of leveling up: you can use armor and the focus wand.",
	"It may be wise to make a copy of the blue book. It is not copyrighted, and it is a unique item.",
	"Fruits don't regrow forever so keep farming wheat and mushrooms to secure food.",
	"You might have noticed luminuous purple blocks: it is \"Melding\"; it will expand and become an issue eventually if you don't take action.",
	"A Focus is dedicated to that, but for now, you can just build around a cluster if it starts to expand.",
	"Protection blocks are more intended for multiplayer online games. However, they do affect hostile mobs and melding.",
	"Checkout the Help tab in your inventory screen for more tips.\nGood luck and have fun!",
}

-- A pass to colorize our messages
for i,v in ipairs(help_msg) do
	help_msg[i]=minetest.colorize("lightgreen", help_msg[i])
end

function give_help(name)	
	if not minetest.get_player_by_name(name) then return end
	local n=math.max(storage:get_int(name),1)
	if not help_msg[n] then  -- done with help.
		givetip(name)
		return
	end
	minetest.sound_play("ding", {gain=1, to_player=name}, true)
	minetest.chat_send_player(name, help_msg[n])
	storage:set_int(name, n+1)
	minetest.after(120, give_help, name)
end

local function remind_guest(name)
	if not minetest.get_player_by_name(name) then return end
	if minetest.check_player_privs(name, "interact") then
		givetip(name)
		return
	end
	minetest.chat_send_player(name, minetest.colorize("lightgreen", "YOU ARE IN GUEST MODE. Players can invite you into the game, try to chat with them"))
	minetest.sound_play("ding", {gain=1, to_player=name}, true)
	minetest.after(120, remind_guest, name)
end

minetest.register_on_joinplayer(function(player)
	local name=player:get_player_name()

	if minetest.check_player_privs(name, "privs") then
		minetest.after(10, give_help, name)
	elseif not minetest.check_player_privs(name, "interact") then
		minetest.after(10, remind_guest, name)
	else
		minetest.after(10, givetip, name)
	end
end)

local no=function() return 0 end
local nocraft=minetest.create_detached_inventory("nocraft", {allow_move=no, allow_put=no, allow_take=no})
local nocraft_items=
{
	"default:bush_stem", "farming:seed_wheat", "farming:seed_cotton", "dungeon_crates:crate", "compass:compass", "mf_airtank:airtank", "", "",
	"default:mese_crystal_fragment", "more_mese:mese_crystal_fragment_green", "more_mese:mese_crystal_fragment_grey", "more_mese:mese_crystal_fragment_cyan", "more_mese:mese_crystal_fragment_red", "more_mese:mese_crystal_fragment_orange", "", "",
	"beacon:beacon", "anchorstone:anchorstone", "default:skeleton_key", "3d_armor:helmet_greater","3d_armor:chestplate_greater","3d_armor:leggings_greater","3d_armor:boots_greater","",
	"default:obsidian_shard", "default:obsidian", "a_trees:sapling", "","","","",

}

nocraft:set_size("main", #nocraft_items)
nocraft:set_list("main", nocraft_items)
sfinv.register_page("help", {
	title="HELP",
	get= function(self, player, context)
		local content="label[0,0;Notable items that cannot be crafted:]list[detached:nocraft;main;0,1;8,8;]"
                .."textarea[0.3,5;8,4;;;"..
	minetest.formspec_escape(
	"\nMain differences from default Minetest gameplay:\n\n"..
	"- You need a crafting table. Crafting recipes are often different.\n"..
	"- Eating refills your mana bar first, and then your HP bar.\n"..
	"- Trees must be cut down from top with an axe.\n"..
	"- It take a lot more time (and fuel) to melt things in the furnace.\n"..
	"- Seawater floods mostly like in real life. You cannot build in it.\n"..
	"- Dirt, snow and gravel nodes fall.\n"..
	"- Days are longer, plants take a lot more time to grow.\n"..
	"- Fruits regrow a limited number of times.\n"..
	"- bookshelves are not storage.\n"..
	"- Some blocks can only be removed with obsidian shards.\n"..

	"\nReputation points (RP):\n\n"..
	"- You lose RPs if you die\n"..
	"- Losing all your RPs reverts you to guest status; you don't lose your level, just the 'interact' privilege. Singleplayer cannot lose 'interact', but won't be able to dig loot boxes\n"..
	"- You can give/check RPs with the /rep command\n"..
	"- RPs allow you to level up\n"..

	"\nLevels:\n\n"..
	"- Guest: cannot interact with the world and mobs ignore the player. Another player must give them at least 1 RP in order to actually play.\n"..
	"- Beginner: keep their inventory and lose only 5 RP (later: 10) on death. Cannot use armor and some wands. Level up by choosing a Focus (see /focus).\n"..
	"- Regular: Inventory is stored into 'bones' on death. Can use armor. Level up by choosing a Talent (see /talent).\n"..
	"- Advanced: Inventory drops on the floor on death. Benefits from talent's passive.\n"..
	"The nature of the gift is not reaveled to the player. It can be one of: less wear on fishnet, less wear on mese wand, or better levitation uplift.\n"..

	"\nProtection:\n\n"..
	"Protective blocks protect themselves and their neighbors against digging (by others), fire and blasts.\n"..

	"\nArmor piece bonus:\n\n"..
	"- Helmet: mana regeneration\n"..
	"- Chestplate: mana pool\n"..
	"- Leggings: jump height\n"..
	"- Boots: walk speed\n"..
	"There is a chance that the armor absorbs all of the incoming damage but 1 HP. In this case, all armor elements take damage. Each armor element you wear adds 20% chance.\n"..
	"All armor elements have a weight, which reduces jump height and walk speed (leggings and boots bonus compensate more than their own weight).\n"..

	"\nNPC trader:\n\n"..
	"Traders spawn on any cloth block near furniture (e.g. a bookshelf).\n"..

	"\nFarming:\n\n"..
	"- Use shovels as hoes by right clicking dirt (not grass) with them.\n"..
	"- Dirt must be under sun, with water next to it.\n"..
	"- Harvesting cotton or wheat does not give seeds.\n"..

	"\nLabeling:\n\n"..
	"Keys can be labeled with a custom text by punching a sign.\n"..

	"\nClans:\n\n"..
	"- Players belong either to the Men or Women clan, according to their skin (see /skin)"..
	"- Players of one clan cannot use beacons, D-eacons, anvils, windmills or Void Chests of the other clan.\n"..

	"\nAdditional chat commands:\n\n"..
	"/rep\n"..
	"/chat\n"..
	"/focus\n"..
	"/talent\n"..
	""
	).. "]"
		return sfinv.make_formspec(player, context, content, false, nil)
	end
})



