Skip to content

Commit

Permalink
v1.5.0
Browse files Browse the repository at this point in the history
* Updated to MHv5 and TML11 (including configs)
  • Loading branch information
hamstar0 committed Jul 22, 2019
1 parent ea5b86d commit bd91f2b
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 179 deletions.
3 changes: 0 additions & 3 deletions API.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
namespace Capitalism {
public static partial class CapitalismAPI {
public static CapitalismConfigData GetModSettings() {
return CapitalismMod.Instance.Config;
}
}
}
12 changes: 5 additions & 7 deletions Capitalism.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
<ItemGroup>
<Reference Include="HamstarHelpers, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\Helpers\HamstarHelpers\bin\Release\HamstarHelpers.dll</HintPath>
<HintPath>..\..\Mod Helpers\v5 (tML11)\HamstarHelpers\bin\Release\HamstarHelpers.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\..\..\Reference\tModLoader Patcher\references\Newtonsoft.Json.dll</HintPath>
<HintPath>..\..\..\..\..\My Games\Terraria\ModLoader\references\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="ReLogic">
<HintPath>..\..\..\..\..\My Games\Terraria\ModLoader\references\ReLogic.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -54,10 +57,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Terraria\Terraria.exe</HintPath>
</Reference>
<Reference Include="Terraria.Libraries.ReLogic.ReLogic, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\Reference\Terraria.Libraries.ReLogic.ReLogic.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="API.cs" />
Expand All @@ -71,7 +70,6 @@
<Compile Include="Logic\VendorLogic.cs" />
<Compile Include="CapitalismPlayer.cs" />
<Compile Include="NetProtocol\WorldDataProtocol.cs" />
<Compile Include="NetProtocol\ModSettingsProtocol.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
24 changes: 4 additions & 20 deletions CapitalismMod.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using HamstarHelpers.Components.Config;
using HamstarHelpers.Helpers.DebugHelpers;
using HamstarHelpers.Helpers.TmlHelpers.ModHelpers;
using HamstarHelpers.Helpers.TModLoader.Mods;
using System;
using Terraria.ModLoader;

Expand All @@ -13,34 +11,20 @@ partial class CapitalismMod : Mod {

////////////////

public JsonConfig<CapitalismConfigData> ConfigJson { get; private set; }
public CapitalismConfigData Config => this.ConfigJson.Data;
public CapitalismConfig Config { get; private set; }



////////////////

public CapitalismMod() {
string filename = "Capitalism Config.json";
this.ConfigJson = new JsonConfig<CapitalismConfigData>( filename, ConfigurationDataBase.RelativePath, new CapitalismConfigData() );
this.Config = new CapitalismConfig();
}

////////////////

public override void Load() {
CapitalismMod.Instance = this;

this.LoadConfig();
}

private void LoadConfig() {
if( !this.ConfigJson.LoadFile() ) {
this.ConfigJson.SaveFile();
}

if( this.Config.UpdateToLatestVersion() ) {
LogHelpers.Log( "Capitalism updated to " + this.Version.ToString() );
this.ConfigJson.SaveFile();
}
}

public override void Unload() {
Expand Down
32 changes: 1 addition & 31 deletions CapitalismMod_Meta.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
using HamstarHelpers.Components.Config;
using HamstarHelpers.Helpers.DebugHelpers;
using System;
using System.IO;
using Terraria;
using System;
using Terraria.ModLoader;


namespace Capitalism {
partial class CapitalismMod : Mod {
public static string GithubUserName => "hamstar0";
public static string GithubProjectName => "tml-capitalism-mod";

public static string ConfigFileRelativePath {
get { return ConfigurationDataBase.RelativePath + Path.DirectorySeparatorChar + CapitalismConfigData.ConfigFileName; }
}
public static void ReloadConfigFromFile() {
if( Main.netMode != 0 ) {
throw new Exception( "Cannot reload configs outside of single player." );
}
if( CapitalismMod.Instance != null ) {
if( !CapitalismMod.Instance.ConfigJson.LoadFile() ) {
CapitalismMod.Instance.ConfigJson.SaveFile();
}
}
}

public static void ResetConfigFromDefaults() {
if( Main.netMode != 0 ) {
throw new Exception( "Cannot reset to default configs outside of single player." );
}

var newConfig = new CapitalismConfigData();
//new_config.SetDefaults();

CapitalismMod.Instance.ConfigJson.SetData( newConfig );
CapitalismMod.Instance.ConfigJson.SaveFile();
}
}
}
1 change: 0 additions & 1 deletion CapitalismNPC.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HamstarHelpers.Components.Errors;
using HamstarHelpers.Helpers.DebugHelpers;
using System;
using Terraria;
using Terraria.ModLoader;
Expand Down
16 changes: 3 additions & 13 deletions CapitalismPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
using Terraria.ModLoader.IO;
using Capitalism.Logic;
using Capitalism.NetProtocol;
using HamstarHelpers.Services.Promises;
using HamstarHelpers.Components.Network;
using HamstarHelpers.Helpers.DebugHelpers;
using HamstarHelpers.Services.Hooks.LoadHooks;


namespace Capitalism {
Expand Down Expand Up @@ -46,13 +44,6 @@ public override void OnEnterWorld( Player player ) {

var mymod = (CapitalismMod)this.mod;

if( Main.netMode == 0 ) {
if( !mymod.ConfigJson.LoadFile() ) {
mymod.ConfigJson.SaveFile();
LogHelpers.Alert( "Capitalism config " + mymod.Version.ToString() + " created." );
}
}

if( Main.netMode == 0 ) {
this.OnSingleConnect();
}
Expand All @@ -67,9 +58,8 @@ private void OnSingleConnect() {
}

private void OnClientConnect() {
Promises.AddWorldLoadOncePromise( () => {
PacketProtocolRequestToServer.QuickRequest<ModSettingsProtocol>( -1 );
PacketProtocolRequestToServer.QuickRequest<WorldDataProtocol>( -1 );
LoadHooks.AddWorldLoadOnceHook( () => {
WorldDataProtocol.QuickRequest();
} );
}

Expand Down
4 changes: 2 additions & 2 deletions CapitalismWorld.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HamstarHelpers.Helpers.WorldHelpers;
using HamstarHelpers.Helpers.World;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

Expand All @@ -9,7 +9,7 @@ class CapitalismWorld : ModWorld {
public string ID {
get {
if( string.IsNullOrEmpty(this._ID) ) {
return WorldHelpers.GetUniqueId(true);
return WorldHelpers.GetUniqueIdForCurrentWorld(true);
} else {
return this._ID;
}
Expand Down
57 changes: 38 additions & 19 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,65 @@
using HamstarHelpers.Components.Config;
using HamstarHelpers.Helpers.User;
using System;
using System.ComponentModel;
using Terraria;
using Terraria.ModLoader.Config;


namespace Capitalism {
public class CapitalismConfigData : ConfigurationDataBase {
public readonly static string ConfigFileName = "Capitalism Config.json";
public class CapitalismConfig : ModConfig {
public override ConfigScope Mode => ConfigScope.ServerSide;


////////////////

public string VersionSinceUpdate = "";
////////////////

[DefaultValue( true )]
public bool Enabled = true;


[Tooltip( "A factor for computing markup." )]
[DefaultValue( 0.8f )]
public float MarkupExponent = 0.8f;

[Tooltip( "A factor for computing markup." )]
[DefaultValue( 50f )]
public float MarkupDivisor = 50f;

[Tooltip("% markup if a Tax Collector NPC has moved in.")]
[DefaultValue( 1.02f )]
public float TaxMarkupPercent = 1.02f;

[Tooltip("% markup after an NPC has died.")]
[DefaultValue( 1.5f )]
public float InfuriationMarkupPercent = 1.5f;


[Tooltip("% that markup prices 'decay' twice per day.")]
[DefaultValue( 0.95f )]
public float BiDailyDecayMarkdownPercent = 0.95f;

public float FemaleBloodMoonMarkupPercent = 1.1f;
public float LovestruckMarkdownPercent = 0.9f;
public float StinkyMarkupPercent = 1.1f;

[Tooltip("% scale during a blood moon from female NPCs.")]
[DefaultValue( 1.1f )]
public float FemaleBloodMoonSellPricePercent = 1.1f;

[Tooltip( "% scale from a 'lovestruck' NPC." )]
[DefaultValue( 0.9f )]
public float LovestruckSellPricePercent = 0.9f;

////////////////
[Tooltip( "% scale from a 'stinky' NPC." )]
[DefaultValue( 1.1f )]
public float StinkySellPricePercent = 1.1f;

public bool UpdateToLatestVersion() {
var mymod = CapitalismMod.Instance;
var newConfig = new CapitalismConfigData();
var versSince = this.VersionSinceUpdate != "" ?
new Version( this.VersionSinceUpdate ) :
new Version();

if( versSince >= mymod.Version ) {
return false;
}

this.VersionSinceUpdate = mymod.Version.ToString();
////////////////

public override bool AcceptClientChanges( ModConfig pendingConfig, int whoAmI, ref string message ) {
if( !UserHelpers.HasBasicServerPrivilege(Main.player[whoAmI]) ) {
message = "Insufficient privilege.";
return false;
}
return true;
}
}
Expand Down
27 changes: 13 additions & 14 deletions Logic/CapitalismLogic.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using HamstarHelpers.Components.Config;
using HamstarHelpers.Helpers.DebugHelpers;
using HamstarHelpers.Helpers.ItemHelpers;
using HamstarHelpers.Helpers.PlayerHelpers;
using HamstarHelpers.Helpers.WorldHelpers;
using HamstarHelpers.Helpers.Debug;
using HamstarHelpers.Helpers.Items;
using HamstarHelpers.Helpers.Players;
using HamstarHelpers.Helpers.World;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;


Expand Down Expand Up @@ -54,8 +53,8 @@ public void LoadVendorsForCurrentPlayer( CapitalismMod mymod, TagCompound tags,
string jsonTotalPurchases = tags.GetString( worldId + "_vendor_total_purchases_str_" + i );
string jsonTotalSpendings = tags.GetString( worldId + "_vendor_total_spendings_str_" + i );

float[] totalPurchases = JsonConfig<float[]>.Deserialize( jsonTotalPurchases );
float[] totalSpendings = JsonConfig<float[]>.Deserialize( jsonTotalSpendings );
float[] totalPurchases = JsonConvert.DeserializeObject<float[]>( jsonTotalPurchases );
float[] totalSpendings = JsonConvert.DeserializeObject<float[]>( jsonTotalSpendings );

//if( (DebugHelper.DEBUGMODE & 1) > 0 ) {
// ErrorLogger.Log( " load " + world_id + "_vendor_npc_types_" + i + ": " + npc_type );
Expand Down Expand Up @@ -97,8 +96,8 @@ public void SaveVendorsForCurrentPlayer( TagCompound tags ) {
float[] totalPurchases, totalSpendings;

vendor.SaveTotalSpendings( out totalPurchaseTypes, out totalSpendingsTypes, out totalPurchases, out totalSpendings );
string jsonTotalPurchases = JsonConfig<float[]>.Serialize( totalPurchases );
string jsonTotalSpendings = JsonConfig<float[]>.Serialize( totalSpendings );
string jsonTotalPurchases = JsonConvert.SerializeObject( totalPurchases );
string jsonTotalSpendings = JsonConvert.SerializeObject( totalSpendings );

tags.Set( worldId + "_vendor_npc_types_" + i, npcType );
tags.Set( worldId + "_vendor_total_purchase_types_" + i, totalPurchaseTypes );
Expand Down Expand Up @@ -137,7 +136,7 @@ public void Update( Player player ) {
var mymod = CapitalismMod.Instance;

if( player.talkNPC != -1 ) {
long money = PlayerItemHelpers.CountMoney( player );
long money = PlayerItemHelpers.CountMoney( player, false );
long spent = this.LastMoney - money;

this.LastMoney = money;
Expand Down Expand Up @@ -182,12 +181,12 @@ public void Update( Player player ) {
private Item AccountForPurchase( Player player, long spent, Item lastBuyItem ) {
NPC talkNpc = Main.npc[player.talkNPC];
if( talkNpc == null || !talkNpc.active ) {
ErrorLogger.Log( "AccountForPurchase - No shop npc." );
LogHelpers.Log( "AccountForPurchase - No shop npc." );
return null;
}

var mymod = CapitalismMod.Instance;
ISet<int> possiblePurchases = PlayerItemFinderHelpers.FindPossiblePurchaseTypes( player, spent );
ISet<int> possiblePurchases = ItemFinderHelpers.FindPossiblePurchaseTypes( player.inventory, spent );
Item item = null;
int stack = 1;

Expand Down Expand Up @@ -217,7 +216,7 @@ private Item AccountForPurchase( Player player, long spent, Item lastBuyItem ) {
}
if( item == null ) {
if( lastBuyItem != null ) {
var vendor = this.GetOrCreateVendor( WorldHelpers.GetUniqueId(true), talkNpc.type );
var vendor = this.GetOrCreateVendor( WorldHelpers.GetUniqueIdForCurrentWorld(true), talkNpc.type );
int value = (int)vendor.GetPriceOf( lastBuyItem.type );

if( (spent % value) == 0 ) {
Expand Down
8 changes: 4 additions & 4 deletions Logic/VendorLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using Terraria;
using System;
using HamstarHelpers.Helpers.NPCHelpers;
using HamstarHelpers.Helpers.NPCs;


namespace Capitalism.Logic {
Expand Down Expand Up @@ -110,7 +110,7 @@ private int UpdateShopItem( Item item, Chest shop = null ) {
// Female NPCs during a bloodmoon markup their prices
bool isGrill = NPCTownHelpers.GetFemaleTownNpcTypes().Contains( this.NpcType );
if( Main.bloodMoon && isGrill ) {
price = (int)((float)price * mymod.Config.FemaleBloodMoonMarkupPercent);
price = (int)((float)price * mymod.Config.FemaleBloodMoonSellPricePercent);
}

NPC npc = NPCFinderHelpers.FindFirstNpcByType( this.NpcType );
Expand All @@ -119,15 +119,15 @@ private int UpdateShopItem( Item item, Chest shop = null ) {
if( npc != null && player != null ) {
// Stinky players markup prices
if( player.FindBuffIndex( 120 ) >= 0 ) {
price = (int)((float)price * mymod.Config.StinkyMarkupPercent);
price = (int)((float)price * mymod.Config.StinkySellPricePercent);
}

// Love struck NPCs markdown prices
if( npc.FindBuffIndex( 119 ) >= 0 ) {
bool isGendered = !NPCTownHelpers.GetNonGenderedTownNpcTypes().Contains( this.NpcType );

if( isGendered && (player.Male && isGrill) || (!player.Male && !isGrill) ) {
price = (int)((float)price * mymod.Config.LovestruckMarkdownPercent);
price = (int)((float)price * mymod.Config.LovestruckSellPricePercent);
}
}
}
Expand Down
Loading

0 comments on commit bd91f2b

Please sign in to comment.