-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppGameKitUtils.agc
598 lines (598 loc) · 13.4 KB
/
AppGameKitUtils.agc
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#constant true = 1
#constant false = 0
#constant null = ""
#constant NL = chr(13) + chr(10)
#constant PI = 3.1415926535897
function remainder(number1, number2)
ret = number1-(number1/number2)*number2
endfunction ret
function factorial(number as integer)
if number<=1
fact=1
else
fact=number*factorial(number-1)
endif
endfunction fact
function comb(number1, number2)
if number1 >= number2
result as float
result = factorial(number1)/factorial(number2)/factorial(number1-number2)
else
result = 0
endif
endfunction result
function copysign(number1, number2)
ret = abs(number1)*(abs(number2)/number2)
endfunction ret
function gcd(number1, number2)
while number2 <> 0
temp = number2
number2 = remainder(number1,number2)
number1 = temp
endwhile
endfunction number1
function prodI(array as integer[], start as integer)
ret = 1
for i = start to array.length
ret = ret * array[i]
next
endfunction ret
function prod#(array as float[], start as integer)
ret = 1
for i = start to array.length
ret = ret * array[i]
next
endfunction ret
function rad(degrees)
ret as float
ret = degrees*(PI/180)
endfunction ret
function deg(radians)
ret as float
ret = radians*(180/PI)
endfunction ret
function join(array as string[], delimeter as string)
ret as String
for i=0 to array.length
ret = ret + array[i] + delimeter
next
endfunction ret
function sumI(array as integer[])
ret as integer
ret = 0
for i=0 to array.length
ret = ret + array[i]
next
endfunction ret
function sum#(array as float[])
ret as float
ret = 0
for i=0 to array.length
ret = ret + array[i]
next
endfunction ret
function includes$(array as String[],find as String)
ret as integer
for i=0 to array.length
if CompareString(array[i],find) = true
ret = ret + true
endif
next
if ret > 0
ret = 1
else
ret = 0
endif
endfunction ret
function includes#(array as Float[],find as Float)
ret as integer
for i=0 to array.length
if CompareString(str(array[i]),str(find)) = true
ret = ret + true
endif
next
if ret > 0
ret = 1
else
ret = 0
endif
endfunction ret
function includesI(array as Integer[],find as Integer)
ret as integer
for i=0 to array.length
if CompareString(str(array[i]),str(find)) = true
ret = ret + true
endif
next
if ret > 0
ret = 1
else
ret = 0
endif
endfunction ret
function Print$(array as string[])
buffer as String
buffer = "["
for i=0 to array.length
buffer = buffer+chr(34)+array[i]+chr(34)+","
next
if array.length <> -1
buffer = mid(buffer,1,len(buffer)-1)+"]"
else
buffer = "[]"
endif
print(buffer)
endfunction
function Print#(array as float[])
buffer as String
buffer = "["
for i=0 to array.length
buffer = buffer+str(array[i])+","
next
if array.length <> -1
buffer = mid(buffer,1,len(buffer)-1)+"]"
else
buffer = "[]"
endif
print(buffer)
endfunction
function PrintI(array as integer[])
buffer as String
buffer = "["
for i=0 to array.length
buffer = buffer+str(array[i])+","
next
if array.length <> -1
buffer = mid(buffer,1,len(buffer)-1)+"]"
else
buffer = "[]"
endif
print(buffer)
endfunction
function extendSF(array$ as string[], array# as float[], STRbefore# as integer)
ret as string[]
if STRbefore# = true
ret = array$
for i=0 to array#.length
ret.insert(str(array#[i]))
next
else
for i=0 to array#.length
ret.insert(str(array#[i]))
next
for i=0 to array$.length
ret.insert(array$[i])
next
endif
endfunction ret
function extendSI(array$ as string[], arrayI as integer[], STRbeforeIbool as integer)
ret as string[]
if STRbeforeIbool = true
ret = array$
for i=0 to arrayI.length
ret.insert(str(arrayI[i]))
next
else
for i=0 to arrayI.length
ret.insert(str(arrayI[i]))
next
for i=0 to array$.length
ret.insert(array$[i])
next
endif
endfunction ret
function extendFI(array# as float[], arrayI as integer[], FbeforeIbool as integer)
ret as string[]
if FbeforeIbool = true
for i=0 to array#.length
ret.insert(str(array#[i]))
next
for i=0 to arrayI.length
ret.insert(str(arrayI[i]))
next
else
for i=0 to arrayI.length
ret.insert(str(arrayI[i]))
next
for i=0 to array#.length
ret.insert(str(array#[i]))
next
endif
endfunction ret
function extendSS(array1 as string[], array2 as string[])
ret as string[]
ret = array1
for i=0 to array2.length
ret.insert(array2[i])
next
endfunction ret
function extendII(array1 as integer[], array2 as integer[])
ret as integer[]
ret = array1
for i=0 to array2.length
ret.insert(array2[i])
next
endfunction ret
function extendFF(array1 as float[], array2 as float[])
ret as float[]
ret = array1
for i=0 to array2.length
ret.insert(array2[i])
next
endfunction ret
function min#(array as float[])
ret as float
ret = array[0]
for i=0 to array.length
if array[i] < ret
ret = array[i]
endif
next
endfunction ret
function minI(array as integer[])
ret as integer
ret = array[0]
for i=0 to array.length
if array[i] < ret
ret = array[i]
endif
next
endfunction ret
function max#(array as float[])
ret as float
ret = array[0]
for i=0 to array.length
if array[i] > ret
ret = array[i]
endif
next
endfunction ret
function maxI(array as integer[])
ret as integer
ret = array[0]
for i=0 to array.length
if array[i] > ret
ret = array[i]
endif
next
endfunction ret
function char_range(range as string)
number1 as integer
number2 as integer
ret as string[]
number1 = asc(mid(range,1,1))
number2 = asc(mid(range,3,1))
for i=number1 to number2
ret.insert(chr(i))
next
endfunction ret
function split(stringToSplit as string, delimeterSymbol as String)
splitArray as String[]
count as integer
count = CountStringTokens(stringToSplit, delimeterSymbol)
for i = 1 to count
splitArray.insert(GetStringToken(stringToSplit, delimeterSymbol, i))
next i
endfunction splitArray
function capitalize(text as string)
ret as String
ret = upper(mid(text,1,1))+mid(text,2,len(text))
endfunction ret
function list(text as string)
splitArray as string[]
for i=1 to len(text)
splitArray.insert(Mid(text,i,1))
next
endfunction splitArray
function center(text as string, number as integer, padding as string)
count as integer
count = (number - len(text)) / 2
ret as String
if count < 0
ret = ""
else
ret = repeat$(padding,count) + text + repeat$(padding,count)
endif
endfunction ret
function endsWith(text as string, search as string)
ret = CompareString(mid(text,len(text)-len(search)+1,len(text)-len(search)-1),search,1,-1)
endfunction ret
function repeat$(text as string, count as integer)
ret as String
for i=0 to count - 1
ret = ret + text
next
endfunction ret
function isAlnum(text as String)
array as String[]
array = list(text)
alphanum as integer[]
for i=48 to 57
alphanum.insert(i)
next
for i=65 to 122
if 90 < i and i < 97
//pass
else
alphanum.insert(i)
endif
next
ret as integer
for i=0 to array.length
if includesI(alphanum,asc(array[i])) = true
ret = ret + 1
endif
next
if array.length = ret-1
ret = 1
else
ret = 0
endif
endfunction ret
function isAlpha(text as String)
array as String[]
array = list(text)
alpha as integer[]
for i=65 to 122
if 90 < i and i < 97
//pass
else
alpha.insert(i)
endif
next
ret as integer
for i=0 to array.length
if includesI(alpha,asc(array[i])) = true
ret = ret + 1
endif
next
if array.length = ret-1
ret = 1
else
ret = 0
endif
endfunction ret
function isDecimal(text as String)
array as String[]
array = list(text)
decimal as integer[]
for i=48 to 57
decimal.insert(i)
next
ret as integer
for i=0 to array.length
if includesI(decimal,asc(array[i])) = true
ret = ret + 1
endif
next
if array.length = ret-1
ret = 1
else
ret = 0
endif
endfunction ret
function isIdentifier(text as String)
array as String[]
array = list(text)
alphanum as integer[]
for i=48 to 57
alphanum.insert(i)
next
for i=65 to 122
if 90 < i and i < 97
if i = 95
alphanum.insert(i)
endif
else
alphanum.insert(i)
endif
next
ret as integer
for i=0 to array.length
if includesI(alphanum,asc(array[i])) = true
ret = ret + 1
endif
next
numbers as integer[9] = [48,49,50,51,52,53,54,55,56,57]
if array.length = ret-1
ret = 1
if includesI(numbers,asc(array[0])) = true
ret = 0
endif
else
ret = 0
endif
endfunction ret
function isSpace(text as String)
array as string[]
array = list(text)
ret as integer
for i=0 to array.length
if CompareString(array[i]," ") = true
ret = ret + 1
endif
next
if ret = array.length-1
ret = 1
else
ret = 0
endif
endfunction ret
function indexOf(text as string, substring as string)
text_array as string[]
substring_array as string[]
text_array = list(text)
substring_array = list(substring)
start as integer
endI as integer
start = 0
endI = 0
while start < len(text)
if CompareString(text_array[start+endI],substring_array[endI]) = false
start = start + 1
endI = 0
continue
endif
endI = endI + 1
if endI = len(substring)
exitfunction start
endif
endwhile
endfunction -1
function isUpper(text as String)
array as string[]
array = list(text)
lower_letters as integer[]
for i=97 to 122
lower_letters.insert(i)
next
for i=0 to array.length
if includesI(lower_letters,asc(array[i]))
exitfunction false
endif
next
endfunction true
function isLower(text as String)
array as string[]
array = list(text)
upper_letters as integer[]
for i=65 to 90
upper_letters.insert(i)
next
for i=0 to array.length
if includesI(upper_letters,asc(array[i]))
exitfunction false
endif
next
endfunction true
function isTitle(text as string)
array as string[]
array = split(text," ")
substring_array as string[]
substring_array = list(array[0])
points as integer
if isUpper(substring_array[0]) = true then points = points + 1
for i = 1 to substring_array.length
if isUpper(substring_array[i]) = false then points = points + 1
next
if points = substring_array.length + 1 then exitfunction true
endfunction false
function just(text as string, substring as string, count as integer, mode as string)
if CompareString(lower(mode),"l")
exitfunction text + repeat$(substring,count-len(text))
elseif CompareString(lower(mode),"r")
exitfunction repeat$(substring,count-len(text)) + text
endif
endfunction ""
function strip(text as string, array as string[])
ret1 as String[]
ret2 as string
text_array as string[]
text_array = list(text)
for i=0 to text_array.length
if includes$(array,text_array[i]) = false
ret1.insert(text_array[i])
endif
next
ret2 = join(ret1,"")
endfunction ret2
function reFind(text as string, start as string, end$ as string)
ret as string
ret = mid(text,indexOf(text,start)+len(start)+1,indexOf(text,end$)-indexof(text,start)-len(start))
endfunction ret
function getTextBoundingBoxOffsetY(textId as integer)
textWidth = GetTextTotalWidth(textId)
startingX = GetTextX(textId)
startingY = GetTextY(textId)
textR = GetTextColorRed(textId)
textG = GetTextColorGreen(textId)
textB = GetTextColorBlue(textId)
background = CreateSprite(0)
SetSpriteSize(background, GetWindowWidth(), GetWindowHeight())
spriteR = textR + 100
if spriteR > 255 then spriteR = spriteR - 255
spriteG = textG + 100
if spriteG > 255 then spriteG = spriteG - 255
spriteB = textB + 100
if spriteB > 255 then spriteB = spriteB - 255
SetSpriteColor(background, spriteR, spriteG, spriteB, 255)
SetSpriteDepth(background, 0)
originalDepth = GetTextDepth(textId)
SetTextDepth(textId,0)
Render()
img = GetImage(startingX, startingY, textWidth, GetWindowHeight())
memImg = CreateMemblockFromImage(img)
SetTextDepth(textId,originalDepth)
DeleteSprite(background)
memWidth = GetMemblockInt(memImg, 0) - 1 // or textWidth
for y = 0 to GetMemblockInt(memImg, 4) step 1
for x = 0 to memWidth step 1
memOffset = y * memWidth * 4 + x * 4 + 12
memR = GetMemblockByte(memImg, memOffset)
memG = GetMemblockByte(memImg, memOffset + 1)
memB = GetMemblockByte(memImg, memOffset + 2)
if textR = memR and textG = memG and textB = memB
exitfunction y
endif
next x
next y
endfunction 0
function wordWrap(text as integer, maxWidth as integer, forceStrict as integer, lineBreakChar as string)
originalText as string
originalText = GetTextString(text)
words as string[]
words = split(originalText, " ")
newText as string
flag = 0
for i=0 to words.length
if flag <> 0
SetTextString(text, GetTextString(text) + " " + words[i])
else
SetTextString(text,words[i])
endif
length = GetTextTotalWidth(text)
if length <= maxWidth or flag = 0
if length > maxWidth and forceStrict = 1
newText = newText + strictWordWrap(words[i], maxWidth, GetTextSize(text), lineBreakChar)
else
if mid(newText,len(newText)-1,-1) = chr(13)+chr(10) or len(newText) = 0
newText = newText + words[i]
else
newText = newText + " " + words[i]
endif
flag = flag + 1
endif
else
newText = newText + NL
i = i - 1
flag = 0
endif
next
SetTextString(text, newText)
endfunction
function strictWordWrap(word as string, maxWidth as integer, textSize as integer, lineBreakChar as string)
text = CreateText("")
SetTextSize(text, textSize)
letters as string[]
letters = list(word)
newString as string
buffer as string
flag = 0
for i=0 to letters.length
if flag <> 0
SetTextString(text,GetTextString(text)+letters[i])
else
SetTextString(text,letters[i])
endif
length = GetTextTotalWidth(text)
if length <= maxWidth or flag = 0
buffer = buffer + letters[i]
flag = flag + 1
else
buffer = buffer + lineBreakChar + NL
i = i - 1
flag = 0
endif
next
DeleteText(text)
endfunction buffer