Add system repopulator

Runs every 20 seconds to keep number of ships in system up
This commit is contained in:
cim 2013-07-03 15:48:25 +01:00
parent 4205ba932c
commit 242002cf1a
4 changed files with 183 additions and 25 deletions

View File

@ -171,7 +171,7 @@ this.systemWillPopulate = function()
location: "LANE_PS",
groupCount: pstraders,
callback: function(pos) {
var r2t = system.addShips("trader",1,pos,0)[0];
var r2t = system.addShips("sunskim-trader",1,pos,0)[0];
r2t.setBounty(0,"setup actions");
// ensure sufficient insulation
// tested at Aenqute - see [Universe makeSunSkimmer]
@ -191,32 +191,13 @@ this.systemWillPopulate = function()
});
/* Add pirates */
var addPirates = function(pos)
{
var size = Math.random()*4;
if (system.government >= 6)
{
size = size/2;
}
else if (system.government <= 1)
{
size += Math.random()*3;
}
size = Math.ceil(size);
log("oolite-populator","Pirate pack, size: "+size);
var pg = system.addGroup("pirate",size,pos,2.5E3);
for (var i=0;i<pg.ships.length;i++)
{
pg.ships[i].setBounty(20+system.government+size+Math.floor(Math.random()*8),"setup actions");
}
};
system.setPopulator("oolite-route1-pirates",
{
priority: 10,
location: "LANE_WP",
groupCount: pirates,
callback: addPirates
callback: this._addPirates
});
system.setPopulator("oolite-route2-pirates",
@ -224,7 +205,7 @@ this.systemWillPopulate = function()
priority: 10,
location: "LANE_PS",
groupCount: pspirates,
callback: addPirates
callback: this._addPirates
});
/* Add hunters */
@ -338,6 +319,113 @@ this.systemWillPopulate = function()
}
// function responsible for replenishing system contents
this.systemWillRepopulate = function()
{
// incoming traders, more frequent in rich economies
if (Math.random() < 0.06+0.01*(8-system.info.economy))
{
if (Math.random() < 0.2)
{
var newskimmer = system.addShips("sunskim-trader",1,[0,0,0],7500)[0];
var reqIns = 1000/(1+newskimmer.maxSpeed);
if (reqIns > 12)
{
reqIns = 12;
}
if (newskimmer.heatInsulation < reqIns)
{
newskimmer.heatInsulation = reqIns;
}
newskimmer.switchAI("route2sunskimAI.plist");
}
else
{
system.addShips("trader",1,[0,0,0],7500)[0];
}
return;
}
// replace lost patrols (more frequently in safe systems)
if (Math.random() < 0.05+0.02*(1+system.info.government))
{
var current = system.countShipsWithPrimaryRole("police");
var target = system.info.government;
if (current < target)
{
var newpolice = system.mainStation.launchShipWithRole("police");
if (Math.random() < 0.2)
{
newpolice.switchAI("route2patrolAI.plist");
}
else
{
newpolice.switchAI("route1patrolAI.plist");
}
}
else
{
// enough police, add a bounty hunter instead?
current = system.countShipsWithPrimaryRole("hunter");
if (system.info.government <= 1)
{
target = 4;
}
else
{
target = system.info.government/2;
}
if (current < target)
{
var newhunter = system.addShips("hunter",1,[0,0,0],7500)[0];
if (Math.random() < 0.2)
{
newhunter.switchAI("route2patrolAI.plist");
}
}
}
return;
}
// replace lost pirates
if (Math.random() < 0.02*(8-system.info.government))
{
var current = system.countShipsWithPrimaryRole("pirate");
var target = 3*(8-system.info.government);
if (current < target)
{
// temporary hack: pirates don't currently have the AI to fly
// to their raiding grounds, so for now just magically have
// them appear on the spacelane when the player isn't looking
do
{
if (Math.random() < 0.15)
{
var pos = Vector3D.interpolate(system.sun.position, system.mainPlanet.position, 0.3+Math.random()*0.5);
}
else
{
var pos = Vector3D.interpolate([0,0,0], system.mainPlanet.position, Math.random()*0.8);
}
}
while (pos.distanceTo(player.ship) < 51200);
this._addPirates(pos);
}
return;
}
// Thargoid invasions
if (Math.random() < 0.01)
{
system.addShips("thargoid",1,[0,0,0],7500);
}
}
/* And the equivalent functions for interstellar space */
this.interstellarSpaceWillPopulate = function()
{
log(this.name,"Interstellar populator");
@ -350,4 +438,44 @@ this.interstellarSpaceWillPopulate = function()
system.addShips("thargoid",1,pos,0);
}
});
}
}
this.interstellarSpaceWillRepopulate = function()
{
if (system.countShipsWithPrimaryRole("thargoid") < 2)
{
if (Math.random() > 0.01)
{
system.addShips("thargoid",1,[0,0,0],25600);
}
else
{
// everyone's getting ambushed today
system.addShips("trader",1,[0,0,0],6400);
}
}
}
/* Utility functions */
this._addPirates = function(pos)
{
var size = Math.random()*4;
if (system.government >= 6)
{
size = size/2;
}
else if (system.government <= 1)
{
size += Math.random()*3;
}
size = Math.ceil(size);
log("oolite-populator","Pirate pack, size: "+size);
var pg = system.addGroup("pirate",size,pos,2.5E3);
for (var i=0;i<pg.ships.length;i++)
{
pg.ships[i].setBounty(20+system.government+size+Math.floor(Math.random()*8),"setup actions");
}
}

View File

@ -11788,7 +11788,8 @@ Vector positionOffsetForShipInRotationToAlignment(ShipEntity* ship, Quaternion q
there's a nova in the works, the AI asked us not to, or we're in
interstellar space.
*/
[UNIVERSE witchspaceShipWithPrimaryRole:[self primaryRole]];
// now handled by system repopulator
// [UNIVERSE witchspaceShipWithPrimaryRole:[self primaryRole]];
}
// MKW 2011.02.27 - Moved here from ShipEntityAI so escorts reliably follow

View File

@ -145,6 +145,8 @@ enum
#define MIN_DISTANCE_TO_BUOY 750.0f // don't add ships within this distance
#define MIN_DISTANCE_TO_BUOY2 (MIN_DISTANCE_TO_BUOY * MIN_DISTANCE_TO_BUOY)
#define SYSTEM_REPOPULATION_INTERVAL 20.0f;
#ifndef OO_LOCALIZATION_TOOLS
#define OO_LOCALIZATION_TOOLS 1
#endif
@ -258,7 +260,9 @@ enum
NSMutableArray *allPlanets;
NSMutableDictionary *populatorSettings;
OOTimeDelta next_repopulation;
NSString *system_repopulator;
NSArray *closeSystems;
BOOL strict;

View File

@ -416,6 +416,7 @@ GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEVEL, DOC
[screenBackgrounds release];
[gameView release];
[populatorSettings release];
[system_repopulator release];
[localPlanetInfoOverrides release];
[activeWormholes release];
@ -836,6 +837,8 @@ GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEVEL, DOC
[self clearSystemPopulator];
NSString *populator = [systeminfo oo_stringForKey:@"populator" defaultValue:@"interstellarSpaceWillPopulate"];
[system_repopulator release];
system_repopulator = [[systeminfo oo_stringForKey:@"repopulator" defaultValue:@"interstellarSpaceWillRepopulate"] retain];
JSContext *context = OOJSAcquireContext();
[PLAYER doWorldScriptEvent:OOJSIDFromString(populator) inContext:context withArguments:NULL count:0 timeLimit:kOOJSLongTimeLimit];
OOJSRelinquishContext(context);
@ -894,6 +897,8 @@ GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEVEL, DOC
forTarget:nil];
}
next_repopulation = randf() * SYSTEM_REPOPULATION_INTERVAL;
OOLogOutdentIf(kOOLogUniversePopulateWitchspace);
}
@ -1203,6 +1208,9 @@ GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEVEL, DOC
// [self populateSpaceFromHyperPoint:witchPos toPlanetPosition: a_planet->position andSunPosition: a_sun->position];
[self clearSystemPopulator];
NSString *populator = [systeminfo oo_stringForKey:@"populator" defaultValue:@"systemWillPopulate"];
[system_repopulator release];
system_repopulator = [[systeminfo oo_stringForKey:@"repopulator" defaultValue:@"systemWillRepopulate"] retain];
JSContext *context = OOJSAcquireContext();
[PLAYER doWorldScriptEvent:OOJSIDFromString(populator) inContext:context withArguments:NULL count:0 timeLimit:kOOJSLongTimeLimit];
OOJSRelinquishContext(context);
@ -1285,6 +1293,8 @@ GLfloat docked_light_specular[4] = { DOCKED_ILLUM_LEVEL, DOCKED_ILLUM_LEVEL, DOC
forTarget:nil];
OO_DEBUG_POP_PROGRESS();
}
next_repopulation = randf() * SYSTEM_REPOPULATION_INTERVAL;
}
- (void) clearSystemPopulator
@ -5809,6 +5819,15 @@ OOINLINE BOOL EntityInRange(HPVector p1, Entity *e2, float range)
}
- (void) repopulateSystem
{
JSContext *context = OOJSAcquireContext();
[PLAYER doWorldScriptEvent:OOJSIDFromString(system_repopulator) inContext:context withArguments:NULL count:0 timeLimit:kOOJSLongTimeLimit];
OOJSRelinquishContext(context);
next_repopulation = SYSTEM_REPOPULATION_INTERVAL;
}
- (void) update:(OOTimeDelta)inDeltaT
{
volatile OOTimeDelta delta_t = inDeltaT * [self timeAccelerationFactor];
@ -5816,6 +5835,12 @@ OOINLINE BOOL EntityInRange(HPVector p1, Entity *e2, float range)
OOLog(@"universe.profile.update",@"Begin update");
if (EXPECT(!no_update))
{
next_repopulation -= delta_t;
if (next_repopulation < 0)
{
[self repopulateSystem];
}
unsigned i, ent_count = n_entities;
Entity *my_entities[ent_count];