sort the index used for display order of the "view known spells" list (sortmode == SORTBY_xxx), or sort the spellbook itself to make the current display order stick (sortmode == SORTRETAINORDER) */
| 1924 | list (sortmode == SORTBY_xxx), or sort the spellbook itself to make |
| 1925 | the current display order stick (sortmode == SORTRETAINORDER) */ |
| 1926 | staticfn void |
| 1927 | sortspells(void) |
| 1928 | { |
| 1929 | int i; |
| 1930 | #if defined(SYSV) || defined(DGUX) |
| 1931 | unsigned n; |
| 1932 | #else |
| 1933 | int n; |
| 1934 | #endif |
| 1935 | |
| 1936 | if (gs.spl_sortmode == SORTBY_CURRENT) |
| 1937 | return; |
| 1938 | for (n = 0; n < MAXSPELL && spellid(n) != NO_SPELL; ++n) |
| 1939 | continue; |
| 1940 | if (n < 2) |
| 1941 | return; /* not enough entries to need sorting */ |
| 1942 | |
| 1943 | if (!gs.spl_orderindx) { |
| 1944 | /* we haven't done any sorting yet; list is in casting order */ |
| 1945 | if (gs.spl_sortmode == SORTBY_LETTER /* default */ |
| 1946 | || gs.spl_sortmode == SORTRETAINORDER) |
| 1947 | return; |
| 1948 | /* allocate enough for full spellbook rather than just N spells */ |
| 1949 | gs.spl_orderindx = (int *) alloc(MAXSPELL * sizeof(int)); |
| 1950 | for (i = 0; i < MAXSPELL; i++) |
| 1951 | gs.spl_orderindx[i] = i; |
| 1952 | } |
| 1953 | |
| 1954 | if (gs.spl_sortmode == SORTRETAINORDER) { |
| 1955 | struct spell tmp_book[MAXSPELL]; |
| 1956 | |
| 1957 | /* sort svs.spl_book[] rather than spl_orderindx[]; |
| 1958 | this also updates the index to reflect the new ordering (we |
| 1959 | could just free it since that ordering becomes the default) */ |
| 1960 | for (i = 0; i < MAXSPELL; i++) |
| 1961 | tmp_book[i] = svs.spl_book[gs.spl_orderindx[i]]; |
| 1962 | for (i = 0; i < MAXSPELL; i++) |
| 1963 | svs.spl_book[i] = tmp_book[i], gs.spl_orderindx[i] = i; |
| 1964 | gs.spl_sortmode = SORTBY_LETTER; /* reset */ |
| 1965 | return; |
| 1966 | } |
| 1967 | |
| 1968 | /* usual case, sort the index rather than the spells themselves */ |
| 1969 | qsort((genericptr_t) gs.spl_orderindx, n, |
| 1970 | sizeof *gs.spl_orderindx, spell_cmp); |
| 1971 | return; |
| 1972 | } |
| 1973 | |
| 1974 | /* called if the [sort spells] entry in the view spells menu gets chosen */ |
| 1975 | staticfn boolean |