Created
August 15, 2011 10:09
-
-
Save ecylmz/1145994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
loadfile(GetDataPath() .. "Scripts/Locale.lua")() | |
local defaultPointsPerTurn = 3 | |
local costsByWeapons = { | |
[amGrenade] = 1, [amClusterBomb] = 1, [amBazooka] = 1, [amBee] = 2, [amShotgun] = 1, [amMine] = 1, [amDEagle] = 1, [amDynamite] = 2, | |
[amFirePunch] = 1, [amWhip] = 1, [amPickHammer] = 1, [amBaseballBat] = 2, [amMortar] = 1, [amCake] = 3, [amSeduction] = 1, | |
[amWatermelon] = 3, [amHellishBomb] = 3, [amDrill] = 2, [amBallgun] = 3, [amRCPlane] = 3, [amSniperRifle] = 1, [amMolotov] = 1, | |
[amBirdy] = 2, [amBlowTorch] = 1, [amGasBomb] = 1, [amFlamethrower] = 2, [amSMine] = 2, [amSnowball] = 1, [amKamikaze] = 1 | |
} | |
local costsByAirWeapons = { | |
[amAirAttack] = 2, [amMineStrike] = 2, [amNapalm] = 2, [amDrillStrike] = 2 | |
} | |
local costsByTools = { | |
[amTeleport] = 1, [amGirder] = 2, [amSwitch] = 2, [amLowGravity] = 1, [amResurrector] = 2, [amRope] = 2, [amParachute] = 1, | |
[amJetpack] = 2, [amPortalGun] = 2, [amLaserSight] = 1, [amVampiric] = 2, [amExtraDamage] = 2, [amInvulnerable] = 1, [amExtraTime] = 1 | |
} | |
local costsByWeaponsAndTools = {} --initialised in onGameStart | |
function onGameInit() | |
GameFlags = band(bor(GameFlags, gfResetWeps), bnot(gfInfAttack + gfPerHogAmmo)) | |
end | |
function onAmmoStoreInit() | |
SetAmmo(amSkip, 9, 0, 0, 0) | |
for weapon,cost in pairs(costsByWeapons) do | |
SetAmmo(weapon, 0, 0, 0, 1) | |
end | |
for weapon,cost in pairs(costsByAirWeapons) do | |
SetAmmo(weapon, 0, 0, 0, 1) | |
end | |
for tool,cost in pairs(costsByTools) do | |
SetAmmo(tool, 0, 1, 0, 1) | |
end | |
end | |
function onGameStart() | |
if not MapHasBorder() then | |
for airWeapon,cost in pairs(costsByAirWeapons) do | |
costsByWeapons[airWeapon] = cost | |
end | |
end | |
for weapon,cost in pairs(costsByWeapons) do | |
costsByWeaponsAndTools[weapon] = cost | |
end | |
for tool,cost in pairs(costsByTools) do | |
costsByWeaponsAndTools[tool] = cost | |
end | |
ShowMission(loc("Random Weapon Basket"), loc("A game of some luck"), loc("Each turn you'll get up to three random weapons depending on their strength and team balance."), -amSkip, 0) | |
end | |
local hogs = {} --current hogs in game | |
function onGearAdd(gear) | |
if GetGearType(gear) == gtHedgehog then | |
hogs[gear] = true | |
end | |
end | |
function onGearDelete(gear) | |
if GetGearType(gear) == gtHedgehog then | |
hogs[gear] = nil | |
end | |
end | |
function onNewTurn() | |
local pointsLeft = defaultPointsPerTurn --"points" to be allocated on weapons | |
local maxCost = getMaxCost(pointsLeft) --max cost of a weapon, balanced by clan health | |
local givenWeapons = {} --weapons given for this turn | |
local weaponSet = costsByWeapons --choose first weapon without tools | |
while pointsLeft > 0 do | |
local newWeapon = getRandomWeapon(weaponSet, math.min(pointsLeft, maxCost), givenWeapons) | |
if newWeapon == nil then | |
break | |
end | |
givenWeapons[newWeapon] = true | |
AddAmmo(CurrentHedgehog, newWeapon) | |
pointsLeft = pointsLeft - weaponSet[newWeapon] | |
weaponSet = costsByWeaponsAndTools --now consider tools as well | |
end | |
end | |
function getMaxCost(maxPoints) | |
local currentClan = GetHogClan(CurrentHedgehog) | |
local currentClanHealth = 0 | |
local otherClansHealth = 0 | |
local otherClans = {} | |
local numOtherClans = 0 | |
for hog,temp in pairs(hogs) do | |
local clan = GetHogClan(hog) | |
if clan == currentClan then | |
currentClanHealth = currentClanHealth + GetHealth(hog) + 40 | |
else | |
otherClansHealth = otherClansHealth + GetHealth(hog) + 40 | |
if otherClans[clan] == nil then | |
otherClans[clan] = true | |
numOtherClans = numOtherClans + 1 | |
end | |
end | |
end | |
if currentClanHealth == 0 or numOtherClans == 0 then | |
return maxPoints | |
end | |
local ratio = math.pow(otherClansHealth / numOtherClans / currentClanHealth, 1.61803399) | |
return math.min(maxPoints, math.max(1, math.ceil(maxPoints * ratio))) | |
end | |
function getRandomWeapon(weaponSet, maxCost, givenWeapons) | |
local possibleWeapons = {} | |
for weapon,cost in pairs(weaponSet) do | |
if cost <= maxCost and givenWeapons[weapon] == nil then | |
table.insert(possibleWeapons, weapon) | |
end | |
end | |
if #possibleWeapons == 0 then | |
return nil | |
end | |
return possibleWeapons[GetRandom(#possibleWeapons) + 1] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment