--Druid Mod V0.3 modded by clp

local Infinite = 99999

local ratSetting = {
	config = {
		summonItem = "dead rat",
		rating = 1
	},
	
	creatureList = {
		{ "m_rat_e", 0.5 },
		{ "summoned_tushkano_normal", Infinite }
	}
}

local boarSetting = {
	config = {
		summonItem = "mutant_boar_leg",
		rating = 4
	},

	creatureList = {
		{ "summoned_boar_weak", 0.5 },
		{ "summoned_boar_normal", 0.8 },
		{ "summoned_boar_strong", Infinite }
	}
}

local fleshSetting = {
	config = {
		summonItem = "mutant_flesh_eye",
		rating = 3
	},
	
	creatureList = {
		{ "summoned_flesh_weak", 0.5 },
		{ "summoned_flesh_normal", 0.8 },
		{ "summoned_flesh_strong", Infinite }
	}
}

local dogSetting = {
	config = {
		summonItem = "mutant_dog_tail",
		rating = 3
	},
	
	creatureList = {
		{ "summoned_dog_weak", 0.5 },
		{ "summoned_dog_normal", 0.8 },
		{ "summoned_dog_strong", Infinite }
	}
}

local pseudodogSetting = {
	config = {
		summonItem = "mutant_psevdodog_tail",
		rating = 5
	},
	
	creatureList = {
		{ "summoned_pseudodog_weak", 0.5 },
		{ "summoned_pseudodog_normal", 0.8 },
		{ "summoned_pseudodog_strong", Infinite }
	}
}

local snorkSetting = {
	config = {
		summonItem = "mutant_snork_leg",
		rating = 5
	},
	
	creatureList = {
		{ "summoned_snork_outdoor", 0.4 },
		{ "summoned_snork_normal", 0.7 },
		{ "summoned_snork_strong", 0.9 },
		{ "summoned_aes_snork", Infinite }
	}
}

local bloodsuckerSetting = {
	config = {
		summonItem = "mutant_krovosos_jaw",
		rating = 7
	},

	creatureList = {
		{ "summoned_bloodsucker_weak", 0.4 },
		{ "summoned_bloodsucker_normal", 0.7 },
		{ "summoned_bloodsucker_strong", 0.9 },
		{ "summoned_bloodsucker_redforest", Infinite }
	}
}

local burerSetting = {
	config = {
		summonItem = "mutant_burer_hand",
		rating = 9
	},

	creatureList = {
		{ "summoned_burer_normal", 0.4 },
		{ "summoned_burer_normal_black", 0.8 },
		{ "summoned_burer_leader", Infinite }
	}
}

local zombieSetting = {
	config = {
		summonItem = "mutant_zombie_hand",
		rating = 5
	},

	creatureList = {
		{ "summoned_zombie_weak", 0.1 },
		{ "summoned_zombie_weak_1", 0.2 },
		{ "summoned_zombie_weak_2", 0.3 },
		{ "summoned_zombie_normal", 0.36666 },
		{ "summoned_zombie_normal_1", 0.43333 },
		{ "summoned_zombie_normal_2", 0.5 },
		{ "summoned_fracture_weak", 0.56666 },
		{ "summoned_fracture_weak_1", 0.63333 },
		{ "summoned_fracture_weak_2", 0.7 },
		{ "summoned_zombie_strong", 0.85 },
		{ "summoned_zombie_immortal", 0.95 },
		{ "summoned_zombie_ghost", Infinite }
	}
}

local birdSetting = {
	config = {
		summonItem = "nil",
		rating = 0
	},
	
	creatureList = {
		{ "m_crow", Infinite }
	}
}

local controllerSetting = {
	config = {
		summonItem = "mutant_controller_pda",
		rating = 6
	},

	creatureList = {
		{ "summoned_m_controller_normal", 0.4 },
		{ "summoned_m_controller_normal_fat", 0.8 },
		{ "summoned_m_controller_old", 0.9 },
		{ "summoned_m_controller_old_fat", Infinite }
	}
}

local giantSetting = {
	config = {
		summonItem = "mutant_giant_heart",
		rating = 10
	},

	creatureList = {
		{ "summoned_gigant_normal", 0.9 },
		{ "summoned_gigant_strong", Infinite }
	}
}

local catSetting = {
	config = {
		summonItem = "mutant_cat_meat",
		rating = 2
	},

	creatureList = {
		{ "summoned_cat_weak", 0.2 },
		{ "summoned_cat_weak_1", 0.4 },
		{ "summoned_cat_weak_2", 0.6 },
		{ "summoned_cat_weak_3", 0.8 },
		{ "summoned_cat_weak_4", Infinite }
	}
}

local chimeraSetting = {
	config = {
		summonItem = "mutant_chimera_eye",
		rating = 8
	},

	creatureList = {
		{ "summoned_chimera_weak", 0.7 },
		{ "summoned_chimera_normal", Infinite }
	}
}

local poltergeistSetting = {
	config = {
		summonItem = "mutant_poltergeist_soul",
		rating = 6
	},
	
	creatureList = {
		{ "summoned_m_poltergeist_normal_flame", 0.5 },
		{ "summoned_m_poltergeist_strong_flame", Infinite }
	}
}

local MonsterTypes = {
	["bird"] 		= birdSetting,
	["rat"] 		= ratSetting,
	["boar"] 		= boarSetting,
	["flesh"] 		= fleshSetting,
	["dog"] 		= dogSetting,
	["pseudodog"] 	= pseudodogSetting,
	["snork"] 		= snorkSetting,
	["bloodsucker"] = bloodsuckerSetting,
	["controller"] 	= controllerSetting,
	["giant"] 		= giantSetting,
	["zombie"] 		= zombieSetting,
	["cat"] 		= catSetting,
	["chimera"] 	= chimeraSetting,
	["burer"] 		= burerSetting,
	["poltergeist"] = poltergeistSetting
}

function message( text, time )
	news_manager.send_tip( db.actor, text, nil, nil, time )
end

function getRankCoeff()
	local rank = db.actor:character_rank()
	if ( rank < 300 ) then return 1 end
	if ( rank < 600 ) then return 1.05 end
	if ( rank < 900 ) then return 1.15 end
	return 1.3
end

function determineSubtypeRandomly( subtypes )
	if ( subtypes == nil or
		subtypes.creatureList == nil or
		#( subtypes.creatureList ) <= 0 ) then
		return nil
	end
	
	local randomNumber = math.random() * getRankCoeff()
	for i = 1, #( subtypes.creatureList ) do
		if ( randomNumber <= subtypes.creatureList[ i ][ 2 ] ) then
			return subtypes.creatureList[ i ][ 1 ]
		end
	end
	return nil
end

function calculateRandomPosition( position, radius, yOffset )
	local randomPosition = position
	randomPosition.y = randomPosition.y + yOffset
	randomPosition.x = randomPosition.x + math.random( -radius, radius )
	randomPosition.z = randomPosition.z + math.random( -radius, radius )
	return randomPosition
end

function summonMonsters( name, number, position, radius )
	local statistic = {}
	for i = 1, number do
		local monsterType = determineSubtypeRandomly( MonsterTypes[ name ] )
		if ( monsterType ~= nil ) then

			local creature = alife():create( monsterType,
				calculateRandomPosition( position, radius, 2 ),
				db.actor:level_vertex_id(),
				db.actor:game_vertex_id() )
			if ( statistic[ monsterType ] == nil ) then
				statistic[ monsterType ] = 0
			end
			statistic[ monsterType ] = statistic[ monsterType ] + 1
		end
	end
	
	for name, num in pairs( statistic ) do
		local text = tostring( num ).."  "..name.."."
		message( text, 10000 )
	end
end

function spawnMonstersDirectly( type )
	alife():create( type,
		calculateRandomPosition( db.actor:position(), 5, 2 ),
		db.actor:level_vertex_id(),
		db.actor:game_vertex_id() )
end

function addItemToInventory( type )
	alife():create( type, db.actor:position(), db.actor:level_vertex_id(), db.actor:game_vertex_id(), db.actor:id() )
end

function summonMonstersNearProtagonist( priority )
	statistic = {}
	local function isItemForSummoning( item )
		for monsterType, subtype in pairs( MonsterTypes ) do
			if( subtype == nil or subtype.config == nil ) then --for debug
				return nil
			end
			local summonItem = subtype.config.summonItem
			if ( item:section() == summonItem ) then
				if ( statistic[ monsterType ] == nil ) then
					statistic[ monsterType ] = {}
				end
				table.insert( statistic[ monsterType ], item:id() )
			end
		end
	end
	db.actor:inventory_for_each( isItemForSummoning )

	if ( priority == "all" ) then
		for monsterType, items in pairs( statistic ) do
			summonMonsters( monsterType, #items, db.actor:position(), 5 )
			for i = 1, #items do
				alife():release( alife():object( items[ i ] ), true )
			end
		end
		return
	end

	local weakMonsterType, strongMonsterType = nil, nil
	for monsterType, items in pairs( statistic ) do
		if ( weakMonsterType == nil or
			MonsterTypes[ monsterType ].config.rating < MonsterTypes[ weakMonsterType ].config.rating ) then
			weakMonsterType = monsterType
		end

		if ( strongMonsterType == nil or
			MonsterTypes[ monsterType ].config.rating > MonsterTypes[ strongMonsterType ].config.rating ) then
			strongMonsterType = monsterType
		end
	end
	
	if( weakMonsterType == nil and strongMonsterType == nil ) then
		return
	end
	
	local monsterType
	local itemRemoved
	if ( priority == "weak" ) then
		monsterType = weakMonsterType
		itemRemoved = statistic[ weakMonsterType ][ 1 ]
	end
	if ( priority == "strong" ) then
		monsterType = strongMonsterType
		itemRemoved = statistic[ strongMonsterType ][ 1 ]
	end

	summonMonsters( monsterType, 1, db.actor:position(), 5 )
	alife():release( alife():object( itemRemoved ), true )
end

-- Druid Mod -- end

-- Enviroment tool        clp

function adjustTimeScale()
	local scale = level.get_time_factor()
	if ( scale == 1 ) then
		level.set_time_factor( 1500 )
	else
		level.set_time_factor( 1 )
	end
end