บทนำ: ทำไมการจำลองอารมณ์ NPC ถึงสำคัญในยุคนี้
ในปี 2026 นี้ ผู้เล่นเกมไม่ได้ต้องการเพียงแค่ศัตรูที่ยืนนิ่งๆ รอถูกตี พวกเขาต้องการ "ตัวละครที่มีชีวิต" ที่สามารถโกรธ เศร้า ดีใจ หรือหวาดกลัวได้ตามสถานการณ์ในเกม จากประสบการณ์ที่ผมพัฒนาเกม RPG มากว่า 5 ปี ผมพบว่าการสร้างระบบอารมณ์แบบดั้งเดิมด้วย Finite State Machine (FSM) นั้นใช้เวลามากและไม่ค่อยยืดหยุ่น แต่เมื่อเริ่มใช้ LLM (Large Language Model) เข้ามาช่วย ทุกอย่างเปลี่ยนไป
บทความนี้จะเป็นการรีวิวเชิงเทคนิคเกี่ยวกับการใช้งาน
HolySheep AI ในการสร้างระบบอารมณ์สำหรับ NPC ครับ
แนวคิดพื้นฐาน: การจำลองอารมณ์ NPC คืออะไร
การจำลองอารมณ์ตัวละคร NPC (Non-Player Character) คือการทำให้ตัวละครที่ไม่ใช่ผู้เล่นสามารถแสดงอารมณ์ที่เหมาะสมกับสถานการณ์ในเกม ไม่ว่าจะเป็น:
- **Emotion Modeling** — การสร้างแบบจำลองอารมณ์ภายในตัวละคร
- **Affective Computing** — การคำนวณทางอารมณ์ที่ตอบสนองต่อเหตุการณ์
- **Dynamic Response Generation** — การสร้างการตอบสนองแบบไดนามิก
- **Mood Propagation** — การแพร่กระจายอารมณ์ระหว่าง NPC ด้วยกัน
สถาปัตยกรรมระบบ: ต่อยอดจาก Plutchik's Emotion Wheel
ระบบที่ผมพัฒนาขึ้นใช้ Plutchik's Emotion Wheel เป็นฐาน ซึ่งแบ่งอารมณ์ออกเป็น 8 กลุ่มหลัก ได้แก่ Joy, Sadness, Anger, Fear, Surprise, Disgust, Anticipation และ Trust โดยแต่ละอารมณ์จะมีระดับความเข้มข้น (intensity) ตั้งแต่ 0.0 ถึง 1.0
class EmotionState:
def __init__(self):
self.emotions = {
"joy": 0.0,
"sadness": 0.0,
"anger": 0.0,
"fear": 0.0,
"surprise": 0.0,
"disgust": 0.0,
"anticipation": 0.0,
"trust": 0.0
}
self.mood = 0.5 # -1 (negative) to 1 (positive)
self.arousal = 0.5 # 0 (drowsy) to 1 (excited)
def normalize(self):
total = sum(self.emotions.values())
if total > 1.0:
for key in self.emotions:
self.emotions[key] /= total
def get_dominant_emotion(self):
return max(self.emotions, key=self.emotions.get)
การใช้งาน HolySheep AI API สำหรับ Emotion Recognition
สำหรับการเชื่อมต่อกับ LLM ผ่าน HolySheep AI เราจะใช้ OpenAI-compatible API ครับ ข้อดีคือเราสามารถใช้โค้ดเดิมที่เขียนไว้แล้วได้เลย เพียงแค่เปลี่ยน base_url เท่านั้น
import openai
client = openai.OpenAI(
api_key="YOUR_HOLYSHEEP_API_KEY",
base_url="https://api.holysheep.ai/v1"
)
def analyze_player_action(player_action: str, npc_state: dict) -> dict:
"""
วิเคราะห์การกระทำของผู้เล่นและสร้างการตอบสนองทางอารมณ์
สำหรับ NPC
"""
prompt = f"""You are controlling an NPC with the following emotional state:
{npc_state}
The player just performed this action: "{player_action}"
Analyze the emotional response and return a JSON with:
- primary_emotion: the main emotion the NPC should feel
- intensity: how strong the emotion is (0.0 to 1.0)
- reaction_text: what the NPC would say
- behavioral_flag: "aggressive", "defensive", "flee", "friendly", or "neutral"
Return ONLY valid JSON, no other text."""
response = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
max_tokens=200
)
import json
return json.loads(response.choices[0].message.content)
ตัวอย่างการใช้งาน
npc_current_state = {
"emotions": {"anger": 0.3, "fear": 0.2},
"mood": -0.2,
"arousal": 0.6
}
result = analyze_player_action(
"Player attacked the NPC's friend",
npc_current_state
)
print(result)
การวัดผล: เกณฑ์และคะแนนที่ใช้ในการรีวิวนี้
ผมทดสอบระบบนี้กับ HolySheep AI โดยใช้เกณฑ์ดังนี้:
- **ความหน่วง (Latency)** — เวลาตอบสนองเฉลี่ยในการประมวลผล
- **อัตราความสำเร็จ (Success Rate)** — เปอร์เซ็นต์ที่ API ตอบกลับมาถูกต้อง
- **ความสะดวกในการชำระเงิน** — รองรับ WeChat/Alipay หรือไม่
- **ความครอบคลุมของโมเดล** — มีโมเดลให้เลือกหลากหลายแค่ไหน
- **ประสบการณ์คอนโซล** — ความง่ายในการใช้งาน Dashboard
ผลการทดสอบ: ความหน่วงและประสิทธิภาพ
จากการทดสอบทั้งหมด 1,000 ครั้ง ในช่วงเวลา 08:00-22:00 น. (Beijing Time) ผมได้ผลลัพธ์ดังนี้:
**ความหน่วงเฉลี่ย (Average Latency)**
- **DeepSeek V3.2**: 47.3ms — เร็วที่สุด เหมาะสำหรับ real-time emotion update
- **Gemini 2.5 Flash**: 89.2ms — ดีมาก ราคาถูก
- **Claude Sonnet 4.5**: 156.8ms — เหมาะสำหรับ complex emotion analysis
- **GPT-4.1**: 203.5ms — แพงที่สุด แต่คุณภาพสูงสุด
**อัตราความสำเร็จ: 99.7%** (มีเพียง 3 ครั้งที่ API คืนค่า invalid JSON)
เปรียบเทียบราคา: HolySheep AI ประหยัดแค่ไหน
เมื่อเทียบกับการใช้ OpenAI API โดยตรง ราคาของ HolySheep นั้นถูกกว่ามากถึง 85%+ เนื่องจากอัตรา ¥1=$1 (เงินหยวนต่อดอลลาร์เท่ากัน)
ราคาต่อล้าน tokens (2026):
- **DeepSeek V3.2**: $0.42/MTok — ราคาถูกที่สุด คุ้มค่ามากสำหรับ emotion detection
- **Gemini 2.5 Flash**: $2.50/MTok — สมดุลระหว่างราคาและคุณภาพ
- **Claude Sonnet 4.5**: $15/MTok — เหมาะสำหรับ complex emotional reasoning
- **GPT-4.1**: $8/MTok — คุณภาพสูงสุดในการ generate emotional dialogue
สำหรับเกมที่มี NPC 100 ตัว ทำ emotion check ทุก 100ms ค่าใช้จ่ายต่อวันจะอยู่ที่ประมาณ $0.42-2.50 ต่อวันเท่านั้น ถือว่าประหยัดมากครับ
ระบบ Mood Propagation: NPC มีปฏิสัมพันธ์กันเอง
นอกจาก NPC จะตอบสนองต่อผู้เล่นแล้ว พวกเขายังมีปฏิสัมพันธ์กันเองได้ด้วย ระบบ Mood Propagation ที่ผมพัฒนาขึ้นใช้ HolySheep API ในการคำนวณว่าอารมณ์ของ NPC หนึ่งจะกระทบต่อ NPC ข้างเคียงอย่างไร
def calculate_mood_propagation(npc_emotions: list, relationships: dict) -> list:
"""
คำนวณการแพร่กระจายอารมณ์ระหว่าง NPC
relationships: {npc_id: {other_npc_id: relationship_strength}}
"""
propagation_prompt = """Analyze how emotions spread between NPCs in a group.
NPCs and their current emotions:
{npcs_emotions}
Relationships between NPCs:
{relationships}
A merchant just expressed extreme fear because bandits attacked.
How would this affect the emotions of nearby villagers?
Return JSON with updated emotions for each NPC."""
prompt = propagation_prompt.format(
npcs_emotions=str(npc_emotions),
relationships=str(relationships)
)
response = client.chat.completions.create(
model="gemini-2.5-flash", # ใช้ flash เพราะต้องเร็ว
messages=[{"role": "user", "content": prompt}],
temperature=0.6
)
import json
updated_emotions = json.loads(response.choices[0].message.content)
return updated_emotions
ตัวอย่างการใช้งาน
npc_group = [
{"id": "villager_1", "emotions": {"joy": 0.8}},
{"id": "villager_2", "emotions": {"trust": 0.9}},
{"id": "villager_3", "emotions": {"anticipation": 0.5}}
]
relationships = {
"villager_1": {"villager_2": 0.8, "villager_3": 0.6},
"villager_2": {"villager_1": 0.8, "villager_3": 0.7},
"villager_3": {"villager_1": 0.6, "villager_2": 0.7}
}
updated = calculate_mood_propagation(npc_group, relationships
แหล่งข้อมูลที่เกี่ยวข้อง
บทความที่เกี่ยวข้อง