Changing solar based on whether

Started by Kyna Tiona, December 27, 2016, 09:08:00 PM

Previous topic - Next topic

Kyna Tiona

So eclipses' prevalence kinda bother me. Putting aside the utterly baffling astronomical ramifications, I just don't think it makes for fun gameplay. Solar generators do need some level of unreliability to them, though, otherwise they'd be ridiculously amazing. I figured a reasonable and tidy compromise was to edit weather conditions such that most of them reduced the effectiveness of solar by a bit.

I'm new to modding RimWorld, so I was hoping I could accomplish this by just changing some numbers in the Weathers.xml, but it looks like there isn't a setting for changing the effectiveness of solar. Does anyone know if there's such a setting that exists but isn't normally used, or would this be a much more troublesome goal than I'd hoped?

Grim

Hello,

I'm not really a RimWorld modding expert but I've had a look at the Solar generator class and the only thing that affects it's power output is the sky's current glow amount while I won't post the entire class I will display the relevant snippet below.


public class CompPowerPlantSolar : CompPowerPlant
{
private const float FullSunPower = 1700f;

private const float NightPower = 0f;

private static readonly Vector2 BarSize = new Vector2(2.3f, 0.14f);

private static readonly Material PowerPlantSolarBarFilledMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.5f, 0.475f, 0.1f));

private static readonly Material PowerPlantSolarBarUnfilledMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.15f, 0.15f, 0.15f));
                 
                //Here
protected override float DesiredPowerOutput
{
get
{
                                 //the power output is smoothly changed here depending on the sky's current glow and the roofedpoweroutputfactor
return Mathf.Lerp(0f, 1700f, this.parent.Map.skyManager.CurSkyGlow) * this.RoofedPowerOutputFactor;
}
}



Hopefully someone can correct me but I don't think you can you achieve what you want by changing existing XML Weather Defs.

If you were to go ahead and attempt this I would suggest instead modifying the solar generator rather than the weather incidents, try override the base CompPowerPlantSolar class with your own CompPowerPlantSolar class, using your own implementation of DesiredPowerOutput that allows the solar generator to take into account different weather types.

(the WeatherManager object has access to the current map weather)

I hope this helps some.

Grim

Kyna Tiona

Oh, fun fun. Not seeing any settings for sky glow in the weather XMLs, though, and I'm not sure of any way to find one. Changing the code directly would definitely be the best and easiest solution, but I couldn't make heads or tails of the wiki's tutorial on how to unpack the C# scripts into something I could modify. >_<

Grim


QuoteOh, fun fun. Not seeing any settings for sky glow in the weather XMLs, though, and I'm not sure of any way to find one. Changing the code directly would definitely be the best and easiest solution, but I couldn't make heads or tails of the wiki's tutorial on how to unpack the C# scripts into something I could modify. >_<

It's worth noting, you shouldn't/will not be modifying any of the game's base files, so you are not so much "unpacking" the game scripts as viewing how the developers do things and applying this in your own scripts.

A Quick Guide to Viewing RimWorld's API


  • Dowload http://ilspy.net/
  • Run iLSpy
  • Click File > Open
  • Navigate to <RimWorld Installation Folder>/RimWorldWin_Data/Managed
  • Open the File Assembly-CSharp.dll


This will let you view how the code for how the game implements mechanics/objects/buildings/things, as an example to find the bit of code I pasted you, I did the above steps once the .dll is loaded I searched for "solar" which lead me to the component (CompPowerPlantSolar = Solar Power Plant Component of the Building).


You could then set up a Visual Studio or Sharp Develop Project following (this is how I have learnt to do this, the tutorial is still relevant) http://rimworldwiki.com/wiki/Modding_Tutorials/Setting_up_a_solution


Once your project is set up or if you get stuck with some part of setting it up message on this post or inbox me and I will help you get it set up, if you would like.

Then I will post another mini tutorial here if you are interested walking through your specific request as it looks to me to be something others could apply to their own projects (Taking a base game object and making small changes).

That said bare in mind I am not a modding expert by any shot or even a programmer this is a hobby for me so the way I describe may be shot down by a more experienced modder or developer as a potentially hard-to-find-rage-inducing bug waiting to happen, but I think we should be fine, we are not modifying anything in the base game just copying it and modifying that.


But essentially we will create an copy of the Building Definition (in the XML) of the Solar Power Plant but in our version we will change the Component used from the base game version to our own that we will create in the Visual Studio Project (Or SharpDevelop).

I did a quick proof of concept and currently I have a Solar Power Generator in game that is almost identical to the vanilla one except in foggy weather it only provides 236 units of power instead of the full 1700.

Kyna Tiona

Quote from: Grim on December 28, 2016, 05:54:40 AM
But essentially we will create an copy of the Building Definition (in the XML) of the Solar Power Plant but in our version we will change the Component used from the base game version to our own that we will create in the Visual Studio Project (Or SharpDevelop).

I did a quick proof of concept and currently I have a Solar Power Generator in game that is almost identical to the vanilla one except in foggy weather it only provides 236 units of power instead of the full 1700.

Oh! Yes, that makes a lot more sense, and I can imagine it would cause fewer compatibility issues later on. This sounds like more fun than I expected, I'll mess with iLSpy later tonight~

Grim

Hello again,

I took a bit of time to create a Visual Studio project for this and created the component script you will need it's all uploaded at https://github.com/Grim-/LinksSolarWeatherMod

Gif of it in action - http://imgur.com/a/A1Ot7

Everything you need is in the Github Repo, Just use the green "Clone or Download" button and add it to your mods folder and see how it works in-game, then browse to /Source/ folder after have installed visual studio and click "ExampleMod.sln" this will open the project and the "CompPowerPlantSolarWeather" class created it has comments to show you the bits you would need to edit.

skullywag

Skullywag modded to death.
I'd never met an iterator I liked....until Zhentar saved me.
Why Unity5, WHY do you forsake me?

Grim


Kyna Tiona

Okay, so I love your code comments. Thank you for being the sort of person who comments~

Fun gif! Going to have to see if the abrupt change in the power output looks less odd in normal gameplay, rather than changing it through debug functions. (Though I need to play with those, too, since I haven't.) In the worst case, I may have to figure something out for gradually adjusting the power factor over a given amount of time. Which would preferably be in direct proportion to however long it takes weather conditions, which would require reaching into other things, so hopefully I won't actually feel the need to go totally OCD over this.

I did a quick google for how badly solar panels are generally affected by weather, and used the only rough numerical estimate I could find to fudge some numbers for all of the various weather conditions. (Minus Clear and Dry Thunderstorm, naturally.) I also discovered that unbeknownst to me, Toxic Fallout already had a slight penalty to solar power, so I don't have to figure out how to work with map conditions. I'll play with it for a while, probably tweak it a bit, and let you know how it goes.

Kyna Tiona

Quote from: Grim on December 29, 2016, 03:51:51 PM
Quote from: skullywag on December 29, 2016, 03:36:32 PM
based on whether what?

*Drum roll*

I think they mean *Weather

Oh, snap! I totally did do that! And I'm usually so good with homonyms!...

[Composes herself] Anyway, I keep getting this error:



My perilous predisposition for puns aside, I'm not sure where I went wrong. I modified the relevant portion of the .xml to read as:

      <li Class="CompProperties_Power">
        <compClass>WeatherableSolarFluctuations.CompPowerPlantSolarWeather</compClass>


And I modified the relevant portion of the .cs to read as:

namespace WeatherableSolarFluctuations
{
    [StaticConstructorOnStartup]
    public class CompPowerPlantSolarWeather : CompPowerPlant
    {


I changed a name or two, but it's the same structure as what you were using, so I'm not sure how I broke it. I did alter the code a bit, so maybe I introduced an error that caused the script to not be loaded, which caused a "could not find" error? I can't figure out how to get Mono's error checking properly hooked up to UnityEngine or RimWorld, so it's really quite possible I'm creating a compile error and don't know it. I would've expected an error message for that, though, so I'm baffled.

Grim

Quote from: Caraise Link on December 29, 2016, 05:43:58 PM
Okay, so I love your code comments. Thank you for being the sort of person who comments~

I always kick myself if I don't at least leave myself a comment to refer back to haha.


Quote from: Caraise Link on December 29, 2016, 05:43:58 PM
Fun gif! Going to have to see if the abrupt change in the power output looks less odd in normal gameplay, rather than changing it through debug functions. (Though I need to play with those, too, since I haven't.) In the worst case, I may have to figure something out for gradually adjusting the power factor over a given amount of time. Which would preferably be in direct proportion to however long it takes weather conditions, which would require reaching into other things, so hopefully I won't actually feel the need to go totally OCD over this.

This can be done and everything you would need has already been used at least once in the mod so far :) I won't post the code changes here but I'll be sure to comment them in file so you can read through and see whats been removed and changed, I'll leave the old code bits in there all commented out. Just check the GitHub link and re-download the folder.

Quote from: Caraise Link on December 29, 2016, 05:43:58 PM
I did a quick google for how badly solar panels are generally affected by weather, and used the only rough numerical estimate I could find to fudge some numbers for all of the various weather conditions. (Minus Clear and Dry Thunderstorm, naturally.) I also discovered that unbeknownst to me, Toxic Fallout already had a slight penalty to solar power, so I don't have to figure out how to work with map conditions. I'll play with it for a while, probably tweak it a bit, and let you know how it goes.

Do let me know! Tinkering is the best part, you find out you can do things you didn't think about and suddenly the mod is getting a little bigger :p

Grim

#11
Quote from: Caraise Link on December 29, 2016, 09:05:08 PM
Quote from: Grim on December 29, 2016, 03:51:51 PM
Quote from: skullywag on December 29, 2016, 03:36:32 PM
based on whether what?

*Drum roll*

I think they mean *Weather

Oh, snap! I totally did do that! And I'm usually so good with homonyms!...

[Composes herself] Anyway, I keep getting this error:



My perilous predisposition for puns aside, I'm not sure where I went wrong. I modified the relevant portion of the .xml to read as:

      <li Class="CompProperties_Power">
        <compClass>WeatherableSolarFluctuations.CompPowerPlantSolarWeather</compClass>


And I modified the relevant portion of the .cs to read as:

namespace WeatherableSolarFluctuations
{
    [StaticConstructorOnStartup]
    public class CompPowerPlantSolarWeather : CompPowerPlant
    {


I changed a name or two, but it's the same structure as what you were using, so I'm not sure how I broke it. I did alter the code a bit, so maybe I introduced an error that caused the script to not be loaded, which caused a "could not find" error? I can't figure out how to get Mono's error checking properly hooked up to UnityEngine or RimWorld, so it's really quite possible I'm creating a compile error and don't know it. I would've expected an error message for that, though, so I'm baffled.

Well you're referencing the <compClass> correctly so it must be one of the other changes you made I think you may have made a syntax error in the file as you said so the class isnt compiling and therefore doesn't exist.

I would suggest re-downloading the whole thing of GitHub again since I've made changes for it to nicely change power output when the weather changes, then make your changes again one a time :)



Obligatory Grim likes gifs

http://imgur.com/a/e12UT

Also: I would get visual studio community edition (which is free) mono develop just isn't very good to the point  Unity3d who develop it have created a visual studio plugin so you can develop for unity in visual studio instead because of the amount of problems it causes.

Kyna Tiona

Whoa, that does nicely change the power output. Other four weather defs added, code OCD satisfied, testing commencing~

Kyna Tiona

So I'd noticed some weird behavior in the solar panels that didn't seem to make sense with the changes I'd made to the code, so I'd been scrounging around trying to figure out what was going on. Logger.Message() didn't seem to be doing anything, even though I knew that block was firing, so I was having trouble pinning it down.

...At which point I realized that the .cs may be what the code is compiled from, but it appears the .dll sitting in SolarWeather\Assemblies is what the game is actually running. Which explained a lot, since it confirms that no, Logger.Message() works, the block really wasn't actually firing. >_<

So, for anyone who's been reading this to get a handle on modding: editing XML in notepad (or Notepad++, which is free and excellent) is okay, but editing C# script in notepad is pointless. You can change it, (albeit with more difficulty), but you can't compile it without Mono or Visual Studio or something, so the game can't actually read it. Both of those are free and occasionally sworn by, so check them out~


Grim

Quote from: Caraise Link on December 31, 2016, 09:12:46 PM
So I'd noticed some weird behavior in the solar panels that didn't seem to make sense with the changes I'd made to the code, so I'd been scrounging around trying to figure out what was going on. Logger.Message() didn't seem to be doing anything, even though I knew that block was firing, so I was having trouble pinning it down.

...At which point I realized that the .cs may be what the code is compiled from, but it appears the .dll sitting in SolarWeather\Assemblies is what the game is actually running. Which explained a lot, since it confirms that no, Logger.Message() works, the block really wasn't actually firing. >_<

So, for anyone who's been reading this to get a handle on modding: editing XML in notepad (or Notepad++, which is free and excellent) is okay, but editing C# script in notepad is pointless. You can change it, (albeit with more difficulty), but you can't compile it without Mono or Visual Studio or something, so the game can't actually read it. Both of those are free and occasionally sworn by, so check them out~




Done that myself haha.

Also what you may be looking for is Log.Message() to log things to the debug log :) Logger.Message logs to unity's debug log, which isn't the one in game (I think anyway :p, I use Log.Message() because it just works)