RimWorld source code

Started by Tynan, April 30, 2014, 05:06:33 PM

Previous topic - Next topic

Tynan

A4 and later will include some source files for modder reference.

I thought I might as well release them here early; they might help someone.

http://puu.sh/8tVfJ.zip
Tynan Sylvester - @TynanSylvester - Tynan's Blog

zelldot

Thanks tynan this will prove very helpful when writing dll's in my mod, and I'm sure will be helpful for the other modders too, I have not had a look at it yet as I'm tired and working on some simple things in xml before bed, but I'm sure to dive in when something arises that I want to edit.

WorldOfIllusion

What is this? I see comments, proper formatting, no internal variables named 'current'... it... its beautiful.
In all seriousness, this is fantastic. Much easier to read than what we can get through something like ILSpy.
Artistically challenged modder seeking artistically talented texturer's help. Please, please, PM me :)

pawnstorm

Awesome sauce! Also, nice selection of classes, seems to cover a lot of interesting things :).

Architect

mmmm, that new code smell...
Check out BetterPower+ and all its derivatives by clicking the picture below.

It adds many new methods of power generation and uses for it, as well as other things such as incidents.


bleedo


Crimsonknight3

I like the comments being put into the code, it's very helpful for people who aren't great programmers haha :) It certainly helps me anyway :)

dadog99

Where is the image for a standard metal wall located?

iame6162013

Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Robert J. Hanlon: "Never attribute to malice that which is adequately explained by stupidity."

Crimsonknight3

With the new alpha4 it states in the modding file that partial source code is included with the game but it isn't there? ..>

Tynan

Crap, you're right. The build script must have broken and I didn't notice. Let me fix it.
Tynan Sylvester - @TynanSylvester - Tynan's Blog

Tynan

All right, fixed and hotfixed. If you re-download the game you'll get Alpha4b which is identical in the built version, but includes the source.
Tynan Sylvester - @TynanSylvester - Tynan's Blog

Crimsonknight3

Quote from: Tynan on June 02, 2014, 08:36:42 AM
All right, fixed and hotfixed. If you re-download the game you'll get Alpha4b which is identical in the built version, but includes the source.


Lol We all have those moments :p Thank you :)

mrnic3

Hi Tynan,

Could you add GenAI, it seems not do decompile well.
And somhow you changed the way targets are selected between alpha 3 and 4.

Regards,
Mr Nice

Tynan

Here's how GenAI looks now:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using Verse;
using RimWorld;


namespace Verse.AI{
public static class GenAI
{
public static Thing BestShootTarget(IntVec3 root,
Thing searcher,
RegionTraverseParameters traverseParams,
Predicate<Thing> validator,
float maxDistance,
float minDistance,
bool needsLOStoDynamic,
bool needsLOStoStatic,
bool shootBurning = true )
{
Pawn searcherPawn = searcher as Pawn;

//Figure out what enumerable to search through the find the target
//Can include enemy pawns or buildings
IEnumerable<Thing> targetEnum;

SquadBrain searcherKing = null;
if( searcherPawn != null )
searcherKing = searcherPawn.GetSquadBrain();
if( searcherKing != null )
{
//Special target list: Anything on king's hit list
targetEnum = searcherKing.GetAttackTargets();
}
else
{
//Standard target list: All hostile pawns
targetEnum = PawnTargetsFor( searcher.Faction );
}

float minDistanceSquared = minDistance*minDistance;
Predicate<Thing> inValidator = t=>
{
if( validator != null && !validator(t) )
return false;

if( searcherPawn != null && !searcherPawn.CanReach(t, PathMode.Touch) )
return false;

if( (root - t.Position).LengthHorizontalSquared < minDistanceSquared )
return false;

if( needsLOStoDynamic || needsLOStoStatic )
                {
                    if (!searcher.CanSee(t))
                    {
                        if (t is Pawn)
                        {
                            if (needsLOStoDynamic)
                                return false;
                        }
                        else
                        {
                            if (needsLOStoStatic)
                                return false;
                        }
                    }
                }

Pawn p = t as Pawn;
if( p != null && p.Incapacitated )
return false;

if( !shootBurning && t.IsBurning() )
return false;

return true;
};


return GenClosest.ClosestThingReachableGlobal( searcher.Position,
targetEnum,
PathMode.Touch,
traverseParams,
maxDistance: maxDistance,
validator: inValidator);
}

public static IEnumerable<Thing> PawnTargetsFor( Faction faction )
{
return from pawn in Find.ListerPawns.PawnsHostileTo(faction)
where !pawn.Incapacitated
select (Thing)pawn;
}



//This method resembles Ability.ShootLineFromTo a little too much. Perhaps they should merge somehow
public static bool CanSee( this Thing seer, Thing target )
{
foreach( IntVec3 sourceCell in ShootLeanUtility.ShootingSourcesFromTo(seer.Position, target.Position) )
{
foreach( IntVec3 destCell in ShootLeanUtility.ShootableSquaresOf( target ) )
{
if( GenSight.LineOfSight( sourceCell, destCell ) )
return true;
}
}

return false;
}


public static bool MachinesLike(Faction machineFaction, Pawn p)
{
if( p.Faction == null )
return false;

if ( p.JailerFaction == machineFaction )
return false;

return !p.Faction.IsHostileToward(machineFaction);

//return p.Faction == machineFaction;
}

public static bool CanUseItem( Pawn p, Thing item )
{
if( item.IsForbidden( p.Faction ) )
return false;

if( !p.CanReserveAndReach( item, ReservationType.Total, PathMode.ClosestTouch ) )
return false;

return true;
}
}}




/*  Old ClosestEnemyTarget loop
float MaxDistanceSquared = MaxDistance*MaxDistance;
float ClosestSquaredDist = float.MaxValue;
Pawn ClosestEnemy = null;
foreach( Pawn p in Find.PawnManager.PawnsWithHostilityTo[Searcher.Team] )
{
float SquaredDist = (p.Position - Searcher.Position).LengthHorizontalSquared;

if( SquaredDist < ClosestSquaredDist && SquaredDist <= MaxDistanceSquared )
{
if( NeedsLOS && !Searcher.CanSee(p) )
continue;

ClosestSquaredDist = SquaredDist;
ClosestEnemy = p;
}
}
return ClosestEnemy;
*/
Tynan Sylvester - @TynanSylvester - Tynan's Blog