Skip to content

Commit

Permalink
Merge pull request CleverRaven#74823 from PatrikLundell/typify
Browse files Browse the repository at this point in the history
Add typed standard coordinate constants + typify active_item_cache
  • Loading branch information
kevingranade authored Jul 2, 2024
2 parents 1bd5c31 + 453914f commit 48eb345
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 69 deletions.
21 changes: 14 additions & 7 deletions src/active_item_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ bool item_reference::has_watertight_container() const
} );
}

bool active_item_cache::add( item &it, point location, item *parent,
bool active_item_cache::add( item &it, point_sm_ms location, item *parent,
std::vector<item_pocket const *> const &pocket_chain )
{
return active_item_cache::add( it, rebase_rel( location ), parent, pocket_chain );
}

bool active_item_cache::add( item &it, point_rel_ms location, item *parent,
std::vector<item_pocket const *> const &pocket_chain )
{
std::vector<item_pocket const *> pockets = pocket_chain;
Expand Down Expand Up @@ -140,7 +146,7 @@ std::vector<item_reference> active_item_cache::get_special( special_item_type ty
return matching_items;
}

void active_item_cache::subtract_locations( const point &delta )
void active_item_cache::subtract_locations( const point_rel_ms &delta )
{
for( std::pair<const int, std::list<item_reference>> &pair : active_items ) {
for( item_reference &ir : pair.second ) {
Expand All @@ -149,23 +155,24 @@ void active_item_cache::subtract_locations( const point &delta )
}
}

void active_item_cache::rotate_locations( int turns, const point &dim )
void active_item_cache::rotate_locations( int turns, const point_rel_ms &dim )
{
for( std::pair<const int, std::list<item_reference>> &pair : active_items ) {
for( item_reference &ir : pair.second ) {
ir.location = ir.location.rotate( turns, dim );
// Should 'rotate' be propaged up to the typed coordinates?
ir.location = point_rel_ms( ir.location.raw().rotate( turns, dim.raw() ) );
}
}
}

void active_item_cache::mirror( const point &dim, bool horizontally )
void active_item_cache::mirror( const point_rel_ms &dim, bool horizontally )
{
for( std::pair<const int, std::list<item_reference>> &pair : active_items ) {
for( item_reference &ir : pair.second ) {
if( horizontally ) {
ir.location.x = dim.x - 1 - ir.location.x;
ir.location.x() = dim.x() - 1 - ir.location.x();
} else {
ir.location.y = dim.y - 1 - ir.location.y;
ir.location.y() = dim.y() - 1 - ir.location.y();
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/active_item_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#include <unordered_map>
#include <vector>

#include "point.h"
#include "coordinates.h"
#include "safe_reference.h"

class item;
class item_pocket;

// A struct used to uniquely identify an item within a submap or vehicle.
struct item_reference {
point location;
point_rel_ms location;
safe_reference<item> item_ref;
// parent invalidating would also invalidate item_ref so it's safe to use a raw pointers here
item *parent = nullptr;
Expand Down Expand Up @@ -58,8 +58,12 @@ class active_item_cache
/**
* Adds the reference to the cache. Does nothing if the reference is already in the cache.
* Relies on the fact that item::processing_speed() is a constant.
* These two operations are really the same, but tailored to their usages.
* The submap coordinates are for submaps, and the relative ones are for vehicles.
*/
bool add( item &it, point location, item *parent = nullptr,
bool add( item &it, point_sm_ms location, item *parent = nullptr,
std::vector<item_pocket const *> const &pocket_chain = {} );
bool add( item &it, point_rel_ms location, item *parent = nullptr,
std::vector<item_pocket const *> const &pocket_chain = {} );

/**
Expand Down Expand Up @@ -88,9 +92,9 @@ class active_item_cache
*/
std::vector<item_reference> get_special( special_item_type type );
/** Subtract delta from every item_reference's location */
void subtract_locations( const point &delta );
void rotate_locations( int turns, const point &dim );
void mirror( const point &dim, bool horizontally );
void subtract_locations( const point_rel_ms &delta );
void rotate_locations( int turns, const point_rel_ms &dim );
void mirror( const point_rel_ms &dim, bool horizontally );
};

#endif // CATA_SRC_ACTIVE_ITEM_CACHE_H
149 changes: 149 additions & 0 deletions src/coordinate_constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#pragma once
#ifndef CATA_SRC_COORDINATE_CONSTANTS_H
#define CATA_SRC_COORDINATE_CONSTANTS_H

#include "coords_fwd.h"
#include "point.h"

// Typed versions of the standard set of relative location constants.
const point_rel_ms point_rel_ms_min{ point_min };
const point_rel_ms point_rel_ms_zero { point_zero };
const point_rel_ms point_rel_ms_north{point_north};
const point_rel_ms point_rel_ms_north_east{ point_north_east };
const point_rel_ms point_rel_ms_east{point_east};
const point_rel_ms point_rel_ms_south_east{ point_south_east };
const point_rel_ms point_rel_ms_south {point_south};
const point_rel_ms point_rel_ms_south_west{ point_south_west };
const point_rel_ms point_rel_ms_west {point_west};
const point_rel_ms point_rel_ms_north_west{ point_north_west };

const point_abs_ms point_abs_ms_min{ point_min };
const point_sm_ms point_sm_ms_min{ point_min };
const point_omt_ms point_omt_ms_min{ point_min };
const point_bub_ms point_bub_ms_min{ point_min };

const point_abs_ms point_abs_ms_zero{ point_zero };
const point_sm_ms point_sm_ms_zero{ point_zero };
const point_omt_ms point_omt_ms_zero{ point_zero };
const point_bub_ms point_bub_ms_zero{ point_zero };

const point_rel_sm point_rel_sm_min{ point_min };
const point_rel_sm point_rel_sm_zero{ point_zero };
const point_rel_sm point_rel_sm_north{ point_north };
const point_rel_sm point_rel_sm_north_east{ point_north_east };
const point_rel_sm point_rel_sm_east{ point_east };
const point_rel_sm point_rel_sm_south_east{ point_south_east };
const point_rel_sm point_rel_sm_south{ point_south };
const point_rel_sm point_rel_sm_south_west{ point_south_west };
const point_rel_sm point_rel_sm_west{ point_west };
const point_rel_sm point_rel_sm_north_west{ point_north_west };

const point_abs_sm point_abs_sm_min{ point_min };
const point_omt_sm point_sm_sm_min{ point_min };
const point_om_sm point_omt_sm_min{ point_min };
const point_bub_sm point_bub_sm_min{ point_min };

const point_abs_sm point_abs_sm_zero{ point_zero };
const point_omt_sm point_sm_sm_zero{ point_zero };
const point_om_sm point_omt_sm_zero{ point_zero };
const point_bub_sm point_bub_sm_zero{ point_zero };

const point_rel_omt point_rel_omt_min{ point_min };
const point_rel_omt point_rel_omt_zero{ point_zero };
const point_rel_omt point_rel_omt_north{ point_north };
const point_rel_omt point_rel_omt_north_east{ point_north_east };
const point_rel_omt point_rel_omt_east{ point_east };
const point_rel_omt point_rel_omt_south_east{ point_south_east };
const point_rel_omt point_rel_omt_south{ point_south };
const point_rel_omt point_rel_omt_south_west{ point_south_west };
const point_rel_omt point_rel_omt_west{ point_west };
const point_rel_omt point_rel_omt_north_west{ point_north_west };

const point_rel_om point_rel_om_min{ point_min };
const point_rel_om point_rel_om_zero{ point_zero };
const point_rel_om point_rel_om_north{ point_north };
const point_rel_om point_rel_om_north_east{ point_north_east };
const point_rel_om point_rel_om_east{ point_east };
const point_rel_om point_rel_om_south_east{ point_south_east };
const point_rel_om point_rel_om_south{ point_south };
const point_rel_om point_rel_om_south_west{ point_south_west };
const point_rel_om point_rel_om_west{ point_west };
const point_rel_om point_rel_om_north_west{ point_north_west };

const point_abs_omt point_abs_omt_min{ point_min };
const point_om_omt point_om_omt_min{ point_min };
const point_abs_seg point_abs_seg_min{ point_min };
const point_abs_om point_abs_om_min{ point_min };

const point_abs_omt point_abs_omt_zero{ point_zero };
const point_om_omt point_om_omt_zero{ point_zero };
const point_abs_seg point_abs_seg_zero{ point_zero };
const point_abs_om point_abs_om_zero{ point_zero };

const tripoint_rel_ms tripoint_rel_ms_min{ tripoint_min };
const tripoint_rel_ms tripoint_rel_ms_zero{ tripoint_zero };
const tripoint_rel_ms tripoint_rel_ms_north{ tripoint_north };
const tripoint_rel_ms tripoint_rel_ms_north_east{ tripoint_north_east };
const tripoint_rel_ms tripoint_rel_ms_east{ tripoint_east };
const tripoint_rel_ms tripoint_rel_ms_south_east{ tripoint_south_east };
const tripoint_rel_ms tripoint_rel_ms_south{ tripoint_south };
const tripoint_rel_ms tripoint_rel_ms_south_west{ tripoint_south_west };
const tripoint_rel_ms tripoint_rel_ms_west{ tripoint_west };
const tripoint_rel_ms tripoint_rel_ms_north_west{ tripoint_north_west };
const tripoint_rel_ms tripoint_rel_ms_above{ tripoint_above };
const tripoint_rel_ms tripoint_rel_ms_below{ tripoint_below };

const tripoint_abs_ms tripoint_abs_ms_min{ tripoint_min };
const tripoint_sm_ms tripoint_sm_ms_min{ tripoint_min };
const tripoint_omt_ms tripoint_omt_ms_min{ tripoint_min };
const tripoint_bub_ms tripoint_bub_ms_min{ tripoint_min };

const tripoint_abs_ms tripoint_abs_ms_zero{ tripoint_zero };
const tripoint_sm_ms tripoint_sm_ms_zero{ tripoint_zero };
const tripoint_omt_ms tripoint_omt_ms_zero{ tripoint_zero };
const tripoint_bub_ms tripoint_bub_ms_zero{ tripoint_zero };

const tripoint_rel_sm tripoint_rel_sm_min{ tripoint_min };
const tripoint_rel_sm tripoint_rel_sm_zero{ tripoint_zero };
const tripoint_rel_sm tripoint_rel_sm_north{ tripoint_north };
const tripoint_rel_sm tripoint_rel_sm_north_east{ tripoint_north_east };
const tripoint_rel_sm tripoint_rel_sm_east{ tripoint_east };
const tripoint_rel_sm tripoint_rel_sm_south_east{ tripoint_south_east };
const tripoint_rel_sm tripoint_rel_sm_south{ tripoint_south };
const tripoint_rel_sm tripoint_rel_sm_south_west{ tripoint_south_west };
const tripoint_rel_sm tripoint_rel_sm_west{ tripoint_west };
const tripoint_rel_sm tripoint_rel_sm_north_west{ tripoint_north_west };
const tripoint_rel_sm tripoint_rel_sm_above{ tripoint_above };
const tripoint_rel_sm tripoint_rel_sm_below{ tripoint_below };

const tripoint_abs_sm tripoint_abs_sm_min{ tripoint_min };
const tripoint_om_sm tripoint_om_sm_min{ tripoint_min };
const tripoint_bub_sm tripoint_bub_sm_min{ tripoint_min };

const tripoint_abs_sm tripoint_abs_sm_zero{ tripoint_zero };
const tripoint_om_sm tripoint_om_sm_zero{ tripoint_zero };
const tripoint_bub_sm tripoint_bub_sm_zero{ tripoint_zero };

const tripoint_rel_omt tripoint_rel_omt_min{ tripoint_min };
const tripoint_rel_omt tripoint_rel_omt_zero{ tripoint_zero };
const tripoint_rel_omt tripoint_rel_omt_north{ tripoint_north };
const tripoint_rel_omt tripoint_rel_omt_north_east{ tripoint_north_east };
const tripoint_rel_omt tripoint_rel_omt_east{ tripoint_east };
const tripoint_rel_omt tripoint_rel_omt_south_east{ tripoint_south_east };
const tripoint_rel_omt tripoint_rel_omt_south{ tripoint_south };
const tripoint_rel_omt tripoint_rel_omt_south_west{ tripoint_south_west };
const tripoint_rel_omt tripoint_rel_omt_west{ tripoint_west };
const tripoint_rel_omt tripoint_rel_omt_north_west{ tripoint_north_west };
const tripoint_rel_omt tripoint_rel_omt_above{ tripoint_above };
const tripoint_rel_omt tripoint_rel_omt_below{ tripoint_below };

const tripoint_abs_omt tripoint_abs_omt_min{ tripoint_min };
const tripoint_om_omt tripoint_om_omt_min{ tripoint_min };
const tripoint_abs_seg tripoint_abs_seg_min{ tripoint_min };
const tripoint_abs_om tripoint_abs_om_min{ tripoint_min };

const tripoint_abs_omt tripoint_abs_omt_zero{ tripoint_zero };
const tripoint_om_omt tripoint_om_omt_zero{ tripoint_zero };
const tripoint_abs_seg tripoint_abs_seg_zero{ tripoint_zero };
const tripoint_abs_om tripoint_abs_om_zero{ tripoint_zero };
#endif // CATA_SRC_COORDINATE_CONSTANTS_H
16 changes: 16 additions & 0 deletions src/coordinates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ void real_coords::fromabs( const point &abs )
om_pos.y = om_sub.y / 2;
}

point_rel_ms rebase_rel( point_sm_ms p )
{
return point_rel_ms( p.raw() );
}
point_rel_ms rebase_rel( point_omt_ms p )
{
return point_rel_ms( p.raw() );
Expand All @@ -42,6 +46,10 @@ point_rel_ms rebase_rel( point_bub_ms p )
{
return point_rel_ms( p.raw() );
}
point_sm_ms rebase_sm( point_rel_ms p )
{
return point_sm_ms( p.raw() );
}
point_omt_ms rebase_omt( point_rel_ms p )
{
return point_omt_ms( p.raw() );
Expand All @@ -50,6 +58,10 @@ point_bub_ms rebase_bub( point_rel_ms p )
{
return point_bub_ms( p.raw() );
}
tripoint_rel_ms rebase_rel( tripoint_sm_ms p )
{
return tripoint_rel_ms( p.raw() );
}
tripoint_rel_ms rebase_rel( tripoint_omt_ms p )
{
return tripoint_rel_ms( p.raw() );
Expand All @@ -58,6 +70,10 @@ tripoint_rel_ms rebase_rel( tripoint_bub_ms p )
{
return tripoint_rel_ms( p.raw() );
}
tripoint_sm_ms rebase_sm( tripoint_rel_ms p )
{
return tripoint_sm_ms( p.raw() );
}
tripoint_omt_ms rebase_omt( tripoint_rel_ms p )
{
return tripoint_omt_ms( p.raw() );
Expand Down
4 changes: 4 additions & 0 deletions src/coordinates.h
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,17 @@ using coords::project_combine;
using coords::project_bounds;

// Rebase relative coordinates to the base you know they're actually relative to.
point_rel_ms rebase_rel( point_sm_ms );
point_rel_ms rebase_rel( point_omt_ms p );
point_rel_ms rebase_rel( point_bub_ms p );
point_sm_ms rebase_sm( point_rel_ms p );
point_omt_ms rebase_omt( point_rel_ms p );
point_bub_ms rebase_bub( point_rel_ms p );

tripoint_rel_ms rebase_rel( tripoint_sm_ms p );
tripoint_rel_ms rebase_rel( tripoint_omt_ms p );
tripoint_rel_ms rebase_rel( tripoint_bub_ms p );
tripoint_sm_ms rebase_sm( tripoint_rel_ms p );
tripoint_omt_ms rebase_omt( tripoint_rel_ms p );
tripoint_bub_ms rebase_bub( tripoint_rel_ms p );

Expand Down
18 changes: 15 additions & 3 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14112,12 +14112,19 @@ bool item::process_gun_cooling( Character *carrier )
bool item::process( map &here, Character *carrier, const tripoint &pos, float insulation,
temperature_flag flag, float spoil_multiplier_parent, bool watertight_container, bool recursive )
{
process_relic( carrier, pos );
return item::process( here, carrier, tripoint_bub_ms( pos ), insulation, flag,
spoil_multiplier_parent, watertight_container, recursive );
}

bool item::process( map &here, Character *carrier, const tripoint_bub_ms &pos, float insulation,
temperature_flag flag, float spoil_multiplier_parent, bool watertight_container, bool recursive )
{
process_relic( carrier, pos.raw() );
if( recursive ) {
contents.process( here, carrier, pos, type->insulation_factor * insulation, flag,
contents.process( here, carrier, pos.raw(), type->insulation_factor * insulation, flag,
spoil_multiplier_parent, watertight_container );
}
return process_internal( here, carrier, pos, insulation, flag, spoil_multiplier_parent,
return process_internal( here, carrier, pos.raw(), insulation, flag, spoil_multiplier_parent,
watertight_container );
}

Expand Down Expand Up @@ -14774,6 +14781,11 @@ bool item::on_drop( const tripoint &pos )
}

bool item::on_drop( const tripoint &pos, map &m )
{
return item::on_drop( tripoint_bub_ms( pos ), m );
}

bool item::on_drop( const tripoint_bub_ms &pos, map &m )
{
// dropping liquids, even currently frozen ones, on the ground makes them
// dirty
Expand Down
6 changes: 6 additions & 0 deletions src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,9 @@ class item : public visitable
* @param map A map object associated with that position.
* @return true if the item was destroyed during placement.
*/
// TODO: Get rid of untyped overload.
bool on_drop( const tripoint &pos, map &map );
bool on_drop( const tripoint_bub_ms &pos, map &map );

/**
* Consume a specific amount of items of a specific type.
Expand Down Expand Up @@ -1447,9 +1449,13 @@ class item : public visitable
* should than delete the item wherever it was stored.
* Returns false if the item is not destroyed.
*/
// TODO: Get rid of untyped overload.
bool process( map &here, Character *carrier, const tripoint &pos, float insulation = 1,
temperature_flag flag = temperature_flag::NORMAL, float spoil_multiplier_parent = 1.0f,
bool watertight_container = false, bool recursive = true );
bool process( map &here, Character *carrier, const tripoint_bub_ms &pos, float insulation = 1,
temperature_flag flag = temperature_flag::NORMAL, float spoil_multiplier_parent = 1.0f,
bool watertight_container = false, bool recursive = true );

bool leak( map &here, Character *carrier, const tripoint &pos, item_pocket *pocke = nullptr );

Expand Down
Loading

0 comments on commit 48eb345

Please sign in to comment.