how can I determine that the unit belongs to me?

how can I determine that the unit belongs to me?

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

how can I determine that the unit belongs to me?

Post by Broker »

may be like this?

uid = mouseSelection
myTeamID=Spring.GetMyTeamID()

if myTeamID==getUnitTeam(uid) then .......
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: how can I determine that the unit belongs to me?

Post by knorke »

what unit do you mean with "the unit"?

Code: Select all

if Spring.GetMyTeamID () == Spring.GetUnitTeam (unitID) then
--unit belongs to me
end
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

I'm trying to upgrade the widget Smart_select.

Code: Select all

local team = (playing and GetMyTeamID())
mouseSelection = GetUnitsInScreenRectangle(x, y, x1, y1, team)
local battle_unit = false
battle_unit variable is true if mouseSelection contains at least one combat unit.
Mylist includes all constructors, mylist_comm includes commanders.

Code: Select all

for i=1, #mouseSelection do
	uid = mouseSelection[i]
	udid = GetUnitDefID(uid)
	if (mylist[udid]==false and mylist_comm[udid]==false )then
        	battle_unit = true
		break
	end
end
The code below leaves in mouseSelection only combat units.

Code: Select all

if (battle_unit) then
	tmp = {}
	for i=1, #mouseSelection do
		uid = mouseSelection[i]
		udid = GetUnitDefID(uid)
		if (mylist[udid]== false and mylist_comm[udid]==false) then
			tmp[#tmp+1] = uid
		end
		end
	mouseSelection = tmp
end

The problem is that if my commander to be surrounded by other people's fighting units, the variable battle_unit is true, although in mouseSelection be only my commander.

So I added the code myTeamID == getUnitTeam (uid) in the condition.

Code: Select all

if (mylist[udid]==false and mylist_comm[udid]==false and myTeamID==getUnitTeam(uid) )then
result

[f=0002823] Error in Update(): [string "LuaUI\Widgets\unit_smart_select.lua"]:355: Bad unitID parameter in GetUnitTeam()
[f=0002823] Removed widget: SmartSelect
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: how can I determine that the unit belongs to me?

Post by zwzsg »

If you do: mouseSelection = GetUnitsInScreenRectangle(x, y, x1, y1, team)

With a team argument, why do you need to test again for team later?

Can you pastebin the whole widget? I suspect something wrong outside of those particular bits.
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

modified widget is very useful the widgets gui_idle_builders_new and unit_auto_group_com. control becomes like a supreme commander

if in mouseSelection are builders and military units the script selects only combat units.

Code: Select all

local battle_unit = false
				
for i=1, #mouseSelection do
	uid = mouseSelection[i]
	udid = GetUnitDefID(uid)
	if (mylist[udid]==false and mylist_comm[udid]==false ) then
		battle_unit = true
		break
	end
end
if (battle_unit) then
	tmp = {}
	for i=1, #mouseSelection do
	uid = mouseSelection[i]
	udid = GetUnitDefID(uid)
	if (mylist[udid]== false and mylist_comm[udid]==false) then
		tmp[#tmp+1] = uid
	end
end
mouseSelection = tmp
Attachments
unit_smart_select.lua
(13.61 KiB) Downloaded 38 times
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: how can I determine that the unit belongs to me?

Post by zwzsg »

You store the unit selection in widget:MousePress
You use the unit selection in widget:Update

Inbetween, a few frames may have elapsed, unit IDs may have become invalid.

Either do everyting in widget:MousePress, or everything in widget:Update.

And bring back the Spring.GetMyTeamID from widget:Initialize as well, you wouldn't want your widget to break when a player change team ingame, would you?

Or... maybe the same selection is used in many calls to widget:Update, in which case you have to find a way to trim the dead units from it.
Google_Frog
Moderator
Posts: 2464
Joined: 12 Oct 2007, 09:24

Re: how can I determine that the unit belongs to me?

Post by Google_Frog »

Why not use 'widget:SelectionChanged(selectedUnits)' ?
gajop
Moderator
Posts: 3051
Joined: 05 Aug 2009, 20:42

Re: how can I determine that the unit belongs to me?

Post by gajop »

Google_Frog wrote:Why not use 'widget:SelectionChanged(selectedUnits)' ?
iirc this isn't a default spring event and it may not exist depending on the widgethandler you're using
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

I do not understand the difference.
works:
my commander
my commander and my constructor
my commander, my constructor and my combat unit

My commander and the commander of the enemy or the constructor or building.

but does not work
My commander and another combat unit.

what is the difference between.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: how can I determine that the unit belongs to me?

Post by knorke »

How/in which game is it even possible to select enemy units?
Or do you mean in replays or as spectator?
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

All experiments were conducted in BA.


My attempt to change the logic of the widget is:

If builders and combat units get together in the selection rectangle, the final group will be involve only combat units. If there are not combat units, the final group will be involve only builders.


It can simply be explained by these screenshots:

This screenshot shows that the selected group involves only the Commander.

Image

Then the builder adds to this group.

Image

Now here comes the first combat unit and builders eliminates.
Image

There is a problem in this screenshot.
Despite the fact that only my builder is in the framework of selection rectangle, he is not active. I understand this is due to the fact that my ally bot builds attack plane in the aircraft factory which refers to the fighting units.

Image

It is impossible to choose my builder if there is ally combat unit.

The last screenshot shows that the allocation works only for my combat units despite that there are other enemy units are in the frame.
Image
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

HELP )))))
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: how can I determine that the unit belongs to me?

Post by knorke »

hm, seems like something that can not be answered without looking at the widget in detail and understanding it..
That might be some work, not sure if somebody is in the mood for that.
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

Maybe the problem is that GetTeamUnits (team) returns not only my units. units = GetTeamUnits (team). variable units contains my units and allied units as they have the same team.
SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: how can I determine that the unit belongs to me?

Post by SirMaverick »

Broker wrote:Maybe the problem is that GetTeamUnits (team) returns not only my units. units = GetTeamUnits (team). variable units contains my units and allied units as they have the same team.
Are you sure? The engine differs between teams and allies. One team is what one player "normally" controls. In a 2v2 there are 2 teams on each side (each team is controlled by one player) and each side has one alliance containing both teams.

Post your setup (script.txt) and the widget else it's very hard to help when we might talk about different things.
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

why variable batle_unit is true when other player’s combat unit is in the selection area? Despite the fact that only my builder is in the selection area.

Code: Select all

local batle_unit = false
				
for i=1, #mouseSelection do
	uid = mouseSelection[i]
	udid = GetUnitDefID(uid)
	if (mylist[udid]==false and mylist_comm[udid]==false) then 
		batle_unit = true
		break
	end
end
mouseSelection = GetUnitsInScreenRectangle(x, y, x1, y1, team)

Code: Select all

local function GetUnitsInScreenRectangle(x1, y1, x2, y2, team)
	local units
	if (team) then
		units = GetTeamUnits(team)
	else
		units = GetAllUnits()
	end
	
	local left, right = sort(x1, x2)
	local bottom, top = sort(y1, y2)

	local result = {}

	for i=1, #units do
		local uid = units[i]
		x, y, z = GetUnitPosition(uid)
		x, y = WorldToScreenCoords(x, y, z)
		if (left <= x and x <= right) and (top >= y and y >= bottom) then
			result[#result+1] = uid
		end
	end
	return result
end

Code: Select all

local playing = GetPlayerInfo(myPlayerID).spectating == false
local team = (playing and GetMyTeamID())
Attachments
_script.txt
(1.63 KiB) Downloaded 24 times
unit_smart_select.lua
(13.71 KiB) Downloaded 24 times
SirMaverick
Posts: 834
Joined: 19 May 2009, 21:10

Re: how can I determine that the unit belongs to me?

Post by SirMaverick »

In Update():

Code: Select all

local playing = GetPlayerInfo(myPlayerID).spectating == false
That doesn't work. GetPlayerInfo(myPlayerID).spectating is always nil.
As a result:
=> playing is false
=> team is false
=> GetAllUnits() is called, instead of GetTeamUnits()
=> units not from your team are checked, too
=> combat units not from your team will trigger "batle_unit = true"

Change the line to:

Code: Select all

local playing = select(3,GetPlayerInfo(myPlayerID)) == false
Broker
Posts: 156
Joined: 02 Jul 2012, 13:16

Re: how can I determine that the unit belongs to me?

Post by Broker »

it is live ! thank you.
Post Reply

Return to “Lua Scripts”