Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
* New formula for item markup (b=base value, t=total purchases): b + ( (b * t)^0.8 ) / 50
* Fixed semi-critical bulk purchases bug
  • Loading branch information
hamstar committed May 8, 2017
1 parent dca3c16 commit f51d7df
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 81 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vs/
bin/
obj/
Properties/
Properties/
.gitignore
2 changes: 1 addition & 1 deletion Capitalism.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<Compile Include="CapitalismMod.cs" />
<Compile Include="CapitalismNetProtocol.cs" />
<Compile Include="CapitalismNPC.cs" />
<Compile Include="Utils\Debug.cs" />
<Compile Include="Utils\DebugHelper.cs" />
<Compile Include="Logic\VendorLogic.cs" />
<Compile Include="CapitalismPlayer.cs" />
<Compile Include="CapitalismWorld.cs" />
Expand Down
11 changes: 7 additions & 4 deletions CapitalismMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
namespace Capitalism {
public class ConfigurationData {
public string VersionSinceUpdate = "";
public float MarkupPercent = 0.02f;

public float MarkupExponent = 0.8f;
public float MarkupDivisor = 50f;
public float TaxMarkupPercent = 1.02f;
public float InfuriationMarkupPercent = 1.5f;

public float BiDailyDecayMarkdownPercent = 0.95f;
public float MarkupCapExponentBase = 0.996f;

public float FemaleBloodMoonMarkupPercent = 1.1f;
public float LovestruckMarkdownPercent = 0.9f;
public float StinkyMarkupPercent = 1.1f;
Expand All @@ -22,7 +25,7 @@ public class ConfigurationData {


public class CapitalismMod : Mod {
public readonly static Version ConfigVersion = new Version(1, 2, 2);
public readonly static Version ConfigVersion = new Version(1, 3, 0);
public JsonConfig<ConfigurationData> Config { get; private set; }


Expand Down Expand Up @@ -85,7 +88,7 @@ public override void HandlePacket( BinaryReader reader, int whoAmI ) {
////////////////

public override void PostDrawInterface( SpriteBatch sb ) {
Debug.PrintToBatch( sb );
DebugHelper.PrintToBatch( sb );
}
}
}
11 changes: 6 additions & 5 deletions CapitalismPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CapitalismPlayer : ModPlayer {

public override void Initialize() {
if( this.Logic == null ) {
this.Logic = new CapitalismLogic( (CapitalismMod)this.mod );
this.Logic = new CapitalismLogic();
}
}

Expand Down Expand Up @@ -43,11 +43,12 @@ public override void OnEnterWorld( Player player ) {

public override void Load( TagCompound tags ) {
try {
var mymod = (CapitalismMod)this.mod;
int total_worlds = tags.GetInt( "vendor_world_count" );

for( int i=0; i< total_worlds; i++ ) {
string id = tags.GetString( "vendor_world_id_" + i );
this.Logic.LoadVendorsForCurrentPlayer( tags, id );
this.Logic.LoadVendorsForCurrentPlayer( mymod, tags, id );
}
} catch( Exception e ) {
ErrorLogger.Log( e.ToString() );
Expand All @@ -72,21 +73,21 @@ public override TagCompound Save() {
////////////////

public override void PreUpdate() {
this.Logic.Update( this.player );
this.Logic.Update( (CapitalismMod)this.mod, this.player );
}


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

public void UpdateGivenShop( int npc_type, Chest shop, ref int nextSlot ) {
if( this.Logic != null ) {
this.Logic.UpdateGivenShop( this.player, npc_type, shop, ref nextSlot );
this.Logic.UpdateGivenShop( (CapitalismMod)this.mod, this.player, npc_type, shop, ref nextSlot );
}
}

public bool InfuriateVendor( int npc_type ) {
if( this.Logic != null ) {
return this.Logic.InfuriateVendor( npc_type );
return this.Logic.InfuriateVendor( (CapitalismMod)this.mod, npc_type );
}
return false;
}
Expand Down
103 changes: 65 additions & 38 deletions Logic/CapitalismLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,28 @@ namespace Capitalism.Logic {
class CapitalismLogic {
private static bool IsDay = false;

private CapitalismMod MyMod;

public IDictionary<string, IDictionary<int, VendorLogic>> VendorWorlds { get; private set; }
private IDictionary<long, double> SellPrices = new Dictionary<long, double>();

private long LastMoney = 0;
private Item LastBuyItem = null;
private IDictionary<int, KeyValuePair<int, int>> PrevInventoryInfos = new Dictionary<int, KeyValuePair<int, int>>();
private KeyValuePair<int, int> PrevMouseInfo;



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

public CapitalismLogic( CapitalismMod mymod ) {
public CapitalismLogic() {
this.VendorWorlds = new Dictionary<string, IDictionary<int, VendorLogic>>();
this.MyMod = mymod;
}

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

public void LoadVendorsForCurrentPlayer( TagCompound tags, string world_id ) {
public void LoadVendorsForCurrentPlayer( CapitalismMod mymod, TagCompound tags, string world_id ) {
try {
int vendor_count = tags.GetInt( world_id + "_vendor_count" );
if( (Debug.DEBUGMODE & 1) > 0 ) {
if( (DebugHelper.DEBUGMODE & 1) > 0 ) {
ErrorLogger.Log( " load vendor count: " + vendor_count );
}

Expand All @@ -57,7 +55,7 @@ public void LoadVendorsForCurrentPlayer( TagCompound tags, string world_id ) {
float[] total_purchases = JsonConfig<float[]>.Deserialize( json_total_purchases );
float[] total_spendings = JsonConfig<float[]>.Deserialize( json_total_spendings );

if( (Debug.DEBUGMODE & 1) > 0 ) {
if( (DebugHelper.DEBUGMODE & 1) > 0 ) {
ErrorLogger.Log( " load " + world_id + "_vendor_npc_types_" + i + ": " + npc_type );
ErrorLogger.Log( " load " + world_id + "_vendor_total_purchase_types_" + i + ": " + string.Join( ",", total_purchase_types ) );
ErrorLogger.Log( " load " + world_id + "_vendor_total_spendings_types_" + i + ": " + string.Join( ",", total_spendings_types ) );
Expand All @@ -67,7 +65,7 @@ public void LoadVendorsForCurrentPlayer( TagCompound tags, string world_id ) {

vendors[npc_type] = VendorLogic.Create( npc_type );
if( vendors[npc_type] != null ) {
vendors[npc_type].LoadTotalPurchases( this.MyMod, total_purchase_types, total_spendings_types, total_purchases, total_spendings );
vendors[npc_type].LoadTotalPurchases( mymod, total_purchase_types, total_spendings_types, total_purchases, total_spendings );
}
}
} catch( Exception e ) {
Expand All @@ -82,7 +80,7 @@ public void SaveVendorsForCurrentPlayer( TagCompound tags ) {
IDictionary<int, VendorLogic> vendors = world_vendors.Value;

tags.Set( world_id + "_vendor_count", vendors.Count );
if( (Debug.DEBUGMODE & 1) > 0 ) {
if( (DebugHelper.DEBUGMODE & 1) > 0 ) {
ErrorLogger.Log( " save " + world_id + "_vendor_count: " + vendors.Count );
}

Expand All @@ -106,7 +104,7 @@ public void SaveVendorsForCurrentPlayer( TagCompound tags ) {
tags.Set( world_id + "_vendor_total_purchases_str_" + i, json_total_purchases );
tags.Set( world_id + "_vendor_total_spendings_str_" + i, json_total_spendings );

if( (Debug.DEBUGMODE & 1) > 0 ) {
if( (DebugHelper.DEBUGMODE & 1) > 0 ) {
ErrorLogger.Log( " save " + world_id + "_vendor_npc_types_" + i + ": " + (int)npc_type );
ErrorLogger.Log( " save " + world_id + "_vendor_total_purchase_types_" + i + ": " + String.Join( ",", total_purchase_types ) );
ErrorLogger.Log( " save " + world_id + "_vendor_total_spendings_types_" + i + ": " + String.Join( ",", total_spendings_types ) );
Expand All @@ -120,18 +118,18 @@ public void SaveVendorsForCurrentPlayer( TagCompound tags ) {

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

private bool IsReady( Player player ) {
private bool IsReady( CapitalismMod mymod, Player player ) {
if( Main.netMode == 2 ) { return false; } // Client of single only
if( player.whoAmI != Main.myPlayer ) { return false; } // Current player only
if( this.MyMod.GetModWorld<CapitalismWorld>().ID == "" ) { return false; }
if( mymod.GetModWorld<CapitalismWorld>().ID == "" ) { return false; }
if( this.StartupDelay++ < 60*2 ) { return false; }
return true;
}
private int StartupDelay = 0;


public void Update( Player player ) {
if( !this.IsReady( player ) ) {
public void Update( CapitalismMod mymod, Player player ) {
if( !this.IsReady( mymod, player ) ) {
CapitalismLogic.IsDay = Main.dayTime;
return;
}
Expand All @@ -140,10 +138,12 @@ public void Update( Player player ) {
long money = PlayerHelper.CountMoney( player );
long spent = this.LastMoney - money;

this.LastMoney = money;

if( spent > 0 ) {
this.AccountForPurchase( player, spent );
this.AccountForPurchase( mymod, player, spent, ref this.LastBuyItem );
} else if( spent < 0 ) {
this.AccountForSale( player, -spent );
this.AccountForSale( mymod, player, -spent );
}

// Snapshot current inventory
Expand All @@ -156,59 +156,84 @@ public void Update( Player player ) {
this.PrevInventoryInfos[i] = new KeyValuePair<int, int>( 0, 0 );
}
}

if( Main.mouseItem != null ) {
this.PrevMouseInfo = new KeyValuePair<int, int>( Main.mouseItem.type, Main.mouseItem.stack );
} else {
this.PrevMouseInfo = new KeyValuePair<int, int>( 0, 0 );
}

this.LastMoney = money;
}

// Advance day
if( CapitalismLogic.IsDay != Main.dayTime ) {
CapitalismLogic.IsDay = Main.dayTime;

this.DecayAllVendorPrices( player );
this.DecayAllVendorPrices( mymod, player );
}
}


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

private void AccountForPurchase( Player player, long spent ) {
private void AccountForPurchase( CapitalismMod mymod, Player player, long spent, ref Item last_buy_item ) {
var modworld = mymod.GetModWorld<CapitalismWorld>();
if( modworld.ID.Length == 0 ) {
ErrorLogger.Log( "AccountForPurchase - No world id set." );
return;
}
NPC talk_npc = Main.npc[player.talkNPC];
if( talk_npc == null || !talk_npc.active ) {
ErrorLogger.Log( "AccountForPurchase - No shop npc." );
return;
}
ISet<int> possible_purchases = PlayerHelper.FindPossiblePurchaseTypes( player, spent );

Item item = null;
int stack = 1;

if( possible_purchases.Count > 0 ) {
var changes_at = PlayerHelper.FindInventoryChanges( player, this.PrevMouseInfo, this.PrevInventoryInfos );
changes_at = ItemHelper.FilterByTypes( changes_at, possible_purchases );

if( changes_at.Count == 1 ) {
foreach( var entry in changes_at ) {
Item item;
if( entry.Key == -1 ) {
item = Main.mouseItem;
} else {
item = player.inventory[entry.Key];
}

if( item != null ) {
this.BoughtFrom( player, Main.npc[player.talkNPC], item, entry.Value.Value );
//stack = entry.Value.Value;
break;
}
}
}
} else {
if( last_buy_item != null ) {
var vendor = this.GetOrCreateVendor( modworld.ID, talk_npc.type );
int value = (int)vendor.GetPriceOf( mymod, last_buy_item.type );

if( (spent % value) == 0 ) {
item = last_buy_item;
stack = (int)(spent / value);
}
}
}

if( item != null ) {
this.BoughtFrom( mymod, player, talk_npc, item, stack );
last_buy_item = item;
}
}

private void AccountForSale( Player player, long earned ) {
private void AccountForSale( CapitalismMod mymod, Player player, long earned ) {
// TODO
}

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

public void UpdateGivenShop( Player player, int npc_type, Chest shop, ref int nextSlot ) {
var modworld = this.MyMod.GetModWorld<CapitalismWorld>();
public void UpdateGivenShop( CapitalismMod mymod, Player player, int npc_type, Chest shop, ref int nextSlot ) {
var modworld = mymod.GetModWorld<CapitalismWorld>();
if( modworld.ID.Length == 0 ) {
return;
}
Expand All @@ -219,11 +244,11 @@ public void UpdateGivenShop( Player player, int npc_type, Chest shop, ref int ne
return;
}

vendor.UpdateShop( this.MyMod, shop );
vendor.UpdateShop( mymod, shop );
}

public bool InfuriateVendor( int npc_type ) {
var modworld = this.MyMod.GetModWorld<CapitalismWorld>();
public bool InfuriateVendor( CapitalismMod mymod, int npc_type ) {
var modworld = mymod.GetModWorld<CapitalismWorld>();
if( modworld.ID.Length == 0 ) {
ErrorLogger.Log( "InfuriateVendor - No world id set." );
return false;
Expand All @@ -235,7 +260,7 @@ public bool InfuriateVendor( int npc_type ) {
return false;
}

vendor.Infuriate( this.MyMod );
vendor.Infuriate( mymod );
return true;
}

Expand All @@ -259,16 +284,18 @@ public VendorLogic GetOrCreateVendor( string world_id, int npc_type ) {

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

public void BoughtFrom( Player player, NPC npc, Item item, long _ ) {
var modworld = this.MyMod.GetModWorld<CapitalismWorld>();
public void BoughtFrom( CapitalismMod mymod, Player player, NPC npc, Item item, int stack ) {
var modworld = mymod.GetModWorld<CapitalismWorld>();
if( modworld.ID.Length == 0 ) {
ErrorLogger.Log( "BoughtFrom - No world id." );
return;
}

var vendor = this.GetOrCreateVendor( modworld.ID, npc.type );
if( vendor != null ) {
vendor.AddPurchase( this.MyMod, item.type );
for( int i=0; i<stack; i++ ) {
vendor.AddPurchase( mymod, item.type );
}
}
}

Expand All @@ -289,16 +316,16 @@ public void BoughtFrom( Player player, NPC npc, Item item, long _ ) {
}*/


public void DecayAllVendorPrices( Player player ) {
var modworld = this.MyMod.GetModWorld<CapitalismWorld>();
public void DecayAllVendorPrices( CapitalismMod mymod, Player player ) {
var modworld = mymod.GetModWorld<CapitalismWorld>();
if( modworld.ID == null || modworld.ID.Length == 0 ) {
ErrorLogger.Log( "DecayAllVendorPrices - No world id." );
return;
}

foreach( var kv in this.GetOrCreateWorldVendors(modworld.ID) ) {
if( kv.Value != null ) {
kv.Value.DecayPrices( this.MyMod );
kv.Value.DecayPrices( mymod );
}
}
}
Expand Down
Loading

0 comments on commit f51d7df

Please sign in to comment.