Stress and accent in different cases

I have been struggling with the moving accents in greek since I started. I've discovered some regularities: -ότητες in genetive case always becomes -οτήτων and -ματα -> -μάτων. But I can't tell why πόλεμος becomes πολέμου or αυτοκίνητο -> αυτοκινήτου. Are there more regularities and rules for the placement of accents? Does it depend on the number of syllables before or after the given accent? Or do you just have to learn them one by one?

There are some rules, but they do not explain everything. The acute accent can fall on the last, second to last, or third to last syllable, but if the last syllable has a long vowel or diphthong, it can only fall on the last two syllables. That is why you say πόλεμος but πολέμου. However, αι and οι do not count as long for the purposes of accent, so you say πόλεμοι. Then there are cases like πόλεως, which seems to violate this rule, but that is because it comes from an older πόληος. You can read all about it in any good grammar of classical Greek.

PS. I am talking about classical Greek. In modern Greek you can explain these things only historically.

Hi.
This is the normal behaviour.
The accent cannot stand back further than the penultimate syllable in genitives ending in -ου, as well as in accusatives in -ους.
There are some exceptions : compound nouns that are formed with two substantives like ανθόκηπος and some nouns that changed their gender from masculine to neutre since ancient greek do not follow the rule. (like σίδερο or δάχτυλο)
Feminine nouns in -ος absolutely always follow the rule.

You can also see/hear: του δάσκαλου, του δήμαρχου, του άγγελου (but not του άνθρωπου). The basic accent is the accent of the nominative singular (ο δάσκαλος, ο δήμαρχος etc.) and is preserved in all cases except in those above. The fact that the accent does not remain on the same syllable is explained historically, as mentioned before. Speaking about trisyllable nouns of the second declension (Anc. Gr.) that take their basic accent on the 3rd to last syllable (e.g. ο άνθρωπος), the rule is that "when the vowel in the ultima is long (e.g. ου, ων, ους), the accent is forced forward to the penult".