/
manufacture.c
1643 lines (1433 loc) · 51.7 KB
/
manufacture.c
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include "manufacture.h"
#include "asc.h"
#include "context_menu.h"
#include "cursors.h"
#include "elconfig.h"
#include "elwindows.h"
#include "gamewin.h"
#include "hud.h"
#include "init.h"
#include "interface.h"
#include "item_info.h"
#ifdef JSON_FILES
#include "json_io.h"
#endif
#include "loginwin.h"
#include "multiplayer.h"
#include "textures.h"
#include "translate.h"
#include "sound.h"
#include "io/elpathwrapper.h"
#include "errors.h"
#ifdef OPENGL_TRACE
#include "gl_init.h"
#endif
#define MIX_SLOT_OFFSET 36
#define GRID_COLS 12
#define GRID_ROWS 3
int wanted_num_recipe_entries = 10;
int disable_manuwin_keypress = 0;
const int max_num_recipe_entries = 500;
static int pipeline_x = 0;
static int last_changed_slot=-1;
static item manufacture_list[ITEM_NUM_ITEMS];
static int recipe_win= -1;
static size_t cm_recipewin = CM_INIT_VALUE;
enum { CMRIC_ADD=0, CMRIC_CLEAR, CMRIC_DELETE, CMRIC_SORT };
static size_t num_recipe_entries = 0;
static size_t max_prev_num_recipe_entries = 0;
static size_t num_displayed_recipes = 5;
static recipe_entry *recipes_store = NULL;
static recipe_entry manu_recipe;
static int cur_recipe = 0;
static Uint32 last_recipe_key_time = 0;
static char recipe_name_filter[20] = {0};
static int recipe_win_scroll_id = -1;
static int recipes_shown=0;
static int recipes_loaded=0;
static int mouse_over_recipe = -1;
static int mouse_over_main_pos = -1;
static int mouse_over_pipe_pos = -1;
static unsigned char items_string[350]={0};
static size_t last_items_string_id = (size_t)-1;
static item last_mix[NUM_MIX_SLOTS];
static int recipe_names_changed = 0;
static int initialised_recipe_names = 0;
static int SLOT_SIZE = 0;
static int pipe_control_width = 0;
static int recipe_win_width = 0;
static int manufacture_menu_x_len = 0;
static int manufacture_menu_y_len = 0;
static int text_y_offset = 0;
static int recipe_y_offset = 0;
static int control_elem_size = 0;
static int clear_button_id = 1;
static int mixone_button_id = 2;
static int mixall_button_id = 3;
/* compare two item ids and return true if they match or either is not set */
static int item_ids_match(Uint16 lid, Uint16 rid)
{
if ((lid == unset_item_uid) || (rid == unset_item_uid) || (lid == rid))
return 1;
else
return 0;
}
static void copy_items_to_recipe_items(recipe_item *recipe_items, item *items, size_t items_offset)
{
size_t j;
for (j=0; j<NUM_MIX_SLOTS; j++)
{
recipe_items[j].id = items[j+items_offset].id;
recipe_items[j].image_id = items[j+items_offset].image_id;
recipe_items[j].quantity = items[j+items_offset].quantity;
}
}
static void copy_recipe_items_to_items(item *items, size_t items_offset, recipe_item *recipe_items)
{
size_t j;
for (j=0; j<NUM_MIX_SLOTS; j++)
{
memset(&items[j+items_offset], 0, sizeof(item));
items[j+items_offset].id = recipe_items[j].id;
items[j+items_offset].image_id = recipe_items[j].image_id;
items[j+items_offset].quantity = recipe_items[j].quantity;
}
}
/* called on client exit to free memory and clean up */
void cleanup_manufacture(void)
{
size_t i;
for (i=0; i<max_prev_num_recipe_entries; i++)
if (recipes_store[i].name != NULL)
free(recipes_store[i].name);
free(recipes_store);
recipes_store = NULL;
max_prev_num_recipe_entries = num_recipe_entries = 0;
}
/* initialse recipe name vars */
static void init_recipe_names(void)
{
size_t i;
if (initialised_recipe_names)
return;
for (i=0; i<NUM_MIX_SLOTS; i++)
last_mix[i].quantity = 0;
initialised_recipe_names = 1;
}
/* create a new recipe name entry */
static void new_recipe_name(size_t recipe_no, const char *name)
{
size_t len = strlen(name);
if ((recipe_no >= num_recipe_entries) || (recipes_store[recipe_no].name != NULL))
return;
recipes_store[recipe_no].name = (char *)malloc(len+1);
safe_strncpy(recipes_store[recipe_no].name, name, len+1);
recipe_names_changed = 1;
}
/* save recipe names to file if any have changed */
static void save_recipe_names(void)
{
char fname[128];
FILE *fp;
size_t i;
int errorflag = 0;
safe_snprintf(fname, sizeof(fname), "recipes_%s.names",get_lowercase_username());
/* do not save if unchanged, unless the file does not exists and we have some recipes */
if (!(recipe_names_changed || (num_recipe_entries && (file_exists_config(fname) != 1))))
return;
recipe_names_changed = 0;
fp = open_file_config(fname,"w");
if(fp == NULL)
{
LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno));
return;
}
for (i=0; i<num_recipe_entries; i++)
{
if (recipes_store[i].name != NULL)
{
if (fputs(recipes_store[i].name, fp) < 0)
{
errorflag = 1;
break;
}
}
if (fputc('\n', fp) != '\n')
{
errorflag = 1;
break;
}
}
if (errorflag)
LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno));
fclose(fp);
}
/* load saved recipe names from file */
static void load_recipe_names(void)
{
char fname[128];
FILE *fp;
char line [128];
size_t recipe_no;
recipe_names_changed = 0;
safe_snprintf(fname, sizeof(fname), "recipes_%s.names",get_lowercase_username());
/* sliently ignore non existing file */
if (file_exists_config(fname)!=1)
return;
fp = open_file_config(fname,"r");
if(fp == NULL)
{
LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno));
return;
}
recipe_no = 0;
while (fgets(line, sizeof(line), fp) != NULL)
{
size_t len = strlen(line);
while ((len > 0) && ((line[len-1] == '\r') || (line[len-1] == '\n') || (line[len-1] == ' ')))
{
line[len-1] = '\0';
len--;
}
if (len > 0)
new_recipe_name(recipe_no, line);
recipe_no++;
}
fclose(fp);
}
/* each time a mix is called, save the ingredients - so we can compare later */
static void save_last_mix(void)
{
size_t i;
for (i=MIX_SLOT_OFFSET;i<MIX_SLOT_OFFSET+NUM_MIX_SLOTS;i++)
if (manufacture_list[i].quantity > 0)
last_mix[i-MIX_SLOT_OFFSET] = manufacture_list[i];
}
/* if the name is set, delete it */
static void clear_recipe_name(size_t recipe_no)
{
if ((recipe_no < num_recipe_entries) && (recipes_store[recipe_no].name != NULL))
{
free(recipes_store[recipe_no].name);
recipes_store[recipe_no].name = NULL;
recipe_names_changed = 1;
}
}
/* Compare the last mixed ingredients to all the recipes without a name.
* If one matches then set the name to the provided string.
*/
void check_for_recipe_name(const char *name)
{
size_t i, recipe_index;
int num_last_ing = 0;
// exit now if no last recipe
for (i=0; i<NUM_MIX_SLOTS; i++)
if (last_mix[i].quantity > 0)
num_last_ing++;
if (!num_last_ing)
return;
// check any not-set recipe name to see if the recipe matches the last_mix
for (recipe_index=0; recipe_index<num_recipe_entries; recipe_index++)
{
int num_recipe_ing = 0;
int num_match_ing = 0;
size_t recipe_item_index;
item last_mix_cpy[NUM_MIX_SLOTS];
recipe_item *recipe = recipes_store[recipe_index].items;
// move on if already have name, no recipe or if the ingredient counts don't match
if (recipes_store[recipe_index].name != NULL)
continue;
for (i=0; i<NUM_MIX_SLOTS; i++)
if (recipe[i].quantity > 0)
num_recipe_ing++;
if (!num_recipe_ing || (num_recipe_ing!=num_last_ing))
continue;
// allowing for any order, check if ingredients are the same
memcpy(last_mix_cpy, last_mix, sizeof(item)*NUM_MIX_SLOTS);
for (recipe_item_index=0; recipe_item_index<NUM_MIX_SLOTS; recipe_item_index++)
{
size_t last_mix_slot_index;
if (recipe[recipe_item_index].quantity < 1)
continue;
for (last_mix_slot_index=0; last_mix_slot_index<NUM_MIX_SLOTS; last_mix_slot_index++)
if ((last_mix_cpy[last_mix_slot_index].quantity > 0) &&
(recipe[recipe_item_index].quantity == last_mix_cpy[last_mix_slot_index].quantity) &&
(recipe[recipe_item_index].image_id == last_mix_cpy[last_mix_slot_index].image_id) &&
item_ids_match(recipe[recipe_item_index].id, last_mix_cpy[last_mix_slot_index].id))
{
last_mix_cpy[last_mix_slot_index].quantity = 0;
num_match_ing++;
break;
}
}
// if ingredients are the same, eureka!
if (num_match_ing == num_recipe_ing)
{
new_recipe_name(recipe_index, name);
break;
}
}
/* clear the last mix in all cases */
for (i=0; i<NUM_MIX_SLOTS; i++)
last_mix[i].quantity = 0;
}
/* callback for options window to change the number of recipe entries */
void change_num_recipe_entries(int * var, int value)
{
if ((value >= 0) && (value < max_num_recipe_entries))
*var = value;
else
return;
if (!recipes_loaded)
return;
/* never reduce the allocated memory so we can reduce and increase while keeping recipes */
if (wanted_num_recipe_entries > max_prev_num_recipe_entries)
{
recipes_store = (recipe_entry *)realloc(recipes_store, wanted_num_recipe_entries * sizeof(recipe_entry));
memset(&recipes_store[max_prev_num_recipe_entries], 0,
sizeof(recipe_entry)*(wanted_num_recipe_entries-max_prev_num_recipe_entries));
max_prev_num_recipe_entries = wanted_num_recipe_entries;
}
num_recipe_entries = wanted_num_recipe_entries;
/* keep current recipe in range */
if (cur_recipe >= num_recipe_entries)
cur_recipe = num_recipe_entries - 1;
/* resize the window if its now too big for the number of recipes */
if (num_displayed_recipes > num_recipe_entries)
{
num_displayed_recipes = num_recipe_entries;
resize_window(recipe_win, recipe_win_width, num_displayed_recipes*SLOT_SIZE);
}
/* reset the scroll length and position to show the new slot */
vscrollbar_set_bar_len(recipe_win, recipe_win_scroll_id, num_recipe_entries - num_displayed_recipes);
vscrollbar_set_pos(recipe_win, recipe_win_scroll_id, num_recipe_entries - num_displayed_recipes);
}
/* The manu window was not setting item ids when saving recipes
* so for previously saved recipes, all uid are set to zero. The
* value for unset uids is (Uint16)-1 so this needs fixing. From now on
* saved recipes with set the uid properly.
* Zero is the uid for sunflowers (image id 25) so don't change that.
*/
static void fix_recipe_uids(recipe_item items[NUM_MIX_SLOTS])
{
int i;
for (i=0; i<NUM_MIX_SLOTS; i++)
if ((items[i].quantity > 0) && (items[i].id == 0) && (items[i].image_id != 25))
items[i].id = unset_item_uid;
}
/* load recipes, done just after login when we have the player name */
void load_recipes (){
char fname[128];
FILE *fp;
size_t i;
int logged = 0;
off_t file_size = 0;
const size_t recipe_size = sizeof(item)*NUM_MIX_SLOTS;
int num_recipes_in_file = -1;
#ifdef JSON_FILES
int have_json_file = 0;
#endif
if (recipes_loaded) {
/*
* save existing recipes instead of loading them if we are already logged in
* this will take place when relogging after disconnection
*/
save_recipes();
return;
}
#ifdef JSON_FILES
if (get_use_json_user_files())
{
USE_JSON_DEBUG("Loading json file");
/* try to use the json file first ... */
safe_snprintf(fname, sizeof(fname), "%srecipes_%s.json", get_path_config(), get_lowercase_username());
if ((num_recipes_in_file = json_open_recipes(fname)) >= 0)
have_json_file = 1;
}
#endif
#ifdef JSON_FILES
if (!have_json_file)
{
#endif
safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",get_lowercase_username());
/* get file length, if a valid length */
file_size = get_file_size_config(fname);
if ((file_size > 0) && (file_size % recipe_size == 0))
num_recipes_in_file = file_size / recipe_size - 1; // -1 as last is current in pipline
#ifdef JSON_FILES
}
#endif
/* adjust the number of recipe slots if required */
if ((num_recipes_in_file > wanted_num_recipe_entries) && (num_recipes_in_file < max_num_recipe_entries))
{
wanted_num_recipe_entries = num_recipes_in_file;
set_var_OPT_INT("wanted_num_recipe_entries", wanted_num_recipe_entries);
}
/* allocate and initialise the recipe store */
num_recipe_entries = max_prev_num_recipe_entries = wanted_num_recipe_entries;
recipes_store = (recipe_entry *)calloc(num_recipe_entries, sizeof(recipe_entry));
if (recipes_store == NULL)
{
max_prev_num_recipe_entries = num_recipe_entries = 0;
return;
}
recipes_loaded=1;
init_recipe_names();
#ifdef JSON_FILES
if (have_json_file)
{
cur_recipe = json_load_recipes(recipes_store, num_recipe_entries, manu_recipe.items);
if ((cur_recipe < 0) || (cur_recipe >= num_recipe_entries))
cur_recipe = 0;
return;
}
// if there is no json file, or json use disabled, try to load the old binary format
USE_JSON_DEBUG("Loading binary file");
#endif
/* if the file exists but is not a valid size, don't use it */
if ((file_size > 0) && (file_size % recipe_size != 0))
{
LOG_ERROR("%s: Invalid format (size mismatch) \"%s\"\n", reg_error_str, fname);
return;
}
/* sliently ignore non existing file */
if (file_exists_config(fname)!=1)
return;
fp = open_file_config(fname,"rb");
if(fp == NULL){
LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno));
return;
}
/* attempt to read all the recipies we're expecting */
for (i=0; !feof(fp) && i<num_recipe_entries; i++)
{
item current[NUM_MIX_SLOTS];
if (fread (current, recipe_size, 1, fp) != 1)
{
if (!logged)
{
LOG_ERROR("%s() fail during read of file [%s] : %s\n", __FUNCTION__, fname, strerror(errno));
logged = 1;
}
memset(recipes_store[i].items, 0, sizeof(recipe_item) * NUM_MIX_SLOTS);
break;
}
copy_items_to_recipe_items(recipes_store[i].items, current, 0);
fix_recipe_uids(recipes_store[i].items);
}
/* if there is another, use it as the current recipe in the manufacturing pipeline */
if (!feof(fp))
{
item current[NUM_MIX_SLOTS];
if (fread (current,recipe_size,1, fp) != 1)
memset(manu_recipe.items, 0, sizeof(recipe_item) * NUM_MIX_SLOTS);
else
{
copy_items_to_recipe_items(manu_recipe.items, current, 0);
fix_recipe_uids(manu_recipe.items);
}
}
fclose (fp);
load_recipe_names();
}
/* save the recipe names, done on exit and after each change */
void save_recipes(){
char fname[128];
FILE *fp;
size_t i;
if (!recipes_loaded)
return;
#ifdef JSON_FILES
if (get_use_json_user_files())
{
USE_JSON_DEBUG("Saving json file");
/* save in json format always */
safe_snprintf(fname, sizeof(fname), "%srecipes_%s.json",get_path_config(), get_lowercase_username());
if (json_save_recipes(fname, recipes_store, num_recipe_entries, cur_recipe, manu_recipe.items) < 0)
LOG_ERROR("%s: %s \"%s\"\n", reg_error_str, cant_open_file, fname);
return;
}
USE_JSON_DEBUG("Saving binary file");
#endif
save_recipe_names();
safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",get_lowercase_username());
fp=open_file_config(fname,"wb");
if(fp == NULL){
LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno));
return;
}
for (i=0; i<num_recipe_entries+1; i++)
{
item store[NUM_MIX_SLOTS];
recipe_item *current = (i<num_recipe_entries) ?recipes_store[i].items :manu_recipe.items;
copy_recipe_items_to_items(store, 0, current);
if (fwrite (store,sizeof(item)*NUM_MIX_SLOTS,1, fp) != 1)
{
LOG_ERROR("%s() fail during write of file [%s] : %s\n", __FUNCTION__, fname, strerror(errno));
break;
}
}
fclose(fp);
}
/* examine the specified recipe and the items available then set the recipe status true if can make */
static void check_if_possible_recipe(recipe_entry *this_recipe)
{
int i;
int possible = 1;
if (!recipes_loaded || this_recipe == NULL)
return;
for (i=0; possible && i<NUM_MIX_SLOTS; i++)
{
if (this_recipe->items[i].quantity > 0)
{
int not_found = 1;
int j;
for (j=0; possible && j<MIX_SLOT_OFFSET; j++)
if ((manufacture_list[j].quantity > 0) &&
(this_recipe->items[i].image_id == manufacture_list[j].image_id) &&
item_ids_match(this_recipe->items[i].id, manufacture_list[j].id))
{
if(this_recipe->items[i].quantity > manufacture_list[j].quantity)
possible = 0;
not_found = 0;
break;
}
if (not_found)
possible = 0;
}
}
this_recipe->status = possible;
}
void build_manufacture_list()
{
int i,j,k;
for(i=0;i<MIX_SLOT_OFFSET+NUM_MIX_SLOTS;i++)manufacture_list[i].quantity=0;
//ok, now see which items are resources
j=0;
for(i=0;i<ITEM_WEAR_START;i++) {
if(item_list[i].quantity && item_list[i].is_resource) {
manufacture_list[j].quantity=item_list[i].quantity;
manufacture_list[j].image_id=item_list[i].image_id;
manufacture_list[j].id=item_list[i].id;
manufacture_list[j].pos=item_list[i].pos;
j++;
}
}
//now check for all items in all recipes
for(k=0;k<num_recipe_entries;k++){
check_if_possible_recipe(&recipes_store[k]);
}
check_if_possible_recipe(&manu_recipe);
//all there? good, put them in from current recipe
if(manu_recipe.status) {
for(i=0; i<NUM_MIX_SLOTS; i++) {
if(manu_recipe.items[i].quantity > 0) {
for(j=0;j<MIX_SLOT_OFFSET;j++){
if ((manufacture_list[j].quantity > 0) &&
(manufacture_list[j].quantity >= manu_recipe.items[i].quantity) &&
(manufacture_list[j].image_id == manu_recipe.items[i].image_id) &&
item_ids_match(manufacture_list[j].id, manu_recipe.items[i].id)) {
//found an empty space in the "production pipe"
manufacture_list[j].quantity -= manu_recipe.items[i].quantity;
manufacture_list[i+MIX_SLOT_OFFSET].quantity = manu_recipe.items[i].quantity;
manufacture_list[i+MIX_SLOT_OFFSET].pos = manufacture_list[j].pos;
manufacture_list[i+MIX_SLOT_OFFSET].image_id = manufacture_list[j].image_id;
manufacture_list[i+MIX_SLOT_OFFSET].id = manufacture_list[j].id;
break;
}
}
} else {
manufacture_list[i+MIX_SLOT_OFFSET].quantity = 0;
}
}
}
}
static void use_recipe(int recipe_to_use)
{
memcpy(&manu_recipe, &recipes_store[recipe_to_use], sizeof(recipe_entry));
}
//DRAWING FUNCTIONS
static void draw_recipe_controls(window_info *win){
int wpx=pipeline_x + SLOT_SIZE*NUM_MIX_SLOTS;
int wpy=manufacture_menu_y_len-recipe_y_offset;
int lpx=pipe_control_width;
int lpy=SLOT_SIZE;
int half_plus_len = lpx/2-control_elem_size, half_plus_width = max2i(win->current_scale, 1);
int yp_ctr = wpy - control_elem_size + lpy/2;
int xp_ctr = wpx + control_elem_size + half_plus_len;
if (recipes_shown)
{
/* Up arrow */
glBegin(GL_TRIANGLES);
glVertex3i(wpx+lpx/2, wpy+lpy-control_elem_size*2, 0); //top
glVertex3i(wpx+control_elem_size, wpy+lpy, 0); //left
glVertex3i(wpx+lpx-control_elem_size, wpy+lpy, 0); //right
glEnd();
}
else
{
/* Dn arrow */
glBegin(GL_TRIANGLES);
glVertex3i(wpx+lpx/2, wpy+lpy, 0); //bottom
glVertex3i(wpx+control_elem_size, wpy+lpy-control_elem_size*2, 0); //left
glVertex3i(wpx+lpx-control_elem_size, wpy+lpy-control_elem_size*2, 0); //right
glEnd();
}
/* Add btn */
glBegin(GL_QUADS);
glVertex3i(xp_ctr-half_plus_len, yp_ctr-half_plus_width, 0);
glVertex3i(xp_ctr+half_plus_len, yp_ctr-half_plus_width, 0);
glVertex3i(xp_ctr+half_plus_len, yp_ctr+half_plus_width, 0);
glVertex3i(xp_ctr-half_plus_len, yp_ctr+half_plus_width, 0);
glVertex3i(xp_ctr-half_plus_width, yp_ctr-half_plus_len, 0);
glVertex3i(xp_ctr+half_plus_width, yp_ctr-half_plus_len, 0);
glVertex3i(xp_ctr+half_plus_width, yp_ctr+half_plus_len, 0);
glVertex3i(xp_ctr-half_plus_width, yp_ctr+half_plus_len, 0);
glEnd();
}
//draws a NUM_MIX_SLOTSx1 grid of items+grid
static int draw_production_pipe(window_info *win, int x, int y, int recipe_num){
int i,valid;
Uint8 str[80];
recipe_item *the_list;
recipe_item temp_list[NUM_MIX_SLOTS];
//if recipe_num is negative we draw the current manufacture_list, else a recipe
if (recipe_num<0) {
copy_items_to_recipe_items(temp_list, manufacture_list, MIX_SLOT_OFFSET);
the_list = temp_list;
valid=1;
} else {
the_list=recipes_store[recipe_num].items;
valid=recipes_store[recipe_num].status;
}
glEnable(GL_TEXTURE_2D);
//ok, now let's draw the mixed objects
for(i=0;i<NUM_MIX_SLOTS;i++) {
glColor3f(1.0f,1.0f,1.0f);
if(the_list[i].quantity > 0){
int x_start,y_start;
int use_large = (mouse_over_pipe_pos == i) && enlarge_text();
int y_offset = SLOT_SIZE - ((use_large) ?win->default_font_len_y :win->small_font_len_y);
//get the x and y
x_start=x+SLOT_SIZE*(i%NUM_MIX_SLOTS)+1;
y_start=y;
draw_item(the_list[i].image_id,x_start,y_start,SLOT_SIZE);
safe_snprintf((char *)str, sizeof(str), "%i", the_list[i].quantity);
if (use_large)
draw_string_shadowed_zoomed(x_start, y_start+y_offset, (unsigned char*)str, 1, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, win->current_scale);
else
draw_string_small_shadowed_zoomed(x_start, y_start+y_offset, (unsigned char*)str, 1, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, win->current_scale);
if (!valid)
gray_out(x_start,y_start,SLOT_SIZE);
}
}
glDisable(GL_TEXTURE_2D);
//draw the grid, in red if selected
if (recipe_num==cur_recipe) glColor3f(1.0f,0.0f,0.0f);
else glColor3fv(gui_color);
rendergrid(NUM_MIX_SLOTS,1,x, y, SLOT_SIZE, SLOT_SIZE);
glEnable(GL_TEXTURE_2D);
return 1;
}
static int display_manufacture_handler(window_info *win)
{
Uint8 str[80];
int i;
// keep the main and recipe windows opacity in sync
if (recipe_win >= 0)
{
static int last_recipes_opaque = -1, last_main_opaque = -1;
if (win->opaque != last_main_opaque)
last_recipes_opaque = last_main_opaque = windows_list.window[recipe_win].opaque = win->opaque;
else if (windows_list.window[recipe_win].opaque != last_recipes_opaque)
last_recipes_opaque = last_main_opaque = win->opaque = windows_list.window[recipe_win].opaque;
}
glColor3fv(gui_color);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f,1.0f,1.0f);
//ok, now let's draw the objects...
for(i=35;i>=0;i--){
if(manufacture_list[i].quantity > 0) {
int x_start,y_start;
int use_large = (mouse_over_main_pos == i) && enlarge_text();
//get the x and y
x_start=SLOT_SIZE*(i%GRID_COLS)+1;
y_start=SLOT_SIZE*(i/GRID_COLS);
draw_item(manufacture_list[i].image_id,x_start,y_start,SLOT_SIZE);
safe_snprintf((char *)str, sizeof(str), "%i",manufacture_list[i].quantity);
y_start += (i&1) ? 2 : SLOT_SIZE - ((use_large) ?win->default_font_len_y :win->small_font_len_y);
if (use_large)
draw_string_shadowed_zoomed(x_start, y_start, (unsigned char*)str, 1, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, win->current_scale);
else
draw_string_small_shadowed_zoomed(x_start, y_start, (unsigned char*)str, 1, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, win->current_scale);
}
}
//ok, now let's draw the mixed objects
draw_production_pipe(win, pipeline_x,manufacture_menu_y_len-recipe_y_offset, -1);
//now, draw the inventory text, if any.
if (last_items_string_id != inventory_item_string_id)
{
put_small_text_in_box_zoomed((unsigned char*)inventory_item_string,
strlen(inventory_item_string), win->len_x-2*win->small_font_max_len_x,
items_string, win->current_scale);
last_items_string_id = inventory_item_string_id;
}
draw_string_small_zoomed(win->small_font_max_len_x, manufacture_menu_y_len-text_y_offset,
items_string, 4, win->current_scale);
// Render the grid *after* the images. It seems impossible to code
// it such that images are rendered exactly within the boxes on all
// cards
glDisable(GL_TEXTURE_2D);
glColor3fv(gui_color);
//draw the grid
rendergrid(GRID_COLS, GRID_ROWS, 0, 0, SLOT_SIZE, SLOT_SIZE);
//Draw recipe control buttons
draw_recipe_controls(win);
glEnable(GL_TEXTURE_2D);
mouse_over_main_pos = -1;
mouse_over_pipe_pos = -1;
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
return 1;
}
static void clear_recipe_filter(void)
{
recipe_name_filter[0] = '\0';
last_recipe_key_time = 0;
}
static void toggle_recipe_window(void)
{
if (!recipes_loaded)
return;
recipes_shown=!recipes_shown;
if (recipes_shown)
show_window(recipe_win);
else
{
hide_window(recipe_win);
clear_recipe_filter();
}
}
/* draw the recipes - finishing any resize to snap to a reasonable size */
static int recipe_dropdown_draw(window_info *win){
static int resizing = 0;
size_t i;
int first_displayed_recipe = vscrollbar_get_pos (win->window_id, recipe_win_scroll_id);
int relative_cur = cur_recipe - first_displayed_recipe;
int help_line = 0;
int find_active = 0;
/* if resizing wait until we stop */
if (win->resized)
resizing = 1;
/* once we stop, snap the window size to fit nicely */
else if (resizing)
{
resizing = 0;
num_displayed_recipes = (int)(win->len_y / SLOT_SIZE);
if (num_displayed_recipes > num_recipe_entries)
num_displayed_recipes = num_recipe_entries;
resize_window(win->window_id, recipe_win_width, num_displayed_recipes*SLOT_SIZE);
}
/* draw all the shown recipes */
for (i=0; i<num_displayed_recipes && i<num_recipe_entries; i++)
draw_production_pipe(win,0,SLOT_SIZE*i,i+first_displayed_recipe);
/* if shown, the current (highlighted in red) recipe needs drawing again and its name displayed */
if ((relative_cur >= 0) && (relative_cur < num_displayed_recipes))
{
draw_production_pipe(win,0,SLOT_SIZE*relative_cur,cur_recipe);
if ((cur_recipe < num_recipe_entries) && (recipes_store[cur_recipe].name != NULL))
show_help(recipes_store[cur_recipe].name, win->len_x+5, SLOT_SIZE*relative_cur+(SLOT_SIZE-win->small_font_len_y)/2, win->current_scale);
}
/* display any name search text */
if (strlen(recipe_name_filter))
{
if (SDL_GetTicks() > (last_recipe_key_time+5000))
clear_recipe_filter();
else
{
static char tmp[30];
safe_snprintf(tmp, sizeof(tmp), "%s[%s]", item_list_find_str, recipe_name_filter);
show_help(tmp, 0, win->len_y + 10 + win->small_font_len_y*help_line++, win->current_scale);
find_active = 1;
}
}
/* display the name of the recipe with the mouse over, plus general help */
if (mouse_over_recipe != -1)
{
size_t actual_recipe = mouse_over_recipe + first_displayed_recipe;
if ((actual_recipe != cur_recipe) &&
(actual_recipe < num_recipe_entries) &&
(recipes_store[actual_recipe].name != NULL))
show_help(recipes_store[actual_recipe].name, win->len_x+5,
SLOT_SIZE*mouse_over_recipe+(SLOT_SIZE-win->small_font_len_y)/2, win->current_scale);
if (show_help_text)
{
#ifdef ANDROID
show_help(long_touch_cm_options_str, 0, win->len_y + 10 + win->small_font_len_y*help_line++, win->current_scale);
#else
show_help(cm_help_options_str, 0, win->len_y + 10 + win->small_font_len_y*help_line++, win->current_scale);
#endif
show_help(recipe_select_str, 0, win->len_y + 10 + win->small_font_len_y*help_line++, win->current_scale);
show_help(recipe_load_str, 0, win->len_y + 10 + win->small_font_len_y*help_line++, win->current_scale);
show_help(find_active?recipe_during_find_str:recipe_find_str, 0, win->len_y + 10 + win->small_font_len_y*help_line++, win->current_scale);
}
}
mouse_over_recipe = -1;
return 1;
}
/* select the specified recipe and and make sure the window is hidden */
static void select_recipe(int the_recipe)
{
cur_recipe = the_recipe;
use_recipe(cur_recipe);
recipes_shown=0;
clear_recipe_filter();
hide_window(recipe_win);
build_manufacture_list();
}
/* key presses in the window used for a recipe name search string */
static int keypress_recipe_handler(window_info *win, int mx, int my, SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
{
last_recipe_key_time = SDL_GetTicks();
if ((key_code == SDLK_RETURN || key_code == SDLK_KP_ENTER) && (key_mod & KMOD_CTRL))
{
select_recipe(cur_recipe);
return 1;
}
if ((key_unicode == '`') || (key_mod & KMOD_CTRL) || (key_mod & KMOD_ALT))
return 0;
if (key_code == SDLK_ESCAPE)
{
clear_recipe_filter();
return 1;
}
if (string_input(recipe_name_filter, sizeof(recipe_name_filter), key_code, key_unicode, key_mod) || (key_code == SDLK_RETURN) || (key_code == SDLK_KP_ENTER))
{
if (strlen(recipe_name_filter))
{
size_t i;
for (i=cur_recipe+1; i<cur_recipe+num_recipe_entries; i++)
{
size_t check = i % num_recipe_entries;
if (recipes_store[check].name != NULL &&
safe_strcasestr(recipes_store[check].name, strlen(recipes_store[check].name),
recipe_name_filter, strlen(recipe_name_filter)) != NULL)
{
/* have matching name so set as the current and make sure visible */
int first_displayed_recipe = vscrollbar_get_pos (win->window_id, recipe_win_scroll_id);
int new_pos = first_displayed_recipe;
cur_recipe = check;
if (cur_recipe < first_displayed_recipe)
new_pos = cur_recipe;
else if (cur_recipe >= (first_displayed_recipe+num_displayed_recipes))
new_pos = cur_recipe - (num_displayed_recipes - 1);
else
break;
vscrollbar_set_pos(win->window_id, recipe_win_scroll_id, new_pos);
break;
}
}
}
return 1;
}
return 0;
}
/* keypress in main window is passed to recipe window search */
static int keypress_manufacture_handler(window_info *win, int mx, int my, SDL_Keycode key_code, Uint32 key_unicode, Uint16 key_mod)
{
if (!disable_manuwin_keypress && (recipe_win > -1) && (recipe_win < windows_list.num_windows))
{
window_info *win_recp = &windows_list.window[recipe_win];
int current_recipes_shown = recipes_shown; // so we don't undo keypress_recipe_handler() work
if (win_recp != NULL && keypress_recipe_handler(win_recp, mx, my, key_code, key_unicode, key_mod))
{
if (!recipes_shown && (current_recipes_shown == recipes_shown))
toggle_recipe_window();
return 1;
}
}
return 0;
}
//CLICK HANDLERS
static int recipe_dropdown_click_handler(window_info *win, int mx, int my, Uint32 flags){
static int last_clicked=0;
static int last_recipe=0;
int first_displayed_recipe = vscrollbar_get_pos (win->window_id, recipe_win_scroll_id);
int rel_curr;
if (flags & ELW_WHEEL_UP) {
cur_recipe=(cur_recipe-1+num_recipe_entries)%num_recipe_entries;
rel_curr = cur_recipe - first_displayed_recipe;
if ((rel_curr < 0) || (rel_curr >= num_displayed_recipes))
vscrollbar_set_pos(win->window_id, recipe_win_scroll_id, cur_recipe);
} else
if (flags & ELW_WHEEL_DOWN) {