Get rid of table.maxn in all Lua code
authorWuzzy <Wuzzy2@mail.ru>
Mon, 19 Nov 2018 00:19:21 +0100
changeset 14235 8edbdd3a1fe7
parent 14234 e590fb0f2567
child 14236 74bf2d906097
Get rid of table.maxn in all Lua code
share/hedgewars/Data/Scripts/Multiplayer/Continental_supplies.lua
share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua
share/hedgewars/Data/Scripts/Tracker.lua
--- a/share/hedgewars/Data/Scripts/Multiplayer/Continental_supplies.lua	Sun Nov 18 21:01:16 2018 +0100
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Continental_supplies.lua	Mon Nov 19 00:19:21 2018 +0100
@@ -452,16 +452,16 @@
 end
 
 function SpawnRandomCrate(x,y,strength)
-	local tot=table.maxn(CS.WEAPONS_SUPPORT)+table.maxn(CS.WEAPONS_DAMAGE)
+	local tot=#CS.WEAPONS_SUPPORT + #CS.WEAPONS_DAMAGE
 	local rand=GetRandom(tot)+1
 
-	if(rand>table.maxn(CS.WEAPONS_SUPPORT))
+	if(rand > #CS.WEAPONS_SUPPORT)
 	then
-		local weapon=rand-table.maxn(CS.WEAPONS_SUPPORT)
+		local weapon = rand - #CS.WEAPONS_SUPPORT
 
 		while(wepNotValidBorder(CS.WEAPONS_DAMAGE[weapon][1])==false)
 		do
-			if(weapon>=table.maxn(CS.WEAPONS_DAMAGE))
+			if(weapon >= #CS.WEAPONS_DAMAGE)
 			then
 				weapon=0
 			end
@@ -582,7 +582,7 @@
 
 	if(rand_weaponset_power < power)
 	then
-		local numberofweapons=table.maxn(weptype)
+		local numberofweapons = #weptype
 
 		local random_weapon = math.abs(GetRandom(numberofweapons)+1)
 
@@ -909,8 +909,8 @@
 	continentinfo[4]={}
 	if(icon==1000)
 	then
-		local mid=table.maxn(CS.WEAPONS_DAMAGE)
-		local max=mid+table.maxn(CS.WEAPONS_SUPPORT)
+		local mid = #CS.WEAPONS_DAMAGE
+		local max = mid + #CS.WEAPONS_SUPPORT
 		local ic=(string.byte(string) % max)+1
 
 		if(ic>mid)
@@ -1233,7 +1233,7 @@
 
 			if(CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]==0)
 			then
-				CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=GetRandom(table.maxn(CS.CONTINENT_INFORMATION))+1
+				CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=GetRandom(#CS.CONTINENT_INFORMATION)+1
 				SetContinentWeapons()
 			end
 			local currCont=CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]
@@ -1589,13 +1589,13 @@
 	then
 		CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)] + direction
 
-		if(CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]> table.maxn(CS.CONTINENT_INFORMATION))
+		if(CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]> #CS.CONTINENT_INFORMATION)
 		then
 			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=1
 		end
 		if(CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]<=0)
 		then
-			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=table.maxn(CS.CONTINENT_INFORMATION)
+			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)] = #CS.CONTINENT_INFORMATION
 		end
 		SetContinentWeapons()
 
@@ -1687,7 +1687,7 @@
 		PlaySound(sndPlaced)
 		if(CurrentHedgehog and CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]==0)
 		then
-			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=GetRandom(table.maxn(CS.CONTINENT_INFORMATION))+1
+			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=GetRandom(#CS.CONTINENT_INFORMATION)+1
 			SetContinentWeapons()
 			HideMission()
 			ShowContinentLabel(0)
@@ -1708,7 +1708,7 @@
 	then
 		if(GetCurAmmoType()==amSwitch)
 		then
-			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=GetRandom(table.maxn(CS.CONTINENT_INFORMATION))+1
+			CS.TEAM_CONTINENT[GetHogTeamName(CurrentHedgehog)]=GetRandom(#CS.CONTINENT_INFORMATION)+1
 			SetContinentWeapons()
 			SetWeapon(amSkip)
 			PlaySound(sndMineTick)
--- a/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua	Sun Nov 18 21:01:16 2018 +0100
+++ b/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua	Mon Nov 19 00:19:21 2018 +0100
@@ -30,7 +30,7 @@
         local ammo = getTeamValue(name, "ammo")
         -- If there is no ammo, get a random one from the list and store it
         if ammo == nil then
-            ammo = weapons[GetRandom(table.maxn(weapons)) + 1]
+            ammo = weapons[GetRandom(#weapons) + 1]
             setTeamValue(name, "ammo", ammo)
         end
         -- Add the ammo for the hog
--- a/share/hedgewars/Data/Scripts/Tracker.lua	Sun Nov 18 21:01:16 2018 +0100
+++ b/share/hedgewars/Data/Scripts/Tracker.lua	Mon Nov 19 00:19:21 2018 +0100
@@ -34,7 +34,7 @@
     if trackingTeams and GetGearType(gear) == gtHedgehog then
     	hogs = teams[GetHogTeamName(gear)]
         if hogs ~= nil then
-            if table.maxn(hogs) == 1 then
+            if #hogs == 1 then
                 hogs = nil
             else
 				for k, hog in ipairs(hogs) do
@@ -89,7 +89,7 @@
     	hogs = teams[GetHogTeamName(gear)]
     	
         if hogs ~= nil then
-            if table.maxn(hogs) == 1 then
+            if #hogs == 1 then
                 hogs = nil
             else
                 for k, hog in ipairs(hogs) do
@@ -286,7 +286,7 @@
 
 -- Run a function on hogs in a clan
 function runOnHogsInClan(func, clan)
-    for i = 1, table.maxn(clans) do
+    for i = 1, ClansCount do
         if clans[i] == clan then
             for k, hog in ipairs(teams[i]) do
                 func(hog)
@@ -297,7 +297,7 @@
 
 -- Run a function on hogs in other clans
 function runOnHogsInOtherClans(func, clan)
-    for i = 1, table.maxn(clans) do
+    for i = 1, ClansCount do
         if clans[i] ~= clan then
             for k, hog in ipairs(teams[i]) do
                 func(hog)