aboutsummaryrefslogtreecommitdiff
path: root/Northstar.CustomServers/mod/scripts/vscripts/ai/_ai_marvin_faces.gnut
blob: e6d3bcf0a9ceb77a8836f5a21285fba1b5bc6f49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
untyped

global function MarvinFaces_Init

global function MarvinFace
global function MarvinThinksAwhile
global function MarvinFaceExists
global function SetMarvinBodyType
global function MarvinSetFace

function MarvinFaces_Init()
{

	RegisterSignal( "StopThinking" )

	AddSpawnCallback( "npc_marvin", MarvinSpawnCallback )

	SetupMarvinFaces()
}

function SetupMarvinFaces()
{
	// setup marvin face mappings
	level.marvinFaces <- {}
	level.marvinFaces[ MARVIN_TYPE_WORKER ] <-
	{
		none = 0
		happy = 1
		sad = 2
		angry = 3
		think1 = 4
		think2 = 5
		question = 6
	}

	// Use the yellow worker marvin skins since shooters are from SP and shooter value = 0 which is LevelEd's default.
	// Real shooter skins are 7-13.  If we want real skins, we can add them here and adjust the marvin spawn points per level.
	level.marvinFaces[ MARVIN_TYPE_SHOOTER ] <-
	{
		none = 0
		happy = 1
		sad = 2
		angry = 3
		think1 = 4
		think2 = 5
		question = 6
	}

	level.marvinFaces[ MARVIN_TYPE_FIREFIGHTER ] <-
	{
		none = 14
		happy = 15
		sad = 16
		angry = 17
		think1 = 18
		think2 = 19
		question = 20
	}

	// No idea what this type of marvin is...legacy stuff. Just make them yellow.
	level.marvinFaces[ MARVIN_TYPE_MARVINONE ] <-
	{
		none = 0
		happy = 1
		sad = 2
		angry = 3
		think1 = 4
		think2 = 5
		question = 6
	}

// Nothing uses this except debug statements that are commented out
/*
	level.marvinFaceNames <- {}
	// invert map for tests
	foreach ( key, val in level.marvinFace )
	{
		level.marvinFaceNames[ val ] <- key
	}
*/
}

void function MarvinFace( entity marvin )
{
	thread MarvinFaceThink( marvin )
}

void function MarvinFaceThink( entity marvin )
{
	//printl( "Setting up marvin face for " + marvin )
	for ( ;; )
	{
		waitthread MarvinUndamagedFacePicker( marvin )

//		printl( "damaged " + marvin )
		if ( !IsAlive( marvin ) )
			break

		waitthread MarvinWounded( marvin )

		if ( !IsAlive( marvin ) )
			break
	}

	if ( IsValid_ThisFrame( marvin ) )
		MarvinSetFace( marvin, "none" )
}

function MarvinWounded( marvin )
{
	marvin.EndSignal( "OnDeath" )
	MarvinSetFace( marvin, "sad" )
	wait 2.3
	waitthread MarvinThinksAwhile( marvin, RandomFloatRange( 2, 4 ) )
}

void function EntSignals( entity ent, string signal )
{
	if ( IsValid_ThisFrame( ent ) )
		ent.Signal( signal )
}

function MarvinThinksAwhile( marvin, time )
{
	expect entity( marvin )

	marvin.EndSignal( "StopThinking" )
	delaythread( time ) EntSignals( marvin, "StopThinking" )

	// think for a bit
	for ( ;; )
	{
		MarvinSetFace( marvin, "think1" )
		wait 0.4
		MarvinSetFace( marvin, "think2" )
		wait 0.4
	}
}

function MarvinUndamagedFacePicker( marvin )
{
	marvin.EndSignal( "OnDeath" )
	marvin.EndSignal( "OnDamaged" )
	local i

	for ( ;; )
	{
		if ( !marvin.GetEnemy() )
		{
			MarvinSetFace( marvin, "happy" )
			marvin.WaitSignal( "OnFoundEnemy" )
		}

		waitthread MarvinThinksAwhile( marvin, RandomFloatRange( 2, 4 ) )

		if ( marvin.GetEnemy() )
		{
			MarvinSetFace( marvin, "angry" )
			marvin.WaitSignal( "OnLostEnemy" )
		}
	}
}

function MarvinSetFace( self, face )
{
//	printl( self + " got face " + face )
	Assert( MarvinFaceExists( self, face ), "No face " + face + " in level.marvinFace" )

	//prin( "Changing " + self + " face from " + level.marvinFaceNames[ skin ] + " to " + face )
	self.SetSkin( GetMarvinFace( self, face ) )
	self.Signal( "StopThinking" )
}

function MarvinFaceExists( npc_marvin, face )
{
	local marvinType = GetMarvinBodyType( npc_marvin )

	if ( marvinType in level.marvinFaces )
		return true

//	return ( face in level.marvinFaces[ marvinType ] )
}

function GetMarvinFace( npc_marvin, face )
{
	local marvinType = GetMarvinBodyType( npc_marvin )

	Assert( MarvinFaceExists( npc_marvin, face ), "No face " + face + " in level.marvinFace" )

	local faceID = level.marvinFaces[ marvinType ][ face ]

	return faceID
}

function SetMarvinBodyType( npc_marvin )
{
	if( "bodytype" in npc_marvin.s )
	{
		Assert( npc_marvin.s.bodytype >= MARVIN_TYPE_SHOOTER && npc_marvin.s.bodytype <= MARVIN_TYPE_FIREFIGHTER, "Specified invalid body type index " + npc_marvin.s.bodytype + ", Use values from 0-2 instead." )

		switch( npc_marvin.s.bodytype )
		{
			case MARVIN_TYPE_FIREFIGHTER:
				local index = npc_marvin.FindBodyGroup( "firefighter" )
				local state = 1
				npc_marvin.SetBodygroup( index, state )
				break
		}
	}
}

function GetMarvinBodyType( npc_marvin )
{
	local bodyType = MARVIN_TYPE_WORKER

	if( "bodytype" in npc_marvin.s )
		bodyType = npc_marvin.s.bodytype

	return bodyType
}

void function MarvinSpawnCallback( entity npc_marvin )
{
	SetMarvinBodyType( npc_marvin )
	npc_marvin.SetDeathNotifications( true ) //Primarily so we can do HandleDeathPackage for Marvins. Can just add a deathcallback if this is too expensive
}