(RESOLVED) Need help for my new script “Strategy”

4 replies [Last post]
Wuzzy
Wuzzy's picture
User offline. Last seen 28 weeks 3 days ago. Offline
Joined: 2012-06-20
Posts: 1304

Hi, I was playing around with a new script, which I call “Stragegy”.
It’s a very simple one but with some significant changes:
When you use a girder, a dirt ball, a land cannon or a resurrection, your turn ends.
The idea is that you have to use these tools strategically. Either use one of these tools *or* attack, but you can’t have both anymore. The script is intended to work with all kinds of schemes.

Here’s the code of the script:

do
 HedgewarsScriptLoad("/Scripts/Locale.lua")

 --[[ “100” is a placeholder until I am able to
 query the game scheme’s retreat time.
 ]]
 local retreatTimePercentage = 100

 function onGameInit()
  Goals = loc("Girders, dirt balls, land cannons and resurrections end your turn!")
 end

 function onGearAdd(gear)
  local gt = GetGearType(gear)
  if(gt == gtSnowball or gt == gtLandGun or gt == gtGirder) then
   endTurn(3000)
  end
 end

 function onGearDelete(gear)
  if(GetGearType(gear) == gtResurrector) then
   endTurn(3000)
  end
 end

 function endTurn(baseRetreatTime)
  SetState(CurrentHedgehog,bor(GetState(CurrentHedgehog),gstAttacked))
  TurnTimeLeft = div(baseRetreatTime  * retreatTimePercentage, 100)
 end
end

How to install: Copy and paste this into a new file, call it “Strategy.lua”. Put it into %your_Hedgewars_directory%/Data/Scripts/Multiplayer.

Now this script works already fine for resurrection, dirt ball and the land cannon.
After using these weapons, you have a retreat time of 3 seconds and after that, your turn ends.

But the script fails for girders. When you place a girder, the timer also jumps to 3 seconds but you are still able to use weapons. I don’t see what is wrong here.

Also retreat time is handled subobtimal. The retreat time will always be 3 seconds; the game scheme’s retreat time percentage has no effect. That’s because I’m not able to get this number at the moment.
Nemo told me that this information is not exposed to Lua yet. Sad Smiley

Edit 1: Fixed a rounding problem in the division, thanks to nemo.

Hi, I am a Hedgewars developer. Smile

Vatten
Vatten's picture
User offline. Last seen 1 year 33 weeks ago. Offline
Joined: 2009-09-02
Posts: 48

--Hello there!

--I managed to make an ugly fix to solve that issue, well here it is:

HedgewarsScriptLoad("/Scripts/Locale.lua")

--[[
“100” is a placeholder until I am able to
query the game scheme’s retreat time.
]]

local retreatTimePercentage = 100
local GlobalGirderEndTurn=0

function onGameInit()
Goals = loc("Girders, dirt balls, land cannons and resurrections end your turn!")
end

function onGearAdd(gear)

if(GetGearType(gear) == gtSnowball)
then
endTurn(3000)
end
end

function onGearDelete(gear)

local gt = GetGearType(gear)

if(gt == gtResurrector or gt == gtLandGun or gt == gtGirder)
then
endTurn(3000)
end
end

function endTurn(baseRetreatTime)
if(GetCurAmmoType()==amGirder)
then
ParseCommand("setweap " .. string.char(amSkip))
GlobalGirderEndTurn=GetAmmoCount(CurrentHedgehog, amGirder)
else
SetState(CurrentHedgehog,bor(GetState(CurrentHedgehog),gstAttacked))
TurnTimeLeft = baseRetreatTime / 100 * retreatTimePercentage
end

end

function onGameTick20()
if(GlobalGirderEndTurn==GetAmmoCount(CurrentHedgehog, amGirder)+1)
then
endTurn(3000)
GlobalGirderEndTurn=0
elseif(GlobalGirderEndTurn>0)
then
ParseCommand("setweap " .. string.char(amGirder))
GlobalGirderEndTurn=0
end
end

--Have fun!--

Vattenania, my beloved motherland, my beloved homeland! You are always with me in my thoughts and in my heart!

Wuzzy
Wuzzy's picture
User offline. Last seen 28 weeks 3 days ago. Offline
Joined: 2012-06-20
Posts: 1304

Yeah, it’s a hack, true. But it works so far, thanks.

There is another minor problem with the script in general: The tool descriptions still say “Usage does not end round.” or something like that. This is of course not true anymore with this script.

Hi, I am a Hedgewars developer. Smile

Wuzzy
Wuzzy's picture
User offline. Last seen 28 weeks 3 days ago. Offline
Joined: 2012-06-20
Posts: 1304

Hi! I have made an updated version of this script for Hedgewars 0.9.20. Now rubber is supported, too.

Data/Scripts/Multiplayer/Strategy.lua:

do
	
	HedgewarsScriptLoad("/Scripts/Locale.lua")
	
	--[[
	“100” is a placeholder until I am able to
	query the game scheme’s retreat time.
	]]
	
	local retreatTimePercentage = 100
	local GlobalGirderEndTurn = 0
	local GlobalGirderType = amNothing
	
	function onGameInit()
		Goals = loc("Girders, rubbers, dirt balls, land cannons and resurrections end your turn!")
	end
	
	function onGearAdd(gear)
		if(GetGearType(gear) == gtSnowball) then
			endTurn(3000)
		end
	end
	
	function onGearDelete(gear)
	
		local gt = GetGearType(gear)
	
		if(gt == gtResurrector or gt == gtLandGun or gt == gtGirder) then
			endTurn(3000)
		end
	end
	
	function endTurn(baseRetreatTime)
		local at = GetCurAmmoType()
		if(at==amGirder or at == amRubber) then
			ParseCommand("setweap " .. string.char(amSkip))
			GlobalGirderType = at
			GlobalGirderEndTurn=GetAmmoCount(CurrentHedgehog, at)
		else
			SetState(CurrentHedgehog,bor(GetState(CurrentHedgehog),gstAttacked))
			TurnTimeLeft = div(baseRetreatTime * retreatTimePercentage, 100)
		end
	
	end
	
	function onGameTick20()
		if(GlobalGirderType ~= amNothing) then
			if(GlobalGirderEndTurn==GetAmmoCount(CurrentHedgehog, GlobalGirderType)+1) then
				endTurn(3000)
			elseif(GlobalGirderEndTurn>0) then
				ParseCommand("setweap " .. string.char(GlobalGirderType))
			end
			GlobalGirderEndTurn = 0
			GlobalGirderType = amNothing
		end
	end
end

Data/Scripts/Multiplayer/Strategy.cfg:

Strategy
Strategy

You can, but don’t have to, use the script together with the weapon and game schemes called “Strategy”.
http://www.hedgewars.org/node/3857#comment-29047
Together with this scheme, this script already works pretty well.

The code to find out when a girder or rubber has been placed is still very hackish.
I discovered another bug: It does not work if the amount of girders or rubbers is infinite. In this case, the turn does not end when one has been placed.

Hi, I am a Hedgewars developer. Smile

Wuzzy
Wuzzy's picture
User offline. Last seen 28 weeks 3 days ago. Offline
Joined: 2012-06-20
Posts: 1304

Okay, I guess I finally made it! I got rid of the last game-breaking bug (infinite girders bug, see above).

I managed to create a more or less working version of my Strategy script! Sadly, it only works in current experimentals (pre 0.9.21), sorry. But it is playable and without serious bugs anymore.

The code is still a bit hacky and still needs some very ugly workarounds (see comments), but at least it works now and does not seem to have serious bugs anymore. Especially the girder and rubber placement is now OK.

You can download the script here as HWP file (with description):
http://hh.unit22.org/addons/Script/Strategy_v1/

Please post any further discussion about the script here:
http://www.hedgewars.org/node/5948

Hi, I am a Hedgewars developer. Smile

User login

Copyright © 2004-2024 Hedgewars Project. All rights reserved. [ contact ]