Difference between revisions of "Modding:Coord"

From DoomRL Wiki

Jump to: navigation, search
(Created page)
 
m (drltable used)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
{{994 modding}}
 
 
 
Coords (short for coordinates) are used to describe positions on the map. They are also occassionally used to indicate relative positions.
 
Coords (short for coordinates) are used to describe positions on the map. They are also occassionally used to indicate relative positions.
  
 
== Properties ==
 
== Properties ==
 +
Although coords are technically userdata, they can be indexed like normal tables.
 +
 +
{{drltable|Coord|3|{{Table3Col
 +
  |es=background: #333;
 +
  |c1=font-weight:bold; text-align:right; vertical-align:top; padding:0px 3px;
 +
  |c2=vertical-align:top; padding:0px 2px;
 +
  |c3=padding:0px 2px;
 +
  |'''Integer'''|x|This is the x-coordinate of the coord. In DoomRL, x increases going from left to right, starting at 1 on the left edge of the map.
 +
  |'''Integer'''|y|This is the y-coordinate of the coord. In DoomRL, y increases going from top to bottom, starting at 1 on the upper edge of the map.
 +
  |}}
 +
}}
  
Although coords are technically userdata, they can be indexed like normal tables.
 
{|style="border: 2px solid darkred; border-spacing: 0; font-size: 90%; margin: 0.25em 0.5em;"
 
|colspan=3 style="background: darkred; color: yellow; font-size: 120%; text-align: center"|'''Coord'''
 
|-
 
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''Integer'''
 
|style="vertical-align:top; padding-right: 2ex;"|x
 
|This is the x-coordinate of the coord. In DoomRL, x increases going from left to right, starting at 1 on the left edge of the map.
 
|-style="background: #333;"
 
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''Integer'''
 
|style="vertical-align:top; padding-right: 2ex;"|y
 
|This is the y-coordinate of the coord. In DoomRL, y increases going from top to bottom, starting at 1 on the upper edge of the map.
 
|}
 
 
== API ==
 
== API ==
  
 
+
{{drltable|Coord Interface|2|{{Table2Col
{|style="border: 2px solid darkred; border-spacing: 0; font-size: 90%; margin: 0.25em 0.5em;"
+
  |es=background: #333;
|colspan=2 style="background: darkred; color: yellow; font-size: 120%; text-align: center"|'''Coord Interface'''
+
  |c1=font-weight:bold; text-align:right; vertical-align:top; padding:0px 2px;
|-
+
  |c2=padding:0px 2px;
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_new|new]]('''integer''' x, '''integer''' y)  
|coord.[[#coord_new|new]]('''integer''' x, '''integer''' y)  
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_unm|unm]]([[Modding:Coord|Coord]] c)  
|-style="background: #333;"
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_add|add]]([[Modding:Coord|Coord]] lhs, [[Modding:Coord|Coord]] rhs)  
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_sub|sub]]([[Modding:Coord|Coord]] lhs, [[Modding:Coord|Coord]] rhs)  
|coord.[[#coord_unm|unm]]([[Modding:Coord|Coord]] c)  
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_mul|mul]]([[Modding:Coord|Coord]] or '''integer''' lhs, [[Modding:Coord|Coord]] or '''integer''' rhs)  
|-
+
  |'''boolean'''|coord.[[#coord_eq|eq]]([[Modding:Coord|Coord]] lhs, [[Modding:Coord|Coord]], rhs)  
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_abs|abs]]([[Modding:Coord|Coord]] c)  
|coord.[[#coord_add|add]]([[Modding:Coord|Coord]] lhs, [[Modding:Coord|Coord]] rhs)  
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_sign|sign]]([[Modding:Coord|Coord]] c)  
|-style="background: #333;"
+
  |'''integer'''|coord.[[#coord_distance|distance]]([[Modding:Coord|Coord]] c1, [[Modding:Coord|Coord]] c2)  
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
  |'''number'''|coord.[[#coord_real_distance|real_distance]]([[Modding:Coord|Coord]] c1, [[Modding:Coord|Coord]] c2)  
|coord.[[#coord_sub|sub]]([[Modding:Coord|Coord]] lhs, [[Modding:Coord|Coord]] rhs)  
+
  |[[Modding:Coord|Coord]]|coord.[[#coord_random|random]]([[Modding:Coord|Coord]] c1, [[Modding:Coord|Coord]] c2)  
|-
+
  |'''integer''', '''integer'''|[[Modding:Coord|Coord]]:[[#coord_get|get]]()  
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
  |'''string'''|[[Modding:Coord|Coord]]:[[#coord_tostring|tostring]]()  
|coord.[[#coord_mul|mul]]([[Modding:Coord|Coord]] or '''integer''' lhs, [[Modding:Coord|Coord]] or '''integer''' rhs)  
+
  |[[Modding:Coord|Coord]]|[[Modding:Coord|Coord]]:[[#coord_clone|clone]]()  
|-style="background: #333;"
+
  |'''void'''|[[Modding:Coord|Coord]]:[[#coord_random_shift|random_shift]]('''integer''' value)  
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''boolean'''
+
  |[[Modding:Coord|Coord]]|[[Modding:Coord|Coord]]:[[#coord_random_shifted|random_shifted]]('''integer''' value)
|coord.[[#coord_eq|eq]]([[Modding:Coord|Coord]] lhs, [[Modding:Coord|Coord]], rhs)  
+
  |[[Modding:Coord|CoordIterator]]|[[Modding:Coord|Coord]]:[[#coord_cross_coords|cross_coords]]()
|-
+
  |[[Modding:Coord|CoordIterator]]|[[Modding:Coord|Coord]]:[[#coord_around_coords|around_coords]]()
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
  |}}
|coord.[[#coord_abs|abs]]([[Modding:Coord|Coord]] c)  
+
}}
|-style="background: #333;"
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
|coord.[[#coord_sign|sign]]([[Modding:Coord|Coord]] c)  
+
|-
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''integer'''
+
|coord.[[#coord_distance|distance]]([[Modding:Coord|Coord]] c1, [[Modding:Coord|Coord]] c2)  
+
|-style="background: #333;"
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''number'''
+
|coord.[[#coord_real_distance|real_distance]]([[Modding:Coord|Coord]] c1, [[Modding:Coord|Coord]] c2)  
+
|-
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
|coord.[[#coord_random|random]]([[Modding:Coord|Coord]] c1, [[Modding:Coord|Coord]] c2)  
+
|-style="background: #333;"
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''integer''', '''integer'''
+
|[[Modding:Coord|Coord]]:[[#coord_get|get]]()  
+
|-
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''string'''
+
|[[Modding:Coord|Coord]]:[[#coord_tostring|tostring]]()  
+
|-style="background: #333;"
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
|[[Modding:Coord|Coord]]:[[#coord_clone|clone]]()  
+
|-
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|'''void'''
+
|[[Modding:Coord|Coord]]:[[#coord_random_shift|random_shift]]('''integer''' value)  
+
|-style="background: #333;"
+
|style="text-align: right; vertical-align:top; padding-right: 2ex;"|[[Modding:Coord|Coord]]
+
|[[Modding:Coord|Coord]]:[[#coord_random_shifted|random_shifted]]('''integer''' value)  
+
|}
+
  
 
{{Anchor|coord_new}}
 
{{Anchor|coord_new}}
Line 140: Line 109:
 
;[[Modding:Coord|Coord]]:random_shifted('''integer''' value) → [[Modding:Coord|Coord]]
 
;[[Modding:Coord|Coord]]:random_shifted('''integer''' value) → [[Modding:Coord|Coord]]
 
:As random_shift, except instead of changing the existing coord, a new coord is returned.
 
:As random_shift, except instead of changing the existing coord, a new coord is returned.
=== Constants ===
+
----
 
+
{{Anchor|coord_cross_coords}}
 +
;[[Modding:Coord|Coord]]:cross_coords() → [[Modding:Coord|Coord]] '''iterator'''
 +
:Gives an iterator over all coords that are orthogonally adjacent to the given coord (i.e., not diagonals). This is ordered as follows: west, east, north, then south.
 +
----
 +
{{Anchor|coord_around_coords}}
 +
;[[Modding:Coord|Coord]]:around_coords() → [[Modding:Coord|Coord]] '''iterator'''
 +
:Gives an iterator over all coords adjacent to the given coord. It begins at the coord above and to the left of the given coord, then follows clockwise around the given coord.
  
 +
=== Constants ===
 
{|style="border: 2px solid darkred; border-spacing: 0; font-size: 90%; margin: 0.25em 0.5em;"
 
{|style="border: 2px solid darkred; border-spacing: 0; font-size: 90%; margin: 0.25em 0.5em;"
 
|colspan=3 style="background: darkred; color: yellow; font-size: 120%; text-align: center"|'''Constant Coords'''
 
|colspan=3 style="background: darkred; color: yellow; font-size: 120%; text-align: center"|'''Constant Coords'''

Latest revision as of 22:06, 24 March 2012

Coords (short for coordinates) are used to describe positions on the map. They are also occassionally used to indicate relative positions.

Properties

Although coords are technically userdata, they can be indexed like normal tables.

Coord
Integer x This is the x-coordinate of the coord. In DoomRL, x increases going from left to right, starting at 1 on the left edge of the map.
Integer y This is the y-coordinate of the coord. In DoomRL, y increases going from top to bottom, starting at 1 on the upper edge of the map.

API

Coord Interface
Coord coord.new(integer x, integer y)
Coord coord.unm(Coord c)
Coord coord.add(Coord lhs, Coord rhs)
Coord coord.sub(Coord lhs, Coord rhs)
Coord coord.mul(Coord or integer lhs, Coord or integer rhs)
boolean coord.eq(Coord lhs, Coord, rhs)
Coord coord.abs(Coord c)
Coord coord.sign(Coord c)
integer coord.distance(Coord c1, Coord c2)
number coord.real_distance(Coord c1, Coord c2)
Coord coord.random(Coord c1, Coord c2)
integer, integer Coord:get()
string Coord:tostring()
Coord Coord:clone()
void Coord:random_shift(integer value)
Coord Coord:random_shifted(integer value)
CoordIterator Coord:cross_coords()
CoordIterator Coord:around_coords()

coord.new(integer x, integer y) → Coord
This creates a new coord with the given x and y.

coord.unm(Coord c) → Coord
-Coord c → Coord
This creates a new coord whose coordinates are the inverse (negative) of the original coordinates.

coord.add(Coord lhs, Coord rhs) → Coord
Coord lhs + Coord rhs → Coord
This creates a new coord whose coordinates are the sums of the corresponding coordinates of lhs and rhs.

coord.sub(Coord lhs, Coord rhs) → Coord
Coord lhs - Coord rhs → Coord
This creates a new coord whose coordinates are the differences of the corresponding coordinates of lhs and rhs.

coord.mul(Coord or integer lhs, Coord or integer rhs) → Coord
Coord or integer lhs * Coord or integer rhs → Coord
With two coords, this creates a new coord whose coordinates are the products of the corresponding coordinates of lhs and rhs. With an integer, a new coord is returned with the coordinates of the original coord scaled by the integer. (This doesn't apply to two integers.)

coord.eq(Coord lhs, Coord, rhs) → boolean
Coord lhs == Coord rhs → boolean
This determines if the given coords are coordinate-wise equal. (They might still be different objects! Beware using coords as table keys.)

coord.abs(Coord c) → Coord
This creates a new coord with coordinates that are the absolute value of the corresponding original coordinates.

coord.sign(Coord c) → Coord
This creates a new coord by applying the sign function (1 for positive, 0 for zero, -1 for negative) to each coordinate of the original coord.

coord.distance(Coord c1, Coord c2) → integer
Calculates the distance between the given coords.

coord.real_distance(Coord c1, Coord c2) → number
Calculates the (approximate) Euclidean distance between the biven coords.

coord.random(Coord c1, Coord c2) → Coord
Returns a new coord uniformly at random whose x is between the x coordinates of c1 and c2 (inclusive) and whose y is between the y coordinates of c1 and c2. The coordinates of c1 should be less than or equal to the corresponding coordinates of c2.

Coord:get() → integer, integer
Returns the x and y coordinates of the coord (in order).

Coord:tostring() → string
Returns a string containing the x and y coordinates separated by a comma.

Coord:clone() → Coord
Returns a copy of the coord.

Coord:random_shift(integer value)
Changes the coord uniformly at random to a different coord (or possibly the same coord) whose x and y are within value of the originals. value is optional; it defaults to 1.

Coord:random_shifted(integer value) → Coord
As random_shift, except instead of changing the existing coord, a new coord is returned.

Coord:cross_coords() → Coord iterator
Gives an iterator over all coords that are orthogonally adjacent to the given coord (i.e., not diagonals). This is ordered as follows: west, east, north, then south.

Coord:around_coords() → Coord iterator
Gives an iterator over all coords adjacent to the given coord. It begins at the coord above and to the left of the given coord, then follows clockwise around the given coord.

Constants

Constant Coords
null coord.UNIT This is a coord with 1 in both coordinates. Be careful not to change it.
null coord.ZERO This is a coord with 0 in both coordinates. Be careful not to change it.
Personal tools