Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: [starting relationship via rules] things that you`ll eventually step into...  (Read 4684 times)

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff

Hi.

Tried to create my own rule, but it doesn`t seem to work. The point of this rule is to create starting relationships between player and other factions based on player choices.

The rule is packed into a jar and put in 'package com.fs.starfarer.api.impl.campaign.rulecmd' as it must be present there in order to use it in rules.csv. Here is the code that it uses:


Spoiler
Quote
package com.fs.starfarer.api.impl.campaign.rulecmd;

import java.util.List;
import java.util.Map;

import com.fs.starfarer.api.Global;
import com.fs.starfarer.api.campaign.InteractionDialogAPI;
//import com.fs.starfarer.api.campaign.ReputationActionResponsePlugin.ReputationAdjustme ntResult;
import com.fs.starfarer.api.campaign.RepLevel;
import com.fs.starfarer.api.campaign.rules.MemoryAPI;
//import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActionEnvelope;
//import com.fs.starfarer.api.impl.campaign.CoreReputationPlugin.RepActions;
import com.fs.starfarer.api.util.Misc.Token;

/**
 * SetRep <factionId> <RepActions action>
 */
public class SetStartingRep extends BaseCommandPlugin {
  
   public boolean execute(String ruleId, InteractionDialogAPI dialog, List<Token> params, Map<String, MemoryAPI> memoryMap) {
      String Faction = params.get(0).getString(memoryMap);

        String prefix = "";

        if (Faction.contains("rsf"))
        {
            Global.getSector().getFaction("player").setRelationship("ISA", -0.2f);
            Global.getSector().getFaction("player").setRelationship("XLE", 0f);
            Global.getSector().getFaction("player").setRelationship("UIN", 0f);
            Global.getSector().getFaction("player").setRelationship("FFS", -0.1f);
            prefix = "RSF";

        }

        else if (Faction.contains("xle"))
        {
            Global.getSector().getFaction("player").setRelationship("ISA", 0f);
            Global.getSector().getFaction("player").setRelationship("RSF", 0f);
            Global.getSector().getFaction("player").setRelationship("UIN", 0f);
            Global.getSector().getFaction("player").setRelationship("FFS", -0.5f);
            prefix = "XLE";
        }

        else if (Faction.contains("uin"))
        {
            Global.getSector().getFaction("player").setRelationship("ISA", 0f);
            Global.getSector().getFaction("player").setRelationship("RSF", 0f);
            Global.getSector().getFaction("player").setRelationship("XLE", 0f);
            Global.getSector().getFaction("player").setRelationship("FFS", -0.5f);
            prefix = "UIN";
        }

        else if (Faction.contains("isa"))
        {
            Global.getSector().getFaction("player").setRelationship("UIN", 0f);
            Global.getSector().getFaction("player").setRelationship("RSF", -0.1f);
            Global.getSector().getFaction("player").setRelationship("XLE", 0f);
            Global.getSector().getFaction("player").setRelationship("FFS", -0.1f);
            prefix = "ISA";
        }

        if (Faction.contains("_t"))
        {
            Global.getSector().getFaction("player").setRelationship(prefix, RepLevel.NEUTRAL);
            Global.getSector().getFaction("player").setRelationship("independent",RepLevel.FAVORABLE);
            Global.getSector().getFaction("player").setRelationship("pirates", RepLevel.HOSTILE);
        }

        else if (Faction.contains("_s"))
        {
            Global.getSector().getFaction("player").setRelationship(prefix, RepLevel.SUSPICIOUS);
            Global.getSector().getFaction("player").setRelationship("independents", RepLevel.NEUTRAL);
            Global.getSector().getFaction("player").setRelationship("pirates", RepLevel.SUSPICIOUS);
        }

        else if (Faction.contains("_b"))
        {
            Global.getSector().getFaction("player").setRelationship(prefix, RepLevel.FAVORABLE);
            Global.getSector().getFaction("player").setRelationship("independents", RepLevel.NEUTRAL);
            Global.getSector().getFaction("player").setRelationship("pirates",RepLevel.HOSTILE);
        }

        return !Faction.isEmpty();
   }
}
[close]

The rules.csv has ' SetStartingRep "rsf_t" ' as one of the options. I don`t get any errors nither in the way of crashes or any entries in the log. The rule seems to be running (as it compiles without problems), but has no effect.

A little example. If you select trader and then RSF as your faction - you get a call for SetStartingRep with "rsf_t" string, so it should trigger the 'contains "rsf"' check and 'contains "_t"' check. Those should set pirates and ISA to be hostile towards you, but i get only neutral stance with all factions.

Guess there is something i`m doing wrong with the return value or this whole thing should not work that way at all.

I wonder what is generated first - player or factions? If it is the later - than there is no way to set up starting relationship at all via rules other than somehow wire your choices with SectorGen.
« Last Edit: November 06, 2014, 11:32:52 PM by Okim »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24123
    • View Profile
Re: [rules] things that you`ll eventually step into...
« Reply #1 on: November 06, 2014, 01:29:31 PM »

Seeing the relevant rules you've got in your rules.csv might help here.

Also, you can try adding a breakpoint with a debugger to make sure you're actually ending up in the command code when you expect to be.
Logged

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff
Re: [rules] things that you`ll eventually step into...
« Reply #2 on: November 06, 2014, 01:37:17 PM »

Here is it:

Quote
ngcRSF_T,NewGameOptionSelected,"$option == ngcRSF
$career == trader","NGCSetStartingLocation lomonosov 3000 -3000
SetStartingRep ""rsf_t""
NGCAddShip ""rsf7-trn-mule1_variant""
AddText ""Added Mule-class freighter"" textFriendColor
NGCAddCargo RESOURCES regular_crew 2
NGCAddCargo RESOURCES supplies 10
AddText ""Added 2 crew"" textFriendColor
AddText ""Added 10 supplies"" textFriendColor
NGCAddCargo RESOURCES fuel 10
AddText ""Added 10 fuel"" textFriendColor
FireBest NGCStep3",,,
« Last Edit: November 06, 2014, 01:38:55 PM by Okim »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24123
    • View Profile
Re: [rules] things that you`ll eventually step into...
« Reply #3 on: November 06, 2014, 02:22:27 PM »

Looks like it should be ok at first glance.

One thing to check is to make sure you don't have a rule with the same id somewhere else, that could overwrite this rule silently.

Are you seeing the right text show up when you click on the option? (i.e. "Added Mule-class Freighter"). I'd change that text and check the modified version shows up just to make sure you're picking up the right rule.

If that bears out and it's still not working, I'd really suggest using the debugger.
Logged

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff
Re: [rules] things that you`ll eventually step into...
« Reply #4 on: November 06, 2014, 02:54:47 PM »

I wonder what happens first - this particular player creation takes place or the relationship generation from sector gen? If it is the first - wont all the factions be null at the moment of this player generation rule taking place?

If it is so - than i see the problem that happens here.

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24123
    • View Profile
Re: [rules] things that you`ll eventually step into...
« Reply #5 on: November 06, 2014, 03:02:09 PM »

Ah, that makes sense - yeah, sector generation will take place after all that.
Logged

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff
Re: [rules] things that you`ll eventually step into...
« Reply #6 on: November 06, 2014, 03:14:38 PM »

Hm. So all i need is to somehow pass the choices the player have made into sectorgen relationship init function, right? I do remember i`ve done it before with the old player generation plugin. But how should i do it now?

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24123
    • View Profile
Re: [rules] things that you`ll eventually step into...
« Reply #7 on: November 06, 2014, 03:27:05 PM »

I'd say via a static variable somewhere. Have a class with all the data you want, have commands modify it, and then store an instance of that class in a static variable.

NewGameDialogPluginImpl is also something to look at, just for understanding the flow a bit better. You can provide a custom implementation of this too, but I don't *think* it's necessary here.
Logged

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff
Re: [rules] things that you`ll eventually step into...
« Reply #8 on: November 06, 2014, 11:25:29 PM »

Ok. Judging from the doc about rules I can use a variable that can be stored by those rules in memory for a specified number of game days. What if I run a new rule right at the start of the game when everything is generated? If I understand everything correctly - I can call this stored variable, check what it has and use it in my SetStartingRep, right?

I just need to figure out how to call up a rule right at the start of the game and if this wont be too problematic to code (harder than writing a static variable export/import).

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24123
    • View Profile

Not exactly. The memory isn't just magically always there, it has to be created by whoever is calling the rule. In the case of new game creation, it's in NewGameDialogPluginImpl, like so:

Code: java
entity = dialog.getInteractionTarget();
memory = entity.getMemoryWithoutUpdate();
data = (CharacterCreationData) memory.get("$characterData");
memoryMap = new HashMap<String, MemoryAPI>();
memoryMap.put(MemKeys.LOCAL, memory);
memoryMap.put(MemKeys.GLOBAL, Global.getFactory().createMemory());

The "entity" this code refers to is actually just a faked-up entity created by the new game creation process, because an entity is needed for an interaction dialog to work. The "global" memory is likewise a separate instance; the proper global memory used by the campaign doesn't exist at that point. So, these memory objects aren't going to be available once the campaign starts.

That's why I'm suggesting using a static object somewhere, to make sure it's something you can access from whatever script you run on game start to make the proper tweaks based on game creation choices.


(For entity interactions in the campaign, the RuleBasedInteractionDialogPlugin does the work of getting the right memory objects, updating them, and putting them into memoryMap, so you don't have to worry about it.)
Logged

Okim

  • Admiral
  • *****
  • Posts: 2161
    • View Profile
    • Okim`s Modelling stuff

Well... i found a much easier way to achieve what i want without messing with memory and static variables.

The thing is pretty simple - for each specific choice you get 1 specific commodity that differs per trader/smuggler/hunter (say - ore, rare ore/organics) and 1 more specific commodity per each faction. Once the game starts (after NCG) it calls a checker command that compares your cargo with what is specified in it`s conditions. If two items match a condition - you are supposed to have picked that particular options during player creation and thus are subject to that specific relationship changes.

Might look pretty messy and non-professional at all, but actually it is quite simple and somewhat creative :P


Ignore this :) First of all - cargo can be accessed only through fleet which is again not created by the time this rep changing function is called (resulting in NPE).

So a static variable is indeed the only (and freakingly easy) way to fix this problem.
« Last Edit: November 07, 2014, 01:29:49 PM by Okim »
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24123
    • View Profile

Alright, ignoring :)
Logged