1+ from cached_property import cached_property
2+ from datetime import datetime
3+
4+ import numpy as np
5+
16from module .campaign .campaign_base import CampaignBase
27from module .campaign .run import CampaignRun
38from module .combat .assets import BATTLE_PREPARATION
9+ from module .combat .emotion import Emotion
410from module .equipment .assets import *
511from module .equipment .fleet_equipment import FleetEquipment
612from module .exception import CampaignEnd , ScriptError
2228SIM_VALUE = 0.92
2329
2430
31+ class GemsEmotion (Emotion ):
32+
33+ def check_reduce (self , battle ):
34+ """
35+ Overwrite emotion.check_reduce()
36+ Check emotion before entering a campaign.
37+
38+ Args:
39+ battle (int): Battles in this campaign
40+
41+ Raise:
42+ CampaignEnd: Pause current task to prevent emotion control in the future.
43+ """
44+
45+ if not self .is_calculate :
46+ return
47+
48+ method = self .config .Fleet_FleetOrder
49+
50+ if method == 'fleet1_all_fleet2_standby' :
51+ battle = (battle , 0 )
52+ elif method == 'fleet1_standby_fleet2_all' :
53+ battle = (0 , battle )
54+ else :
55+ raise ScriptError (f'Unknown fleet order: { method } ' )
56+
57+ battle = tuple (np .array (battle ) * self .reduce_per_battle_before_entering )
58+ logger .info (f'Expect emotion reduce: { battle } ' )
59+
60+ self .update ()
61+ self .record ()
62+ self .show ()
63+ recovered = max ([f .get_recovered (b ) for f , b in zip (self .fleets , battle )])
64+ if recovered > datetime .now ():
65+ logger .info ('Delay current task to prevent emotion control in the future' )
66+ self .config .GEMS_EMOTION_TRIGGRED = True
67+ raise CampaignEnd ('Emotion withdraw' )
68+
69+ def wait (self , fleet_index ):
70+ pass
71+
72+
2573class GemsCampaignOverride (CampaignBase ):
2674
2775 def handle_combat_low_emotion (self ):
@@ -73,10 +121,16 @@ def load_campaign(self, name, folder='campaign_main'):
73121 super ().load_campaign (name , folder )
74122
75123 class GemsCampaign (GemsCampaignOverride , self .module .Campaign ):
76- pass
124+
125+ @cached_property
126+ def emotion (self ) -> GemsEmotion :
127+ return GemsEmotion (config = self .config )
77128
78129 self .campaign = GemsCampaign (device = self .campaign .device , config = self .campaign .config )
79- self .campaign .config .override (Emotion_Mode = 'ignore' )
130+ if self .change_flagship or self .change_vanguard :
131+ self .campaign .config .override (Emotion_Mode = 'ignore_calculate' )
132+ else :
133+ self .campaign .config .override (Emotion_Mode = 'ignore' )
80134 self .campaign .config .override (EnemyPriority_EnemyScaleBalanceWeight = 'S1_enemy_first' )
81135
82136 @property
@@ -308,6 +362,8 @@ def get_templates(common_dd):
308362 logger .error (f'Invalid CommonDD setting: { common_dd } ' )
309363 raise ScriptError (f'Invalid CommonDD setting: { common_dd } ' )
310364
365+ _new_emotion : int
366+
311367 def flagship_change_execute (self ):
312368 """
313369 Returns:
@@ -325,7 +381,9 @@ def flagship_change_execute(self):
325381
326382 ship = self .get_common_rarity_cv ()
327383 if ship :
328- self ._ship_change_confirm (min (ship , key = lambda s : (s .level , - s .emotion )).button )
384+ target_ship = min (ship , key = lambda s : (s .level , - s .emotion ))
385+ self ._new_emotion = target_ship .emotion
386+ self ._ship_change_confirm (target_ship .button )
329387
330388 logger .info ('Change flagship success' )
331389 return True
@@ -364,7 +422,9 @@ def vanguard_change_execute(self):
364422
365423 ship = self .get_common_rarity_dd ()
366424 if ship :
367- self ._ship_change_confirm (max (ship , key = lambda s : s .emotion ).button )
425+ target_ship = max (ship , key = lambda s : s .emotion )
426+ self ._new_emotion = min (self ._new_emotion , target_ship .emotion )
427+ self ._ship_change_confirm (target_ship .button )
368428
369429 logger .info ('Change vanguard ship success' )
370430 return True
@@ -384,13 +444,19 @@ def triggered_stop_condition(self, oil_check=True):
384444 logger .hr ('TRIGGERED LV32 LIMIT' )
385445 return True
386446
387- if self .campaign .map_is_auto_search and self . campaign . config .GEMS_EMOTION_TRIGGRED :
447+ if self .campaign .config .GEMS_EMOTION_TRIGGRED :
388448 self ._trigger_emotion = True
389449 logger .hr ('TRIGGERED EMOTION LIMIT' )
390450 return True
391451
392452 return super ().triggered_stop_condition (oil_check = oil_check )
393453
454+ def set_emotion (self , emotion ):
455+ if self .config .Fleet_FleetOrder == 'fleet1_standby_fleet2_all' :
456+ self .campaign .config .set_record (Emotion_Fleet2Value = emotion )
457+ else :
458+ self .campaign .config .set_record (Emotion_Fleet1Value = emotion )
459+
394460 def run (self , name , folder = 'campaign_main' , mode = 'normal' , total = 0 ):
395461 """
396462 Args:
@@ -416,11 +482,15 @@ def run(self, name, folder='campaign_main', mode='normal', total=0):
416482 # End
417483 if self ._trigger_lv32 or self ._trigger_emotion :
418484 success = True
485+ self ._new_emotion = 200 # magic value to exceed 150
419486 if self .change_flagship :
420487 success = self .flagship_change ()
421488 if self .change_vanguard :
422489 success = success and self .vanguard_change ()
423490
491+ if self ._new_emotion < 200 :
492+ self .set_emotion (self ._new_emotion )
493+
424494 if is_limit and self .config .StopCondition_RunCount <= 0 :
425495 logger .hr ('Triggered stop condition: Run count' )
426496 self .config .StopCondition_RunCount = 0
0 commit comments