Skip to content

Commit

Permalink
mapgen: add commands to place NPC zones
Browse files Browse the repository at this point in the history
NPC zones can be added by faction id and zone type.
  • Loading branch information
mlangsdorf committed May 5, 2019
1 parent 94ff9db commit 9b20564
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doc/MAPGEN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* 2.5.17 "sealed_item"
* 2.5.18 "graffiti"
* 2.6.19 "translate_ter"
* 2.6.20 "zones"
* 2.6 "rotation"
* 3 update_mapgen
* 3.1 overmap tile specification
Expand Down Expand Up @@ -691,6 +692,12 @@ normal mapgen, but it is useful for setting a baseline with update_mapgen.
- "from": (required, string) the terrain id of the terrain to be transformed
- "to": (required, string) the terrain id that the from terrain will transformed into

### 2.5.20 "zones"
Places a zone for an NPC faction. NPCs in the faction will use the zone to influence the AI.
- "type": (required, string) must be one of NPC_RETREAT, NPC_NO_INVESTIGATE, or NPC_INVESTIGATE_ONLY. NPCs will prefer to retreat towards NPC_RETREAT zones. They will not move to the see the source of unseen sounds coming from NPC_NO_INVESTIGATE zones. They will not move to the see the source of unseen sounds coming from outside NPC_INVESTIGATE_ONLY zones.
- "faction": (required, string) the faction id of the NPC faction that will use the zone.
- "name": (optional, string) the name of the zone.

# 2.7 "rotation"
Rotates the generated map after all the other mapgen stuff has been done. The value can be a single integer or a range (out of which a value will be randomly chosen). Example:
```JSON
Expand Down
51 changes: 51 additions & 0 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,57 @@ void zone_manager::swap( zone_data &a, zone_data &b )
std::swap( a, b );
}

void zone_manager::rotate_zones( map &target_map, const int turns )
{
if( turns == 0 ) {
return;
}
const tripoint a_start = target_map.getabs( tripoint( 0, 0, 0 ) );
const tripoint a_end = target_map.getabs( tripoint( 23, 23, 0 ) );
for( zone_data &zone : zones ) {
const tripoint z_start = zone.get_start_point();
const tripoint z_end = zone.get_end_point();
if( ( a_start.x <= z_start.x && a_start.y <= z_start.y ) &&
( a_end.x > z_start.x && a_end.y >= z_start.y ) &&
( a_start.x <= z_end.x && a_start.y <= z_end.y ) &&
( a_end.x >= z_end.x && a_end.y >= a_end.y ) ) {
tripoint z_l_start = target_map.getlocal( z_start );
tripoint z_l_end = target_map.getlocal( z_end );
tripoint new_z_l_start = z_l_start;
tripoint new_z_l_end = z_l_end;
switch( turns ) {
case 1:
new_z_l_start.x = 23 - z_l_start.y;
new_z_l_start.y = z_l_start.x;
new_z_l_end.x = 23 - z_l_end.y;
new_z_l_end.y = z_l_end.x;
break;
case 2:
new_z_l_start.x = 23 - z_l_start.x;
new_z_l_start.y = 23 - z_l_start.y;
new_z_l_end.x = 23 - z_l_end.x;
new_z_l_end.y = 23 - z_l_end.y;
break;
case 3:
new_z_l_start.x = z_l_start.y;
new_z_l_start.y = 23 - z_l_start.x;
new_z_l_start.x = z_l_end.y;
new_z_l_start.y = 23 - z_l_end.x;
break;
}
tripoint new_z_start = target_map.getabs( new_z_l_start );
tripoint new_z_end = target_map.getabs( new_z_l_end );
tripoint first = new_z_start;
tripoint second = new_z_end;
if( second < first ) {
second = new_z_start;
first = new_z_end;
}
zone.set_position( std::make_pair( first, second ), false );
}
}
}

void zone_manager::start_sort( const std::vector<tripoint> &src_sorted )
{
for( auto &src : src_sorted ) {
Expand Down
2 changes: 2 additions & 0 deletions src/clzones.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class JsonOut;
class JsonObject;
class item;
class faction;
class map;

using faction_id = string_id<faction>;
const faction_id your_fac( "your_followers" );
Expand Down Expand Up @@ -318,6 +319,7 @@ class zone_manager
cata::optional<std::string> query_name( const std::string &default_name = "" ) const;
cata::optional<zone_type_id> query_type() const;
void swap( zone_data &a, zone_data &b );
void rotate_zones( map &target_map, const int turns );

void start_sort( const std::vector<tripoint> &src_sorted );
void end_sort();
Expand Down
37 changes: 36 additions & 1 deletion src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
#include <cmath>

#include "ammo.h"
#include "clzones.h"
#include "computer.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "debug.h"
#include "drawing_primitives.h"
#include "enums.h"
#include "faction.h"
#include "game.h"
#include "item_group.h"
#include "itype.h"
Expand Down Expand Up @@ -1645,7 +1647,34 @@ class jmapgen_translate : public jmapgen_piece
dat.m.translate( from, to );
}
};

/**
* Place a zone
*/
class jmapgen_zone : public jmapgen_piece
{
public:
zone_type_id zone_type;
faction_id faction;
std::string name = "";
jmapgen_zone( JsonObject &jsi ) : jmapgen_piece() {
if( jsi.has_string( "faction" ) && jsi.has_string( "type" ) ) {
std::string fac_id = jsi.get_string( "faction" );
faction = faction_id( fac_id );
std::string zone_id = jsi.get_string( "type" );
zone_type = zone_type_id( zone_id );
if( jsi.has_string( "name" ) ) {
name = jsi.get_string( "name" );
}
}
}
void apply( const mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y,
const float /*mdensity*/, mission * /*miss*/ ) const override {
zone_manager &mgr = zone_manager::get_manager();
const tripoint start = dat.m.getabs( tripoint( x.val, y.val, 0 ) );
const tripoint end = dat.m.getabs( tripoint( x.valmax, y.valmax, 0 ) );
mgr.add( name, zone_type, faction, false, true, start, end );
}
};

static void load_weighted_entries( JsonObject &jsi, const std::string &json_key,
weighted_int_list<std::string> &list )
Expand Down Expand Up @@ -2148,6 +2177,7 @@ mapgen_palette mapgen_palette::load_internal( JsonObject &jo, const std::string
new_pal.load_place_mapings<jmapgen_liquid_item>( jo, "liquids", format_placings );
new_pal.load_place_mapings<jmapgen_graffiti>( jo, "graffiti", format_placings );
new_pal.load_place_mapings<jmapgen_translate>( jo, "translate", format_placings );
new_pal.load_place_mapings<jmapgen_zone>( jo, "zones", format_placings );

return new_pal;
}
Expand Down Expand Up @@ -2326,6 +2356,7 @@ bool mapgen_function_json_base::setup_common( JsonObject jo )
objects.load_objects<jmapgen_nested>( jo, "place_nested" );
objects.load_objects<jmapgen_graffiti>( jo, "place_graffiti" );
objects.load_objects<jmapgen_translate>( jo, "translate_ter" );
objects.load_objects<jmapgen_zone>( jo, "place_zones" );

if( !mapgen_defer::defer ) {
is_ready = true; // skip setup attempts from any additional pointers
Expand Down Expand Up @@ -7548,6 +7579,10 @@ void map::rotate( int turns )
}
}
}

// rotate zones
zone_manager &mgr = zone_manager::get_manager();
mgr.rotate_zones( *this, turns );
}

// Hideous function, I admit...
Expand Down

0 comments on commit 9b20564

Please sign in to comment.