• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[General] Copying x,y values between regions

Status
Not open for further replies.

sentrywiz

S

sentrywiz

I have two regions, equally sized. In one region, player puts units in formation.
Then the units from one region needs to be copied into another.
That's not a problem, but I want the units to be copied in the formation they were placed
so it makes sense both for the player. That means in my head to copy the X and Y values
in one region per unit into the other region.

But I don't know how to do that. That's what I would like to know.
 

sentrywiz

S

sentrywiz

Oh you can have alot. 100+ is best answer

The formation size will be inside the loop when units are copied.
So max I think 200 or 300 per player, but it all depends on gameplay.
 

sentrywiz

S

sentrywiz

Compute the offset between the two regions. Then iterate through all units in the one region, add the offset between the two regions, and create a copy at that point which will be in the other region in the same formation.

Thats what I want, but the calculation of one region in relation to another is unknown.

Say one unit is at 0,0. Also lets say since I wanna copy that unit, in the other region 0,0 is 300,-150. I dont know how tp calculate that offset.
 
  1. Get the x/y of the center of region_1, that is now x_center1 and y_center1.
  2. Get the x/y of the center of region_2, that is now x_center2 and y_center2.

  3. Now calculate the difference between both x/y of both regions, x/y_offset, using x/y_center:
    x_offset = x_center2 - x_center1
    y_offset = y_center2 - y_center1

    Now you have the offset of the regions, and we can simply apply this same offset also to units.

  4. PickAllUnits inside region_1, get the unit type of picked unit and do:
    x_new = (X of Picked Unit) + x_offset
    y _new= (Y of Picked Unit) + y_offset
    type = Unit Type of Picked Unit
    Create Unit of Type (type) at x_new, y_new

    You can also copy the unit's angle of course, life, etc.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
Thats what I want, but the calculation of one region in relation to another is unknown.
If it is unknown, it is impossible. Not like it matters in such a case since nothing else will work since everything else is unknown.
Say one unit is at 0,0. Also lets say since I wanna copy that unit, in the other region 0,0 is 300,-150. I dont know how tp calculate that offset.
But this sounds like it is very much known. I am now confused...

This is assuming by region you mean GUI region which is the JASS rect type. The JASS region type is not really suitable for this since it represents a collection of map cells rather than a well defined bounded area. For JASS region one would have to explicitly track an origin when creating them.

Iterate units in source region. For each unit in source region, get their position and subtract the origin (or a consistent corner) position to work out internal region offset. Then add this offset position to the origin position of the destination region to work out where to create the unit. Create the unit. This gets repeated for all units in the source region, creating a unit in the destination region for each one.
 
Last edited:

sentrywiz

S

sentrywiz

First of all thanks to both of you. Sorry I was too "intoxicated" with work and events to reply earlier.

@IcemanBo - wow. That looks exactly like what I want. Will try it out asap and respond
@Dr Super Good - Your complexity knowledge always impresses me. Didn't know that jass rect aren't suitable, I was ready to use jass for this one, since manipulating x,y coordinates is easier in jass imo. Its not too hard with GUI either, but takes a loop or two.

Actually I am not copying units. I have buildings tbh, but should work the same.
What I need are the x,y coords, I have to use buildings to add upgrades and so.

My alternate solution is to make three regions per player, split the army into front, back and flank. Any buildings in those regions would spawn units in that particular region. But that is a rough solution, gives the illusion that its a full formation, but its not.
 

sentrywiz

S

sentrywiz

JASS rects are GUI regions and are suitable...

You said above "JASS region types are not really suitable for this since they are a collection of map cells rather than a well defined bounded area. For JASS region one would have to explicitly track an origin when creating them."

That looks like a hassle to manage instead of just making a GUI region.
 
Level 7
Joined
Apr 17, 2017
Messages
316
Here's what you can do: Get the x and point of region1 and two. Then calculate each unit's distance to the x and y of that region. To get the distance we use SquareRoot(((x1-x2*(x1-x2))+((y1-y2)*(y1-y2))). Once you get the distance you need to calculate the angle which you can do it by set angle = Atan2(y1-y2, x1-x2) Note = this is angle in radians to turn it into degrees; you do this
set angle = angle*180/bj_PI.

You have the angle and distance now what ? To implement it you need to add distance*Cos or SinBJ ( depending on the y or x. If it's x, then you use CosBJ, or vice versa) + x/y of that region.

I have created a basic map. Here is the code for lazy people just like me:
  • test xy
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • -------- to make this work in your map: just change region1x, region2x, region1y, region2y --------
      • -------- when your units are in region1, and you press escape, all of them will be teleported to region 2 with the same distance and angle preserved. --------
      • Set unitgroup = (Units in Region1 <gen>)
      • Set region1x = (X of (Center of Region1 <gen>))
      • Set region1y = (Y of (Center of Region1 <gen>))
      • Set region2x = (X of (Center of Region2 <gen>))
      • Set region2y = (Y of (Center of Region2 <gen>))
      • Unit Group - Pick every unit in unitgroup and do (Actions)
        • Loop - Actions
          • -------- to store each unit in region --------
          • Set max = (max + 1)
          • Set pickedunits[max] = (Picked unit)
      • For each (Integer cur) from 1 to max, do (Actions)
        • Loop - Actions
          • -------- getting x and y coordinates of units in the region --------
          • Custom script: set udg_unitx[udg_cur] = GetUnitX(udg_pickedunits[udg_cur])
          • Custom script: set udg_unity[udg_cur] = GetUnitY(udg_pickedunits[udg_cur])
          • -------- calculating the distance and angle for each unit --------
          • Custom script: set udg_distance[udg_cur] = SquareRoot(((udg_unitx[udg_cur]-udg_region1x)*(udg_unitx[udg_cur]-udg_region1x))+((udg_unity[udg_cur]-udg_region1y)*(udg_unity[udg_cur]-udg_region1y)))
          • Custom script: set udg_angle[udg_cur] = Atan2(udg_unity[udg_cur]-udg_region1y, udg_unitx[udg_cur]-udg_region1x)
          • Custom script: set udg_angle[udg_cur] = udg_angle[udg_cur]*180/bj_PI
          • -------- calculating the final coordinates in the region 2 --------
          • Custom script: set udg_endx[udg_cur] = udg_region2x+udg_distance[udg_cur]*CosBJ(udg_angle[udg_cur])
          • Custom script: set udg_endy[udg_cur] = udg_region2y+udg_distance[udg_cur]*SinBJ(udg_angle[udg_cur])
          • -------- moving units --------
          • Custom script: call SetUnitX(udg_pickedunits[udg_cur], udg_endx[udg_cur])
          • Custom script: call SetUnitY(udg_pickedunits[udg_cur], udg_endy[udg_cur])
          • -------- after this, leak removal and stuff --------
          • Custom script: set udg_unitx[udg_cur] = 0
          • Custom script: set udg_unity[udg_cur] = 0
          • Custom script: set udg_distance[udg_cur] = 0
          • Custom script: set udg_angle[udg_cur] = 0
          • Custom script: set udg_endx[udg_cur] =0
          • Custom script: set udg_endy[udg_cur] = 0
      • Custom script: set udg_max = 0
      • Custom script: set udg_cur = 0
      • Custom script: call DestroyGroup(udg_unitgroup)
 

Attachments

  • Region 1 to 2.w3x
    19.1 KB · Views: 20

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
You said above "JASS region types are not really suitable for this since they are a collection of map cells rather than a well defined bounded area. For JASS region one would have to explicitly track an origin when creating them."

That looks like a hassle to manage instead of just making a GUI region.
Accidently left in a plural, sorry.

"The JASS region type is not really suitable for this since it represents a collection of map cells rather than a well defined bounded area."

Here's what you can do: Get the x and point of region1 and two. Then calculate each unit's distance to the x and y of that region. To get the distance we use SquareRoot(((x1-x2*(x1-x2))+((y1-y2)*(y1-y2))). Once you get the distance you need to calculate the angle which you can do it by set angle = Atan2(y1-y2, x1-x2) Note = this is angle in radians to turn it into degrees; you do this
set angle = angle*180/bj_PI.

You have the angle and distance now what ? To implement it you need to add distance*Cos or SinBJ ( depending on the y or x. If it's x, then you use CosBJ, or vice versa) + x/y of that region.

I have created a basic map. Here is the code for lazy people just like me:
This does not need trigonometry to solve... As stated you track the relative offset in X/Y pairs.
 
Status
Not open for further replies.
Top