📄 aipemotion.cpp
字号:
//**********************************************************************
// aipEmotion.cpp - function bodies for aipEmotion.h
//
// Copyright (c) 1999, 2005, 2008 Brian Marshall
//
// See the license at end of this file.
//
// Developers/Contributers:
// [BRM] Brian Marshall - Calgary - bmarshal@agt.net
//
// 08/06/16 [BRM] small changes for portability
// 05/11/19 [BRM] emotion take_msg() calls pandemonium take_msg()
// 05/11/15 [BRM] hope: fear, greed, curiosity now aspects
// 05/10/24 [BRM] aipHope::set_g - simplified and slightly changed
// weaken functions now do not weaken to aipNeutral
// 05/10/24 [BRM] made emotion slowly_degrade optional
// 05/09/10 [BRM] development began
//
//----------------------------------------------------------------------
#include "aipEmotion.h"
#include <string.h>
#include <stdio.h>
//======================================================================
// aipAspect - Aspect to an emotion
//
// All function bodies are in the header file
//
//======================================================================
// aipEmotion - a pure virtual base class.
//
//----------------------------------------------------------------------
// Constructor
aipEmotion::aipEmotion () {
m_g = aipNeutral;
m_aspects = new aipPandemonium;
m_g_before_take_msg = aipNeutral;
m_g_before_prev_msg = aipNeutral;
m_should_slowly_degrade = 1;
}
//----------------------------------------------------------------------
// Destructor
aipEmotion::~aipEmotion () {
if (m_aspects) delete m_aspects;
}
//----------------------------------------------------------------------
// add_aspect - add and aspect to this emotion
void aipEmotion::add_aspect (aipAspect *x) {
x->set_owner_emotion(this);
m_aspects->add(x);
}
//----------------------------------------------------------------------
// dump_to_ptr
void aipEmotion::dump_to_ptr (char *p) const {
long x = m_g.numeric_value();
x = (x<-999999) ? -999999 : ( (x>999999) ? 999999 : x );
sprintf (p,"%ld",x);
}
//----------------------------------------------------------------------
// aspect_iterator - return an iterator to the aspects of this emotion.
aipAspectItr aipEmotion::aspect_iterator() const {
aipAspectItr i(aspect_pandemonium());
return i;
}
//----------------------------------------------------------------------
// slowly_degrade
void aipEmotion::slowly_degrade () {
if ( g() != aipNeutral && g() == g_before_take_msg() &&
g() == g_before_prev_msg() ) {
weaken(aipIntensity_Slightly);
}
}
//----------------------------------------------------------------------
// take_msg - take a message and distribute to aspects
//
// Subclasses should, in general, NOT override this function.
// Subclasses can define specific behavior by overriding the
// pre_msg_behavior() and post_msg_behavior() functions.
// Instances can have aspects added to them.
void aipEmotion::take_msg (aipMsg *m) {
m_g_before_prev_msg = m_g_before_take_msg;
m_g_before_take_msg = m_g;
pre_msg_behavior(m);
m_aspects->take_msg(m);
post_msg_behavior(m);
}
//----------------------------------------------------------------------
// set_g
void aipEmotion::set_g (aipG x) { m_g = x; }
//----------------------------------------------------------------------
// floor and ceiling
void aipEmotion::floor (aipG x) { if (m_g < x) set_g(x); }
void aipEmotion::ceiling (aipG x) { if (m_g > x) set_g(x); }
//----------------------------------------------------------------------
// reset m_g
void aipEmotion::reset (aipG x) { set_g(x); }
//======================================================================
// aipPosEmotion - emotion where goodness is Neutral or Positive
//
//----------------------------------------------------------------------
// set_g
void aipPosEmotion::set_g (aipG x) {
aipEmotion::set_g ( (x < aipNeutral) ? aipNeutral : x );
}
//----------------------------------------------------------------------
// strengthen
void aipPosEmotion::strengthen (long intensity_constant) {
aipG x = aipNeutral;
if (intensity_constant == aipIntensity_Slightly) {
x = aipVerySlightlyGood;
} else if (intensity_constant == aipIntensity_A_Little) {
x = aipSlightlyGood;
} else if (intensity_constant == aipIntensity_Somewhat) {
x = aipLittleBitGood;
} else if (intensity_constant == aipIntensity_A_Fair_Bit) {
x = aipSomewhatGood;
} else if (intensity_constant == aipIntensity_Quite_A_Bit) {
x = aipFairlyGood;
} else if (intensity_constant == aipIntensity_A_Lot) {
x = aipQuiteGood;
}
set_g (g() + x);
}
//----------------------------------------------------------------------
// weaken (so long as the value does not fall to aipNeutral)
void aipPosEmotion::weaken (long intensity_constant) {
aipG cur_g = g();
if (cur_g < aipSlightlyGood) return;
aipG x = aipNeutral;
if (intensity_constant == aipIntensity_Slightly) {
x = aipVerySlightlyGood + cur_g.calc_fraction(1,8);
} else if (intensity_constant == aipIntensity_A_Little) {
x = aipVerySlightlyGood + cur_g.calc_fraction(1,6);
} else if (intensity_constant == aipIntensity_Somewhat) {
x = aipVerySlightlyGood + cur_g.calc_fraction(1,4);
} else if (intensity_constant == aipIntensity_A_Fair_Bit) {
x = aipVerySlightlyGood + cur_g.calc_fraction(3,8);
} else if (intensity_constant == aipIntensity_Quite_A_Bit) {
x = aipVerySlightlyGood + cur_g.calc_fraction(1,2);
} else if (intensity_constant == aipIntensity_A_Lot) {
x = aipVerySlightlyGood + cur_g.calc_fraction(3,4);
}
if (x >= cur_g) x = cur_g - 1;
set_g (cur_g - x); // virtual function ensures in is not negative.
}
//======================================================================
// aipNegEmotion - emotion where goodness is Neutral or Negative
//
//----------------------------------------------------------------------
// set_g
void aipNegEmotion::set_g (aipG x) {
aipEmotion::set_g ( (x > aipNeutral) ? aipNeutral : x );
}
//----------------------------------------------------------------------
// strengthen
void aipNegEmotion::strengthen (long intensity_constant) {
aipG x = aipNeutral;
if (intensity_constant == aipIntensity_Slightly) {
x = aipVerySlightlyBad;
} else if (intensity_constant == aipIntensity_A_Little) {
x = aipSlightlyBad;
} else if (intensity_constant == aipIntensity_Somewhat) {
x = aipLittleBitBad;
} else if (intensity_constant == aipIntensity_A_Fair_Bit) {
x = aipSomewhatBad;
} else if (intensity_constant == aipIntensity_Quite_A_Bit) {
x = aipFairlyBad;
} else if (intensity_constant == aipIntensity_A_Lot) {
x = aipQuiteBad;
}
set_g (g() + x);
}
//----------------------------------------------------------------------
// weaken (so long as the value does not rise to aipNeutral)
void aipNegEmotion::weaken (long intensity_constant) {
aipG cur_g = g();
if (cur_g > aipSlightlyBad) return;
aipG x = aipNeutral;
if (intensity_constant == aipIntensity_Slightly) {
x = aipVerySlightlyBad + cur_g.calc_fraction(1,8);
} else if (intensity_constant == aipIntensity_A_Little) {
x = aipVerySlightlyBad + cur_g.calc_fraction(1,6);
} else if (intensity_constant == aipIntensity_Somewhat) {
x = aipVerySlightlyBad + cur_g.calc_fraction(1,4);
} else if (intensity_constant == aipIntensity_A_Fair_Bit) {
x = aipVerySlightlyBad + cur_g.calc_fraction(3,8);
} else if (intensity_constant == aipIntensity_Quite_A_Bit) {
x = aipVerySlightlyBad + cur_g.calc_fraction(1,2);
} else if (intensity_constant == aipIntensity_A_Lot) {
x = aipVerySlightlyBad + cur_g.calc_fraction(3,4);
}
if (x <= cur_g) x = cur_g + 1;
set_g (cur_g - x); // virtual function ensures in is not positive.
}
//======================================================================
// aipCompEmotion - compound emotion - aspects are emotions
//
//----------------------------------------------------------------------
// calc_compound - calculate m_g as the sum of the goodness of
// each emotion-aspect.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -