share/hedgewars/Data/Scripts/Multiplayer/Balanced_Random_Weapon.lua
changeset 13716 6d57f5f61c09
parent 13173 4d1cf0d76eb7
child 13717 ed22eb551a75
equal deleted inserted replaced
13715:6aef91dbbd44 13716:6d57f5f61c09
       
     1 --[[
       
     2 Balanced Random Weapon
       
     3 
       
     4 Every turn, each hog gets 1-3 random weapons. Weapons are reset every turn.
       
     5 
       
     6 = CUSTOMIZATION =
       
     7 The weapon chances are chosen with the weapons scheme.
       
     8 
       
     9 The "ammo count" tab is used to set the probability level that you get
       
    10 equipped with the ammo at the start of a turn:
       
    11 
       
    12 * infinity = always get this weapon
       
    13 * 3-8 bullets = high probability (more bullets don't make it more likely)
       
    14 * 2 bullets = medium probability
       
    15 * 1 bullet = low probability
       
    16 * 0 bullets = never
       
    17 
       
    18 For utilities, the low and medium probabilities are the same.
       
    19 
       
    20 The "probabilities" tab is, as usual, for crate probabilities.
       
    21 The "ammo in crate" and "delay" tabs also work as expected.
       
    22 ]]
       
    23 
     1 HedgewarsScriptLoad("/Scripts/Locale.lua")
    24 HedgewarsScriptLoad("/Scripts/Locale.lua")
     2 HedgewarsScriptLoad("/Scripts/Tracker.lua")
    25 HedgewarsScriptLoad("/Scripts/Tracker.lua")
     3 
    26 
     4 local weapons = { amGrenade, amClusterBomb, amBazooka, amBee, amShotgun, amMine, amDEagle, amDynamite, amFirePunch, amWhip, amPickHammer, amBaseballBat, amMortar, amCake, amSeduction, amWatermelon, amHellishBomb, amDrill, amBallgun, amRCPlane, amSniperRifle, amMolotov, amBirdy, amBlowTorch, amGasBomb, amFlamethrower, amSMine, amKamikaze, amMinigun, amAirMine, amKnife }
    27 local weapons = {}
     5 
    28 local weapons_values = {}
     6 --                      G,C,B,B,S,M,D,D,F,W,P,B,M,C,S,W,H,D,B,R,S,M,B,B,G,F,S,K,M,A,K
    29 local airweapons = {}
     7 local weapons_values = {1,1,1,2,1,1,1,2,1,1,1,2,1,3,1,3,3,2,3,3,1,1,2,1,1,2,2,1,3,1,2}
    30 local airweapons_values = {}
     8 
    31 local utilities = {}
     9 local airweapons = { amAirAttack, amMineStrike, amNapalm, amDrillStrike }
    32 local utilities_values = {}
    10 
    33 
    11 --                         A,M,N,D
    34 local isUtility, isAirWeapon
    12 local airweapons_values = {2,2,2,2}
       
    13 
       
    14 local utilities = { amTeleport, amGirder, amSwitch, amLowGravity, amResurrector, amRope, amParachute, amJetpack, amPortalGun, amSnowball }
       
    15 
       
    16 --                        T,G,S,L,R,R,P,J,P,S
       
    17 local utilities_values = {1,2,2,1,2,2,1,2,2,2}
       
    18 
    35 
    19 function randomAmmo()
    36 function randomAmmo()
       
    37 --[[
       
    38 = WEAPON SELECTION ALGORITHM =
       
    39 Each turn, a team gets 3 "points". Each ammo that has been activated
       
    40 has a "cost" of 1-3 which is derived from the ammo probability specified
       
    41 from the ammo menu (see getCost).
       
    42 Utilities are forced to have a cost of 1-2.
       
    43 
       
    44 Steps:
       
    45 1. Add a random weapon to ammo and subtract cost
       
    46 2. If there's still points left:
       
    47     a. Forget any item in mind
       
    48     b. Choose a random weapon and keep it in mind (but don't add it to the ammo yet)
       
    49     c. Choose a random utility and keep it in mind (but don't add it to the ammo yet)
       
    50     d. Forget any items which are either too expensive or have already been taken by this hedgehog
       
    51     e. Randomly add one of the items which are still in mind to the hedgehog's ammo and substract cost
       
    52     f. Return to step 2
       
    53 
       
    54 If 0 points are left, the algorithm terminates.
       
    55 ]]
       
    56 
    20     local n = 3   --"points" to be allocated on weapons
    57     local n = 3   --"points" to be allocated on weapons
    21 
    58 
    22     --pick random weapon and subtract cost
    59     --pick random weapon and subtract cost
    23     local r = GetRandom(table.maxn(weapons_values)) + 1
    60     local r = GetRandom(table.maxn(weapons_values)) + 1
    24     local picked_items = {}
    61     local picked_items = {}
    94 
   131 
    95 function onGameInit()
   132 function onGameInit()
    96     DisableGameFlags(gfPerHogAmmo)
   133     DisableGameFlags(gfPerHogAmmo)
    97     EnableGameFlags(gfResetWeps)
   134     EnableGameFlags(gfResetWeps)
    98     Goals = loc("Each turn you get 1-3 random weapons")
   135     Goals = loc("Each turn you get 1-3 random weapons")
       
   136 
       
   137     isUtility = {
       
   138         [amTeleport] = true,
       
   139         [amGirder] = true,
       
   140         [amSwitch] = true,
       
   141         [amLowGravity] = true,
       
   142         [amResurrector] = true,
       
   143         [amRope] = true,
       
   144         [amParachute] = true,
       
   145         [amJetpack] = true,
       
   146         [amPortalGun] = true,
       
   147         [amRubber] = true,
       
   148         [amTardis] = true,
       
   149         [amLandGun] = true,
       
   150         [amExtraTime] = true,
       
   151         [amVampiric] = true,
       
   152         [amLaserSight] = true,
       
   153         [amExtraDamage] = true,
       
   154         [amInvulnerable] = true,
       
   155 
       
   156         -- unusual classification
       
   157         [amSnowball] = true,
       
   158     }
       
   159 
       
   160     isAirWeapon = {
       
   161         [amAirAttack] = true,
       
   162         [amMineStrike] = true,
       
   163         [amNapalm] = true,
       
   164         [amDrillStrike] = true,
       
   165         [amPiano] = true,
       
   166     }
       
   167 
       
   168 end
       
   169 
       
   170 local function getCost(ammoType, ammoCount)
       
   171     if ammoCount == 0 or ammoCount == 9 then
       
   172         return 0
       
   173     else
       
   174         local max
       
   175         if isUtility[ammoType] then
       
   176             -- Force-limit cost of utilities to 2 because utilities with
       
   177             -- a cost of 3 could never be "paid"
       
   178             max = 2
       
   179         else
       
   180             max = 3
       
   181         end
       
   182         return math.max(1, math.min(max, 4 - ammoCount))
       
   183     end
    99 end
   184 end
   100 
   185 
   101 function onGameStart()
   186 function onGameStart()
   102     trackTeams()
   187     trackTeams()
   103     if MapHasBorder() == false then
   188     if MapHasBorder() == false then
   104         for i, w in pairs(airweapons) do
   189         for a = 0, AmmoTypeMax do
   105             table.insert(weapons, w)
   190             if a ~= amNothing and a ~= amSkip then
   106         end
   191                 local ammoCount = GetAmmo(a)
   107         for i, w in pairs(airweapons_values) do
   192                 local cost = getCost(a, ammoCount)
   108             table.insert(weapons_values, w)
   193                 if cost > 0 and isAirWeapon[a] then
       
   194                     table.insert(airweapons, a)
       
   195                     table.insert(airweapons_values, cost)
       
   196                 end
       
   197             end
   109         end
   198         end
   110     end
   199     end
   111 end
   200 end
   112 
   201 
   113 function onAmmoStoreInit()
   202 function onAmmoStoreInit()
   114     SetAmmo(amSkip, 9, 0, 0, 0)
   203     SetAmmo(amSkip, 9, 0, 0, 0)
   115 
   204 
   116     SetAmmo(amExtraDamage, 0, 1, 0, 1)
   205     for a=0, AmmoTypeMax do
   117     SetAmmo(amInvulnerable, 0, 1, 0, 1)
   206         if a ~= amNothing and a ~= amSkip then
   118     SetAmmo(amExtraTime, 0, 1, 0, 1)
   207             local ammoCount, prob, delay, ammoInCrate = GetAmmo(a)
   119     SetAmmo(amLaserSight, 0, 1, 0, 1)
   208             local cost = getCost(a, ammoCount)
   120     SetAmmo(amVampiric, 0, 1, 0, 1)
   209             if (not isAirWeapon[a]) then
   121 
   210                 if cost > 0 then
   122     for i, w in pairs(utilities) do
   211                     if isUtility[a] then
   123         SetAmmo(w, 0, 0, 0, 1)
   212                         table.insert(utilities, a)
   124     end
   213                         table.insert(utilities, cost)
   125 
   214                     else
   126     for i, w in pairs(weapons) do
   215                         table.insert(weapons, a)
   127         SetAmmo(w, 0, 0, 0, 1)
   216                         table.insert(weapons_values, cost)
   128     end
   217                     end
   129 
   218                 end
   130     for i, w in pairs(airweapons) do
   219             else
   131         SetAmmo(w, 0, 0, 0, 1)
   220                 prob = 0
       
   221             end
       
   222             local realAmmoCount
       
   223             if ammoCount ~= 9 then
       
   224                 realAmmoCount = 0
       
   225             else
       
   226                 realAmmoCount = 1
       
   227             end
       
   228             SetAmmo(a, realAmmoCount, prob, delay, ammoInCrate)
       
   229         end
   132     end
   230     end
   133 end
   231 end
   134 
   232 
   135 function onNewTurn()
   233 function onNewTurn()
   136     runOnGears(assignAmmo)
   234     runOnGears(assignAmmo)