Ludeon Forums

RimWorld => Mods => Topic started by: battlemage64 on November 05, 2017, 08:56:24 PM

Title: Method for Grid Location 1 Higher
Post by: battlemage64 on November 05, 2017, 08:56:24 PM
Is there a C Sharp built-in RimWorld function to get and/or set the grid location with 1 higher y-value? For example, instead of this line:
map.terrainGrid.SetTerrain(base.Position, TerrainDef.Named("TerrainName"));
You could have:
map.terrainGrid.SetTerrain(new Position(base.Position.x, base.Position.y + 1), TerrainDef.Named("TerrainName"));
Is there any way to do that?

P.S. I wasn't sure whether to put this in "help" since it's just concept not a problem.
Title: Re: Method for Grid Location 1 Higher
Post by: faltonico on November 05, 2017, 11:07:32 PM
For the little i understand of the subject, the "above" layers in Rimworld are unique and have particular names, so basically you wouldn't be able to do +1, i think that if you want another one you would have to define it (but don't quote me on that ;)).
Title: Re: Method for Grid Location 1 Higher
Post by: Alistaire on November 06, 2017, 04:18:11 AM
Position is not a struct/class in RimWorld, "new Position(x, y)" isn't used. Instead, IntVec3 is used for cell coordinates.

map.terrainGrid.SetTerrain takes IntVec3 arguments. IntVec3 is structured as (x, y, z) with .y being unused/ignored by methods related to cells, such that taking the map as an XY-plane, IntVec3 is (x, 0, y) for use as cell coordinates.

Since the second argument is never extracted from the IntVec3 (methods check vec.x, vec.z) there's no use in setting it.




Now as a second interpretation, maybe you'd like to get cell (1, 0, 10) from the position (1, 0, 9). In that case:

map.terrainGrid.SetTerrain(base.Position, TerrainDef.Named("TerrainName"));

.. should turn into ..

map.terrainGrid.SetTerrain(base.Position + IntVec3.North, TerrainDef.Named("TerrainName"));

.. since IntVec3 can be added and subtracted, and IntVec3.North = (0, 0, 1).
Title: Re: Method for Grid Location 1 Higher
Post by: battlemage64 on November 06, 2017, 06:50:43 AM
Thanks! That's exactly what I needed. One last thing, can it be multiplied? Can I do "base.Position + IntVec3.North * 3" to go 3 spaces up? I'm guessing yes, but I'm not sure.
Title: Re: Method for Grid Location 1 Higher
Post by: CannibarRechter on November 06, 2017, 07:40:00 AM
Slightly off topic, if you happen to be dealing with the Altitudes enums, it is perfectly valid to cast those to int, add or substract a number to them, and cast them back to enum. Just make sure you don't exceed the range, that will throw an exception. I use this approach in CompFX, for over/under FX.
Title: Re: Method for Grid Location 1 Higher
Post by: battlemage64 on November 06, 2017, 06:08:00 PM
No, I didn't mean vertical levels, sorry for the confusion. I meant grid locations, which in a 2D game I assumed were x and y.
Title: Re: Method for Grid Location 1 Higher
Post by: Alistaire on November 07, 2017, 01:50:25 AM
Quote from: battlemage64 on November 06, 2017, 06:50:43 AM
Thanks! That's exactly what I needed. One last thing, can it be multiplied? Can I do "base.Position + IntVec3.North * 3" to go 3 spaces up? I'm guessing yes, but I'm not sure.

Yes, and similarly IntVec3.South for (0, 0, -1) == IntVec3.North * -1. If you want to transform the position by like (10,0,-5) it can be faster to create a new intvec using new IntVec3(10, 0, -5).
Title: Re: Method for Grid Location 1 Higher
Post by: CannibarRechter on November 07, 2017, 08:03:17 AM
> No, I didn't mean vertical levels, sorry for the confusion. I meant grid locations, which in a 2D game I assumed were x and y.

In Rimworld, a Vec3 is x, y, z. X is left-right on the screen, z is up-down. y is towards-way from you, the viewer. I'm talking this knowledge from my extensive working with the drawing code. I'm not sure if it directly applies to GRID LOCATIONS, however. Those might be indexed from zero from lower left, or some such. With the drawing code 0, 0, 0 is relative to whatever you are drawing.