Method for Grid Location 1 Higher

Started by battlemage64, November 05, 2017, 08:56:24 PM

Previous topic - Next topic

battlemage64

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.
I love to mod and boy am I bad at it

faltonico

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 ;)).

Alistaire

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).

battlemage64

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.
I love to mod and boy am I bad at it

CannibarRechter

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.
CR All Mods and Tools Download Link
CR Total Texture Overhaul : Gives RimWorld a Natural Feel
CR Moddable: make RimWorld more moddable.
CR CompFX: display dynamic effects over RimWorld objects

battlemage64

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.
I love to mod and boy am I bad at it

Alistaire

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).

CannibarRechter

> 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.
CR All Mods and Tools Download Link
CR Total Texture Overhaul : Gives RimWorld a Natural Feel
CR Moddable: make RimWorld more moddable.
CR CompFX: display dynamic effects over RimWorld objects